From 2811e32873ad5c3bedd13d42c034fc9477806cfc Mon Sep 17 00:00:00 2001 From: Grzegorz Godlewski Date: Tue, 19 Dec 2023 16:05:25 +0100 Subject: [PATCH] chore: applied review notes --- src/payment/agreement_payment_process.test.ts | 1 - src/payment/rejection.ts | 12 ++++----- src/payment/service.ts | 8 ++++-- tests/unit/payment_service.test.ts | 26 +++++++++---------- 4 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/payment/agreement_payment_process.test.ts b/src/payment/agreement_payment_process.test.ts index d9a8350f0..6f779fe28 100644 --- a/src/payment/agreement_payment_process.test.ts +++ b/src/payment/agreement_payment_process.test.ts @@ -227,7 +227,6 @@ describe("AgreementPaymentProcess", () => { invoiceFilter: () => true, }); - // When const debitNote = instance(debitNoteMock); const firstSuccess = await process.addDebitNote(debitNote); diff --git a/src/payment/rejection.ts b/src/payment/rejection.ts index 0ba4717ea..cde5d18ed 100644 --- a/src/payment/rejection.ts +++ b/src/payment/rejection.ts @@ -5,15 +5,13 @@ export enum RejectionReason { UnsolicitedService = "UNSOLICITED_SERVICE", BadService = "BAD_SERVICE", IncorrectAmount = "INCORRECT_AMOUNT", + RejectedByRequestorFilter = "REJECTED_BY_REQUESTOR_FILTER", + /** - * We might get a debit note related to an agreement which is already covered with - * a final invoice. In such cases we don't want to pay for the debit note, - * as the payment will be already made when we accept the invoice. + * Use it when you're processing an event after the agreement reached it's "final state" + * + * By final state we mean: we got an invoice for that agreement */ - NonPayableAgreement = "NON_PAYABLE_AGREEMENT", - Duplicate = "DUPLICATE", - RejectedByRequestorFilter = "REJECTED_BY_REQUESTOR_FILTER", - AlreadyAccepted = "ALREADY_ACCEPTED", AgreementFinalized = "AGREEMENT_FINALIZED", } diff --git a/src/payment/service.ts b/src/payment/service.ts index 3f018e354..47778d1d3 100644 --- a/src/payment/service.ts +++ b/src/payment/service.ts @@ -134,13 +134,17 @@ export class PaymentService { // Reason: We will remove this in 2.0 // eslint-disable-next-line @typescript-eslint/no-unused-vars acceptDebitNotes(_agreementId: string) { + this.logger?.warn( + "PaymentService.acceptDebitNotes is deprecated and will be removed in the next major version. " + + "Use PaymentService.acceptPayments which now also deal with debit notes.", + ); return; } private getNumberOfUnpaidAgreements() { - const unpaidProcesses = [...this.processes.values()].filter((p) => !p.isFinished()); + const inProgress = [...this.processes.values()].filter((p) => !p.isFinished()); - return unpaidProcesses.length; + return inProgress.length; } private async processInvoice(invoice: Invoice) { diff --git a/tests/unit/payment_service.test.ts b/tests/unit/payment_service.test.ts index 64b49b36e..cc6d2de7c 100644 --- a/tests/unit/payment_service.test.ts +++ b/tests/unit/payment_service.test.ts @@ -7,6 +7,11 @@ import { debitNotesEvents, debitNotes, invoices, invoiceEvents } from "../mock/f const logger = new LoggerMock(); const yagnaApi = new YagnaMock().getApi(); +/** + * service.end() waits for invoices to be paid, in unit-tests that should be below 5s + */ +const TEST_PAYMENT_TIMEOUT_MS = 1000; + describe("Payment Service", () => { beforeEach(() => { logger.clear(); @@ -34,23 +39,18 @@ describe("Payment Service", () => { it("should accept and process invoice for agreement", async () => { const paymentService = new PaymentService(yagnaApi, { logger, - paymentTimeout: 100, + paymentTimeout: TEST_PAYMENT_TIMEOUT_MS, }); setExpectedEvents(invoiceEvents); setExpectedInvoices(invoices); await paymentService.createAllocation(); await paymentService.run(); paymentService.acceptPayments(agreement); - // await new Promise((res) => setTimeout(res, 200)); - await logger.expectToInclude(`Invoice accepted from provider ${agreement.provider.name}`, 100); + // + await logger.expectToInclude(`Invoice accepted from provider ${agreement.provider.name}`, 1_000); await paymentService.end(); }); - /** - * service.end() waits for invoices to be paid, in unit-tests that should be below 5s - */ - const TEST_PAYMENT_TIMEOUT_MS = 1000; - it("should accept and process debit note for agreement", async () => { const paymentService = new PaymentService(yagnaApi, { logger, @@ -61,7 +61,7 @@ describe("Payment Service", () => { await paymentService.createAllocation(); await paymentService.acceptPayments(agreement); await paymentService.run(); - await logger.expectToInclude(`DebitNote accepted for agreement ${agreement.id}`, 100); + await logger.expectToInclude(`DebitNote accepted for agreement ${agreement.id}`, 1_000); await paymentService.end(); }); @@ -150,7 +150,7 @@ describe("Payment Service", () => { await paymentService.createAllocation(); paymentService.acceptPayments(agreement); await paymentService.run(); - await new Promise((res) => setTimeout(res, 200)); + await logger.expectToInclude( `Invoice has been rejected for provider ${agreement.provider.name}. Reason: Invoice rejected by Invoice Filter`, 100, @@ -169,7 +169,7 @@ describe("Payment Service", () => { await paymentService.createAllocation(); await paymentService.acceptPayments(agreement); await paymentService.run(); - await logger.expectToInclude(`DebitNote accepted for agreement ${agreement.id}`, 100); + await logger.expectToInclude(`DebitNote accepted for agreement ${agreement.id}`, 1_000); await paymentService.end(); }); @@ -183,8 +183,8 @@ describe("Payment Service", () => { await paymentService.createAllocation(); await paymentService.run(); paymentService.acceptPayments(agreement); - await new Promise((res) => setTimeout(res, 200)); - await logger.expectToInclude(`Invoice accepted from provider ${agreement.provider.name}`, 100); + + await logger.expectToInclude(`Invoice accepted from provider ${agreement.provider.name}`, 1_000); await paymentService.end(); }); });