Skip to content

Commit

Permalink
refactor(functions): send confirmation to dev email after sending fir…
Browse files Browse the repository at this point in the history
…st payout email (#831)
  • Loading branch information
mkue authored May 16, 2024
1 parent c906fbd commit 98cb83a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 20 deletions.
48 changes: 32 additions & 16 deletions functions/src/cron/first-payout-email/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { logger } from 'firebase-functions';
import { onSchedule } from 'firebase-functions/v2/scheduler';
import { DateTime } from 'luxon';
import { FirestoreAdmin } from '../../../../shared/src/firebase/admin/FirestoreAdmin';
import { toFirebaseAdminTimestamp } from '../../../../shared/src/firebase/admin/utils';
import { FirstPayoutEmailTemplateData, SendgridMailClient } from '../../../../shared/src/sendgrid/SendgridMailClient';
import { Contribution, CONTRIBUTION_FIRESTORE_PATH } from '../../../../shared/src/types/contribution';
import { LanguageCode } from '../../../../shared/src/types/language';
import { User, USER_FIRESTORE_PATH } from '../../../../shared/src/types/user';
import { toDateTime } from '../../../../shared/src/utils/date';
import { onSchedule } from 'firebase-functions/v2/scheduler';
import { DateTime } from 'luxon';
import { FirstPayoutEmailTemplateData, SendgridMailClient } from '../../../../shared/src/sendgrid/SendgridMailClient';

export const getFirstPayoutEmailReceivers = async (
firestoreAdmin: FirestoreAdmin,
Expand Down Expand Up @@ -61,20 +62,35 @@ export const getFirstPayoutEmailReceivers = async (
).flat();
};

// Run on the 16th of every month at 00:00
export default onSchedule('0 18 16 * *', async () => { // TODO: change back to '0 0 16 * *
// Run on the 16th of every month at 15:00 UTC
export default onSchedule('0 15 16 * *', async () => {
let message: string = '';
const sendgridClient = new SendgridMailClient(process.env.SENDGRID_API_KEY!);
const firestoreAdmin = new FirestoreAdmin();
try {
const firestoreAdmin = new FirestoreAdmin();
const now = DateTime.now();
const fromDate = DateTime.fromObject({ year: now.year, month: now.month - 1, day: 16, hour: 0 }, { zone: 'utc' });
const toDate = DateTime.fromObject({ year: now.year, month: now.month, day: 16, hour: 0 }, { zone: 'utc' });
const firstPayoutEmailReceivers = await getFirstPayoutEmailReceivers(firestoreAdmin, fromDate, toDate);

const now = DateTime.now();
const fromDate = DateTime.fromObject({ year: now.year, month: now.month - 1, day: 16, hour: 0 }, { zone: 'utc' });
const toDate = DateTime.fromObject({ year: now.year, month: now.month, day: 16, hour: 0 }, { zone: 'utc' });
const firstPayoutEmailReceivers = await getFirstPayoutEmailReceivers(firestoreAdmin, fromDate, toDate);
await Promise.all(
firstPayoutEmailReceivers.map(async (entry) => {
const { email, language, templateData } = entry;
await sendgridClient.sendFirstPayoutEmail(email, language, templateData);
}),
);

await Promise.all(
firstPayoutEmailReceivers.map(async (entry) => {
const { email, language, templateData } = entry;
await sendgridClient.sendFirstPayoutEmail(email, language, templateData);
}),
);
message = `Successfully sent first payout emails to ${firstPayoutEmailReceivers.length} users`;
logger.info(message);
} catch (error) {
message = `Failed to send first payout emails: ${error}`;
logger.error(message);
} finally {
sendgridClient.send({
to: '[email protected]',
from: '[email protected]',
subject: 'First payout email cron job',
text: message,
});
}
});
4 changes: 2 additions & 2 deletions functions/src/cron/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import importExchangeRatesFunction from './exchange-rate-import';
import importPostfinancePaymentsFilesFunction from './postfinance-payments-files-import';
import sendFirstPayoutEmailFunction from './first-payout-email';
import importPostfinancePaymentsFilesFunction from './postfinance-payments-files-import';

export const importPostfinancePaymentsFiles = importPostfinancePaymentsFilesFunction;
export const importExchangeRates = importExchangeRatesFunction;
export const sendFirstPayoutEmail = sendFirstPayoutEmailFunction
export const sendFirstPayoutEmail = sendFirstPayoutEmailFunction;
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"dependencies": {
"@mailchimp/mailchimp_marketing": "^3.0.80",
"@sendgrid/mail": "^8.1.1",
"axios": "^1.6.8",
"firebase-admin": "^12.0.0",
"handlebars": "^4.7.8",
Expand Down
4 changes: 2 additions & 2 deletions shared/src/sendgrid/SendgridMailClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ export class SendgridMailClient extends MailService {
}

sendFirstPayoutEmail = async (email: string, language: LanguageCode, data: FirstPayoutEmailTemplateData) => {
let templateId = '';
let templateId;
if (language === 'de') {
templateId = 'd-3146eb9ee2054b28b22376113eb96ca9';
} else {
templateId = 'd-4e616d721b0240509f468c1e5ff22e6d';
}
await this.send({
to: email,
from: { name: 'Social Income', email: 'hello@socialincome.org' },
from: { name: 'Aurélie Schmiedlin', email: 'auerlie@socialincome.org' },
templateId,
dynamicTemplateData: data,
});
Expand Down

0 comments on commit 98cb83a

Please sign in to comment.