stacker.news/api/resolvers/rewards.js

85 lines
3.7 KiB
JavaScript
Raw Normal View History

import { GraphQLError } from 'graphql'
2023-02-08 19:38:04 +00:00
import { amountSchema, ssValidate } from '../../lib/validate'
2022-12-08 00:04:02 +00:00
import serialize from './serial'
2023-08-14 19:37:09 +00:00
import { ANON_USER_ID } from '../../lib/constants'
2022-12-08 00:04:02 +00:00
export default {
Query: {
2023-08-15 17:41:51 +00:00
rewards: async (parent, { when }, { models }) => {
if (when && isNaN(new Date(when))) {
throw new GraphQLError('invalid date', { extensions: { code: 'BAD_USER_INPUT' } })
}
2022-12-08 00:04:02 +00:00
const [result] = await models.$queryRaw`
2023-08-15 17:41:51 +00:00
WITH day_cte (day) AS (
SELECT COALESCE(${when}::text::timestamp - interval '1 day', date_trunc('day', now() AT TIME ZONE 'America/Chicago'))
)
SELECT coalesce(FLOOR(sum(sats)), 0) as total,
COALESCE(${when}::text::timestamp, date_trunc('day', (now() + interval '1 day') AT TIME ZONE 'America/Chicago')) as time,
json_build_array(
json_build_object('name', 'donations', 'value', coalesce(FLOOR(sum(sats) FILTER(WHERE type = 'DONATION')), 0)),
json_build_object('name', 'fees', 'value', coalesce(FLOOR(sum(sats) FILTER(WHERE type NOT IN ('BOOST', 'STREAM', 'DONATION', 'ANON'))), 0)),
json_build_object('name', 'boost', 'value', coalesce(FLOOR(sum(sats) FILTER(WHERE type = 'BOOST')), 0)),
json_build_object('name', 'jobs', 'value', coalesce(FLOOR(sum(sats) FILTER(WHERE type = 'STREAM')), 0)),
json_build_object('name', 'anon''s stack', 'value', coalesce(FLOOR(sum(sats) FILTER(WHERE type = 'ANON')), 0))
2022-12-08 00:04:02 +00:00
) AS sources
2023-08-15 17:41:51 +00:00
FROM day_cte
CROSS JOIN LATERAL (
2022-12-19 22:27:52 +00:00
(SELECT ("ItemAct".msats - COALESCE("ReferralAct".msats, 0)) / 1000.0 as sats, act::text as type
2022-12-08 00:04:02 +00:00
FROM "ItemAct"
2023-08-10 02:27:53 +00:00
LEFT JOIN "ReferralAct" ON "ReferralAct"."itemActId" = "ItemAct".id
2023-08-15 17:41:51 +00:00
WHERE date_trunc('day', "ItemAct".created_at AT TIME ZONE 'UTC' AT TIME ZONE 'America/Chicago') = day_cte.day AND "ItemAct".act <> 'TIP')
2022-12-08 00:04:02 +00:00
UNION ALL
2022-12-19 22:27:52 +00:00
(SELECT sats::FLOAT, 'DONATION' as type
2022-12-08 00:04:02 +00:00
FROM "Donation"
2023-08-15 17:41:51 +00:00
WHERE date_trunc('day', created_at AT TIME ZONE 'UTC' AT TIME ZONE 'America/Chicago') = day_cte.day)
2023-08-14 19:37:09 +00:00
UNION ALL
(SELECT "ItemAct".msats / 1000.0 as sats, 'ANON' as type
FROM "Item"
JOIN "ItemAct" ON "ItemAct"."itemId" = "Item".id
WHERE "Item"."userId" = ${ANON_USER_ID} AND "ItemAct".act = 'TIP' AND "Item"."fwdUserId" IS NULL
2023-08-15 17:41:51 +00:00
AND date_trunc('day', "ItemAct".created_at AT TIME ZONE 'UTC' AT TIME ZONE 'America/Chicago') = day_cte.day)
2022-12-08 00:04:02 +00:00
) subquery`
2023-08-15 17:41:51 +00:00
return result || { total: 0, time: 0, sources: [] }
},
meRewards: async (parent, { when }, { me, models }) => {
if (!me) {
return null
}
const [result] = await models.$queryRaw`
WITH day_cte (day) AS (
SELECT date_trunc('day', ${when}::text::timestamp AT TIME ZONE 'America/Chicago')
)
SELECT coalesce(sum(sats), 0) as total, json_agg("Earn".*) as rewards
FROM day_cte
CROSS JOIN LATERAL (
(SELECT FLOOR("Earn".msats / 1000.0) as sats, type, rank
FROM "Earn"
WHERE "Earn"."userId" = ${me.id}
AND date_trunc('day', "Earn".created_at AT TIME ZONE 'UTC' AT TIME ZONE 'America/Chicago') = day_cte.day
ORDER BY "Earn".msats DESC)
) "Earn"`
return result
2022-12-08 00:04:02 +00:00
}
},
Mutation: {
donateToRewards: async (parent, { sats }, { me, models }) => {
if (!me) {
throw new GraphQLError('you must be logged in', { extensions: { code: 'UNAUTHENTICATED' } })
2022-12-08 00:04:02 +00:00
}
2023-02-08 19:38:04 +00:00
await ssValidate(amountSchema, { amount: sats })
2022-12-08 00:04:02 +00:00
await serialize(models,
2023-07-26 16:01:31 +00:00
models.$queryRawUnsafe(
2023-07-27 00:18:42 +00:00
'SELECT donate($1::INTEGER, $2::INTEGER)',
2022-12-08 00:04:02 +00:00
sats, Number(me.id)))
return sats
}
}
}