Skip to content

Commit

Permalink
chore: applied review notes
Browse files Browse the repository at this point in the history
  • Loading branch information
grisha87 committed Dec 19, 2023
1 parent ec3cf63 commit 2811e32
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
1 change: 0 additions & 1 deletion src/payment/agreement_payment_process.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ describe("AgreementPaymentProcess", () => {
invoiceFilter: () => true,
});

// When
const debitNote = instance(debitNoteMock);

const firstSuccess = await process.addDebitNote(debitNote);
Expand Down
12 changes: 5 additions & 7 deletions src/payment/rejection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}

Expand Down
8 changes: 6 additions & 2 deletions src/payment/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
26 changes: 13 additions & 13 deletions tests/unit/payment_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand All @@ -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();
});

Expand Down Expand Up @@ -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,
Expand All @@ -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();
});

Expand All @@ -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();
});
});
Expand Down

0 comments on commit 2811e32

Please sign in to comment.