2022-01-19 03:36:46 +00:00
|
|
|
import * as functions from 'firebase-functions'
|
|
|
|
import * as admin from 'firebase-admin'
|
|
|
|
import * as _ from 'lodash'
|
2022-02-23 01:35:25 +00:00
|
|
|
import { getUser } from './utils'
|
2022-05-10 20:59:38 +00:00
|
|
|
import { PrivateUser } from 'common/user'
|
2022-01-19 03:36:46 +00:00
|
|
|
|
|
|
|
export const unsubscribe = functions
|
|
|
|
.runWith({ minInstances: 1 })
|
|
|
|
.https.onRequest(async (req, res) => {
|
2022-03-09 18:39:21 +00:00
|
|
|
let { id, type } = req.query as { id: string; type: string }
|
|
|
|
if (!id || !type) {
|
|
|
|
res.status(400).send('Empty id or type parameter.')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'market-resolved') type = 'market-resolve'
|
|
|
|
|
|
|
|
if (!['market-resolve', 'market-comment', 'market-answer'].includes(type)) {
|
|
|
|
res.status(400).send('Invalid type parameter.')
|
|
|
|
return
|
|
|
|
}
|
2022-01-19 03:36:46 +00:00
|
|
|
|
2022-02-23 01:35:25 +00:00
|
|
|
const user = await getUser(id)
|
2022-01-19 03:36:46 +00:00
|
|
|
|
2022-02-23 01:35:25 +00:00
|
|
|
if (user) {
|
|
|
|
const { name } = user
|
2022-01-19 03:36:46 +00:00
|
|
|
|
|
|
|
const update: Partial<PrivateUser> = {
|
2022-03-09 18:39:21 +00:00
|
|
|
...(type === 'market-resolve' && {
|
2022-02-23 06:05:04 +00:00
|
|
|
unsubscribedFromResolutionEmails: true,
|
|
|
|
}),
|
|
|
|
...(type === 'market-comment' && {
|
|
|
|
unsubscribedFromCommentEmails: true,
|
|
|
|
}),
|
|
|
|
...(type === 'market-answer' && {
|
|
|
|
unsubscribedFromAnswerEmails: true,
|
|
|
|
}),
|
2022-01-19 03:36:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await firestore.collection('private-users').doc(id).update(update)
|
|
|
|
|
2022-02-23 01:35:25 +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.`
|
|
|
|
)
|
2022-02-23 06:05:04 +00:00
|
|
|
else if (type === 'market-answer')
|
|
|
|
res.send(
|
|
|
|
`${name}, you have been unsubscribed from market answer emails on Manifold Markets.`
|
|
|
|
)
|
2022-02-23 01:35:25 +00:00
|
|
|
else res.send(`${name}, you have been unsubscribed.`)
|
2022-01-19 03:36:46 +00:00
|
|
|
} else {
|
|
|
|
res.send('This user is not currently subscribed or does not exist.')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const firestore = admin.firestore()
|