import Firebase from 'firebase-admin' import Mailgun, { AttachmentParams } from 'mailgun-js' const { FIREBASE_CLIENT_EMAIL, FIREBASE_DATABASE_URL, FIREBASE_PRIVATE_KEY, FIREBASE_PROJECT_ID, MAILGUN_API_KEY, MAILGUN_DOMAIN, PUSHOVER_APP_TOKEN, PUSHOVER_USER_KEY, } = process.env // Init firebase (only once) export const firebase = !Firebase.apps.length ? Firebase.initializeApp({ credential: Firebase.credential.cert({ clientEmail: FIREBASE_CLIENT_EMAIL, privateKey: FIREBASE_PRIVATE_KEY, projectId: FIREBASE_PROJECT_ID, }), databaseURL: FIREBASE_DATABASE_URL || 'https://siv-demo.firebaseio.com', }) : Firebase.app() type SerializedTimestamp = { _seconds: number } /** `new Date()`, which Firebase will automatically serialize into `{ _seconds: number }` */ export const newSerializedTimestamp = () => new Date() as unknown as SerializedTimestamp /** Init mailgun */ export const mailgun = Mailgun({ apiKey: MAILGUN_API_KEY as string, domain: MAILGUN_DOMAIN || 'siv.org', }) export const sendEmail = ({ attachment, bcc, from, fromEmail, preheader, recipient, subject, tag, text, }: { attachment?: AttachmentParams bcc?: string from?: string fromEmail?: string preheader?: string recipient: string subject: string tag?: string text: string }) => mailgun.messages().send({ attachment: !attachment ? undefined : new mailgun.Attachment(attachment), bcc, from: `${from || 'SIV Admin'} <${fromEmail || 'election@siv.org'}>`, html: `
| ${preheader ? buildPreheader(preheader) : ''} Secure Internet Voting |