-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
working key and draft for creating/sending batches
- Loading branch information
Showing
2 changed files
with
71 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
|
@@ -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')]; | ||
|
@@ -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*/]; | ||
} | ||
}); | ||
}); })(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
|
||
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) => { | ||
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; | ||
|
||
} | ||
|
||
|
@@ -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') | ||
|
@@ -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); | ||
} | ||
})(); |