Distinguish invoices cancelled by user (#1785)
This commit is contained in:
parent
6a02ea8c5c
commit
d53bc09773
|
@ -489,7 +489,7 @@ const resolvers = {
|
||||||
},
|
},
|
||||||
createWithdrawl: createWithdrawal,
|
createWithdrawl: createWithdrawal,
|
||||||
sendToLnAddr,
|
sendToLnAddr,
|
||||||
cancelInvoice: async (parent, { hash, hmac }, { me, models, lnd, boss }) => {
|
cancelInvoice: async (parent, { hash, hmac, userCancel }, { me, models, lnd, boss }) => {
|
||||||
// stackers can cancel their own invoices without hmac
|
// stackers can cancel their own invoices without hmac
|
||||||
if (me && !hmac) {
|
if (me && !hmac) {
|
||||||
const inv = await models.invoice.findUnique({ where: { hash } })
|
const inv = await models.invoice.findUnique({ where: { hash } })
|
||||||
|
@ -499,7 +499,7 @@ const resolvers = {
|
||||||
verifyHmac(hash, hmac)
|
verifyHmac(hash, hmac)
|
||||||
}
|
}
|
||||||
await finalizeHodlInvoice({ data: { hash }, lnd, models, boss })
|
await finalizeHodlInvoice({ data: { hash }, lnd, models, boss })
|
||||||
return await models.invoice.findFirst({ where: { hash } })
|
return await models.invoice.update({ where: { hash }, data: { userCancel: !!userCancel } })
|
||||||
},
|
},
|
||||||
dropBolt11: async (parent, { hash }, { me, models, lnd }) => {
|
dropBolt11: async (parent, { hash }, { me, models, lnd }) => {
|
||||||
if (!me) {
|
if (!me) {
|
||||||
|
|
|
@ -78,7 +78,7 @@ const typeDefs = `
|
||||||
createInvoice(amount: Int!): InvoiceOrDirect!
|
createInvoice(amount: Int!): InvoiceOrDirect!
|
||||||
createWithdrawl(invoice: String!, maxFee: Int!): Withdrawl!
|
createWithdrawl(invoice: String!, maxFee: Int!): Withdrawl!
|
||||||
sendToLnAddr(addr: String!, amount: Int!, maxFee: Int!, comment: String, identifier: Boolean, name: String, email: String): Withdrawl!
|
sendToLnAddr(addr: String!, amount: Int!, maxFee: Int!, comment: String, identifier: Boolean, name: String, email: String): Withdrawl!
|
||||||
cancelInvoice(hash: String!, hmac: String): Invoice!
|
cancelInvoice(hash: String!, hmac: String, userCancel: Boolean): Invoice!
|
||||||
dropBolt11(hash: String!): Boolean
|
dropBolt11(hash: String!): Boolean
|
||||||
removeWallet(id: ID!): Boolean
|
removeWallet(id: ID!): Boolean
|
||||||
deleteWalletLogs(wallet: String): Boolean
|
deleteWalletLogs(wallet: String): Boolean
|
||||||
|
|
|
@ -36,9 +36,9 @@ export default function useInvoice () {
|
||||||
return { invoice: data.invoice, check: that(data.invoice) }
|
return { invoice: data.invoice, check: that(data.invoice) }
|
||||||
}, [client])
|
}, [client])
|
||||||
|
|
||||||
const cancel = useCallback(async ({ hash, hmac }) => {
|
const cancel = useCallback(async ({ hash, hmac }, { userCancel = false } = {}) => {
|
||||||
console.log('canceling invoice:', hash)
|
console.log('canceling invoice:', hash)
|
||||||
const { data } = await cancelInvoice({ variables: { hash, hmac } })
|
const { data } = await cancelInvoice({ variables: { hash, hmac, userCancel } })
|
||||||
return data.cancelInvoice
|
return data.cancelInvoice
|
||||||
}, [cancelInvoice])
|
}, [cancelInvoice])
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ export default function useQrPayment () {
|
||||||
let paid
|
let paid
|
||||||
const cancelAndReject = async (onClose) => {
|
const cancelAndReject = async (onClose) => {
|
||||||
if (!paid && cancelOnClose) {
|
if (!paid && cancelOnClose) {
|
||||||
const updatedInv = await invoice.cancel(inv)
|
const updatedInv = await invoice.cancel(inv, { userCancel: true })
|
||||||
reject(new InvoiceCanceledError(updatedInv))
|
reject(new InvoiceCanceledError(updatedInv))
|
||||||
}
|
}
|
||||||
resolve(inv)
|
resolve(inv)
|
||||||
|
|
|
@ -225,8 +225,8 @@ export const SET_WALLET_PRIORITY = gql`
|
||||||
|
|
||||||
export const CANCEL_INVOICE = gql`
|
export const CANCEL_INVOICE = gql`
|
||||||
${INVOICE_FIELDS}
|
${INVOICE_FIELDS}
|
||||||
mutation cancelInvoice($hash: String!, $hmac: String) {
|
mutation cancelInvoice($hash: String!, $hmac: String, $userCancel: Boolean) {
|
||||||
cancelInvoice(hash: $hash, hmac: $hmac) {
|
cancelInvoice(hash: $hash, hmac: $hmac, userCancel: $userCancel) {
|
||||||
...InvoiceFields
|
...InvoiceFields
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE "Invoice" ADD COLUMN "userCancel" BOOLEAN;
|
||||||
|
|
||||||
|
-- Migrate existing rows
|
||||||
|
UPDATE "Invoice" SET "userCancel" = false;
|
||||||
|
|
||||||
|
-- Add constraint to ensure consistent cancel state
|
||||||
|
ALTER TABLE "Invoice" ADD CONSTRAINT "Invoice_cancel" CHECK (
|
||||||
|
("cancelled" = true AND "cancelledAt" IS NOT NULL AND "userCancel" IS NOT NULL) OR
|
||||||
|
("cancelled" = false AND "cancelledAt" IS NULL AND "userCancel" IS NULL)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Add trigger to set userCancel to false by default when cancelled updated and userCancel not specified
|
||||||
|
CREATE OR REPLACE FUNCTION invoice_set_user_cancel_default()
|
||||||
|
RETURNS TRIGGER AS $$
|
||||||
|
BEGIN
|
||||||
|
IF NEW.cancelled AND NEW."userCancel" IS NULL THEN
|
||||||
|
NEW."userCancel" := false;
|
||||||
|
END IF;
|
||||||
|
RETURN NEW;
|
||||||
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
||||||
|
|
||||||
|
CREATE TRIGGER invoice_user_cancel_trigger
|
||||||
|
BEFORE UPDATE ON "Invoice"
|
||||||
|
FOR EACH ROW
|
||||||
|
EXECUTE FUNCTION invoice_set_user_cancel_default();
|
||||||
|
|
|
@ -917,6 +917,7 @@ model Invoice {
|
||||||
confirmedIndex BigInt?
|
confirmedIndex BigInt?
|
||||||
cancelled Boolean @default(false)
|
cancelled Boolean @default(false)
|
||||||
cancelledAt DateTime?
|
cancelledAt DateTime?
|
||||||
|
userCancel Boolean?
|
||||||
msatsRequested BigInt
|
msatsRequested BigInt
|
||||||
msatsReceived BigInt?
|
msatsReceived BigInt?
|
||||||
desc String?
|
desc String?
|
||||||
|
|
Loading…
Reference in New Issue