From 75bc1a0bca0c0017cbc23ae9d1c49bf5de9cd44b Mon Sep 17 00:00:00 2001 From: Sam Peters Date: Thu, 5 Oct 2023 14:25:19 -0500 Subject: [PATCH] feat: add get invoice by hash usecase --- .../wallets/get-invoice-for-wallet-by-hash.ts | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 core/api/src/app/wallets/get-invoice-for-wallet-by-hash.ts diff --git a/core/api/src/app/wallets/get-invoice-for-wallet-by-hash.ts b/core/api/src/app/wallets/get-invoice-for-wallet-by-hash.ts new file mode 100644 index 00000000000..164d0c5ce56 --- /dev/null +++ b/core/api/src/app/wallets/get-invoice-for-wallet-by-hash.ts @@ -0,0 +1,26 @@ +import { CouldNotFindWalletInvoiceError } from "@domain/errors" +import { checkedToWalletId } from "@domain/wallets" +import { WalletInvoicesRepository } from "@services/mongoose" + +export const getInvoiceForWalletByHash = async ({ + walletId: uncheckedWalletId, + paymentHash, +}: { + walletId: string + paymentHash: PaymentHash +}) => { + const walletId = checkedToWalletId(uncheckedWalletId) + if (walletId instanceof Error) return walletId + + const walletInvoicesRepository = WalletInvoicesRepository() + + const walletInvoice = await walletInvoicesRepository.findByPaymentHash(paymentHash) + + if (walletInvoice instanceof Error) return walletInvoice + + if (walletInvoice.recipientWalletDescriptor.id !== walletId) { + return new CouldNotFindWalletInvoiceError() + } + + return walletInvoice +}