diff --git a/api/resolvers/rewards.js b/api/resolvers/rewards.js
index c420406f..4f3667ce 100644
--- a/api/resolvers/rewards.js
+++ b/api/resolvers/rewards.js
@@ -3,20 +3,33 @@ import { amountSchema, ssValidate } from '../../lib/validate'
import serialize from './serial'
import { ANON_USER_ID } from '../../lib/constants'
import { getItem } from './item'
+import { datePivot, dayMonthYear } from '../../lib/time'
export default {
Query: {
rewards: async (parent, { when }, { models }) => {
- if (when && isNaN(new Date(when))) {
- throw new GraphQLError('invalid date', { extensions: { code: 'BAD_USER_INPUT' } })
+ if (when) {
+ if (when.length > 2) {
+ throw new GraphQLError('too many dates', { extensions: { code: 'BAD_USER_INPUT' } })
+ }
+ when = when.map(w => {
+ if (isNaN(new Date(w))) {
+ throw new GraphQLError('invalid date', { extensions: { code: 'BAD_USER_INPUT' } })
+ }
+ return dayMonthYear(datePivot(new Date(w), { days: -1 }))
+ })
+ } else {
+ // default to tomorrow's rewards
+ when = [dayMonthYear(new Date())]
}
- const [result] = await models.$queryRaw`
- WITH day_cte (day) AS (
- SELECT COALESCE(${when}::text::timestamp - interval '1 day', date_trunc('day', now() AT TIME ZONE 'America/Chicago'))
+ const results = await models.$queryRaw`
+ WITH days_cte (day) AS (
+ SELECT t::date
+ FROM generate_series(${when[0]}::text::timestamp, ${when[when.length - 1]}::text::timestamp, interval '1 day') AS t
)
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,
+ days_cte.day + interval '1 day' 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)),
@@ -24,16 +37,16 @@ export default {
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))
) AS sources
- FROM day_cte
+ FROM days_cte
CROSS JOIN LATERAL (
(SELECT ("ItemAct".msats - COALESCE("ReferralAct".msats, 0)) / 1000.0 as sats, act::text as type
FROM "ItemAct"
LEFT JOIN "ReferralAct" ON "ReferralAct"."itemActId" = "ItemAct".id
- WHERE date_trunc('day', "ItemAct".created_at AT TIME ZONE 'UTC' AT TIME ZONE 'America/Chicago') = day_cte.day AND "ItemAct".act <> 'TIP')
+ WHERE date_trunc('day', "ItemAct".created_at AT TIME ZONE 'UTC' AT TIME ZONE 'America/Chicago') = days_cte.day AND "ItemAct".act <> 'TIP')
UNION ALL
(SELECT sats::FLOAT, 'DONATION' as type
FROM "Donation"
- WHERE date_trunc('day', created_at AT TIME ZONE 'UTC' AT TIME ZONE 'America/Chicago') = day_cte.day)
+ WHERE date_trunc('day', created_at AT TIME ZONE 'UTC' AT TIME ZONE 'America/Chicago') = days_cte.day)
UNION ALL
-- any earnings from anon's stack that are not forwarded to other users
(SELECT "ItemAct".msats / 1000.0 as sats, 'ANON' as type
@@ -41,33 +54,47 @@ export default {
JOIN "ItemAct" ON "ItemAct"."itemId" = "Item".id
LEFT JOIN "ItemForward" ON "ItemForward"."itemId" = "Item".id
WHERE "Item"."userId" = ${ANON_USER_ID} AND "ItemAct".act = 'TIP'
- AND date_trunc('day', "ItemAct".created_at AT TIME ZONE 'UTC' AT TIME ZONE 'America/Chicago') = day_cte.day
+ AND date_trunc('day', "ItemAct".created_at AT TIME ZONE 'UTC' AT TIME ZONE 'America/Chicago') = days_cte.day
GROUP BY "ItemAct".id, "ItemAct".msats
HAVING COUNT("ItemForward".id) = 0)
- ) subquery`
+ ) subquery
+ GROUP BY days_cte.day
+ ORDER BY days_cte.day ASC`
- return result || { total: 0, time: 0, sources: [] }
+ return results || [{ 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')
+ if (!when || !Array.isArray(when) || when.length > 2) {
+ throw new GraphQLError('invalid date range', { extensions: { code: 'BAD_USER_INPUT' } })
+ }
+ for (const w of when) {
+ if (isNaN(new Date(w))) {
+ throw new GraphQLError('invalid date', { extensions: { code: 'BAD_USER_INPUT' } })
+ }
+ }
+
+ const results = await models.$queryRaw`
+ WITH days_cte (day) AS (
+ SELECT t::date
+ FROM generate_series(${when[0]}::text::timestamp, ${when[when.length - 1]}::text::timestamp, interval '1 day') AS t
)
SELECT coalesce(sum(sats), 0) as total, json_agg("Earn".*) as rewards
- FROM day_cte
+ FROM days_cte
CROSS JOIN LATERAL (
(SELECT FLOOR("Earn".msats / 1000.0) as sats, type, rank, "typeId"
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
+ AND date_trunc('day', "Earn".created_at AT TIME ZONE 'UTC' AT TIME ZONE 'America/Chicago') = days_cte.day
ORDER BY "Earn".msats DESC)
- ) "Earn"`
+ ) "Earn"
+ GROUP BY days_cte.day
+ ORDER BY days_cte.day ASC`
- return result
+ return results
}
},
Mutation: {
diff --git a/api/typeDefs/notifications.js b/api/typeDefs/notifications.js
index ffd5faf6..d18d410b 100644
--- a/api/typeDefs/notifications.js
+++ b/api/typeDefs/notifications.js
@@ -65,6 +65,7 @@ export default gql`
type Earn {
id: ID!
earnedSats: Int!
+ minSortTime: Date!
sortTime: Date!
sources: EarnSources
}
diff --git a/api/typeDefs/rewards.js b/api/typeDefs/rewards.js
index eebd1e1c..220ea7bb 100644
--- a/api/typeDefs/rewards.js
+++ b/api/typeDefs/rewards.js
@@ -2,8 +2,8 @@ import { gql } from 'graphql-tag'
export default gql`
extend type Query {
- rewards(when: String): Rewards!
- meRewards(when: String!): MeRewards
+ rewards(when: [String!]): [Rewards!]
+ meRewards(when: [String!]!): [MeRewards]
}
extend type Mutation {
diff --git a/components/footer-rewards.js b/components/footer-rewards.js
index 75525acc..a00e2ba9 100644
--- a/components/footer-rewards.js
+++ b/components/footer-rewards.js
@@ -12,8 +12,7 @@ const REWARDS = gql`
export default function Rewards () {
const { data } = useQuery(REWARDS, SSR ? { ssr: false } : { pollInterval: 60000, nextFetchPolicy: 'cache-and-network' })
- const total = data?.rewards?.total
-
+ const total = data?.rewards?.[0]?.total
return (
{total ?