Skip to content

Commit

Permalink
feat: reset used coupon count after canceling or returning an invoice
Browse files Browse the repository at this point in the history
  • Loading branch information
AbleKSaju committed Sep 30, 2024
1 parent 66dbf73 commit 71a827f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
25 changes: 22 additions & 3 deletions models/baseModels/Invoice/Invoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ export abstract class Invoice extends Transactional {

if (this.isReturn) {
await this._removeLoyaltyPointEntry();
await this.reduceUsedCountOfCoupons();

return;
}

if (this.isQuote) {
Expand Down Expand Up @@ -248,12 +251,14 @@ export abstract class Invoice extends Transactional {
await this._updatePartyOutStanding();
await this._updateIsItemsReturned();
await this._removeLoyaltyPointEntry();
await this._reduceUsedCountOfCoupon();
}

async _reduceUsedCountOfCoupon() {
await this.reduceUsedCountOfCoupons();
}

async _removeLoyaltyPointEntry() {
if (!this.loyaltyProgram) {
return;
}
await removeLoyaltyPoint(this);
}

Expand Down Expand Up @@ -567,6 +572,20 @@ export abstract class Invoice extends Transactional {
await couponDoc.setAndSync({ used: (couponDoc.used as number) + 1 });
});
}
async reduceUsedCountOfCoupons() {

Check failure on line 575 in models/baseModels/Invoice/Invoice.ts

View workflow job for this annotation

GitHub Actions / setup_and_lint

Async method 'reduceUsedCountOfCoupons' has no 'await' expression
if (!this.coupons?.length) {
return;
}

this.coupons?.map(async (coupon) => {
const couponDoc = await this.fyo.doc.getDoc(
ModelNameEnum.CouponCode,
coupon.coupons
);

await couponDoc.setAndSync({ used: (couponDoc.used as number) - 1 });
});
}

async _updateIsItemsReturned() {
if (!this.isReturn || !this.returnAgainst || this.isQuote) {
Expand Down
4 changes: 4 additions & 0 deletions models/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,10 @@ export function getLoyaltyProgramTier(
}

export async function removeLoyaltyPoint(doc: Doc) {
if (!doc.loyaltyProgram) {
return;
}

const data = (await doc.fyo.db.getAll(ModelNameEnum.LoyaltyPointEntry, {
fields: ['name', 'loyaltyPoints', 'expiryDate'],
filters: {
Expand Down

0 comments on commit 71a827f

Please sign in to comment.