Skip to content

Commit

Permalink
Merge pull request #971 from golemfactory/bugfix/JST-962/finalizing-p…
Browse files Browse the repository at this point in the history
…ayments

fix(lease-process): fixed finalizing already terminated agreements
  • Loading branch information
SewerynKras authored Jun 12, 2024
2 parents 2ee73d8 + 5983a9e commit d2c5d56
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 17 deletions.
4 changes: 3 additions & 1 deletion src/lease-process/lease-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ export class LeaseProcess {
this.logger.debug("Waiting for payment process of agreement to finish", { agreementId: this.agreement.id });
if (this.currentWorkContext) {
await this.activityModule.destroyActivity(this.currentWorkContext.activity);
await this.marketModule.terminateAgreement(this.agreement);
if ((await this.fetchAgreementState()) !== "Terminated") {
await this.marketModule.terminateAgreement(this.agreement);
}
}
await waitForCondition(() => this.paymentProcess.isFinished());
this.logger.debug("Payment process for agreement finalized", { agreementId: this.agreement.id });
Expand Down
2 changes: 1 addition & 1 deletion src/market/agreement/agreement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class Agreement {
* @description if the final state is true, agreement will not change state further anymore
* @return boolean
*/
async isFinalState(): Promise<boolean> {
isFinalState(): boolean {
const state = this.getState();
return state !== "Pending" && state !== "Proposal";
}
Expand Down
1 change: 0 additions & 1 deletion src/payment/agreement_payment_process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ export class AgreementPaymentProcess {
private async rejectDebitNote(debitNote: DebitNote, rejectionReason: RejectionReason, rejectMessage: string) {
try {
await this.paymentModule.rejectDebitNote(debitNote, rejectMessage);
this.logger.warn(`DebitNote rejected`, { reason: rejectMessage });
} catch (error) {
const message = getMessageFromApiError(error);
throw new GolemPaymentError(
Expand Down
23 changes: 15 additions & 8 deletions src/payment/payment.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { PayerDetails } from "./PayerDetails";
import { AgreementPaymentProcess, PaymentProcessOptions } from "./agreement_payment_process";
import { Agreement } from "../market";
import * as EnvUtils from "../shared/utils/env";
import { GolemInternalError } from "../shared/error/golem-error";

export interface PaymentModuleOptions {
/**
Expand Down Expand Up @@ -205,14 +206,20 @@ export class PaymentModuleImpl implements PaymentModule {

async rejectDebitNote(debitNote: DebitNote, reason: string): Promise<DebitNote> {
this.logger.info("Rejecting debit note", { id: debitNote.id, reason });
try {
const rejectedDebitNote = await this.paymentApi.rejectDebitNote(debitNote, reason);
this.events.emit("debitNoteRejected", rejectedDebitNote);
return rejectedDebitNote;
} catch (error) {
this.events.emit("errorAcceptingDebitNote", debitNote, error);
throw error;
}
// TODO: this is not supported by PaymnetAdapter
const message = "Unable to send debitNote rejection to provider. This feature is not yet supported.";
this.logger.warn(message);
this.events.emit("errorRejectingDebitNote", debitNote, new GolemInternalError(message));
return debitNote;
// this.logger.debug("Rejecting debit note", { id: debitNote.id, reason });
// try {
// const rejectedDebitNote = await this.paymentApi.rejectDebitNote(debitNote, reason);
// this.events.emit("debitNoteRejected", rejectedDebitNote);
// return rejectedDebitNote;
// } catch (error) {
// this.events.emit("errorRejectingDebitNote", debitNote, error);
// throw error;
// }
}

/**
Expand Down
15 changes: 9 additions & 6 deletions src/shared/yagna/adapters/payment-api-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,15 @@ export class PaymentApiAdapter implements IPaymentApi {
}
}

async rejectDebitNote(debitNote: DebitNote, reason: string): Promise<DebitNote> {
async rejectDebitNote(debitNote: DebitNote): Promise<DebitNote> {
try {
await this.yagna.payment.rejectDebitNote(debitNote.id, {
rejectionReason: "BAD_SERVICE",
totalAmountAccepted: "0.00",
message: reason,
});
// TODO: this endpoint is not implemented in Yagna, it always responds 501:NotImplemented.
// Reported in https://github.com/golemfactory/yagna/issues/1249
// await this.yagna.payment.rejectDebitNote(debitNote.id, {
// rejectionReason: "BAD_SERVICE",
// totalAmountAccepted: "0.00",
// message: reason,
// });

return this.debitNoteRepo.getById(debitNote.id);
} catch (error) {
Expand All @@ -150,6 +152,7 @@ export class PaymentApiAdapter implements IPaymentApi {
PaymentErrorCode.DebitNoteRejectionFailed,
undefined,
debitNote.provider,
error,
);
}
}
Expand Down

0 comments on commit d2c5d56

Please sign in to comment.