-
Notifications
You must be signed in to change notification settings - Fork 3
/
notifyViewedProject.js
53 lines (46 loc) · 1 KB
/
notifyViewedProject.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 moment = require('moment');
const gql = String.raw;
async function notifyViewedProject(prisma) {
const subscription = await prisma.$subscribe
.customerEvent({
mutation_in: ['CREATED'],
node: {type: 'VIEWED_PROJECT'},
})
.node().$fragment(gql`
fragment CustomerEventWithCustomer on CustomerEvent {
id
customer {
id
}
}
`);
// eslint-disable-next-line no-constant-condition
while (true) {
const {
value: {id, customer},
} = await subscription.next();
const [user] = await prisma.users({
where: {
company: {
customers_some: {id: customer.id},
},
notifications_none: {
customerEvent: {type: 'VIEWED_PROJECT', customer: {id: customer.id}},
createdAt_gt: moment()
.subtract(1, 'days')
.format(),
},
},
});
if (user) {
await prisma.createNotification({
customerEvent: {connect: {id}},
user: {connect: {id: user.id}},
});
}
}
}
module.exports = {
notifyViewedProject,
};