-
Notifications
You must be signed in to change notification settings - Fork 0
/
purchaseNotification.ts
66 lines (60 loc) · 1.97 KB
/
purchaseNotification.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import * as admin from "firebase-admin";
import * as functions from "firebase-functions";
import {initializeApp, internalRecipientIds} from "./common";
initializeApp();
export const purchaseNotification = functions
.firestore
.onDocumentCreated("purchases/{purchase_id}", async (event) => {
try {
const snapshot = event.data;
if (!snapshot) {
console.log("No data associated with the event");
return;
}
const userId: string = snapshot.data().userId;
const productId: string = snapshot.data().productId;
await sendNotification(userId, productId);
} catch (error) {
console.log(error);
}
});
/**
* Send push notifications to internal users on every new purchases.
* @param {string} userId The user who made the purchase.
* @param {string} productId The product id the user purchased.
*/
async function sendNotification(
userId: string,
productId: string,
) {
const userSnap = await admin.firestore().doc("users/" + userId).get();
const username = userSnap.get("name");
const userPhotoUrl = userSnap.get("photoUrl");
const createdOn = userSnap.get("createdOn");
const eightHourMillis = 3600000 * 8;
// eslint-disable-next-line max-len
const joinDate = new Date(+createdOn + eightHourMillis).toLocaleString("zh-TW");
let product;
switch (productId) {
case "budgetplus.premium": {
product = "極簡記帳進階版";
break;
}
}
for (const recipientId of internalRecipientIds) {
const recipient = await admin.firestore().doc("users/" + recipientId).get();
const fcmToken = recipient.get("fcmToken");
const messagePayload = {
token: fcmToken,
data: {
type: "general",
title: "現金入袋 🤑",
body: username + "購買了" + product + "\n加入時間:" + joinDate,
smallImageUrl: userPhotoUrl,
},
};
if (fcmToken != null) {
await admin.messaging().send(messagePayload);
}
}
}