2
0

feat(editor): Add send email integration

This commit is contained in:
Baptiste Arnaud
2022-02-07 18:06:37 +01:00
parent f4336b83cc
commit d6238b3474
48 changed files with 2119 additions and 2606 deletions

View File

@@ -1,5 +1,7 @@
import { Prisma, Credentials as CredentialsFromDb } from 'db'
import { Credentials as CredentialsFromDb } from 'db'
import { OAuth2Client, Credentials } from 'google-auth-library'
import { GoogleSheetsCredentialsData } from 'models'
import { decrypt, encrypt } from 'utils'
import prisma from './prisma'
export const oauth2Client = new OAuth2Client(
@@ -14,14 +16,20 @@ export const getAuthenticatedGoogleClient = async (
const credentials = (await prisma.credentials.findFirst({
where: { id: credentialsId },
})) as CredentialsFromDb
oauth2Client.setCredentials(credentials.data as Credentials)
const data = decrypt(
credentials.data,
credentials.iv
) as GoogleSheetsCredentialsData
oauth2Client.setCredentials(data)
oauth2Client.on('tokens', updateTokens(credentialsId))
return oauth2Client
}
const updateTokens =
(credentialsId: string) => async (credentials: Credentials) =>
prisma.credentials.update({
(credentialsId: string) => async (credentials: Credentials) => {
const { encryptedData, iv } = encrypt(credentials)
return prisma.credentials.update({
where: { id: credentialsId },
data: { data: credentials as Prisma.InputJsonValue },
data: { data: encryptedData, iv },
})
}

View File

@@ -15,6 +15,7 @@
"google-spreadsheet": "^3.2.0",
"models": "*",
"next": "^12.0.7",
"nodemailer": "^6.7.2",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"utils": "*"
@@ -23,6 +24,7 @@
"@types/cors": "^2.8.12",
"@types/google-spreadsheet": "^3.1.5",
"@types/node": "^17.0.4",
"@types/nodemailer": "^6.4.4",
"@types/react": "^17.0.38",
"@typescript-eslint/eslint-plugin": "^5.9.0",
"eslint": "<8.0.0",

View File

@@ -0,0 +1,68 @@
import prisma from 'libs/prisma'
import { SendEmailOptions, SmtpCredentialsData } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
import { createTransport } from 'nodemailer'
import { Options } from 'nodemailer/lib/smtp-transport'
import { decrypt, initMiddleware } from 'utils'
import Cors from 'cors'
const cors = initMiddleware(Cors())
const defaultTransportOptions: Options = {
host: process.env.EMAIL_NOTIFICATIONS_SERVER_HOST,
port: Number(process.env.EMAIL_NOTIFICATIONS_SERVER_PORT),
secure: false,
auth: {
user: process.env.EMAIL_NOTIFICATIONS_SERVER_USER,
pass: process.env.EMAIL_NOTIFICATIONS_SERVER_PASSWORD,
},
}
const defaultFrom = `"${process.env.NEXT_PUBLIC_EMAIL_NOTIFICATIONS_FROM_NAME}" <${process.env.NEXT_PUBLIC_EMAIL_NOTIFICATIONS_FROM_EMAIL}>`
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
await cors(req, res)
if (req.method === 'POST') {
const { credentialsId, recipients, body, subject } = JSON.parse(
req.body
) as SendEmailOptions
const credentials = await prisma.credentials.findUnique({
where: { id: credentialsId },
})
if (!credentials)
return res.status(404).send({ message: "Couldn't find credentials" })
const { host, port, isTlsEnabled, username, password, from } = decrypt(
credentials.data,
credentials.iv
) as SmtpCredentialsData
const transporter = createTransport(
credentialsId === 'default'
? defaultTransportOptions
: {
host,
port,
secure: isTlsEnabled ?? undefined,
auth: {
user: username,
pass: password,
},
}
)
const info = await transporter.sendMail({
from:
credentialsId === 'default'
? defaultFrom
: `"${from.name}" <${from.email}>`,
to: recipients.join(', '),
subject,
text: body,
})
res.status(200).send({ message: 'Email sent!', info })
}
}
export default handler