-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayment_trigger.js
46 lines (40 loc) · 1.21 KB
/
payment_trigger.js
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
const utils = require("./utils/utils");
const payment = require("./models/payment");
const likecount = require("./models/likecount");
module.exports = async () => {
const pipeline = [
{
$match: {
operationType: "delete",
},
},
];
// Monitor new listings using EventEmitter's on() function.
await monitorListingsUsingEventEmitter(payment, pipeline);
await monitorListingsUsingEventEmitter(likecount, pipeline);
};
function closeChangeStream(timeInMs = 60000, changeStream) {
return new Promise((resolve) => {
setTimeout(() => {
console.log("Closing the change stream");
changeStream.close();
resolve();
}, timeInMs);
});
}
async function monitorListingsUsingEventEmitter(
client,
pipeline = []
// timeInMs = 60000,
) {
const changeStream = client.watch(pipeline);
changeStream.on("change", async (next) => {
const coll = next.ns.coll;
await utils.sendFcmMessage(
coll === "likecounts" ? "You Got More Likes" : "Subscription Expired",
coll === "likecounts" ? "Hi, open app & enjoy" : "Please Subscribe Again",
[await utils.getTokenByUserId(next.documentKey._id)]
);
});
// await closeChangeStream(timeInMs, changeStream);
}