This repository was archived by the owner on Mar 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathCrowdfunding.js
62 lines (62 loc) · 1.56 KB
/
Crowdfunding.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
module.exports = {
async packages (crowdfunding, args, {pgdb}) {
return pgdb.public.packages.find({crowdfundingId: crowdfunding.id})
},
async goals (crowdfunding, args, {pgdb}) {
return pgdb.public.crowdfundingGoals.find({crowdfundingId: crowdfunding.id}, {
orderBy: ['people asc', 'money asc']
})
},
async status (crowdfunding, {forceUpdate}, {pgdb}) {
if (!forceUpdate && crowdfunding.result && crowdfunding.result.status) {
return crowdfunding.result.status
}
const money = await pgdb.public.queryOneField(`
SELECT
SUM(pl.total)
FROM
pledges pl
JOIN
packages pa
ON pl."packageId" = pa.id
JOIN
crowdfundings cf
ON
pa."crowdfundingId" = cf.id AND
cf.id = :crowdfundingId
WHERE
pl.status = 'SUCCESSFUL'
`, {
crowdfundingId: crowdfunding.id
}) || 0
const people = await pgdb.public.queryOneField(`
SELECT
COUNT(m.id)
FROM
memberships m
JOIN
pledges pl
ON m."pledgeId" = pl.id
JOIN
packages pa
ON pl."packageId" = pa.id
JOIN
crowdfundings cf
ON
pa."crowdfundingId" = cf.id AND
cf.id = :crowdfundingId
`, {
crowdfundingId: crowdfunding.id
}) || 0
return {money, people}
},
hasEnded (crowdfunding) {
const now = new Date()
return now > new Date(crowdfunding.endDate)
},
endVideo (crowdfunding) {
if (crowdfunding.result) {
return crowdfunding.result.endVideo
}
}
}