@nimee/calculations 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/prices.d.ts +9 -0
- package/dist/prices.js +71 -0
- package/dist/prices.js.map +1 -0
- package/package.json +34 -0
- package/src/index.ts +1 -0
- package/src/prices.ts +68 -0
- package/tsconfig.json +28 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./prices";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./prices"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB"}
|
package/dist/prices.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { IAccountModel, IEventModel, IUserEventModel } from "@nimee/shared-types";
|
|
2
|
+
export declare const getTotalTicketsAmount: (allUsersEvent: IUserEventModel[]) => number;
|
|
3
|
+
export declare const calculateFinalPriceWithNimiFee: (totalPrice: number, event: IEventModel, account: IAccountModel) => {
|
|
4
|
+
nimiFeePercentage: number;
|
|
5
|
+
nimiFee: number;
|
|
6
|
+
finalPriceWithNimiFee: number;
|
|
7
|
+
};
|
|
8
|
+
export declare const calculateFinalPriceWithCouponDiscount: (totalPrice: number, couponDiscount: number) => number;
|
|
9
|
+
export declare const validateFreeOrder: (ticketIds: string[], couponDiscount: number, accountId: string, eventId: string) => Promise<boolean>;
|
package/dist/prices.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.validateFreeOrder = exports.calculateFinalPriceWithCouponDiscount = exports.calculateFinalPriceWithNimiFee = exports.getTotalTicketsAmount = void 0;
|
|
13
|
+
/* Central the prices logic so we won't repeat it */
|
|
14
|
+
const shared_types_1 = require("@nimee/shared-types");
|
|
15
|
+
const api_clients_1 = require("@nimee/api-clients");
|
|
16
|
+
const error_handler_1 = require("@nimee/error-handler");
|
|
17
|
+
// Support both legacy and new userEvent schema (with ticketsInfo and without.)
|
|
18
|
+
const getTotalTicketsAmount = (allUsersEvent) => {
|
|
19
|
+
return allUsersEvent.reduce((acc, userEvent) => {
|
|
20
|
+
var _a;
|
|
21
|
+
// For user-event to one ticket.
|
|
22
|
+
if (!((_a = userEvent === null || userEvent === void 0 ? void 0 : userEvent.ticketsInfo) === null || _a === void 0 ? void 0 : _a.length)) {
|
|
23
|
+
return acc + 1;
|
|
24
|
+
}
|
|
25
|
+
// For user-event to many ticket.
|
|
26
|
+
return acc + userEvent.ticketsInfo.length;
|
|
27
|
+
}, 0);
|
|
28
|
+
};
|
|
29
|
+
exports.getTotalTicketsAmount = getTotalTicketsAmount;
|
|
30
|
+
const calculateFinalPriceWithNimiFee = (totalPrice, event, account) => {
|
|
31
|
+
switch (event.feeCollectionType) {
|
|
32
|
+
case shared_types_1.IFeeCollectionType.PERCENTAGE_FROM_TICKET:
|
|
33
|
+
return { finalPriceWithNimiFee: totalPrice, nimiFee: 0, nimiFeePercentage: account.marketFee };
|
|
34
|
+
case shared_types_1.IFeeCollectionType.ON_TOP_OF_TICKET:
|
|
35
|
+
// minimum 5
|
|
36
|
+
const unroundedValue = Math.max(5, (totalPrice * account.marketFee) / 100);
|
|
37
|
+
const roundedValue = Math.round(unroundedValue * 2) / 2;
|
|
38
|
+
return {
|
|
39
|
+
finalPriceWithNimiFee: roundedValue + totalPrice,
|
|
40
|
+
nimiFee: roundedValue,
|
|
41
|
+
nimiFeePercentage: (roundedValue / totalPrice) * 100,
|
|
42
|
+
};
|
|
43
|
+
default:
|
|
44
|
+
return { finalPriceWithNimiFee: totalPrice, nimiFee: 0, nimiFeePercentage: account.marketFee };
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
exports.calculateFinalPriceWithNimiFee = calculateFinalPriceWithNimiFee;
|
|
48
|
+
const calculateFinalPriceWithCouponDiscount = (totalPrice, couponDiscount) => totalPrice - couponDiscount < 0 ? 0 : totalPrice - couponDiscount;
|
|
49
|
+
exports.calculateFinalPriceWithCouponDiscount = calculateFinalPriceWithCouponDiscount;
|
|
50
|
+
// Validate the order is really free, rather the tickets free or with the coupon discount.
|
|
51
|
+
// we do this so the client won't be able to create orders with price 0 and it won't be validated
|
|
52
|
+
const validateFreeOrder = (ticketIds, couponDiscount, accountId, eventId) => __awaiter(void 0, void 0, void 0, function* () {
|
|
53
|
+
const ticketClient = new api_clients_1.TicketClient();
|
|
54
|
+
const tickets = yield ticketClient.getTicketsByIds({ token: process.env.JWT_ADMIN, ticketIds, accountId, eventId });
|
|
55
|
+
if (!(tickets === null || tickets === void 0 ? void 0 : tickets.length)) {
|
|
56
|
+
throw new error_handler_1.CustomError("tickets_not_found_validate_free_order", 400, `tickets not found when try to validate free order with ticketIds - ${ticketIds} and eventId - ${eventId}`);
|
|
57
|
+
}
|
|
58
|
+
const rawOrderPriceFromTickets = tickets.reduce((acc, ticket) => acc + ticket.price, 0);
|
|
59
|
+
if (rawOrderPriceFromTickets === 0) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
if (couponDiscount) {
|
|
63
|
+
const orderPriceAfterCoupon = (0, exports.calculateFinalPriceWithCouponDiscount)(rawOrderPriceFromTickets, couponDiscount);
|
|
64
|
+
if (orderPriceAfterCoupon === 0) {
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return false;
|
|
69
|
+
});
|
|
70
|
+
exports.validateFreeOrder = validateFreeOrder;
|
|
71
|
+
//# sourceMappingURL=prices.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prices.js","sourceRoot":"","sources":["../src/prices.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,oDAAoD;AACpD,sDAAsG;AACtG,oDAAkD;AAClD,wDAAmD;AAEnD,+EAA+E;AACxE,MAAM,qBAAqB,GAAG,CAAC,aAAgC,EAAU,EAAE;IAChF,OAAO,aAAa,CAAC,MAAM,CAAC,CAAC,GAAW,EAAE,SAA0B,EAAE,EAAE;;QACtE,g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": "nimi calculations like prices for back and front",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"author": "dan goldberg",
|
|
7
|
+
"license": "ISC",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build-ts": "tsc",
|
|
10
|
+
"watch-ts": "tsc -w",
|
|
11
|
+
"build": "npm run build-ts",
|
|
12
|
+
"lint": "eslint -c ../../.eslintrc.js --ext .ts src/**",
|
|
13
|
+
"prettier-format": "prettier --config .prettierrc 'src/**/*.ts' --write",
|
|
14
|
+
"incVersionAndPublish": "npm version patch && npm run publish-it && npm run i-all-workspaces",
|
|
15
|
+
"publish-it": "npm run build && npm publish --scope=nimee --access public",
|
|
16
|
+
"i-all-workspaces": "version=${npm_package_version} npm i @nimee/calculations@$version --workspace=@nimee/event --workspace=@nimee/user-event --workspace=@nimee/send-notification --workspace=@nimee/user --workspace=@nimee/payment --workspace=@nimee/analytics"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@typescript-eslint/eslint-plugin": "^5.18.0",
|
|
20
|
+
"@typescript-eslint/eslint-plugin-tslint": "^5.18.0",
|
|
21
|
+
"@typescript-eslint/parser": "^5.18.0",
|
|
22
|
+
"eslint": "8.22.0",
|
|
23
|
+
"eslint-plugin-jsdoc": "^38.1.6",
|
|
24
|
+
"eslint-plugin-no-null": "^1.0.2",
|
|
25
|
+
"nodemon": "^1.19.1",
|
|
26
|
+
"typescript": "^5.2.2"
|
|
27
|
+
},
|
|
28
|
+
"types": "dist/index.d.ts",
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@nimee/api-clients": "^1.1.15",
|
|
31
|
+
"@nimee/error-handler": "^0.0.7",
|
|
32
|
+
"axios": "1.2.1"
|
|
33
|
+
}
|
|
34
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./prices";
|
package/src/prices.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/* Central the prices logic so we won't repeat it */
|
|
2
|
+
import { IAccountModel, IEventModel, IFeeCollectionType, IUserEventModel } from "@nimee/shared-types";
|
|
3
|
+
import { TicketClient } from "@nimee/api-clients";
|
|
4
|
+
import { CustomError } from "@nimee/error-handler";
|
|
5
|
+
|
|
6
|
+
// Support both legacy and new userEvent schema (with ticketsInfo and without.)
|
|
7
|
+
export const getTotalTicketsAmount = (allUsersEvent: IUserEventModel[]): number => {
|
|
8
|
+
return allUsersEvent.reduce((acc: number, userEvent: IUserEventModel) => {
|
|
9
|
+
// For user-event to one ticket.
|
|
10
|
+
if (!userEvent?.ticketsInfo?.length) {
|
|
11
|
+
return acc + 1;
|
|
12
|
+
}
|
|
13
|
+
// For user-event to many ticket.
|
|
14
|
+
return acc + userEvent.ticketsInfo.length;
|
|
15
|
+
}, 0);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const calculateFinalPriceWithNimiFee = (
|
|
19
|
+
totalPrice: number,
|
|
20
|
+
event: IEventModel,
|
|
21
|
+
account: IAccountModel
|
|
22
|
+
): { nimiFeePercentage: number; nimiFee: number; finalPriceWithNimiFee: number } => {
|
|
23
|
+
switch (event.feeCollectionType) {
|
|
24
|
+
case IFeeCollectionType.PERCENTAGE_FROM_TICKET:
|
|
25
|
+
return { finalPriceWithNimiFee: totalPrice, nimiFee: 0, nimiFeePercentage: account.marketFee };
|
|
26
|
+
case IFeeCollectionType.ON_TOP_OF_TICKET:
|
|
27
|
+
// minimum 5
|
|
28
|
+
const unroundedValue = Math.max(5, (totalPrice * account.marketFee) / 100);
|
|
29
|
+
const roundedValue = Math.round(unroundedValue * 2) / 2;
|
|
30
|
+
return {
|
|
31
|
+
finalPriceWithNimiFee: roundedValue + totalPrice,
|
|
32
|
+
nimiFee: roundedValue,
|
|
33
|
+
nimiFeePercentage: (roundedValue / totalPrice) * 100,
|
|
34
|
+
};
|
|
35
|
+
default:
|
|
36
|
+
return { finalPriceWithNimiFee: totalPrice, nimiFee: 0, nimiFeePercentage: account.marketFee };
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
export const calculateFinalPriceWithCouponDiscount = (totalPrice: number, couponDiscount: number): number =>
|
|
40
|
+
totalPrice - couponDiscount < 0 ? 0 : totalPrice - couponDiscount;
|
|
41
|
+
|
|
42
|
+
// Validate the order is really free, rather the tickets free or with the coupon discount.
|
|
43
|
+
// we do this so the client won't be able to create orders with price 0 and it won't be validated
|
|
44
|
+
export const validateFreeOrder = async (ticketIds: string[], couponDiscount: number, accountId: string, eventId: string) => {
|
|
45
|
+
const ticketClient = new TicketClient();
|
|
46
|
+
const tickets = await ticketClient.getTicketsByIds({ token: process.env.JWT_ADMIN, ticketIds, accountId, eventId });
|
|
47
|
+
if (!tickets?.length) {
|
|
48
|
+
throw new CustomError(
|
|
49
|
+
"tickets_not_found_validate_free_order",
|
|
50
|
+
400,
|
|
51
|
+
`tickets not found when try to validate free order with ticketIds - ${ticketIds} and eventId - ${eventId}`
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const rawOrderPriceFromTickets = tickets.reduce((acc: number, ticket: { price: number }) => acc + ticket.price, 0);
|
|
56
|
+
if (rawOrderPriceFromTickets === 0) {
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (couponDiscount) {
|
|
61
|
+
const orderPriceAfterCoupon = calculateFinalPriceWithCouponDiscount(rawOrderPriceFromTickets, couponDiscount);
|
|
62
|
+
if (orderPriceAfterCoupon === 0) {
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return false;
|
|
68
|
+
};
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compileOnSave": true,
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"module": "commonjs",
|
|
5
|
+
"esModuleInterop": true,
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"target": "es6",
|
|
8
|
+
"noImplicitAny": true,
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
"moduleResolution": "node",
|
|
11
|
+
"sourceMap": true,
|
|
12
|
+
"outDir": "./dist",
|
|
13
|
+
"baseUrl": ".",
|
|
14
|
+
"paths": {
|
|
15
|
+
"*": [
|
|
16
|
+
"node_modules/*",
|
|
17
|
+
"src/types/*"
|
|
18
|
+
]
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"include": [
|
|
22
|
+
"src/**/*"
|
|
23
|
+
],
|
|
24
|
+
"exclude": [
|
|
25
|
+
"node_modules",
|
|
26
|
+
"dist"
|
|
27
|
+
]
|
|
28
|
+
}
|