-
Notifications
You must be signed in to change notification settings - Fork 3
/
intercomTracking.js
53 lines (45 loc) · 1.02 KB
/
intercomTracking.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
/* eslint-disable no-await-in-loop */
const fetch = require('node-fetch');
function updateIntercomUser(email, data) {
return fetch('https://api.intercom.io/users', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.INTERCOM_TOKEN}`,
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
email,
custom_attributes: data,
}),
});
}
async function trackProjectCounts(prisma) {
const subscription = await prisma.$subscribe
.project({
mutation_in: ['CREATED', 'UPDATED', 'DELETED'],
})
.node()
.owner();
// eslint-disable-next-line no-constant-condition
while (true) {
const {
value: {id, email},
} = await subscription.next();
const count = await prisma
.projectsConnection({
where: {
owner: {id},
},
})
.aggregate()
.count();
await updateIntercomUser(email, {'projects-count': count});
}
}
function subscribeToUpdateIntercom(prisma) {
trackProjectCounts(prisma);
}
module.exports = {
subscribeToUpdateIntercom,
};