Skip to content

Commit

Permalink
working key and draft for creating/sending batches
Browse files Browse the repository at this point in the history
  • Loading branch information
NIDHI2023 committed Dec 6, 2024
1 parent d6ec846 commit c6ae35d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 29 deletions.
37 changes: 30 additions & 7 deletions src/scripts/wrapped-email.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,21 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
}
};
exports.__esModule = true;
// import { Resend } from 'resend';
var resend_1 = require("resend");
var firebase_admin_1 = require("firebase-admin");
var dotenv = require("dotenv");
firebase_admin_1["default"].initializeApp({
credential: firebase_admin_1["default"].credential.applicationDefault(),
databaseURL: 'https://qmi-test.firebaseio.com'
});
// i think api key goes in here
// const resend = new Resend('re_123456789');
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);
var createBatches = function (totalEmails, batchSize) {
};
(function () { return __awaiter(void 0, void 0, void 0, function () {
var usersRef, usersSnapshot, userEmails;
var usersRef, usersSnapshot, userEmails, data, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
Expand All @@ -58,8 +63,6 @@ firebase_admin_1["default"].initializeApp({
.get()];
case 1:
usersSnapshot = _a.sent();
// eslint-disable-next-line no-console
console.log("hi");
return [4 /*yield*/, Promise.all(usersSnapshot.docs.map(function (doc) { return __awaiter(void 0, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, doc.get('email')];
Expand All @@ -69,7 +72,27 @@ firebase_admin_1["default"].initializeApp({
userEmails = _a.sent();
// eslint-disable-next-line no-console
console.log(userEmails);
return [2 /*return*/];
_a.label = 3;
case 3:
_a.trys.push([3, 5, , 6]);
return [4 /*yield*/, resend.emails.send({
from: '[email protected]',
to: ['[email protected]'],
bcc: ['[email protected]'],
subject: 'QMI testing',
html: '<strong>It works!</strong>'
})];
case 4:
data = _a.sent();
// eslint-disable-next-line no-console
console.log(data);
return [3 /*break*/, 6];
case 5:
error_1 = _a.sent();
// eslint-disable-next-line no-console
console.error(error_1);
return [3 /*break*/, 6];
case 6: return [2 /*return*/];
}
});
}); })();
63 changes: 41 additions & 22 deletions src/scripts/wrapped-email.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
// import { Resend } from 'resend';
import { Resend } from 'resend';
import admin from "firebase-admin";
import * as dotenv from 'dotenv';

Check failure on line 3 in src/scripts/wrapped-email.ts

View workflow job for this annotation

GitHub Actions / build

'dotenv' should be listed in the project's dependencies. Run 'npm i -S dotenv' to add it

admin.initializeApp({
credential: admin.credential.applicationDefault(),
databaseURL: 'https://qmi-test.firebaseio.com'

});

// i think api key goes in here (below is not a real one)
// const resend = new Resend('re_123456789');
dotenv.config();
const resend = new Resend(process.env.RESEND_API_KEY);

const createBatches = (totalEmails: string[], batchSize: number) => {

Check failure on line 14 in src/scripts/wrapped-email.ts

View workflow job for this annotation

GitHub Actions / build

'createBatches' is assigned a value but never used
let i = 0;
const emailObjs = [];
while (i < totalEmails.length) {

emailObjs.push(
{
from: '[email protected]',
to: ['[email protected]'],
bcc: totalEmails.slice(i, Math.min(i+batchSize, totalEmails.length)),
subject: 'QMI testing batch ' + i + '!',
html: '<strong>It works!</strong>'
}
)
i+= batchSize;
}
return emailObjs;

}

Expand All @@ -19,8 +36,7 @@ const createBatches = (totalEmails: string[], batchSize: number) => {
const usersRef = admin.firestore().collection('users');
// eslint-disable-next-line no-console
console.log('firebase worked');
// using orderBy for email ensure that an email exists

// using orderBy for email field to filter out users that don't have an email
const usersSnapshot = await usersRef
.where('wrapped', '==',true)
.orderBy('email')
Expand All @@ -35,21 +51,24 @@ const createBatches = (totalEmails: string[], batchSize: number) => {

// TODO: Creating batch chunks - max limit of 49 for each batch
// TODO: Ensure we never exceed the 100 emails per day limit
// TODO: look more into resend api idk what's frree so once i make account:
// https://resend.com/docs/api-reference/emails/send-batch-emails
// try {
// const data = await resend.emails.send({
// from: 'Acme <[email protected]>',
// to: ['[email protected]'],
// bcc:
// subject: 'QMI testing',
// html: '<strong>It works!</strong>'
// });

// // eslint-disable-next-line no-console
// console.log(data);
// } catch (error) {
// // eslint-disable-next-line no-console
// console.error(error);
// }
//alternate structure, could use resend.batch.send with arrray of data
try {
//ALT:
// const data = await resend.batch.send(
// createBatches(userEmails, 49));

const data = await resend.emails.send({
from: '[email protected]',
to: ['[email protected]'],
bcc: ['[email protected]'],
subject: 'QMI testing',
html: '<strong>It works!</strong>'
});

// eslint-disable-next-line no-console
console.log(data);
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
}
})();

0 comments on commit c6ae35d

Please sign in to comment.