Skip to content

Commit 7c875d1

Browse files
committed
Don't return invoices cancelled by user
1 parent 9077e8f commit 7c875d1

File tree

7 files changed

+37
-8
lines changed

7 files changed

+37
-8
lines changed

api/resolvers/wallet.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ const resolvers = {
468468
SELECT id FROM "Invoice"
469469
WHERE "userId" = ${me.id}
470470
AND "actionState" = 'FAILED'
471+
AND "userCancel" = false
471472
AND "lockedAt" IS NULL
472473
ORDER BY id DESC
473474
FOR UPDATE SKIP LOCKED
@@ -517,10 +518,10 @@ const resolvers = {
517518
},
518519
createWithdrawl: createWithdrawal,
519520
sendToLnAddr,
520-
cancelInvoice: async (parent, { hash, hmac }, { models, lnd, boss }) => {
521+
cancelInvoice: async (parent, { hash, hmac, userCancel }, { models, lnd, boss }) => {
521522
verifyHmac(hash, hmac)
522523
await finalizeHodlInvoice({ data: { hash }, lnd, models, boss })
523-
return await models.invoice.findFirst({ where: { hash } })
524+
return await models.invoice.update({ where: { hash }, data: { userCancel: !!userCancel } })
524525
},
525526
dropBolt11: async (parent, { hash }, { me, models, lnd }) => {
526527
if (!me) {

api/typeDefs/wallet.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ const typeDefs = `
7979
createInvoice(amount: Int!): InvoiceOrDirect!
8080
createWithdrawl(invoice: String!, maxFee: Int!): Withdrawl!
8181
sendToLnAddr(addr: String!, amount: Int!, maxFee: Int!, comment: String, identifier: Boolean, name: String, email: String): Withdrawl!
82-
cancelInvoice(hash: String!, hmac: String!): Invoice!
82+
cancelInvoice(hash: String!, hmac: String!, userCancel: Boolean): Invoice!
8383
dropBolt11(hash: String!): Boolean
8484
removeWallet(id: ID!): Boolean
8585
deleteWalletLogs(wallet: String): Boolean

components/use-invoice.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ export default function useInvoice () {
3636
return { invoice: data.invoice, check: that(data.invoice) }
3737
}, [client])
3838

39-
const cancel = useCallback(async ({ hash, hmac }) => {
39+
const cancel = useCallback(async ({ hash, hmac }, { userCancel = false } = {}) => {
4040
if (!hash || !hmac) {
4141
throw new Error('missing hash or hmac')
4242
}
4343

4444
console.log('canceling invoice:', hash)
45-
const { data } = await cancelInvoice({ variables: { hash, hmac } })
45+
const { data } = await cancelInvoice({ variables: { hash, hmac, userCancel } })
4646
return data.cancelInvoice
4747
}, [cancelInvoice])
4848

components/use-qr-payment.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default function useQrPayment () {
2020
let paid
2121
const cancelAndReject = async (onClose) => {
2222
if (!paid && cancelOnClose) {
23-
const updatedInv = await invoice.cancel(inv)
23+
const updatedInv = await invoice.cancel(inv, { userCancel: true })
2424
reject(new InvoiceCanceledError(updatedInv))
2525
}
2626
resolve(inv)

fragments/wallet.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,8 @@ export const SET_WALLET_PRIORITY = gql`
225225

226226
export const CANCEL_INVOICE = gql`
227227
${INVOICE_FIELDS}
228-
mutation cancelInvoice($hash: String!, $hmac: String!) {
229-
cancelInvoice(hash: $hash, hmac: $hmac) {
228+
mutation cancelInvoice($hash: String!, $hmac: String!, $userCancel: Boolean) {
229+
cancelInvoice(hash: $hash, hmac: $hmac, userCancel: $userCancel) {
230230
...InvoiceFields
231231
}
232232
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
-- AlterTable
2+
ALTER TABLE "Invoice" ADD COLUMN "userCancel" BOOLEAN;
3+
UPDATE "Invoice" SET "userCancel" = false;
4+
5+
-- make sure there are no invoices that are in an inconsistent cancel state
6+
-- Add constraint to ensure consistent cancel state
7+
ALTER TABLE "Invoice" ADD CONSTRAINT "Invoice_cancel" CHECK (
8+
("cancelled" = true AND "cancelledAt" IS NOT NULL AND "userCancel" IS NOT NULL) OR
9+
("cancelled" = false AND "cancelledAt" IS NULL AND "userCancel" IS NULL)
10+
);
11+
12+
-- Add trigger to set userCancel to false when cancelled updated
13+
CREATE OR REPLACE FUNCTION invoice_set_user_cancel_default()
14+
RETURNS TRIGGER AS $$
15+
BEGIN
16+
IF NEW.cancelled AND NEW."userCancel" IS NULL THEN
17+
NEW."userCancel" := false;
18+
END IF;
19+
RETURN NEW;
20+
END;
21+
$$ LANGUAGE plpgsql;
22+
23+
CREATE TRIGGER invoice_user_cancel_trigger
24+
BEFORE UPDATE ON "Invoice"
25+
FOR EACH ROW
26+
EXECUTE FUNCTION invoice_set_user_cancel_default();
27+

prisma/schema.prisma

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,7 @@ model Invoice {
917917
confirmedIndex BigInt?
918918
cancelled Boolean @default(false)
919919
cancelledAt DateTime?
920+
userCancel Boolean?
920921
lockedAt DateTime?
921922
msatsRequested BigInt
922923
msatsReceived BigInt?

0 commit comments

Comments
 (0)