2022-01-19 03:36:46 +00:00
|
|
|
import * as admin from 'firebase-admin'
|
2022-07-24 07:26:38 +00:00
|
|
|
import { EndpointDefinition } from './api'
|
2022-02-23 01:35:25 +00:00
|
|
|
import { getUser } from './utils'
|
2022-05-15 17:39:42 +00:00
|
|
|
import { PrivateUser } from '../../common/user'
|
2022-01-19 03:36:46 +00:00
|
|
|
|
2022-07-24 07:26:38 +00:00
|
|
|
export const unsubscribe: EndpointDefinition = {
|
|
|
|
opts: { method: 'GET', minInstances: 1 },
|
|
|
|
handler: async (req, res) => {
|
|
|
|
const id = req.query.id as string
|
|
|
|
let type = req.query.type as string
|
|
|
|
if (!id || !type) {
|
|
|
|
res.status(400).send('Empty id or type parameter.')
|
|
|
|
return
|
|
|
|
}
|
2022-03-09 18:39:21 +00:00
|
|
|
|
2022-07-24 07:26:38 +00:00
|
|
|
if (type === 'market-resolved') type = 'market-resolve'
|
2022-03-09 18:39:21 +00:00
|
|
|
|
2022-07-24 07:26:38 +00:00
|
|
|
if (
|
|
|
|
![
|
|
|
|
'market-resolve',
|
|
|
|
'market-comment',
|
|
|
|
'market-answer',
|
|
|
|
'generic',
|
2022-08-19 17:43:57 +00:00
|
|
|
'weekly-trending',
|
2022-07-24 07:26:38 +00:00
|
|
|
].includes(type)
|
|
|
|
) {
|
|
|
|
res.status(400).send('Invalid type parameter.')
|
|
|
|
return
|
|
|
|
}
|
2022-01-19 03:36:46 +00:00
|
|
|
|
2022-07-24 07:26:38 +00:00
|
|
|
const user = await getUser(id)
|
2022-01-19 03:36:46 +00:00
|
|
|
|
2022-07-24 07:26:38 +00:00
|
|
|
if (!user) {
|
|
|
|
res.send('This user is not currently subscribed or does not exist.')
|
|
|
|
return
|
|
|
|
}
|
2022-05-11 15:51:58 +00:00
|
|
|
|
2022-07-24 07:26:38 +00:00
|
|
|
const { name } = user
|
2022-05-11 15:51:58 +00:00
|
|
|
|
2022-07-24 07:26:38 +00:00
|
|
|
const update: Partial<PrivateUser> = {
|
|
|
|
...(type === 'market-resolve' && {
|
|
|
|
unsubscribedFromResolutionEmails: true,
|
|
|
|
}),
|
|
|
|
...(type === 'market-comment' && {
|
|
|
|
unsubscribedFromCommentEmails: true,
|
|
|
|
}),
|
|
|
|
...(type === 'market-answer' && {
|
|
|
|
unsubscribedFromAnswerEmails: true,
|
|
|
|
}),
|
|
|
|
...(type === 'generic' && {
|
|
|
|
unsubscribedFromGenericEmails: true,
|
|
|
|
}),
|
2022-08-19 17:43:57 +00:00
|
|
|
...(type === 'weekly-trending' && {
|
|
|
|
unsubscribedFromWeeklyTrendingEmails: true,
|
|
|
|
}),
|
2022-07-24 07:26:38 +00:00
|
|
|
}
|
2022-05-11 15:51:58 +00:00
|
|
|
|
2022-07-24 07:26:38 +00:00
|
|
|
await firestore.collection('private-users').doc(id).update(update)
|
2022-05-11 15:51:58 +00:00
|
|
|
|
2022-07-24 07:26:38 +00:00
|
|
|
if (type === 'market-resolve')
|
|
|
|
res.send(
|
|
|
|
`${name}, you have been unsubscribed from market resolution emails on Manifold Markets.`
|
|
|
|
)
|
|
|
|
else if (type === 'market-comment')
|
|
|
|
res.send(
|
|
|
|
`${name}, you have been unsubscribed from market comment emails on Manifold Markets.`
|
|
|
|
)
|
|
|
|
else if (type === 'market-answer')
|
|
|
|
res.send(
|
|
|
|
`${name}, you have been unsubscribed from market answer emails on Manifold Markets.`
|
|
|
|
)
|
2022-08-22 22:56:28 +00:00
|
|
|
else if (type === 'weekly-trending')
|
|
|
|
res.send(
|
|
|
|
`${name}, you have been unsubscribed from weekly trending emails on Manifold Markets.`
|
|
|
|
)
|
2022-07-24 07:26:38 +00:00
|
|
|
else res.send(`${name}, you have been unsubscribed.`)
|
|
|
|
},
|
|
|
|
}
|
2022-01-19 03:36:46 +00:00
|
|
|
|
|
|
|
const firestore = admin.firestore()
|