Skip to content

Commit a072346

Browse files
author
VuXfi
committed
feat: add normalizePercent utility for percentage normalization and integrate it into bonus coverage calculations
1 parent 1b9bf08 commit a072346

File tree

12 files changed

+73
-14
lines changed

12 files changed

+73
-14
lines changed

adapters/promotion/default/promotionAdapter.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class PromotionAdapter extends AbstractPromotionAdapter_1.default {
100100
if (!record.worktime)
101101
return true;
102102
try {
103+
//@ts-ignore
103104
return worktime_1.WorkTimeValidator.isWorkNow({ worktime: record.worktime }).workNow;
104105
}
105106
catch (error) {

adapters/promotion/default/promotionAdapter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export class PromotionAdapter extends AbstractPromotionAdapter {
110110
.filter((record) => {
111111
if (!record.worktime) return true;
112112
try {
113+
//@ts-ignore
113114
return WorkTimeValidator.isWorkNow({ worktime: record.worktime }).workNow;
114115
} catch (error) {
115116
sails.log.error("Promotion > helper > error: ", error);

models/BonusProgram.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export interface BonusProgramRecord extends attributes, ORM {
3333
}
3434
declare let Model: {
3535
beforeCreate(init: BonusProgramRecord, cb: (err?: string) => void): void;
36+
beforeUpdate(valuesToUpdate: Partial<BonusProgramRecord>, cb: (err?: string) => void): void;
3637
/**
3738
* Method for registration alive bonus program adapter
3839
* @param bonusProgramAdapter

models/BonusProgram.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Object.defineProperty(exports, "__esModule", { value: true });
66
const adapters_1 = require("../adapters");
77
const uuid_1 = require("uuid");
8+
const normalize_1 = require("../utils/normalize");
89
const aliveBonusPrograms = {};
910
let attributes = {
1011
/** ID */
@@ -51,15 +52,12 @@ let Model = {
5152
init.id = (0, uuid_1.v4)();
5253
}
5354
// defaults
54-
if (!init.coveragePercentage) {
55-
init.coveragePercentage = 1;
55+
if (init.coveragePercentage !== undefined) {
56+
init.coveragePercentage = (0, normalize_1.normalizePercent)(init.coveragePercentage).toNumber();
5657
}
57-
else if (init.coveragePercentage > 1) {
58+
else {
5859
init.coveragePercentage = 1;
5960
}
60-
else if (init.coveragePercentage < 0) {
61-
init.coveragePercentage = 0;
62-
}
6361
if (!init.exchangeRate) {
6462
init.exchangeRate = 1;
6563
}
@@ -72,6 +70,12 @@ let Model = {
7270
}
7371
cb();
7472
},
73+
beforeUpdate(valuesToUpdate, cb) {
74+
if (valuesToUpdate.coveragePercentage !== undefined) {
75+
valuesToUpdate.coveragePercentage = (0, normalize_1.normalizePercent)(valuesToUpdate.coveragePercentage).toNumber();
76+
}
77+
cb();
78+
},
7579
/**
7680
* Method for registration alive bonus program adapter
7781
* @param bonusProgramAdapter

models/BonusProgram.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import ORM from "../interfaces/ORM";
88
import { ORMModel } from "../interfaces/ORMModel";
99

1010
import { v4 as uuid } from "uuid";
11+
import { normalizePercent } from "../utils/normalize";
1112

1213
const aliveBonusPrograms: {
1314
[key: string]: BonusProgramAdapter
@@ -76,12 +77,10 @@ let Model = {
7677
}
7778

7879
// defaults
79-
if (!init.coveragePercentage) {
80-
init.coveragePercentage = 1;
81-
}else if(init.coveragePercentage > 1) {
80+
if (init.coveragePercentage !== undefined) {
81+
init.coveragePercentage = normalizePercent(init.coveragePercentage).toNumber();
82+
} else {
8283
init.coveragePercentage = 1;
83-
} else if (init.coveragePercentage < 0) {
84-
init.coveragePercentage = 0;
8584
}
8685

8786
if (!init.exchangeRate) {
@@ -100,6 +99,13 @@ let Model = {
10099
cb();
101100
},
102101

102+
beforeUpdate(valuesToUpdate: Partial<BonusProgramRecord>, cb: (err?: string) => void) {
103+
if (valuesToUpdate.coveragePercentage !== undefined) {
104+
valuesToUpdate.coveragePercentage = normalizePercent(valuesToUpdate.coveragePercentage).toNumber();
105+
}
106+
cb();
107+
},
108+
103109
/**
104110
* Method for registration alive bonus program adapter
105111
* @param bonusProgramAdapter

models/Group.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ let Model = {
369369
groups = groups.filter(group => {
370370
if (!group.worktime)
371371
return true;
372+
//@ts-ignore
372373
return worktime_1.WorkTimeValidator.isWorkNow({ worktime: group.worktime }).workNow;
373374
});
374375
return groups.sort((a, b) => a.sortOrder - b.sortOrder);

models/Group.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ let Model = {
448448
}
449449
groups = groups.filter(group => {
450450
if (!group.worktime) return true;
451+
//@ts-ignore
451452
return WorkTimeValidator.isWorkNow({ worktime: group.worktime }).workNow
452453
}
453454
);

models/Order.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const phoneValidByMask_1 = require("../libs/phoneValidByMask");
1010
const OrderHelper_1 = require("../libs/helpers/OrderHelper");
1111
const isValue_1 = require("../utils/isValue");
1212
const ProductModifier_1 = require("../libs/ProductModifier");
13+
const normalize_1 = require("../utils/normalize");
1314
let attributes = {
1415
/** Id */
1516
id: {
@@ -774,7 +775,14 @@ let Model = {
774775
throw `Invalid bonus spending strategy: ${bonusSpendingStrategy}`;
775776
}
776777
// Calculate maximum allowed bonus coverage
777-
const maxBonusCoverage = new decimal_js_1.default(amountToDeduct).mul(bonusProgram.coveragePercentage);
778+
const maxBonusCoverage = new decimal_js_1.default(amountToDeduct).mul((0, normalize_1.normalizePercent)(bonusProgram.coveragePercentage));
779+
// Ensure maxBonusCoverage is not greater than amountToDeduct
780+
if (maxBonusCoverage.gt(amountToDeduct)) {
781+
throw {
782+
code: 19,
783+
error: "Max bonus coverage exceeds allowable amount to deduct",
784+
};
785+
}
778786
// Check if the specified bonus spend amount is more than the maximum allowed bonus coverage
779787
let bonusCoverage;
780788
if (spendBonus.amount && new decimal_js_1.default(spendBonus.amount).lessThan(maxBonusCoverage)) {

models/Order.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import ToInitialize from "../hook/initialize";
2727
import { or } from "ajv/dist/compile/codegen";
2828
import { BonusTransaction } from "../adapters/bonusprogram/BonusProgramAdapter";
2929
import { ProductModifier } from "../libs/ProductModifier";
30+
import { normalizePercent } from "../utils/normalize";
3031

3132
export interface PromotionState {
3233
type: string;
@@ -962,8 +963,14 @@ let Model = {
962963
}
963964

964965
// Calculate maximum allowed bonus coverage
965-
const maxBonusCoverage = new Decimal(amountToDeduct).mul(bonusProgram.coveragePercentage);
966-
966+
const maxBonusCoverage = new Decimal(amountToDeduct).mul(normalizePercent(bonusProgram.coveragePercentage));
967+
// Ensure maxBonusCoverage is not greater than amountToDeduct
968+
if (maxBonusCoverage.gt(amountToDeduct)) {
969+
throw {
970+
code: 19,
971+
error: "Max bonus coverage exceeds allowable amount to deduct",
972+
};
973+
}
967974
// Check if the specified bonus spend amount is more than the maximum allowed bonus coverage
968975
let bonusCoverage: Decimal;
969976
if (spendBonus.amount && new Decimal(spendBonus.amount).lessThan(maxBonusCoverage)) {

utils/normalize.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Decimal from "decimal.js";
2+
export declare function normalizePercent(value: string | number | Decimal): Decimal;

0 commit comments

Comments
 (0)