refactor: improve news and email handling, style, folder structure (#16)
This commit is contained in:
@@ -1,43 +1,61 @@
|
||||
import { Resend } from 'resend';
|
||||
|
||||
type EmailTemplate = {
|
||||
interface EmailTemplate {
|
||||
subject: string;
|
||||
template: JSX.Element;
|
||||
};
|
||||
}
|
||||
|
||||
export async function sender(
|
||||
to: string[],
|
||||
recipients: string[],
|
||||
{ subject, template }: EmailTemplate
|
||||
) {
|
||||
if (recipients.length === 0) {
|
||||
console.info(`${subject} email skipped for having zero recipients`);
|
||||
return true;
|
||||
}
|
||||
|
||||
const resend = new Resend(process.env.RESEND_KEY);
|
||||
|
||||
try {
|
||||
const { error } = await resend.batch.send(
|
||||
to.map(t => {
|
||||
return {
|
||||
from: process.env.RESEND_FROM!,
|
||||
to: t,
|
||||
subject,
|
||||
react: template,
|
||||
headers: {
|
||||
'List-Unsubscribe': `<${process.env.HOME_URL}/unsubscribe>`
|
||||
}
|
||||
};
|
||||
})
|
||||
);
|
||||
let response;
|
||||
|
||||
if (recipients.length == 1) {
|
||||
response = await resend.emails.send({
|
||||
from: process.env.RESEND_FROM!,
|
||||
to: recipients[0],
|
||||
subject,
|
||||
react: template,
|
||||
headers: {
|
||||
'List-Unsubscribe': `<${process.env.HOME_URL}/unsubscribe>`
|
||||
}
|
||||
});
|
||||
} else {
|
||||
response = await resend.batch.send(
|
||||
recipients.map(recipient => {
|
||||
return {
|
||||
from: process.env.RESEND_FROM!,
|
||||
to: recipient,
|
||||
subject,
|
||||
react: template,
|
||||
headers: {
|
||||
'List-Unsubscribe': `<${process.env.HOME_URL}/unsubscribe>`
|
||||
}
|
||||
};
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
const { error } = response;
|
||||
|
||||
if (error) {
|
||||
console.log(error);
|
||||
|
||||
console.error(error);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
|
||||
console.info(`${subject} email sent to ${recipients.length} recipients`);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return false;
|
||||
}
|
||||
|
||||
console.log('Email sent', subject, to.length);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user