From 796bd4dc4b7fe47fc8fabe9bc91cc2c0329221d6 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Tue, 16 Apr 2024 17:53:05 +0200 Subject: [PATCH] Add autowithdrawal badge in notifications (#1078) --- api/resolvers/notifications.js | 5 ++- api/resolvers/wallet.js | 50 +++++++++++++++-------------- api/typeDefs/notifications.js | 1 + components/notifications.js | 2 ++ components/notifications.module.css | 7 ++++ fragments/notifications.js | 3 ++ 6 files changed, 43 insertions(+), 25 deletions(-) diff --git a/api/resolvers/notifications.js b/api/resolvers/notifications.js index 3fb72cfe..b6f46bc1 100644 --- a/api/resolvers/notifications.js +++ b/api/resolvers/notifications.js @@ -1,7 +1,7 @@ import { GraphQLError } from 'graphql' import { decodeCursor, LIMIT, nextNoteCursorEncoded } from '@/lib/cursor' import { getItem, filterClause, whereClause, muteClause } from './item' -import { getInvoice } from './wallet' +import { getInvoice, getWithdrawl } from './wallet' import { pushSubscriptionSchema, ssValidate } from '@/lib/validate' import { replyToSubscription } from '@/lib/webPush' import { getSub } from './sub' @@ -444,6 +444,9 @@ export default { InvoicePaid: { invoice: async (n, args, { me, models }) => getInvoice(n, { id: n.id }, { me, models }) }, + WithdrawlPaid: { + withdrawl: async (n, args, { me, models }) => getWithdrawl(n, { id: n.id }, { me, models }) + }, Invitification: { invite: async (n, args, { models }) => { return await models.invite.findUnique({ diff --git a/api/resolvers/wallet.js b/api/resolvers/wallet.js index 9abc6af0..afe39547 100644 --- a/api/resolvers/wallet.js +++ b/api/resolvers/wallet.js @@ -55,6 +55,31 @@ export async function getInvoice (parent, { id }, { me, models, lnd }) { return inv } +export async function getWithdrawl (parent, { id }, { me, models }) { + if (!me) { + throw new GraphQLError('you must be logged in', { extensions: { code: 'FORBIDDEN' } }) + } + + const wdrwl = await models.withdrawl.findUnique({ + where: { + id: Number(id) + }, + include: { + user: true + } + }) + + if (!wdrwl) { + throw new GraphQLError('withdrawal not found', { extensions: { code: 'BAD_INPUT' } }) + } + + if (wdrwl.user.id !== me.id) { + throw new GraphQLError('not ur withdrawal', { extensions: { code: 'FORBIDDEN' } }) + } + + return wdrwl +} + export function createHmac (hash) { const key = Buffer.from(process.env.INVOICE_HMAC_KEY, 'hex') return crypto.createHmac('sha256', key).update(Buffer.from(hash, 'hex')).digest('hex') @@ -99,30 +124,7 @@ export default { } }) }, - withdrawl: async (parent, { id }, { me, models }) => { - if (!me) { - throw new GraphQLError('you must be logged in', { extensions: { code: 'FORBIDDEN' } }) - } - - const wdrwl = await models.withdrawl.findUnique({ - where: { - id: Number(id) - }, - include: { - user: true - } - }) - - if (!wdrwl) { - throw new GraphQLError('withdrawal not found', { extensions: { code: 'BAD_INPUT' } }) - } - - if (wdrwl.user.id !== me.id) { - throw new GraphQLError('not ur withdrawal', { extensions: { code: 'FORBIDDEN' } }) - } - - return wdrwl - }, + withdrawl: getWithdrawl, numBolt11s: async (parent, args, { me, models, lnd }) => { if (!me) { throw new GraphQLError('you must be logged in', { extensions: { code: 'FORBIDDEN' } }) diff --git a/api/typeDefs/notifications.js b/api/typeDefs/notifications.js index 12c58acc..9b3a3e41 100644 --- a/api/typeDefs/notifications.js +++ b/api/typeDefs/notifications.js @@ -95,6 +95,7 @@ export default gql` id: ID! earnedSats: Int! sortTime: Date! + withdrawl: Withdrawl! } type Referral { diff --git a/components/notifications.js b/components/notifications.js index 97338f7a..4a7a99d6 100644 --- a/components/notifications.js +++ b/components/notifications.js @@ -29,6 +29,7 @@ import { LongCountdown } from './countdown' import { nextBillingWithGrace } from '@/lib/territory' import { commentSubTreeRootId } from '@/lib/item' import LinkToContext from './link-to-context' +import { Badge } from 'react-bootstrap' function Notification ({ n, fresh }) { const type = n.__typename @@ -283,6 +284,7 @@ function WithdrawlPaid ({ n }) {
{numWithUnits(n.earnedSats, { abbreviate: false, unitSingular: 'sat was', unitPlural: 'sats were' })} withdrawn from your account {timeSince(new Date(n.sortTime))} + {n.withdrawl.autoWithdraw && autowithdraw}
) } diff --git a/components/notifications.module.css b/components/notifications.module.css index 600ae419..eb1ba076 100644 --- a/components/notifications.module.css +++ b/components/notifications.module.css @@ -26,4 +26,11 @@ .subFormGroup > div { margin-right: 0 !important; +} + +.badge { + color: var(--theme-grey) !important; + background: var(--theme-clickToContextColor) !important; + vertical-align: middle; + margin-left: 0.5rem; } \ No newline at end of file diff --git a/fragments/notifications.js b/fragments/notifications.js index 38bb0bab..b57d1a75 100644 --- a/fragments/notifications.js +++ b/fragments/notifications.js @@ -137,6 +137,9 @@ export const NOTIFICATIONS = gql` id sortTime earnedSats + withdrawl { + autoWithdraw + } } } }