-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
127 lines (123 loc) · 4.4 KB
/
index.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const { postEventDB, fetchEventDB } = require('./services/DynamoDB');
const {
getNewTrials,
getTransactions,
getExpiredAndCancelledTrials,
getChurnedSubscriptions
} = require('./services/Marketplace');
const {
sendNewTrial,
sendTransaction,
sendExpiredAndCancelledTrial,
sendChurnedSubscription,
sendMarketingAttribution
} = require('./services/Destination');
const { convertDate, greaterThan, twoWeeksAgo } = require('./services/Date');
const checkNewTrials = async () => {
const licenses = await getNewTrials();
if(!licenses || licenses.length == 0) return;
for(let index of Object.keys(licenses)){
const license = licenses[index];
const eventName = 'trial_started';
const dbRecordKey = `${eventName}-${license.addonLicenseId}-${convertDate(license.lastUpdated)}`;
const isEventRegistered = await fetchEventDB({
key: dbRecordKey
});
if(!isEventRegistered || !isEventRegistered.Item) {
const wasSent = await sendNewTrial({license, eventName});
if(!wasSent) continue;
await postEventDB({
key: dbRecordKey,
value: convertDate(new Date())
});
}
if(license.attribution && license.attribution.channel) {
const dbMarketingAttributionRecordKey = `marketing_attribution-${license.addonLicenseId}`;
const isMarketingAttributionRegistered = await fetchEventDB({
key: dbMarketingAttributionRecordKey
});
if(!isMarketingAttributionRegistered || !isMarketingAttributionRegistered.Item) {
const wasSent = await sendMarketingAttribution(license);
if(!wasSent) continue;
await postEventDB({
key: dbMarketingAttributionRecordKey,
value: convertDate(new Date())
});
}
}
}
};
const checkTransactions = async () => {
const transactions = await getTransactions();
if(!transactions || transactions.length == 0) return;
for(let index of Object.keys(transactions)){
const transaction = transactions[index];
const eventName = 'subscription_paid';
//start temporary fix
const dbRecordKeyLegacy = `${eventName}-${transaction.transactionId}`;
const isEventRegisteredLegacy = await fetchEventDB({
key: dbRecordKeyLegacy
});
if(isEventRegisteredLegacy.item) continue;
//end temporary fix
const dbRecordKey = `${eventName}-${transaction.addonLicenseId}-${transaction.transactionId}`;
const isEventRegistered = await fetchEventDB({
key: dbRecordKey
});
if(!isEventRegistered || !isEventRegistered.Item) {
const wasSent = await sendTransaction({transaction, eventName});
if(!wasSent) continue;
await postEventDB({
key: dbRecordKey,
value: convertDate(new Date())
});
}
}
};
const checkExpiredAndCancelledTrials = async () => {
const licenses = await getExpiredAndCancelledTrials();
if(!licenses || licenses.length == 0) return;
for(let index of Object.keys(licenses)){
const license = licenses[index];
const eventName = (license.status == 'cancelled')? 'trial_cancelled': 'trial_expired';
const dbRecordKey = `${eventName}-${license.addonLicenseId}-${convertDate(license.lastUpdated)}`;
const isEventRegistered = await fetchEventDB({
key: dbRecordKey
});
if(!isEventRegistered || !isEventRegistered.Item) {
const wasSent = await sendExpiredAndCancelledTrial({license, eventName});
if(!wasSent) continue;
await postEventDB({
key: dbRecordKey,
value: convertDate(new Date())
});
}
}
};
const checkChurnedSubscriptions = async () => {
const licenses = await getChurnedSubscriptions();
if(!licenses || licenses.length == 0) return;
for(let index of Object.keys(licenses)){
const license = licenses[index];
const eventName = 'subscription_churned';
const dbRecordKey = `${eventName}-${license.addonLicenseId}-${convertDate(license.lastUpdated)}`;
const isEventRegistered = await fetchEventDB({
key: dbRecordKey
});
if(!isEventRegistered || !isEventRegistered.Item) {
const wasSent = await sendChurnedSubscription({license, eventName});
if(!wasSent) continue;
await postEventDB({
key: dbRecordKey,
value: convertDate(new Date())
});
}
}
};
exports.handler = async (event, context, callback) => {
await checkNewTrials();
await checkTransactions();
await checkExpiredAndCancelledTrials();
await checkChurnedSubscriptions();
callback(null);
};