manifold/functions/src/unsubscribe.ts
Marshall Polaris eb9b14d6d5
Migrate unsubscribe function to v2 (#637)
* Migrate unsubscribe function to v2

* Move Stripe import because I forgot to do it before
2022-07-10 13:46:00 -07:00

67 lines
1.8 KiB
TypeScript

import { onRequest } from 'firebase-functions/v2/https'
import * as admin from 'firebase-admin'
import { getUser } from './utils'
import { PrivateUser } from '../../common/user'
export const unsubscribe = onRequest({ minInstances: 1 }, 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
}
if (type === 'market-resolved') type = 'market-resolve'
if (
!['market-resolve', 'market-comment', 'market-answer', 'generic'].includes(
type
)
) {
res.status(400).send('Invalid type parameter.')
return
}
const user = await getUser(id)
if (!user) {
res.send('This user is not currently subscribed or does not exist.')
return
}
const { name } = user
const update: Partial<PrivateUser> = {
...(type === 'market-resolve' && {
unsubscribedFromResolutionEmails: true,
}),
...(type === 'market-comment' && {
unsubscribedFromCommentEmails: true,
}),
...(type === 'market-answer' && {
unsubscribedFromAnswerEmails: true,
}),
...(type === 'generic' && {
unsubscribedFromGenericEmails: true,
}),
}
await firestore.collection('private-users').doc(id).update(update)
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.`
)
else res.send(`${name}, you have been unsubscribed.`)
})
const firestore = admin.firestore()