Skip to content

Commit

Permalink
fix(email): keep polling for new notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
BastiDood committed Aug 3, 2024
1 parent d31ba61 commit c653ceb
Showing 1 changed file with 56 additions and 53 deletions.
109 changes: 56 additions & 53 deletions email/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,65 +18,68 @@ const controller = new AbortController();
process.on('SIGINT', () => controller.abort());

async function listenForDraftNotifications(emailer: Emailer, signal: AbortSignal) {
for await (const payload of emailer.db.listen('notify:draft', signal)) {
const email = await emailer.db.begin(async db => {
const notif = await db.getOneDraftNotification();
if (notif === null) return null;
for await (const payload of emailer.db.listen('notify:draft', signal))
while (true) {
const email = await emailer.db.begin(async db => {
const notif = await db.getOneDraftNotification();
if (notif === null) return null;

const meta = (() => {
switch (notif.ty) {
case 'DraftRoundStarted':
return {
emails: db.getValidFacultyAndStaffEmails(),
subject: `[DRAP] Round #${notif.round} for Draft #${notif.draft_id} has begun!`,
message: `Round #${notif.round} for Draft #${notif.draft_id} has begun. For lab heads, kindly check the students module to see the list of students who have chosen your lab.`,
};
case 'DraftRoundSubmitted':
return {
emails: db.getValidStaffEmails(),
subject: `[DRAP] Acknowledgement from ${notif.lab_id.toUpperCase()} for Round #${notif.round} of Draft #${notif.draft_id}`,
message: `${notif.given_name} ${notif.family_name} has submitted their student preferences on behalf of the ${notif.lab_name} for Round #${notif.round} of Draft #${notif.draft_id}.`,
};
case 'LotteryIntervention':
return {
emails: db.getValidFacultyAndStaffEmails(),
subject: `[DRAP] Lottery Intervention for ${notif.lab_id.toUpperCase()} in Draft #${notif.draft_id}`,
message: ``,
};
default:
return null;
}
})();
const meta = (() => {
switch (notif.ty) {
case 'DraftRoundStarted':
return {
emails: db.getValidFacultyAndStaffEmails(),
subject: `[DRAP] Round #${notif.round} for Draft #${notif.draft_id} has begun!`,
message: `Round #${notif.round} for Draft #${notif.draft_id} has begun. For lab heads, kindly check the students module to see the list of students who have chosen your lab.`,
};
case 'DraftRoundSubmitted':
return {
emails: db.getValidStaffEmails(),
subject: `[DRAP] Acknowledgement from ${notif.lab_id.toUpperCase()} for Round #${notif.round} of Draft #${notif.draft_id}`,
message: `${notif.given_name} ${notif.family_name} has submitted their student preferences on behalf of the ${notif.lab_name} for Round #${notif.round} of Draft #${notif.draft_id}.`,
};
case 'LotteryIntervention':
return {
emails: db.getValidFacultyAndStaffEmails(),
subject: `[DRAP] Lottery Intervention for ${notif.lab_id.toUpperCase()} in Draft #${notif.draft_id}`,
message: ``,
};
default:
return null;
}
})();

assert(meta !== null, 'notify:draft => unexpected notification type');
const email = await emailer.send(await meta.emails, meta.subject, meta.message);
assert(await db.dropDraftNotification(notif.notif_id), 'cannot drop non-existent notification');
return email;
});
assert(meta !== null, 'notify:draft => unexpected notification type');
const email = await emailer.send(await meta.emails, meta.subject, meta.message);
assert(await db.dropDraftNotification(notif.notif_id), 'cannot drop non-existent notification');
return email;
});

const logger = emailer.db.logger.child({ payload });
if (email === null) logger.warn({ email });
else logger.info({ email });
}
if (email === null) break;
emailer.db.logger.info({ payload, email });
}
}

async function listenForUserNotifications(emailer: Emailer, signal: AbortSignal) {
for await (const payload of emailer.db.listen('notify:user', signal)) {
const email = await emailer.db.begin(async db => {
const notif = await db.getOneUserNotification();
if (notif === null) return null;
const email = await emailer.send(
[notif.email],
`[DRAP] Assigned to ${notif.lab_id.toUpperCase()}`,
`Hello, ${notif.given_name} ${notif.given_name}! Kindly note that you have been assigned to the ${notif.lab_name}.`,
);
assert(await db.dropUserNotification(notif.notif_id), 'cannot drop non-existent notification');
return email;
});
const logger = emailer.db.logger.child({ payload });
if (email === null) logger.warn({ email });
else logger.info({ email });
}
for await (const payload of emailer.db.listen('notify:user', signal))
while (true) {
const email = await emailer.db.begin(async db => {
const notif = await db.getOneUserNotification();
if (notif === null) return null;

const email = await emailer.send(
[notif.email],
`[DRAP] Assigned to ${notif.lab_id.toUpperCase()}`,
`Hello, ${notif.given_name} ${notif.given_name}! Kindly note that you have been assigned to the ${notif.lab_name}.`,
);

assert(await db.dropUserNotification(notif.notif_id), 'cannot drop non-existent notification');
return email;
});

if (email === null) break;
emailer.db.logger.info({ payload, email });
}
}

// Main event loop of the email worker
Expand Down

0 comments on commit c653ceb

Please sign in to comment.