-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(functions): send confirmation to dev email after sending fir…
…st payout email (#831)
- Loading branch information
Showing
5 changed files
with
38 additions
and
20 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 |
---|---|---|
@@ -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, | ||
|
@@ -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, | ||
}); | ||
} | ||
}); |
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,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; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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