From 098d54c7aaf63c811ed67dccfe1075a474bb0783 Mon Sep 17 00:00:00 2001 From: NIDHI2023 Date: Fri, 6 Dec 2024 21:01:32 -0500 Subject: [PATCH] added checks and documentation --- src/scripts/wrapped-email.js | 25 +++++++++++++++++++++++-- src/scripts/wrapped-email.ts | 14 +++++++++++--- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/scripts/wrapped-email.js b/src/scripts/wrapped-email.js index d9589605..83aacaad 100644 --- a/src/scripts/wrapped-email.js +++ b/src/scripts/wrapped-email.js @@ -44,10 +44,31 @@ firebase_admin_1["default"].initializeApp({ databaseURL: 'https://qmi-test.firebaseio.com' }); dotenv.config(); -// eslint-disable-next-line no-console -console.log(process.env.RESEND_API_KEY); var resend = new resend_1.Resend(process.env.RESEND_API_KEY); +/* Returns an array of email objects to send - should be at most 100 per day. +totalEmails is +batchSize should be 49 or less to maintain free emailing */ var createBatches = function (totalEmails, batchSize) { + var i = 0; + var emailObjs = []; + if (batchSize > 49) { + console.log("Batch size is too large. Must be no more than 49"); + } + while (i < totalEmails.length && emailObjs.length <= 100) { + emailObjs.push({ + from: 'queuemein@cornelldti.org', + to: ['ns848@cornell.edu'], + bcc: totalEmails.slice(i, Math.min(i + batchSize, totalEmails.length)), + subject: 'QMI testing batch ' + i + '!', + html: 'It works!' + }); + i += batchSize; + } + if (emailObjs.length == 100) { + // eslint-disable-next-line no-console + console.log("Reached email limit of 100 emails per day."); + } + return emailObjs; }; (function () { return __awaiter(void 0, void 0, void 0, function () { var usersRef, usersSnapshot, userEmails, data, error_1; diff --git a/src/scripts/wrapped-email.ts b/src/scripts/wrapped-email.ts index cf50ec56..bb092c68 100644 --- a/src/scripts/wrapped-email.ts +++ b/src/scripts/wrapped-email.ts @@ -11,10 +11,16 @@ admin.initializeApp({ dotenv.config(); const resend = new Resend(process.env.RESEND_API_KEY); +/* Returns an array of email objects to send - should be at most 100 per day. +totalEmails is +batchSize should be 49 or less to maintain free emailing */ const createBatches = (totalEmails: string[], batchSize: number) => { let i = 0; const emailObjs = []; - while (i < totalEmails.length) { + if (batchSize > 49) { + console.log("Batch size is too large. Must be no more than 49"); + } + while (i < totalEmails.length && emailObjs.length <= 100) { emailObjs.push( { @@ -27,6 +33,10 @@ const createBatches = (totalEmails: string[], batchSize: number) => { ) i+= batchSize; } + if (emailObjs.length == 100) { + // eslint-disable-next-line no-console + console.log("Reached email limit of 100 emails per day.") + } return emailObjs; } @@ -49,8 +59,6 @@ const createBatches = (totalEmails: string[], batchSize: number) => { // eslint-disable-next-line no-console console.log(userEmails); - // TODO: Creating batch chunks - max limit of 49 for each batch - // TODO: Ensure we never exceed the 100 emails per day limit //alternate structure, could use resend.batch.send with arrray of data try { //ALT: