2023-07-23 15:08:43 +00:00
|
|
|
import { GraphQLError } from 'graphql'
|
2023-02-08 19:38:04 +00:00
|
|
|
import { amountSchema, ssValidate } from '../../lib/validate'
|
2023-09-26 20:15:09 +00:00
|
|
|
import { serializeInvoicable } from './serial'
|
2023-08-14 19:37:09 +00:00
|
|
|
import { ANON_USER_ID } from '../../lib/constants'
|
2023-09-26 20:15:09 +00:00
|
|
|
import { getItem } from './item'
|
2022-12-08 00:04:02 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
Query: {
|
2023-08-15 17:41:51 +00:00
|
|
|
rewards: async (parent, { when }, { models }) => {
|
2023-08-30 00:13:21 +00:00
|
|
|
if (when) {
|
|
|
|
if (when.length > 2) {
|
|
|
|
throw new GraphQLError('too many dates', { extensions: { code: 'BAD_USER_INPUT' } })
|
|
|
|
}
|
2023-08-30 02:05:10 +00:00
|
|
|
when.forEach(w => {
|
2023-08-30 00:13:21 +00:00
|
|
|
if (isNaN(new Date(w))) {
|
|
|
|
throw new GraphQLError('invalid date', { extensions: { code: 'BAD_USER_INPUT' } })
|
|
|
|
}
|
|
|
|
})
|
2023-08-30 15:00:47 +00:00
|
|
|
if (new Date(when[0]) > new Date(when[when.length - 1])) {
|
|
|
|
throw new GraphQLError('bad date range', { extensions: { code: 'BAD_USER_INPUT' } })
|
|
|
|
}
|
2023-08-15 17:41:51 +00:00
|
|
|
}
|
|
|
|
|
2023-08-30 00:13:21 +00:00
|
|
|
const results = await models.$queryRaw`
|
|
|
|
WITH days_cte (day) AS (
|
2023-08-30 02:05:10 +00:00
|
|
|
SELECT date_trunc('day', t)
|
|
|
|
FROM generate_series(
|
2023-09-18 22:48:05 +00:00
|
|
|
COALESCE(${when?.[0]}::text::timestamp - interval '1 day', now() AT TIME ZONE 'America/Chicago'),
|
|
|
|
COALESCE(${when?.[when.length - 1]}::text::timestamp - interval '1 day', now() AT TIME ZONE 'America/Chicago'),
|
2023-08-30 02:05:10 +00:00
|
|
|
interval '1 day') AS t
|
2023-08-15 17:41:51 +00:00
|
|
|
)
|
|
|
|
SELECT coalesce(FLOOR(sum(sats)), 0) as total,
|
2023-08-30 00:13:21 +00:00
|
|
|
days_cte.day + interval '1 day' as time,
|
2023-08-15 17:41:51 +00:00
|
|
|
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-30 00:13:21 +00:00
|
|
|
FROM days_cte
|
2023-08-15 17:41:51 +00:00
|
|
|
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-30 00:13:21 +00:00
|
|
|
WHERE date_trunc('day', "ItemAct".created_at AT TIME ZONE 'UTC' AT TIME ZONE 'America/Chicago') = days_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-30 00:13:21 +00:00
|
|
|
WHERE date_trunc('day', created_at AT TIME ZONE 'UTC' AT TIME ZONE 'America/Chicago') = days_cte.day)
|
2023-08-14 19:37:09 +00:00
|
|
|
UNION ALL
|
multiple forwards on a post (#403)
* multiple forwards on a post
first phase of the multi-forward support
* update the graphql mutation for discussion posts to accept and validate multiple forwards
* update the discussion form to allow multiple forwards in the UI
* start working on db schema changes
* uncomment db schema, add migration to create the new model, and update create_item, update_item
stored procedures
* Propagate updates from discussion to poll, link, and bounty forms
Update the create, update poll sql functions for multi forward support
* Update gql, typedefs, and resolver to return forwarded users in items responses
* UI changes to show multiple forward recipients, and conditional upvote logic changes
* Update notification text to reflect multiple forwards upon vote action
* Disallow duplicate stacker entries
* reduce duplication in populating adv-post-form initial values
* Update item_act sql function to implement multi-way forwarding
* Update referral functions to scale referral bonuses for forwarded users
* Update notification text to reflect non-100% forwarded sats cases
* Update wallet history sql queries to accommodate multi-forward use cases
* Block zaps for posts you are forwarded zaps at the API layer, in addition
to in the UI
* Delete fwdUserId column from Item table as part of migration
* Fix how we calculate stacked sats after partial forwards in wallet history
* Exclude entries from wallet history that are 0 stacked sats from posts with 100% forwarded to other users
* Fix wallet history query for forwarded stacked sats to be scaled by the fwd pct
* Reduce duplication in adv post form, and do some style tweaks for better layout
* Use MAX_FORWARDS constants
* Address various PR feedback
* first enhancement pass
* enhancement pass too
---------
Co-authored-by: keyan <keyan.kousha+huumn@gmail.com>
Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2023-08-23 22:44:17 +00:00
|
|
|
-- any earnings from anon's stack that are not forwarded to other users
|
2023-08-14 19:37:09 +00:00
|
|
|
(SELECT "ItemAct".msats / 1000.0 as sats, 'ANON' as type
|
|
|
|
FROM "Item"
|
|
|
|
JOIN "ItemAct" ON "ItemAct"."itemId" = "Item".id
|
multiple forwards on a post (#403)
* multiple forwards on a post
first phase of the multi-forward support
* update the graphql mutation for discussion posts to accept and validate multiple forwards
* update the discussion form to allow multiple forwards in the UI
* start working on db schema changes
* uncomment db schema, add migration to create the new model, and update create_item, update_item
stored procedures
* Propagate updates from discussion to poll, link, and bounty forms
Update the create, update poll sql functions for multi forward support
* Update gql, typedefs, and resolver to return forwarded users in items responses
* UI changes to show multiple forward recipients, and conditional upvote logic changes
* Update notification text to reflect multiple forwards upon vote action
* Disallow duplicate stacker entries
* reduce duplication in populating adv-post-form initial values
* Update item_act sql function to implement multi-way forwarding
* Update referral functions to scale referral bonuses for forwarded users
* Update notification text to reflect non-100% forwarded sats cases
* Update wallet history sql queries to accommodate multi-forward use cases
* Block zaps for posts you are forwarded zaps at the API layer, in addition
to in the UI
* Delete fwdUserId column from Item table as part of migration
* Fix how we calculate stacked sats after partial forwards in wallet history
* Exclude entries from wallet history that are 0 stacked sats from posts with 100% forwarded to other users
* Fix wallet history query for forwarded stacked sats to be scaled by the fwd pct
* Reduce duplication in adv post form, and do some style tweaks for better layout
* Use MAX_FORWARDS constants
* Address various PR feedback
* first enhancement pass
* enhancement pass too
---------
Co-authored-by: keyan <keyan.kousha+huumn@gmail.com>
Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2023-08-23 22:44:17 +00:00
|
|
|
LEFT JOIN "ItemForward" ON "ItemForward"."itemId" = "Item".id
|
|
|
|
WHERE "Item"."userId" = ${ANON_USER_ID} AND "ItemAct".act = 'TIP'
|
2023-08-30 00:13:21 +00:00
|
|
|
AND date_trunc('day', "ItemAct".created_at AT TIME ZONE 'UTC' AT TIME ZONE 'America/Chicago') = days_cte.day
|
multiple forwards on a post (#403)
* multiple forwards on a post
first phase of the multi-forward support
* update the graphql mutation for discussion posts to accept and validate multiple forwards
* update the discussion form to allow multiple forwards in the UI
* start working on db schema changes
* uncomment db schema, add migration to create the new model, and update create_item, update_item
stored procedures
* Propagate updates from discussion to poll, link, and bounty forms
Update the create, update poll sql functions for multi forward support
* Update gql, typedefs, and resolver to return forwarded users in items responses
* UI changes to show multiple forward recipients, and conditional upvote logic changes
* Update notification text to reflect multiple forwards upon vote action
* Disallow duplicate stacker entries
* reduce duplication in populating adv-post-form initial values
* Update item_act sql function to implement multi-way forwarding
* Update referral functions to scale referral bonuses for forwarded users
* Update notification text to reflect non-100% forwarded sats cases
* Update wallet history sql queries to accommodate multi-forward use cases
* Block zaps for posts you are forwarded zaps at the API layer, in addition
to in the UI
* Delete fwdUserId column from Item table as part of migration
* Fix how we calculate stacked sats after partial forwards in wallet history
* Exclude entries from wallet history that are 0 stacked sats from posts with 100% forwarded to other users
* Fix wallet history query for forwarded stacked sats to be scaled by the fwd pct
* Reduce duplication in adv post form, and do some style tweaks for better layout
* Use MAX_FORWARDS constants
* Address various PR feedback
* first enhancement pass
* enhancement pass too
---------
Co-authored-by: keyan <keyan.kousha+huumn@gmail.com>
Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2023-08-23 22:44:17 +00:00
|
|
|
GROUP BY "ItemAct".id, "ItemAct".msats
|
|
|
|
HAVING COUNT("ItemForward".id) = 0)
|
2023-08-30 00:13:21 +00:00
|
|
|
) subquery
|
|
|
|
GROUP BY days_cte.day
|
|
|
|
ORDER BY days_cte.day ASC`
|
2022-12-08 00:04:02 +00:00
|
|
|
|
2023-08-30 15:00:47 +00:00
|
|
|
return results.length ? results : [{ total: 0, time: '0', sources: [] }]
|
2023-08-15 17:41:51 +00:00
|
|
|
},
|
|
|
|
meRewards: async (parent, { when }, { me, models }) => {
|
|
|
|
if (!me) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2023-08-30 02:05:10 +00:00
|
|
|
if (!when || when.length > 2) {
|
2023-08-30 00:13:21 +00:00
|
|
|
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 (
|
2023-08-30 02:05:10 +00:00
|
|
|
SELECT date_trunc('day', t)
|
|
|
|
FROM generate_series(
|
2023-09-18 22:48:05 +00:00
|
|
|
${when[0]}::text::timestamp,
|
|
|
|
${when[when.length - 1]}::text::timestamp,
|
2023-08-30 02:05:10 +00:00
|
|
|
interval '1 day') AS t
|
2023-08-15 17:41:51 +00:00
|
|
|
)
|
|
|
|
SELECT coalesce(sum(sats), 0) as total, json_agg("Earn".*) as rewards
|
2023-08-30 00:13:21 +00:00
|
|
|
FROM days_cte
|
2023-08-15 17:41:51 +00:00
|
|
|
CROSS JOIN LATERAL (
|
2023-08-29 21:05:09 +00:00
|
|
|
(SELECT FLOOR("Earn".msats / 1000.0) as sats, type, rank, "typeId"
|
2023-08-15 17:41:51 +00:00
|
|
|
FROM "Earn"
|
|
|
|
WHERE "Earn"."userId" = ${me.id}
|
2023-08-30 00:13:21 +00:00
|
|
|
AND date_trunc('day', "Earn".created_at AT TIME ZONE 'UTC' AT TIME ZONE 'America/Chicago') = days_cte.day
|
2023-08-15 17:41:51 +00:00
|
|
|
ORDER BY "Earn".msats DESC)
|
2023-08-30 00:13:21 +00:00
|
|
|
) "Earn"
|
|
|
|
GROUP BY days_cte.day
|
|
|
|
ORDER BY days_cte.day ASC`
|
2023-08-15 17:41:51 +00:00
|
|
|
|
2023-08-30 00:13:21 +00:00
|
|
|
return results
|
2022-12-08 00:04:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
Mutation: {
|
2023-09-24 19:38:37 +00:00
|
|
|
donateToRewards: async (parent, { sats, hash, hmac }, { me, models, lnd }) => {
|
2023-02-08 19:38:04 +00:00
|
|
|
await ssValidate(amountSchema, { amount: sats })
|
|
|
|
|
2023-09-26 20:15:09 +00:00
|
|
|
await serializeInvoicable(
|
|
|
|
models.$queryRaw`SELECT donate(${sats}::INTEGER, ${me?.id || ANON_USER_ID}::INTEGER)`,
|
|
|
|
{ models, lnd, hash, hmac, me, enforceFee: sats }
|
|
|
|
)
|
2022-12-08 00:04:02 +00:00
|
|
|
|
|
|
|
return sats
|
|
|
|
}
|
2023-08-29 21:05:09 +00:00
|
|
|
},
|
|
|
|
Reward: {
|
|
|
|
item: async (reward, args, { me, models }) => {
|
|
|
|
if (!reward.typeId) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
return getItem(reward, { id: reward.typeId }, { me, models })
|
|
|
|
}
|
2022-12-08 00:04:02 +00:00
|
|
|
}
|
|
|
|
}
|