sahab-utils 1.8.0 → 1.8.2
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/constants/types.d.ts +2 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/ts/calcs.js +8 -10
- package/dist/ts/cash.d.ts +13 -0
- package/dist/ts/cash.js +28 -0
- package/package.json +2 -2
|
@@ -949,6 +949,8 @@ export type WeatherData = {
|
|
|
949
949
|
};
|
|
950
950
|
export type PaymentConfigs = {
|
|
951
951
|
isCashAllowed: boolean;
|
|
952
|
+
/** Sahab's surcharge on a cash order, in SAR. Missing, invalid or above MAX_CASH_FEE means no fee. */
|
|
953
|
+
cashFee?: number;
|
|
952
954
|
isGooglePayAllowed?: boolean;
|
|
953
955
|
/** Tap GoSell merchant id for card / Apple Pay SDK config (optional; falls back in app). */
|
|
954
956
|
tapMerchantId?: string;
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -17,6 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("./constants/types"), exports);
|
|
18
18
|
__exportStar(require("./ts/broadcast"), exports);
|
|
19
19
|
__exportStar(require("./ts/calcs"), exports);
|
|
20
|
+
__exportStar(require("./ts/cash"), exports);
|
|
20
21
|
__exportStar(require("./ts/cashback"), exports);
|
|
21
22
|
__exportStar(require("./ts/gifts"), exports);
|
|
22
23
|
__exportStar(require("./ts/iban"), exports);
|
package/dist/ts/calcs.js
CHANGED
|
@@ -195,16 +195,16 @@ function cashSurcharge(order) {
|
|
|
195
195
|
var _a;
|
|
196
196
|
return (order === null || order === void 0 ? void 0 : order.paymentType) === types_1.OrderPaymentType.CASH ? Math.max(0, (0, utils_1.preventNaN)((_a = order === null || order === void 0 ? void 0 : order.sahabExtraPaymentAmount) !== null && _a !== void 0 ? _a : 0)) : 0;
|
|
197
197
|
}
|
|
198
|
-
/**
|
|
199
|
-
*
|
|
200
|
-
function
|
|
198
|
+
/** What was actually collected: the washer's cash, which already includes Sahab's surcharge, or the online charge.
|
|
199
|
+
* The provider holds all of the cash, so the surcharge must stay in this figure for the transfer to take it back. */
|
|
200
|
+
function settledPaymentAmount(order) {
|
|
201
201
|
var _a;
|
|
202
|
-
return
|
|
202
|
+
return (order === null || order === void 0 ? void 0 : order.paymentType) === types_1.OrderPaymentType.CASH ? (0, utils_1.preventNaN)((_a = order === null || order === void 0 ? void 0 : order.paymentAmount) !== null && _a !== void 0 ? _a : 0) : getCustomerTotalToPay(order);
|
|
203
203
|
}
|
|
204
204
|
function getProviderTransferAmount(order) {
|
|
205
205
|
var _a, _b;
|
|
206
206
|
const orderTotal = getServicesTotal(order) - getProviderDiscountsAmount(order);
|
|
207
|
-
const paymentAmount = (order
|
|
207
|
+
const paymentAmount = settledPaymentAmount(order);
|
|
208
208
|
const originalPrice = orderTotal / (1 + (0, utils_1.preventNaN)((_a = order === null || order === void 0 ? void 0 : order.sahabClientPercentage) !== null && _a !== void 0 ? _a : 0));
|
|
209
209
|
const minimumProviderAmount = originalPrice - originalPrice * ((_b = order === null || order === void 0 ? void 0 : order.sahabProviderPercentage) !== null && _b !== void 0 ? _b : 0);
|
|
210
210
|
if ((order === null || order === void 0 ? void 0 : order.paymentType) === types_1.OrderPaymentType.CASH) {
|
|
@@ -217,9 +217,7 @@ function getProviderTransferAmount(order) {
|
|
|
217
217
|
}
|
|
218
218
|
}
|
|
219
219
|
function getProviderProfits(order) {
|
|
220
|
-
return (order === null || order === void 0 ? void 0 : order.paymentType) === types_1.OrderPaymentType.CASH
|
|
221
|
-
? getCustomerTotalToPay(order) - cashSurcharge(order) + getProviderTransferAmount(order)
|
|
222
|
-
: getProviderTransferAmount(order);
|
|
220
|
+
return (order === null || order === void 0 ? void 0 : order.paymentType) === types_1.OrderPaymentType.CASH ? getCustomerTotalToPay(order) + getProviderTransferAmount(order) : getProviderTransferAmount(order);
|
|
223
221
|
}
|
|
224
222
|
function getOnlineProcessingFees(order) {
|
|
225
223
|
var _a;
|
|
@@ -232,12 +230,12 @@ function getOnlineProcessingFees(order) {
|
|
|
232
230
|
function getSahabFees(order) {
|
|
233
231
|
var _a, _b;
|
|
234
232
|
const orderTotal = getServicesTotal(order) - getProviderDiscountsAmount(order);
|
|
235
|
-
const paymentAmount = (order
|
|
233
|
+
const paymentAmount = settledPaymentAmount(order);
|
|
236
234
|
const originalPrice = orderTotal / (1 + (0, utils_1.preventNaN)((_a = order === null || order === void 0 ? void 0 : order.sahabClientPercentage) !== null && _a !== void 0 ? _a : 0));
|
|
237
235
|
const minimumProviderAmount = originalPrice - originalPrice * ((_b = order === null || order === void 0 ? void 0 : order.sahabProviderPercentage) !== null && _b !== void 0 ? _b : 0);
|
|
238
236
|
// When a Sahab-funded discount pushes the customer below the provider's entitlement, Sahab is
|
|
239
237
|
// genuinely subsidising the order — reporting a floor of 0 here overstated margin, so the negative figure stands.
|
|
240
|
-
return paymentAmount - minimumProviderAmount
|
|
238
|
+
return paymentAmount - minimumProviderAmount;
|
|
241
239
|
}
|
|
242
240
|
function calcOrdersData(orders) {
|
|
243
241
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Order, PaymentConfigs } from '../constants/types';
|
|
2
|
+
/** Cash payment — the shared rules. The fee is set from `payment_methods/configs`; the app only shows it
|
|
3
|
+
* and cloudFun is what charges it, so both must reach the same number from the same config. */
|
|
4
|
+
/** A fee above this is read as a typo (5000 for 5.00) and ignored rather than charged to customers. */
|
|
5
|
+
export declare const MAX_CASH_FEE = 50;
|
|
6
|
+
/** What the customer owes ignoring the cash fee, which comes from the payment method and would make a fully discounted order look unpaid. */
|
|
7
|
+
export declare const getOrderTotalWithoutCashFee: (order?: Partial<Order>) => number;
|
|
8
|
+
/** Nothing left to collect: a 100% promo, balance covering everything, or free services. */
|
|
9
|
+
export declare const isFreeOrder: (order?: Partial<Order>) => boolean;
|
|
10
|
+
/** The configured fee in SAR, or 0 when it is missing, not a number, negative or above `MAX_CASH_FEE`. */
|
|
11
|
+
export declare function resolveCashFee(configs?: Pick<PaymentConfigs, 'cashFee'> | null): number;
|
|
12
|
+
/** The fee this order would carry if paid in cash: none when there is nothing to collect. */
|
|
13
|
+
export declare function cashFeeForOrder(order: Partial<Order> | undefined, configs?: Pick<PaymentConfigs, 'cashFee'> | null): number;
|
package/dist/ts/cash.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isFreeOrder = exports.getOrderTotalWithoutCashFee = exports.MAX_CASH_FEE = void 0;
|
|
4
|
+
exports.resolveCashFee = resolveCashFee;
|
|
5
|
+
exports.cashFeeForOrder = cashFeeForOrder;
|
|
6
|
+
const types_1 = require("../constants/types");
|
|
7
|
+
const calcs_1 = require("./calcs");
|
|
8
|
+
/** Cash payment — the shared rules. The fee is set from `payment_methods/configs`; the app only shows it
|
|
9
|
+
* and cloudFun is what charges it, so both must reach the same number from the same config. */
|
|
10
|
+
/** A fee above this is read as a typo (5000 for 5.00) and ignored rather than charged to customers. */
|
|
11
|
+
exports.MAX_CASH_FEE = 50;
|
|
12
|
+
/** What the customer owes ignoring the cash fee, which comes from the payment method and would make a fully discounted order look unpaid. */
|
|
13
|
+
const getOrderTotalWithoutCashFee = (order) => (0, calcs_1.getCustomerTotalToPay)(Object.assign(Object.assign({}, (order || {})), { paymentType: types_1.OrderPaymentType.CREDIT, sahabExtraPaymentAmount: 0 }));
|
|
14
|
+
exports.getOrderTotalWithoutCashFee = getOrderTotalWithoutCashFee;
|
|
15
|
+
/** Nothing left to collect: a 100% promo, balance covering everything, or free services. */
|
|
16
|
+
const isFreeOrder = (order) => (0, exports.getOrderTotalWithoutCashFee)(order) <= 0;
|
|
17
|
+
exports.isFreeOrder = isFreeOrder;
|
|
18
|
+
/** The configured fee in SAR, or 0 when it is missing, not a number, negative or above `MAX_CASH_FEE`. */
|
|
19
|
+
function resolveCashFee(configs) {
|
|
20
|
+
const fee = Number(configs === null || configs === void 0 ? void 0 : configs.cashFee);
|
|
21
|
+
if (!Number.isFinite(fee) || fee <= 0 || fee > exports.MAX_CASH_FEE)
|
|
22
|
+
return 0;
|
|
23
|
+
return Math.round(fee * 100) / 100;
|
|
24
|
+
}
|
|
25
|
+
/** The fee this order would carry if paid in cash: none when there is nothing to collect. */
|
|
26
|
+
function cashFeeForOrder(order, configs) {
|
|
27
|
+
return (0, exports.isFreeOrder)(order) ? 0 : resolveCashFee(configs);
|
|
28
|
+
}
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sahab-utils",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.2",
|
|
4
4
|
"description": "This package contains types and utils functions",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "tsc",
|
|
9
9
|
"deploy": "npm run build && npm publish",
|
|
10
|
-
"test": "npm run build && node tests/loyalty.test.js && node tests/gifts.test.js && node tests/cashback.test.js && node tests/utils.test.js && node tests/wheel.test.js && node tests/calcs.test.js && node tests/orderActivity.test.js && node tests/push.test.js && node tests/broadcast.test.js && node tests/iban.test.js"
|
|
10
|
+
"test": "npm run build && node tests/loyalty.test.js && node tests/gifts.test.js && node tests/cashback.test.js && node tests/utils.test.js && node tests/wheel.test.js && node tests/calcs.test.js && node tests/orderActivity.test.js && node tests/push.test.js && node tests/broadcast.test.js && node tests/iban.test.js && node tests/cash.test.js"
|
|
11
11
|
},
|
|
12
12
|
"repository": {
|
|
13
13
|
"type": "git",
|