2024-03-20 01:37:31 +01:00
|
|
|
import { timeUnitForRange, whenRange } from '@/lib/time'
|
2024-07-10 19:23:05 -05:00
|
|
|
import { viewGroup } from './growth'
|
2024-09-10 18:35:25 +02:00
|
|
|
import { GqlAuthenticationError } from '@/lib/error'
|
2022-12-19 16:27:52 -06:00
|
|
|
|
|
|
|
export default {
|
|
|
|
Query: {
|
2023-11-08 21:15:36 -03:00
|
|
|
referrals: async (parent, { when, from, to }, { models, me }) => {
|
2022-12-19 16:27:52 -06:00
|
|
|
if (!me) {
|
2024-09-10 18:35:25 +02:00
|
|
|
throw new GqlAuthenticationError()
|
2022-12-19 16:27:52 -06:00
|
|
|
}
|
|
|
|
|
2023-11-08 21:15:36 -03:00
|
|
|
const range = whenRange(when, from, to)
|
|
|
|
|
2024-07-10 19:23:05 -05:00
|
|
|
return await models.$queryRawUnsafe(`
|
|
|
|
SELECT date_trunc('${timeUnitForRange(range)}', t) at time zone 'America/Chicago' as time,
|
|
|
|
json_build_array(
|
|
|
|
json_build_object('name', 'referrals', 'value', COALESCE(SUM(referrals), 0)),
|
|
|
|
json_build_object('name', 'one day referrals', 'value', COALESCE(SUM(one_day_referrals), 0)),
|
|
|
|
json_build_object('name', 'referral sats', 'value', FLOOR(COALESCE(SUM(msats_referrals), 0) / 1000.0)),
|
|
|
|
json_build_object('name', 'one day referral sats', 'value', FLOOR(COALESCE(SUM(msats_one_day_referrals), 0) / 1000.0))
|
2022-12-19 16:27:52 -06:00
|
|
|
) AS data
|
2024-07-10 19:23:05 -05:00
|
|
|
FROM ${viewGroup(range, 'user_stats')}
|
|
|
|
WHERE id = ${me.id}
|
|
|
|
GROUP BY time
|
|
|
|
ORDER BY time ASC`, ...range)
|
2022-12-19 16:27:52 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|