-
-
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.
feature(functions): first payout email function (initial version) (#789)
- Loading branch information
Showing
9 changed files
with
213 additions
and
56 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
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 |
---|---|---|
|
@@ -3,8 +3,6 @@ GCLOUD_PROJECT="social-income-staging" | |
FIRESTORE_EMULATOR_HOST=127.0.0.1:8080 | ||
FIREBASE_AUTH_EMULATOR_HOST=127.0.0.1:9099 | ||
|
||
POSTFINANCE_EMAIL_USER=[email protected] | ||
POSTFINANCE_EMAIL_PASSWORD=password | ||
POSTFINANCE_PAYMENTS_FILES_BUCKET=postfinance-payments-files | ||
POSTFINANCE_FTP_HOST=ftp.postfinance.example | ||
POSTFINANCE_FTP_PORT=21 | ||
|
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
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { FirestoreAdmin } from '@socialincome/shared/src/firebase/admin/FirestoreAdmin'; | ||
import { toFirebaseAdminTimestamp } from '@socialincome/shared/src/firebase/admin/utils'; | ||
import { Contribution, CONTRIBUTION_FIRESTORE_PATH } from '@socialincome/shared/src/types/contribution'; | ||
import { User, USER_FIRESTORE_PATH } from '@socialincome/shared/src/types/user'; | ||
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, | ||
from: DateTime, | ||
to: DateTime, | ||
): Promise< | ||
{ | ||
email: string; | ||
templateData: FirstPayoutEmailTemplateData; | ||
}[] | ||
> => { | ||
const users = await firestoreAdmin.collection<User>(USER_FIRESTORE_PATH).get(); | ||
return ( | ||
await Promise.all( | ||
users.docs | ||
.filter((userDoc) => !userDoc.get('test_user')) | ||
.map(async (userDoc) => { | ||
const user = userDoc.data(); | ||
const firstContribution = await firestoreAdmin | ||
.collection(`${USER_FIRESTORE_PATH}/${userDoc.id}/${CONTRIBUTION_FIRESTORE_PATH}`) | ||
.orderBy('created', 'asc') | ||
.limit(1) | ||
.get(); | ||
|
||
if (firstContribution.empty) return []; | ||
const contribution = firstContribution.docs[0].data() as Contribution; | ||
if (!contribution) return []; | ||
if ( | ||
contribution.created >= toFirebaseAdminTimestamp(from) && | ||
contribution.created < toFirebaseAdminTimestamp(to) | ||
) { | ||
return [ | ||
{ | ||
email: user.email, | ||
templateData: { | ||
email: user.email, | ||
first_name: user.personal?.name, | ||
donation_amount: contribution.amount, | ||
currency: contribution.currency, | ||
}, | ||
}, | ||
]; | ||
} | ||
return []; | ||
}), | ||
) | ||
).flat(); | ||
}; | ||
|
||
// Run on the 16th of every month at 00:00 | ||
export default onSchedule('0 0 16 * *', async () => { | ||
const sendgridClient = new SendgridMailClient(process.env.SENDGRID_API_KEY!); | ||
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); | ||
|
||
await Promise.all( | ||
firstPayoutEmailReceivers.map(async (entry) => { | ||
const { email, templateData } = entry; | ||
await sendgridClient.sendFirstPayoutEmail(email, templateData); | ||
}), | ||
); | ||
}); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { MailService } from '@sendgrid/mail'; | ||
import { Currency } from '../types/currency'; | ||
|
||
export type FirstPayoutEmailTemplateData = { | ||
first_name?: string; | ||
donation_amount: number; | ||
currency: Currency; | ||
}; | ||
|
||
export class SendgridMailClient extends MailService { | ||
constructor(apiKey: string) { | ||
super(); | ||
this.setApiKey(apiKey); | ||
} | ||
|
||
sendFirstPayoutEmail = async (email: string, data: FirstPayoutEmailTemplateData) => { | ||
await this.send({ | ||
to: email, | ||
from: '[email protected]', | ||
templateId: 'd-4e616d721b0240509f468c1e5ff22e6d', | ||
dynamicTemplateData: data, | ||
}); | ||
}; | ||
} |
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