unsubscribing from market resolution emails

This commit is contained in:
mantikoros 2022-01-18 17:06:40 -06:00
parent d8b60f4e45
commit dd2f271de8
4 changed files with 54 additions and 9 deletions

View File

@ -1,9 +1,7 @@
import * as admin from 'firebase-admin'
import { Contract } from '../../common/contract' import { Contract } from '../../common/contract'
import { User } from '../../common/user' import { User } from '../../common/user'
import { sendTemplateEmail } from './send-email' import { sendTemplateEmail } from './send-email'
import { getUser } from './utils' import { getPrivateUser, getUser } from './utils'
type market_resolved_template = { type market_resolved_template = {
name: string name: string
@ -21,13 +19,17 @@ export const sendMarketResolutionEmail = async (
contract: Contract, contract: Contract,
resolution: 'YES' | 'NO' | 'CANCEL' | 'MKT' resolution: 'YES' | 'NO' | 'CANCEL' | 'MKT'
) => { ) => {
const privateUser = await getPrivateUser(userId)
if (
!privateUser ||
privateUser.unsubscribedFromResolutionEmails ||
!privateUser.email
)
return
const user = await getUser(userId) const user = await getUser(userId)
if (!user) return if (!user) return
const fbUser = await admin.auth().getUser(userId)
const email = fbUser.email
if (!email) return
const outcome = toDisplayResolution[resolution] const outcome = toDisplayResolution[resolution]
const subject = `Resolved ${outcome}: ${contract.question}` const subject = `Resolved ${outcome}: ${contract.question}`
@ -45,7 +47,12 @@ export const sendMarketResolutionEmail = async (
// https://app.mailgun.com/app/sending/domains/mg.manifold.markets/templates/edit/market-resolved/initial // https://app.mailgun.com/app/sending/domains/mg.manifold.markets/templates/edit/market-resolved/initial
// Mailgun username: james@mantic.markets // Mailgun username: james@mantic.markets
await sendTemplateEmail(email, subject, 'market-resolved', templateData) await sendTemplateEmail(
privateUser.email,
subject,
'market-resolved',
templateData
)
} }
const toDisplayResolution = { YES: 'YES', NO: 'NO', CANCEL: 'N/A', MKT: 'MKT' } const toDisplayResolution = { YES: 'YES', NO: 'NO', CANCEL: 'N/A', MKT: 'MKT' }

View File

@ -10,5 +10,6 @@ export * from './stripe'
export * from './sell-bet' export * from './sell-bet'
export * from './create-contract' export * from './create-contract'
export * from './create-user' export * from './create-user'
export * from './unsubscribe'
export * from './update-contract-metrics' export * from './update-contract-metrics'
export * from './update-user-metrics' export * from './update-user-metrics'

View File

@ -0,0 +1,33 @@
import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
import * as _ from 'lodash'
import { getPrivateUser } from './utils'
import { PrivateUser } from '../../common/user'
export const unsubscribe = functions
.runWith({ minInstances: 1 })
.https.onRequest(async (req, res) => {
let id = req.query.id as string
if (!id) return
let privateUser = await getPrivateUser(id)
if (privateUser) {
let { username } = privateUser
const update: Partial<PrivateUser> = {
unsubscribedFromResolutionEmails: true,
}
await firestore.collection('private-users').doc(id).update(update)
res.send(
username +
', you have been unsubscribed from market resolution emails on Manifold Markets.'
)
} else {
res.send('This user is not currently subscribed or does not exist.')
}
})
const firestore = admin.firestore()

View File

@ -1,7 +1,7 @@
import * as admin from 'firebase-admin' import * as admin from 'firebase-admin'
import { Contract } from '../../common/contract' import { Contract } from '../../common/contract'
import { User } from '../../common/user' import { PrivateUser, User } from '../../common/user'
export const getValue = async <T>(collection: string, doc: string) => { export const getValue = async <T>(collection: string, doc: string) => {
const snap = await admin.firestore().collection(collection).doc(doc).get() const snap = await admin.firestore().collection(collection).doc(doc).get()
@ -22,6 +22,10 @@ export const getUser = (userId: string) => {
return getValue<User>('users', userId) return getValue<User>('users', userId)
} }
export const getPrivateUser = (userId: string) => {
return getValue<PrivateUser>('private-users', userId)
}
export const getUserByUsername = async (username: string) => { export const getUserByUsername = async (username: string) => {
const snap = await firestore const snap = await firestore
.collection('users') .collection('users')