Distinguish invoices cancelled by user (#1785)
This commit is contained in:
		
							parent
							
								
									6a02ea8c5c
								
							
						
					
					
						commit
						d53bc09773
					
				@ -489,7 +489,7 @@ const resolvers = {
 | 
			
		||||
    },
 | 
			
		||||
    createWithdrawl: createWithdrawal,
 | 
			
		||||
    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
 | 
			
		||||
      if (me && !hmac) {
 | 
			
		||||
        const inv = await models.invoice.findUnique({ where: { hash } })
 | 
			
		||||
@ -499,7 +499,7 @@ const resolvers = {
 | 
			
		||||
        verifyHmac(hash, hmac)
 | 
			
		||||
      }
 | 
			
		||||
      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 }) => {
 | 
			
		||||
      if (!me) {
 | 
			
		||||
 | 
			
		||||
@ -78,7 +78,7 @@ const typeDefs = `
 | 
			
		||||
    createInvoice(amount: Int!): InvoiceOrDirect!
 | 
			
		||||
    createWithdrawl(invoice: String!, maxFee: Int!): 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
 | 
			
		||||
    removeWallet(id: ID!): Boolean
 | 
			
		||||
    deleteWalletLogs(wallet: String): Boolean
 | 
			
		||||
 | 
			
		||||
@ -36,9 +36,9 @@ export default function useInvoice () {
 | 
			
		||||
    return { invoice: data.invoice, check: that(data.invoice) }
 | 
			
		||||
  }, [client])
 | 
			
		||||
 | 
			
		||||
  const cancel = useCallback(async ({ hash, hmac }) => {
 | 
			
		||||
  const cancel = useCallback(async ({ hash, hmac }, { userCancel = false } = {}) => {
 | 
			
		||||
    console.log('canceling invoice:', hash)
 | 
			
		||||
    const { data } = await cancelInvoice({ variables: { hash, hmac } })
 | 
			
		||||
    const { data } = await cancelInvoice({ variables: { hash, hmac, userCancel } })
 | 
			
		||||
    return data.cancelInvoice
 | 
			
		||||
  }, [cancelInvoice])
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -20,7 +20,7 @@ export default function useQrPayment () {
 | 
			
		||||
      let paid
 | 
			
		||||
      const cancelAndReject = async (onClose) => {
 | 
			
		||||
        if (!paid && cancelOnClose) {
 | 
			
		||||
          const updatedInv = await invoice.cancel(inv)
 | 
			
		||||
          const updatedInv = await invoice.cancel(inv, { userCancel: true })
 | 
			
		||||
          reject(new InvoiceCanceledError(updatedInv))
 | 
			
		||||
        }
 | 
			
		||||
        resolve(inv)
 | 
			
		||||
 | 
			
		||||
@ -225,8 +225,8 @@ export const SET_WALLET_PRIORITY = gql`
 | 
			
		||||
 | 
			
		||||
export const CANCEL_INVOICE = gql`
 | 
			
		||||
  ${INVOICE_FIELDS}
 | 
			
		||||
  mutation cancelInvoice($hash: String!, $hmac: String) {
 | 
			
		||||
    cancelInvoice(hash: $hash, hmac: $hmac) {
 | 
			
		||||
  mutation cancelInvoice($hash: String!, $hmac: String, $userCancel: Boolean) {
 | 
			
		||||
    cancelInvoice(hash: $hash, hmac: $hmac, userCancel: $userCancel) {
 | 
			
		||||
      ...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?
 | 
			
		||||
  cancelled          Boolean             @default(false)
 | 
			
		||||
  cancelledAt        DateTime?
 | 
			
		||||
  userCancel         Boolean?
 | 
			
		||||
  msatsRequested     BigInt
 | 
			
		||||
  msatsReceived      BigInt?
 | 
			
		||||
  desc               String?
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user