Skip to content

Commit d65a842

Browse files
committed
Feat: Only send for drafts
1 parent 61b8bf2 commit d65a842

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

email/mailmerge/src/pipelines/send.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,45 @@ describe("sendEmails", () => {
123123
expect(mockMailer.sendMail).toHaveBeenCalledTimes(1);
124124
});
125125

126+
it("should send no emails if only send 0", async () => {
127+
const mergeResults: MergeResultWithMetadata<unknown>[] = [
128+
{
129+
record: { field1: "value1" },
130+
/// @ts-expect-error: Mocking previews
131+
previews: ["preview"],
132+
engineInfo: { name: "testEngine", options: {} },
133+
email: { to: ["[email protected]"], subject: "Test Subject", cc: [], bcc: [] },
134+
attachmentPaths: [],
135+
},
136+
{
137+
record: { field1: "value2" },
138+
/// @ts-expect-error: Mocking previews
139+
previews: ["preview"],
140+
engineInfo: { name: "testEngine", options: {} },
141+
email: { to: ["[email protected]"], subject: "Test Subject 2", cc: [], bcc: [] },
142+
attachmentPaths: [],
143+
},
144+
];
145+
(mockStorageBackend.loadMergeResults as jest.Mock).mockReturnValue(mergeResults);
146+
147+
const enginesMap = {
148+
testEngine: mockEngineConstructor,
149+
};
150+
151+
await sendEmails(
152+
mockStorageBackend,
153+
mockMailer,
154+
'"From" <[email protected]>',
155+
enginesMap,
156+
true,
157+
{
158+
onlySend: 0,
159+
},
160+
);
161+
162+
expect(mockMailer.sendMail).toHaveBeenCalledTimes(0);
163+
});
164+
126165
it("should skip records with invalid engine", async () => {
127166
const mergeResults: MergeResultWithMetadata<unknown>[] = [
128167
{

email/mailmerge/src/pipelines/send.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ export async function sendEmails(
4444
) {
4545
logger.info(`Sending mail merge results...`);
4646

47+
if (options?.onlySend === 0) {
48+
logger.warn(`onlySend is set to 0, so no emails will be sent.`);
49+
return;
50+
}
51+
4752
// 1: Load data
4853
logger.info("Loading merge results...");
4954
const results = storageBackend.loadMergeResults();

email/mailmerge/src/pipelines/uploadDrafts.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ import { EmailUploader } from "../graph/index.js";
99
import { EmailString } from "../util/index.js";
1010
import { StorageBackend, MergeResultWithMetadata } from "./storage";
1111

12+
interface UploadDraftsOptions {
13+
/** Time to sleep between sending emails to prevent hitting rate limits */
14+
sleepBetween?: number;
15+
/** Only send this many emails (i.e. the first X emails) */
16+
onlySend?: number;
17+
}
18+
1219
/**
1320
* Upload mail merge results to drafts of an inbox using the Microsoft graph API
1421
*
@@ -28,12 +35,21 @@ export async function uploadDrafts(
2835
storageBackend: StorageBackend,
2936
enginesMap: Record<string, TemplateEngineConstructor> = ENGINES_MAP,
3037
disablePrompt = false,
31-
sleepBetween = 0,
38+
options: UploadDraftsOptions = {
39+
sleepBetween: 0,
40+
},
3241
entraTenantId = process.env["MS_ENTRA_TENANT_ID"],
3342
entraClientId = process.env["MS_ENTRA_CLIENT_ID"],
3443
expectedEmail = "[email protected]",
3544
logger = createLogger("docsoc"),
3645
) {
46+
const { sleepBetween = 0, onlySend } = options;
47+
48+
if (onlySend === 0) {
49+
logger.warn(`onlySend is set to 0, so no emails will be sent.`);
50+
return;
51+
}
52+
3753
logger.info(`Uploading previews to drafts...`);
3854
// 1: Load data
3955
logger.info("Loading merge results...");
@@ -139,6 +155,12 @@ export async function uploadDrafts(
139155
options,
140156
);
141157

158+
// Only send check
159+
if (onlySend && sent >= onlySend) {
160+
logger.warn(`Only sending ${onlySend} emails as requested.`);
161+
break;
162+
}
163+
142164
if (sleepBetween > 0) {
143165
logger.warn(`Sleeping for ${sleepBetween} seconds to prevent hitting rate limits...`);
144166
await new Promise((resolve) => setTimeout(resolve, sleepBetween * 1000));

0 commit comments

Comments
 (0)