sahab-utils 1.8.8 → 1.8.9

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.
@@ -64,7 +64,8 @@ export declare enum OrderStatus {
64
64
  COMPLETED = 5,// 5
65
65
  CANCELLED = 6,// 6
66
66
  RATED = 7,// 7
67
- STARTED = 8
67
+ STARTED = 8,// 8
68
+ PENDING_PAYMENT = 9
68
69
  }
69
70
  export declare enum OrderPaymentType {
70
71
  APPLE_PAY = 0,
@@ -116,7 +117,7 @@ export type Order = {
116
117
  paymentCancelationId?: string;
117
118
  paymentRefundId?: string;
118
119
  paymentCaptureId?: string;
119
- canceledBy?: 'provider' | 'client';
120
+ canceledBy?: 'provider' | 'client' | 'system' | 'admin';
120
121
  movementsCreated?: boolean;
121
122
  orderNote?: string;
122
123
  cancelReason?: string;
@@ -156,6 +157,11 @@ export type Order = {
156
157
  visitNumber?: number;
157
158
  /** Multi-visit package only: what the customer pays for the whole package, stamped on every visit. */
158
159
  originalPrice?: number;
160
+ paymentLinkId?: string;
161
+ paymentLinkUrl?: string;
162
+ payLinkDeadline?: TimestampType;
163
+ payLinkSmsCount?: number;
164
+ createdByStaff?: string;
159
165
  };
160
166
  export type ReduxOrder = {
161
167
  orderId?: string;
@@ -190,7 +196,7 @@ export type ReduxOrder = {
190
196
  paymentCancelationId?: string;
191
197
  paymentRefundId?: string;
192
198
  paymentCaptureId?: string;
193
- canceledBy?: 'provider' | 'client';
199
+ canceledBy?: 'provider' | 'client' | 'system' | 'admin';
194
200
  movementsCreated?: boolean;
195
201
  orderNote?: string;
196
202
  cancelReason?: string;
@@ -20,6 +20,7 @@ var OrderStatus;
20
20
  OrderStatus[OrderStatus["CANCELLED"] = 6] = "CANCELLED";
21
21
  OrderStatus[OrderStatus["RATED"] = 7] = "RATED";
22
22
  OrderStatus[OrderStatus["STARTED"] = 8] = "STARTED";
23
+ OrderStatus[OrderStatus["PENDING_PAYMENT"] = 9] = "PENDING_PAYMENT";
23
24
  })(OrderStatus || (exports.OrderStatus = OrderStatus = {}));
24
25
  var OrderPaymentType;
25
26
  (function (OrderPaymentType) {
package/dist/index.d.ts CHANGED
@@ -7,6 +7,7 @@ export * from './ts/gifts';
7
7
  export * from './ts/iban';
8
8
  export * from './ts/loyalty';
9
9
  export * from './ts/orderActivity';
10
+ export * from './ts/payLink';
10
11
  export * from './ts/push';
11
12
  export * from './ts/utils';
12
13
  export * from './ts/wheel';
package/dist/index.js CHANGED
@@ -23,6 +23,7 @@ __exportStar(require("./ts/gifts"), exports);
23
23
  __exportStar(require("./ts/iban"), exports);
24
24
  __exportStar(require("./ts/loyalty"), exports);
25
25
  __exportStar(require("./ts/orderActivity"), exports);
26
+ __exportStar(require("./ts/payLink"), exports);
26
27
  __exportStar(require("./ts/push"), exports);
27
28
  __exportStar(require("./ts/utils"), exports);
28
29
  __exportStar(require("./ts/wheel"), exports);
@@ -0,0 +1,61 @@
1
+ import { CarSize, Order, OrderStatus, Provider, ProvidedAddon, ProvidedService } from '../constants/types';
2
+ /** Back-office orders paid by a link — the shared contract between sahab-website and cloudFun. */
3
+ export declare const PAY_LINK_WINDOW_MINUTES = 15;
4
+ export declare const PAY_LINK_CANCEL_REASON = "payment not completed";
5
+ /** Fields a pay-link order carries. Generic over the timestamp because cloudFun stores firebase-admin Timestamps and the
6
+ * apps/website read firebase web ones. Server-only bookkeeping (task name, run token) stays in cloudFun. */
7
+ export type PayLinkOrderFields<TTimestamp> = {
8
+ paymentLinkId?: string;
9
+ paymentLinkUrl?: string;
10
+ payLinkDeadline?: TTimestamp;
11
+ payLinkSmsCount?: number;
12
+ createdByStaff?: string;
13
+ };
14
+ export type CreatePayLinkOrderRequest = {
15
+ clientUid: string;
16
+ carId: string;
17
+ /** Index into the client's saved `locations`. */
18
+ locationIndex: number;
19
+ providerId: string;
20
+ serviceIds: string[];
21
+ addonIds?: string[];
22
+ /** Start of the chosen slot, epoch ms. */
23
+ orderDate: number;
24
+ promoCode?: string;
25
+ /** Text the link from the server. Off by default: staff copy it or share it on WhatsApp. */
26
+ sendSms?: boolean;
27
+ };
28
+ export type CreatePayLinkOrderResponse = {
29
+ orderId: string;
30
+ orderStatus: OrderStatus;
31
+ /** Absent when the order was free and confirmed without a payment. */
32
+ paymentLinkUrl?: string;
33
+ /** Epoch ms after which the unpaid order is cancelled. */
34
+ payLinkDeadline?: number;
35
+ smsSent: boolean;
36
+ };
37
+ export type ResendPayLinkRequest = {
38
+ orderId: string;
39
+ };
40
+ /** The message carrying the link — one wording for the server's SMS and the back office's copy/WhatsApp share. */
41
+ export declare const payLinkMessage: (lang: string | undefined, url: string) => string;
42
+ export declare const payLinkCancelledMessage: (lang: string | undefined) => string;
43
+ /** wa.me wants the international number as digits only; local 05… numbers are Saudi. */
44
+ export declare function whatsAppShareUrl(phone: string | undefined, text: string): string;
45
+ export declare const payLinkDeadlineMs: (createdMs: number) => number;
46
+ /** Checkout amounts are integer minor units; rounding here keeps the link and the webhook check in agreement. */
47
+ export declare const toMinorUnits: (sar: number) => number;
48
+ /** The webhook amount matches the order only to the halala, never "close enough". */
49
+ export declare const isPayLinkAmountExact: (orderTotalSar: number, paidMinor: number, currency?: string) => boolean;
50
+ export type PayLinkLinesResult = {
51
+ ok: true;
52
+ lines: Array<ProvidedService | ProvidedAddon>;
53
+ } | {
54
+ ok: false;
55
+ reason: 'NO_SERVICE' | 'UNKNOWN_ITEM' | 'NO_PRICE_FOR_SIZE' | 'PACKAGE_NOT_SUPPORTED';
56
+ };
57
+ /** Order lines priced exactly as the customer app's setProvidedService/setProvidedAddon build them: the
58
+ * provider's price for this car size plus the client percentage. Hidden items and packages are refused. */
59
+ export declare function buildPayLinkOrderLines(provider: Pick<Provider, 'services' | 'addons'>, carSize: CarSize, serviceIds: string[], addonIds?: string[]): PayLinkLinesResult;
60
+ /** A pay-link order that never took money — still waiting, or cancelled for non-payment. Statements and KPIs leave these out. */
61
+ export declare const isNeverPaidPayLinkOrder: (order?: Pick<Order, "orderStatus" | "cancelReason" | "isPaid">) => boolean;
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isNeverPaidPayLinkOrder = exports.isPayLinkAmountExact = exports.toMinorUnits = exports.payLinkDeadlineMs = exports.payLinkCancelledMessage = exports.payLinkMessage = exports.PAY_LINK_CANCEL_REASON = exports.PAY_LINK_WINDOW_MINUTES = void 0;
4
+ exports.whatsAppShareUrl = whatsAppShareUrl;
5
+ exports.buildPayLinkOrderLines = buildPayLinkOrderLines;
6
+ const types_1 = require("../constants/types");
7
+ const calcs_1 = require("./calcs");
8
+ /** Back-office orders paid by a link — the shared contract between sahab-website and cloudFun. */
9
+ exports.PAY_LINK_WINDOW_MINUTES = 15;
10
+ exports.PAY_LINK_CANCEL_REASON = 'payment not completed';
11
+ /** The message carrying the link — one wording for the server's SMS and the back office's copy/WhatsApp share. */
12
+ const payLinkMessage = (lang, url) => lang === 'en'
13
+ ? `Sahab: pay within ${exports.PAY_LINK_WINDOW_MINUTES} minutes to confirm your car wash booking\n${url}`
14
+ : `سحاب: ادفع خلال ${exports.PAY_LINK_WINDOW_MINUTES} دقيقة لتأكيد حجز غسيل سيارتك\n${url}`;
15
+ exports.payLinkMessage = payLinkMessage;
16
+ const payLinkCancelledMessage = (lang) => lang === 'en' ? 'Sahab: your booking was cancelled because the payment was not completed.' : 'سحاب: تم إلغاء حجزك لعدم إتمام الدفع.';
17
+ exports.payLinkCancelledMessage = payLinkCancelledMessage;
18
+ /** wa.me wants the international number as digits only; local 05… numbers are Saudi. */
19
+ function whatsAppShareUrl(phone, text) {
20
+ const digits = (phone !== null && phone !== void 0 ? phone : '').replace(/[^\d]/g, '');
21
+ const international = digits.startsWith('0') ? `966${digits.slice(1)}` : digits;
22
+ return `https://wa.me/${international}?text=${encodeURIComponent(text)}`;
23
+ }
24
+ const payLinkDeadlineMs = (createdMs) => createdMs + exports.PAY_LINK_WINDOW_MINUTES * 60000;
25
+ exports.payLinkDeadlineMs = payLinkDeadlineMs;
26
+ /** Checkout amounts are integer minor units; rounding here keeps the link and the webhook check in agreement. */
27
+ const toMinorUnits = (sar) => Math.round(sar * 100);
28
+ exports.toMinorUnits = toMinorUnits;
29
+ /** The webhook amount matches the order only to the halala, never "close enough". */
30
+ const isPayLinkAmountExact = (orderTotalSar, paidMinor, currency) => (currency !== null && currency !== void 0 ? currency : '').toUpperCase() === 'SAR' && Number.isInteger(paidMinor) && paidMinor === (0, exports.toMinorUnits)(orderTotalSar);
31
+ exports.isPayLinkAmountExact = isPayLinkAmountExact;
32
+ /** Order lines priced exactly as the customer app's setProvidedService/setProvidedAddon build them: the
33
+ * provider's price for this car size plus the client percentage. Hidden items and packages are refused. */
34
+ function buildPayLinkOrderLines(provider, carSize, serviceIds, addonIds = []) {
35
+ var _a, _b, _c, _d, _e, _f, _g;
36
+ if (!serviceIds.length)
37
+ return { ok: false, reason: 'NO_SERVICE' };
38
+ const lines = [];
39
+ for (const id of serviceIds) {
40
+ const service = (_a = provider.services) === null || _a === void 0 ? void 0 : _a.find((s) => s.serviceId === id && !s.hide);
41
+ if (!service)
42
+ return { ok: false, reason: 'UNKNOWN_ITEM' };
43
+ if (((_b = service.numberOfVisits) !== null && _b !== void 0 ? _b : 1) > 1)
44
+ return { ok: false, reason: 'PACKAGE_NOT_SUPPORTED' };
45
+ const price = (_d = (_c = service.prices) === null || _c === void 0 ? void 0 : _c.find((p) => p.size === carSize)) === null || _d === void 0 ? void 0 : _d.price;
46
+ if (!price)
47
+ return { ok: false, reason: 'NO_PRICE_FOR_SIZE' };
48
+ lines.push({
49
+ serviceId: service.serviceId,
50
+ name: service.name,
51
+ imageUrl: service.imageUrl,
52
+ providerId: service.providerId,
53
+ description: service.description,
54
+ price: { size: carSize, price: (0, calcs_1.customerPriceFromProviderPrice)(price) },
55
+ quantity: 1,
56
+ type: 'service',
57
+ time: service.time || 0,
58
+ includedServices: service.includedServices,
59
+ numberOfVisits: 1,
60
+ isDone: false,
61
+ });
62
+ }
63
+ for (const id of addonIds) {
64
+ const addon = (_e = provider.addons) === null || _e === void 0 ? void 0 : _e.find((a) => a.addonId === id && !a.hide);
65
+ if (!addon)
66
+ return { ok: false, reason: 'UNKNOWN_ITEM' };
67
+ const price = (_g = (_f = addon.prices) === null || _f === void 0 ? void 0 : _f.find((p) => p.size === carSize)) === null || _g === void 0 ? void 0 : _g.price;
68
+ if (!price)
69
+ return { ok: false, reason: 'NO_PRICE_FOR_SIZE' };
70
+ lines.push({
71
+ addonId: addon.addonId,
72
+ name: addon.name,
73
+ providerId: addon.providerId,
74
+ description: addon.description,
75
+ price: { size: carSize, price: (0, calcs_1.customerPriceFromProviderPrice)(price) },
76
+ quantity: 1,
77
+ type: 'addon',
78
+ time: addon.time || 0,
79
+ isDone: false,
80
+ });
81
+ }
82
+ return { ok: true, lines };
83
+ }
84
+ /** A pay-link order that never took money — still waiting, or cancelled for non-payment. Statements and KPIs leave these out. */
85
+ const isNeverPaidPayLinkOrder = (order) => (order === null || order === void 0 ? void 0 : order.orderStatus) === types_1.OrderStatus.PENDING_PAYMENT ||
86
+ ((order === null || order === void 0 ? void 0 : order.orderStatus) === types_1.OrderStatus.CANCELLED && order.cancelReason === exports.PAY_LINK_CANCEL_REASON && !order.isPaid);
87
+ exports.isNeverPaidPayLinkOrder = isNeverPaidPayLinkOrder;
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "sahab-utils",
3
- "version": "1.8.8",
3
+ "version": "1.8.9",
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
+ "prepublishOnly": "npm run build",
9
10
  "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 && node tests/cash.test.js && node tests/workingHours.test.js"
11
+ "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 && node tests/workingHours.test.js && node tests/payLink.test.js"
11
12
  },
12
13
  "repository": {
13
14
  "type": "git",