45 lines
2.9 KiB
TypeScript
45 lines
2.9 KiB
TypeScript
import { buildEmailLayout, escapeHtml } from './emailLayout.js';
|
|
import type { SendMailOptions } from 'nodemailer';
|
|
import type { WeightReminder } from '../reminders/weightReminders.js';
|
|
import { loadWeightReminderPortrait } from './birdPortrait.js';
|
|
export { loadWeightReminderPortrait } from './birdPortrait.js';
|
|
|
|
export const buildWeightReminderEmail = async (
|
|
reminders: WeightReminder[],
|
|
frontendUrl: string,
|
|
loadPortrait = loadWeightReminderPortrait,
|
|
): Promise<Pick<SendMailOptions, 'subject' | 'text' | 'html' | 'attachments'>> => {
|
|
if (!reminders.length) throw new Error('A weight reminder email requires at least one bird');
|
|
const flock = reminders[0].workspace_name;
|
|
const attachments: NonNullable<SendMailOptions['attachments']> = [];
|
|
const rows: string[] = [];
|
|
for (const [index, bird] of reminders.entries()) {
|
|
const cid = `weight-bird-${index}`;
|
|
attachments.push({ filename: `${cid}.jpg`, cid, content: await loadPortrait(bird), contentType: 'image/jpeg', contentDisposition: 'inline' });
|
|
rows.push(`<tr>
|
|
<td width="88" style="padding:16px 12px 16px 0;border-bottom:1px solid #d3e5d8;vertical-align:middle;">
|
|
<img src="cid:${cid}" width="76" height="76" alt="${escapeHtml(bird.bird_name)}" style="display:block;border:0;border-radius:16px;" />
|
|
</td>
|
|
<td style="padding:16px 0;border-bottom:1px solid #d3e5d8;vertical-align:middle;word-break:break-word;">
|
|
<p style="margin:0 0 4px;font-size:18px;font-weight:bold;color:#1f2a2a;">${escapeHtml(bird.bird_name)}</p>
|
|
<p style="margin:0;font-size:14px;color:#52645b;">${escapeHtml(bird.species)}</p>
|
|
<p style="margin:6px 0 0;font-size:13px;color:#63562d;">Ready for a weight check</p>
|
|
</td>
|
|
</tr>`);
|
|
}
|
|
const layout = await buildEmailLayout({
|
|
eyebrow: 'Daily weight reminder',
|
|
headline: 'A little check-in for your flock',
|
|
preheader: `A little check-in for ${flock}: it's time to log your birds' weights.`,
|
|
contentHtml: `<p style="margin:0 0 16px;font-size:16px;">These birds in <strong>${escapeHtml(flock)}</strong> haven't had a new weight entry in at least 48 hours.</p><table role="presentation" width="100%" cellspacing="0" cellpadding="0" style="border-collapse:collapse;table-layout:fixed;">${rows.join('')}</table>`,
|
|
action: { url: frontendUrl, label: 'Record their weights' },
|
|
footer: "You'll receive a daily reminder while weights are overdue. A new weight entry restarts the 48-hour clock.",
|
|
});
|
|
return {
|
|
subject: `Weight reminders for ${flock}`,
|
|
text: `Daily weight reminder — ${flock}\n\nThese birds haven't had a new weight entry in at least 48 hours:\n\n${reminders.map(bird => `- ${bird.bird_name} (${bird.species})`).join('\n')}\n\nOpen FlockPal to record their weights: ${frontendUrl}\n\nYou'll receive a daily reminder while weights are overdue.`,
|
|
...layout,
|
|
attachments: [...(layout.attachments ?? []), ...attachments],
|
|
};
|
|
};
|