diff --git a/api/resolvers/notifications.js b/api/resolvers/notifications.js index 54ba9144..63f4ef00 100644 --- a/api/resolvers/notifications.js +++ b/api/resolvers/notifications.js @@ -116,19 +116,6 @@ export default { ) } - if (me.noteEarning) { - queries.push( - `(SELECT "Earn".id::text, "Earn".created_at AS "sortTime", FLOOR(msats / 1000) as "earnedSats", - 'Earn' AS type - FROM "Earn" - WHERE "Earn"."userId" = $1 - AND FLOOR(msats / 1000) > 0 - AND created_at <= $2 - ORDER BY "sortTime" DESC - LIMIT ${LIMIT}+$3)` - ) - } - if (me.noteMentions) { queries.push( `(SELECT "Item".id::TEXT, "Mention".created_at AS "sortTime", NULL as "earnedSats", @@ -179,12 +166,28 @@ export default { LIMIT ${LIMIT}`, me.id, decodedCursor.time, decodedCursor.offset) const { checkedNotesAt } = await models.user.findUnique({ where: { id: me.id } }) + let earn if (decodedCursor.offset === 0) { + if (me.noteEarning) { + const earnings = await models.$queryRaw( + `SELECT MAX("Earn".id)::text, MAX("Earn".created_at) AS "sortTime", FLOOR(SUM(msats) / 1000) as "earnedSats", + 'Earn' AS type + FROM "Earn" + WHERE "Earn"."userId" = $1 + AND created_at >= $2`, me.id, checkedNotesAt) + if (earnings.length > 0 && earnings[0].earnedSats > 0) { + earn = earnings[0] + } + } + await models.user.update({ where: { id: me.id }, data: { checkedNotesAt: new Date() } }) } + console.log(decodedCursor) + return { lastChecked: checkedNotesAt, + earn, cursor: notifications.length === LIMIT ? nextCursorEncoded(decodedCursor) : null, notifications } diff --git a/api/typeDefs/notifications.js b/api/typeDefs/notifications.js index 8745db58..8ffeeee2 100644 --- a/api/typeDefs/notifications.js +++ b/api/typeDefs/notifications.js @@ -44,10 +44,11 @@ export default gql` } union Notification = Reply | Votification | Mention - | Invitification | JobChanged | Earn | InvoicePaid + | Invitification | Earn | JobChanged | InvoicePaid type Notifications { lastChecked: String + earn: Notification cursor: String notifications: [Notification!]! } diff --git a/components/notifications.js b/components/notifications.js index 7cea21ba..f91c6290 100644 --- a/components/notifications.js +++ b/components/notifications.js @@ -110,11 +110,13 @@ function Notification ({ n }) { ) } -export default function Notifications ({ notifications, cursor, lastChecked, variables }) { +export default function Notifications ({ notifications, earn, cursor, lastChecked, variables }) { const { data, fetchMore } = useQuery(NOTIFICATIONS, { variables }) + console.log(data) + if (data) { - ({ notifications: { notifications, cursor } } = data) + ({ notifications: { notifications, earn, cursor } } = data) } const [fresh, old] = @@ -128,6 +130,7 @@ export default function Notifications ({ notifications, cursor, lastChecked, var <> {/* XXX we shouldn't use the index but we don't have a unique id in this union yet */}
+ {earn && } {fresh.map((n, i) => ( ))} diff --git a/fragments/notifications.js b/fragments/notifications.js index 3f16bd6a..81efda40 100644 --- a/fragments/notifications.js +++ b/fragments/notifications.js @@ -10,6 +10,13 @@ export const NOTIFICATIONS = gql` notifications(cursor: $cursor, inc: $inc) { cursor lastChecked + earn { + __typename + ... on Earn { + sortTime + earnedSats + } + } notifications { __typename ... on Mention { @@ -47,10 +54,6 @@ export const NOTIFICATIONS = gql` ...ItemFields } } - ... on Earn { - sortTime - earnedSats - } ... on InvoicePaid { sortTime earnedSats diff --git a/lib/apollo.js b/lib/apollo.js index 7ed808cd..6a3debec 100644 --- a/lib/apollo.js +++ b/lib/apollo.js @@ -87,6 +87,7 @@ export default function getApolloClient () { return { cursor: incoming.cursor, + earn: existing?.earn || incoming.earn, notifications: [...(existing?.notifications || []), ...incoming.notifications], lastChecked: incoming.lastChecked }