From 783a34642fb8edd87502ee1ec5818bfede7d20ef Mon Sep 17 00:00:00 2001 From: David Ernst Date: Tue, 21 May 2024 16:29:53 -0700 Subject: [PATCH] add api/ukraine-updates-subscribe --- pages/api/ukraine-updates-subscribe.ts | 30 ++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 pages/api/ukraine-updates-subscribe.ts diff --git a/pages/api/ukraine-updates-subscribe.ts b/pages/api/ukraine-updates-subscribe.ts new file mode 100644 index 00000000..02d26fcc --- /dev/null +++ b/pages/api/ukraine-updates-subscribe.ts @@ -0,0 +1,30 @@ +import { validate as validateEmail } from 'email-validator' +import { NextApiRequest, NextApiResponse } from 'next' + +import { allowCors } from './_cors' +import { firebase, pushover } from './_services' + +export default allowCors(async (req: NextApiRequest, res: NextApiResponse) => { + const { email: untrimmed } = req.body + const email = untrimmed.trim() + + // Validate email + if (!email) return res.status(400).json({ error: 'Email is required' }) + if (!validateEmail(email)) return res.status(400).json({ error: 'Invalid email' }) + + // Store submission in Firestore + await firebase + .firestore() + .collection('ukraine-updates') + .doc(new Date().toISOString() + ' ' + String(Math.random()).slice(2, 7)) + .set({ + created_at: new Date().toString(), + email, + }) + + // Notify admin via Pushover + pushover(`SIV ukraine signup`, email) + + // Send back success + return res.status(201).json({ success: true }) +})