Skip to content

Commit

Permalink
fix(lease-process): fixed finalizing already terminated agreements
Browse files Browse the repository at this point in the history
  • Loading branch information
mgordel committed Jun 10, 2024
1 parent f523dc4 commit 46c64ae
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 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
7 changes: 6 additions & 1 deletion src/market/agreement/agreement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,13 @@ 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";
}

canBeTerminated(): boolean {
const state = this.getState();
return state !== "Rejected" && state !== "Cancelled" && state !== "Terminated";
}
}
38 changes: 22 additions & 16 deletions src/payment/agreement_payment_process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,17 @@ export class AgreementPaymentProcess {
await this.paymentModule.rejectDebitNote(debitNote, rejectMessage);
this.logger.warn(`DebitNote rejected`, { reason: rejectMessage });
} catch (error) {
const message = getMessageFromApiError(error);
throw new GolemPaymentError(
`Unable to reject debit note ${debitNote.id}. ${message}`,
PaymentErrorCode.DebitNoteRejectionFailed,
undefined,
debitNote.provider,
error,
);
this.logger.warn(`DebitNote rejected`, { reason: rejectMessage });
// TODO: this endpoint is not implemented in Yagna, it always responds 501:NotImplemented.
// Until it is implemented ny Yagna, it only logs as a warning
// const message = getMessageFromApiError(error);
// throw new GolemPaymentError(
// `Unable to reject debit note ${debitNote.id}. ${message}`,
// PaymentErrorCode.DebitNoteRejectionFailed,
// undefined,
// debitNote.provider,
// error,
// );
}
}

Expand Down Expand Up @@ -282,14 +285,17 @@ export class AgreementPaymentProcess {
await this.paymentModule.rejectInvoice(invoice, message);
this.logger.warn(`Invoice rejected`, { reason: message });
} catch (error) {
const message = getMessageFromApiError(error);
throw new GolemPaymentError(
`Unable to reject invoice ${invoice.id} ${message}`,
PaymentErrorCode.InvoiceRejectionFailed,
undefined,
invoice.provider,
error,
);
this.logger.warn(`Invoice rejected`, { reason: message });
// TODO: this endpoint is not implemented in Yagna, it always responds 501:NotImplemented.
// Until it is implemented ny Yagna, it only logs as a warning
// const message = getMessageFromApiError(error);
// throw new GolemPaymentError(
// `Unable to reject invoice ${invoice.id} ${message}`,
// PaymentErrorCode.InvoiceRejectionFailed,
// undefined,
// invoice.provider,
// error,
// );
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/payment/payment.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export class PaymentModuleImpl implements PaymentModule {
this.events.emit("debitNoteRejected", rejectedDebitNote);
return rejectedDebitNote;
} catch (error) {
this.events.emit("errorAcceptingDebitNote", debitNote, error);
this.events.emit("errorRejectingDebitNote", debitNote, error);
throw error;
}
}
Expand Down

0 comments on commit 46c64ae

Please sign in to comment.