Add email integration to be able to broadcast emails to all users.

Resolves #388
This commit is contained in:
Kurt Hutten
2021-06-29 06:37:04 +10:00
parent 7ef8d8d1ff
commit 0da15443cb
13 changed files with 173 additions and 5 deletions

View File

@@ -121,7 +121,8 @@ export const getCurrentUser = async (decoded, { _token, _type }) => {
* requireAuth({ role: ['editor', 'author'] })
* requireAuth({ role: ['publisher'] })
*/
export const requireAuth = ({ role } = {}) => {
export const requireAuth = ({ role }: {role?: string | string[]} = {}) => {
console.log(context.currentUser)
if (!context.currentUser) {
throw new AuthenticationError("You don't have permission to do that.")
}

View File

@@ -0,0 +1,56 @@
import nodemailer, {SendMailOptions} from 'nodemailer'
interface Args {
to: SendMailOptions['to']
from: SendMailOptions['from']
subject: string
text: string
}
interface SuccessResult {
accepted: string[]
rejected: string[]
envelopeTime: number
messageTime: number
messageSize: number
response: string
envelope: {
from: string | false,
to: string[]
},
messageId: string
}
export function sendMail({to, from, subject, text}: Args): Promise<SuccessResult> {
let transporter = nodemailer.createTransport({
host: 'smtp.mailgun.org',
port: 587,
secure: false,
tls: {
ciphers:'SSLv3'
},
auth: {
user: 'postmaster@mail.cadhub.xyz',
pass: process.env.EMAIL_PASSWORD
}
});
console.log({to, from, subject, text});
const emailPromise = new Promise((resolve, reject) => {
transporter.sendMail({
from,
to,
subject,
text,
}, (error, info) => {
if (error) {
reject(error);
} else {
resolve(info);
}
});
}) as any as Promise<SuccessResult>
return emailPromise
}