-
-
Notifications
You must be signed in to change notification settings - Fork 260
/
host-metrics.js
241 lines (209 loc) · 7.64 KB
/
host-metrics.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import config from 'config';
import moment from 'moment';
import { sequelize } from '../models';
import { getFxRate } from './currency';
import { parseToBoolean } from './utils';
function oppositeTotal(total) {
return total !== 0 ? -total : total;
}
async function computeTotal(results, currency) {
let total = 0;
// For sanity reasons, we handle conversion in case there is any currency mismatch
for (const result of results) {
const value = result['_amount'];
if (value) {
const fxRate = await getFxRate(result['_currency'], currency);
total += Math.round(value * fxRate);
}
}
return total;
}
function computeDates(startDate, endDate) {
startDate = startDate ? moment(startDate) : moment().utc().startOf('month');
endDate = endDate ? moment(endDate) : moment(startDate).utc().endOf('month');
return { startDate: startDate.toISOString(), endDate: endDate.toISOString() };
}
export async function getPlatformTips(host, { startDate, endDate } = {}) {
const results = await sequelize.query(
`SELECT
SUM(
CASE
WHEN t2."data"->>'hostToPlatformFxRate' IS NOT NULL THEN
t2."amountInHostCurrency"::numeric / (t2."data"->>'hostToPlatformFxRate')::numeric
ELSE
t2."amountInHostCurrency"
END
) as "_amount",
(
CASE
WHEN t2."data"->>'hostToPlatformFxRate' IS NOT NULL THEN
h."currency"
ELSE
t2."hostCurrency"
END
) as "_currency"
FROM "Transactions" as t1
INNER JOIN "Transactions" as t2
ON t1."TransactionGroup" = t2."TransactionGroup"
INNER JOIN "Collectives" as h
ON t1."HostCollectiveId" = h."id"
WHERE t1."HostCollectiveId" = :HostCollectiveId
AND t1."createdAt" >= :startDate AND t1."createdAt" <= :endDate
AND (t1."kind" IS NULL OR t1."kind" IN ('CONTRIBUTION', 'ADDED_FUNDS'))
AND t2."kind" = 'PLATFORM_TIP'
AND t2."type" = 'CREDIT'
AND t1."deletedAt" IS NULL
AND t2."deletedAt" IS NULL
AND t2."RefundTransactionId" IS NULL
GROUP BY "_currency"`,
{
replacements: { HostCollectiveId: host.id, ...computeDates(startDate, endDate) },
type: sequelize.QueryTypes.SELECT,
},
);
return computeTotal(results, host.currency);
}
// NOTE: we're not looking at the settlementStatus and just SUM all debts of the month
export async function getPendingPlatformTips(host, { startDate, endDate } = {}) {
const results = await sequelize.query(
`SELECT SUM(t."amountInHostCurrency") AS "_amount", t."hostCurrency" as "_currency"
FROM "Transactions" t
INNER JOIN "TransactionSettlements" ts
ON t."TransactionGroup" = ts."TransactionGroup"
AND t."kind" = ts."kind"
WHERE t."HostCollectiveId" = :HostCollectiveId
AND t."isDebt" IS TRUE
AND t."kind" = 'PLATFORM_TIP_DEBT'
AND t."deletedAt" IS NULL
AND ts."deletedAt" IS NULL
AND ts."status" IN ('OWED', 'INVOICED')
AND t."createdAt" >= :startDate
AND t."createdAt" <= :endDate
GROUP BY "hostCurrency"`,
{
replacements: { HostCollectiveId: host.id, ...computeDates(startDate, endDate) },
type: sequelize.QueryTypes.SELECT,
},
);
return computeTotal(results, host.currency);
}
export async function getHostFees(host, { startDate, endDate } = {}) {
let newResults;
if (parseToBoolean(config.ledger.separateHostFees) === true) {
newResults = await sequelize.query(
`SELECT SUM(t1."amountInHostCurrency") as "_amount", t1."hostCurrency" as "_currency"
FROM "Transactions" as t1
WHERE t1."CollectiveId" = :CollectiveId
AND t1."kind" = 'HOST_FEE'
AND t1."createdAt" >= :startDate AND t1."createdAt" <= :endDate
AND t1."deletedAt" IS NULL
GROUP BY t1."hostCurrency"`,
{
replacements: { CollectiveId: host.id, ...computeDates(startDate, endDate) },
type: sequelize.QueryTypes.SELECT,
},
);
}
// TODO(Ledger): We should only run the query below if startDate < newHostFeeDeployDate
const legacyResults = await sequelize.query(
`SELECT SUM(t1."hostFeeInHostCurrency") as "_amount", t1."hostCurrency" as "_currency"
FROM "Transactions" as t1
WHERE t1."HostCollectiveId" = :HostCollectiveId
AND t1."createdAt" >= :startDate AND t1."createdAt" <= :endDate
AND NOT (t1."type" = 'DEBIT' AND t1."kind" = 'ADDED_FUNDS')
AND t1."deletedAt" IS NULL
GROUP BY t1."hostCurrency"`,
{
replacements: { HostCollectiveId: host.id, ...computeDates(startDate, endDate) },
type: sequelize.QueryTypes.SELECT,
},
);
let total = await computeTotal(legacyResults, host.currency);
// amount/hostFeeInHostCurrency is expressed as a negative number
total = oppositeTotal(total);
if (newResults?.length) {
total += await computeTotal(newResults, host.currency);
}
return total;
}
export async function getHostFeeShare(host, { startDate, endDate } = {}) {
if (parseToBoolean(config.ledger.separateHostFees) === true) {
const results = await sequelize.query(
`SELECT SUM(t1."amountInHostCurrency") as "_amount", t1."hostCurrency" as "_currency"
FROM "Transactions" as t1
WHERE t1."CollectiveId" = :CollectiveId
AND t1."type" = 'DEBIT'
AND t1."kind" = 'HOST_FEE_SHARE'
AND t1."createdAt" >= :startDate AND t1."createdAt" <= :endDate
AND t1."deletedAt" IS NULL
GROUP BY t1."hostCurrency"`,
{
replacements: { CollectiveId: host.id, ...computeDates(startDate, endDate) },
type: sequelize.QueryTypes.SELECT,
},
);
let total = await computeTotal(results, host.currency);
// we're looking at the DEBIT, so it's a negative number
total = oppositeTotal(total);
return total;
}
const hostFees = await getHostFees(host, { startDate, endDate });
const plan = await host.getPlan();
const hostFeeSharePercent = plan.hostFeeSharePercent || 0;
return Math.round((hostFees * hostFeeSharePercent) / 100);
}
export async function getPendingHostFeeShare(host, { startDate, endDate } = {}) {
if (parseToBoolean(config.ledger.separateHostFees) === true) {
const results = await sequelize.query(
`SELECT SUM(t."amountInHostCurrency") AS "_amount", t."hostCurrency" as "_currency"
FROM "Transactions" t
INNER JOIN "TransactionSettlements" ts
ON t."TransactionGroup" = ts."TransactionGroup"
AND t."kind" = ts."kind"
WHERE t."CollectiveId" = :CollectiveId
AND t."type" = 'CREDIT'
AND t."kind" = 'HOST_FEE_SHARE_DEBT'
AND t."deletedAt" IS NULL
AND ts."deletedAt" IS NULL
AND ts."status" IN ('OWED', 'INVOICED')
AND t."createdAt" >= :startDate
AND t."createdAt" <= :endDate
GROUP BY "hostCurrency"`,
{
replacements: { CollectiveId: host.id, ...computeDates(startDate, endDate) },
type: sequelize.QueryTypes.SELECT,
},
);
return computeTotal(results, host.currency);
}
const results = await sequelize.query(
`SELECT SUM(t1."hostFeeInHostCurrency") as "_amount", t1."hostCurrency" as "_currency"
FROM "Transactions" as t1
LEFT JOIN "PaymentMethods" pm ON
t1."PaymentMethodId" = pm.id
LEFT JOIN "PaymentMethods" spm ON
spm.id = pm."SourcePaymentMethodId"
WHERE t1."HostCollectiveId" = :HostCollectiveId
AND t1."createdAt" >= :startDate AND t1."createdAt" <= :endDate
AND t1."deletedAt" IS NULL
AND (
pm."service" != 'stripe'
OR pm.service IS NULL
)
AND (
spm.service IS NULL
OR spm.service != 'stripe'
)
GROUP BY t1."hostCurrency"`,
{
replacements: { HostCollectiveId: host.id, ...computeDates(startDate, endDate) },
type: sequelize.QueryTypes.SELECT,
},
);
let total = await computeTotal(results, host.currency);
// amount/hostFeeInHostCurrency is expressed as a negative number
total = oppositeTotal(total);
const plan = await host.getPlan();
const hostFeeSharePercent = plan.hostFeeSharePercent || 0;
return Math.round((total * hostFeeSharePercent) / 100);
}