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-01-19 03:36:46 +00:00
|
|
|
import { PrivateUser } from '../../common/user'
|
|
|
|
|
|
|
|
export const unsubscribe = functions
|
|
|
|
.runWith({ minInstances: 1 })
|
|
|
|
.https.onRequest(async (req, res) => {
|
2022-02-23 01:35:25 +00:00
|
|
|
const { id, type } = req.query as { id: string; type: string }
|
|
|
|
if (!id || !type) 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-02-23 01:35:25 +00:00
|
|
|
unsubscribedFromResolutionEmails: type === 'market-resolve',
|
|
|
|
unsubscribedFromCommentEmails: type === 'market-comment',
|
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.`
|
|
|
|
)
|
|
|
|
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()
|