@managesome/eposmatic-toolkit 0.4.50 → 0.4.52
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/esm/constants/enums/platform.js +5 -0
- package/esm/helpers/utils/offer-item-resolution.js +39 -0
- package/lib/constants/enums/platform.d.ts +1 -0
- package/lib/constants/enums/platform.js +6 -1
- package/lib/helpers/utils/offer-item-resolution.d.ts +28 -0
- package/lib/helpers/utils/offer-item-resolution.js +41 -0
- package/lib/tests/offer-item-resolution.test.js +60 -0
- package/package.json +1 -1
- package/esm/schemas/models/applied_order_discounts.js +0 -103
- package/esm/schemas/types/applied_order_discounts.js +0 -1
- package/lib/helpers/order/payment-collection.d.ts +0 -25
- package/lib/helpers/order/payment-collection.js +0 -51
- package/lib/schemas/models/applied_order_discounts.d.ts +0 -80
- package/lib/schemas/models/applied_order_discounts.js +0 -106
- package/lib/schemas/types/applied_order_discounts.d.ts +0 -22
- package/lib/schemas/types/applied_order_discounts.js +0 -2
- package/lib/tests/materialized-reports/analytics-growth-helpers.test.d.ts +0 -1
- package/lib/tests/materialized-reports/analytics-overview-helpers.test.d.ts +0 -1
- package/lib/tests/materialized-reports/analytics-performance-helpers.test.d.ts +0 -1
- package/lib/tests/payment-collection-timestamps.test.d.ts +0 -1
- package/lib/tests/payment-collection-timestamps.test.js +0 -71
|
@@ -133,6 +133,11 @@ export const PLATFORMS_BY_ORDER_FROM = {
|
|
|
133
133
|
[ORDER_FROM_ENUM.EATEASY]: [PLATFORMS_ENUM.EATEASY_TAKE_AWAY, PLATFORMS_ENUM.EATEASY_DELIVERY],
|
|
134
134
|
[ORDER_FROM_ENUM.KEETA]: [PLATFORMS_ENUM.KEETA_TAKE_AWAY, PLATFORMS_ENUM.KEETA_DELIVERY],
|
|
135
135
|
};
|
|
136
|
+
export const INTERNAL_PLATFORMS = [
|
|
137
|
+
...PLATFORMS_BY_ORDER_FROM[ORDER_FROM_ENUM.WEB],
|
|
138
|
+
...PLATFORMS_BY_ORDER_FROM[ORDER_FROM_ENUM.APP],
|
|
139
|
+
...PLATFORMS_BY_ORDER_FROM[ORDER_FROM_ENUM.POS],
|
|
140
|
+
];
|
|
136
141
|
/* ===========================><========================= */
|
|
137
142
|
export const PLATFORM_NAMES = {
|
|
138
143
|
[PLATFORMS_ENUM.POS_WALK_IN]: 'Pos Walk-in',
|
|
@@ -45,3 +45,42 @@ export function getRewardSelectionQuantity({ offer, stats, isVoucherLinked = fal
|
|
|
45
45
|
}
|
|
46
46
|
return Math.max(1, rewardNumOfItems ?? 1);
|
|
47
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* Units of `offerId`'s item reward already claimed on this cart.
|
|
50
|
+
*
|
|
51
|
+
* A claimed unit is a cart line stamped with the offer id in
|
|
52
|
+
* `item_discount_details` - `loyalty_discount_id`, or `voucher_discount_id`
|
|
53
|
+
* for a voucher-pathway claim. Pure: callers pass the categories in.
|
|
54
|
+
*/
|
|
55
|
+
export function getClaimedRewardQuantity({ categories, offerId }) {
|
|
56
|
+
let totalQuantity = 0;
|
|
57
|
+
for (let c = 0; c < (categories?.length ?? 0); c++) {
|
|
58
|
+
const items = categories?.[c]?.items ?? [];
|
|
59
|
+
for (let i = 0; i < items.length; i++) {
|
|
60
|
+
const discountDetails = items[i].item_discount_details;
|
|
61
|
+
if (discountDetails == null)
|
|
62
|
+
continue;
|
|
63
|
+
if (discountDetails.loyalty_discount_id !== offerId && discountDetails.voucher_discount_id !== offerId) {
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
const quantity = Number(items[i].quantity ?? 0);
|
|
67
|
+
if (Number.isFinite(quantity) && quantity > 0) {
|
|
68
|
+
totalQuantity += quantity;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return totalQuantity;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Units still claimable for `offerId` on this cart.
|
|
76
|
+
*
|
|
77
|
+
* `remaining = allowance - already claimed`, floored at 0. Passing one claim's
|
|
78
|
+
* `num_of_items` as the allowance is what capped repeatable rewards at a single
|
|
79
|
+
* claim; the caller must pass the order's total (the offer stats'
|
|
80
|
+
* `applicable_quantity`), or `num_of_items` only when the claim is one claim's
|
|
81
|
+
* worth (a voucher code).
|
|
82
|
+
*/
|
|
83
|
+
export function getRemainingRewardQuantity({ categories, offerId, allowance }) {
|
|
84
|
+
const claimed = getClaimedRewardQuantity({ categories, offerId });
|
|
85
|
+
return Math.max(0, allowance - claimed);
|
|
86
|
+
}
|
|
@@ -68,6 +68,7 @@ export declare const PLATFORMS_BY_ORDER_FROM: {
|
|
|
68
68
|
EATEASY: PLATFORMS_ENUM[];
|
|
69
69
|
KEETA: PLATFORMS_ENUM[];
|
|
70
70
|
};
|
|
71
|
+
export declare const INTERNAL_PLATFORMS: PLATFORMS_ENUM[];
|
|
71
72
|
export declare const PLATFORM_NAMES: Record<PLATFORMS_ENUM, string>;
|
|
72
73
|
export declare enum INTEGRATIONS_ENUM {
|
|
73
74
|
DOORDASH = "DOORDASH",
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
// Auto-split from constants/ENUMS.ts — see ENUMS.ts barrel for exports.
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.ALL_PLATFORM_PROVIDER_IDS = exports.PLATFORM_PROVIDER_IDS_ENUM = exports.ALL_PLATFORM_TYPES = exports.PLATFORM_TYPE_ENUM = exports.ALL_INTEGRATIONS = exports.INTEGRATIONS_ENUM = exports.PLATFORM_NAMES = exports.PLATFORMS_BY_ORDER_FROM = exports.PLATFORMS_BY_ORDER_TYPE = exports.ALL_PLATFORMS = exports.PLATFORMS_ENUM = void 0;
|
|
4
|
+
exports.ALL_PLATFORM_PROVIDER_IDS = exports.PLATFORM_PROVIDER_IDS_ENUM = exports.ALL_PLATFORM_TYPES = exports.PLATFORM_TYPE_ENUM = exports.ALL_INTEGRATIONS = exports.INTEGRATIONS_ENUM = exports.PLATFORM_NAMES = exports.INTERNAL_PLATFORMS = exports.PLATFORMS_BY_ORDER_FROM = exports.PLATFORMS_BY_ORDER_TYPE = exports.ALL_PLATFORMS = exports.PLATFORMS_ENUM = void 0;
|
|
5
5
|
const order_1 = require("./order");
|
|
6
6
|
var PLATFORMS_ENUM;
|
|
7
7
|
(function (PLATFORMS_ENUM) {
|
|
@@ -136,6 +136,11 @@ exports.PLATFORMS_BY_ORDER_FROM = {
|
|
|
136
136
|
[order_1.ORDER_FROM_ENUM.EATEASY]: [PLATFORMS_ENUM.EATEASY_TAKE_AWAY, PLATFORMS_ENUM.EATEASY_DELIVERY],
|
|
137
137
|
[order_1.ORDER_FROM_ENUM.KEETA]: [PLATFORMS_ENUM.KEETA_TAKE_AWAY, PLATFORMS_ENUM.KEETA_DELIVERY],
|
|
138
138
|
};
|
|
139
|
+
exports.INTERNAL_PLATFORMS = [
|
|
140
|
+
...exports.PLATFORMS_BY_ORDER_FROM[order_1.ORDER_FROM_ENUM.WEB],
|
|
141
|
+
...exports.PLATFORMS_BY_ORDER_FROM[order_1.ORDER_FROM_ENUM.APP],
|
|
142
|
+
...exports.PLATFORMS_BY_ORDER_FROM[order_1.ORDER_FROM_ENUM.POS],
|
|
143
|
+
];
|
|
139
144
|
/* ===========================><========================= */
|
|
140
145
|
exports.PLATFORM_NAMES = {
|
|
141
146
|
[PLATFORMS_ENUM.POS_WALK_IN]: 'Pos Walk-in',
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { IOffer } from '../../schemas';
|
|
2
|
+
import type { IOrderCategory } from '../../schemas';
|
|
2
3
|
import type { IOfferWithStats } from './offers-helpers';
|
|
3
4
|
/**
|
|
4
5
|
* Single source for the questions every frontend asks before it can claim a
|
|
@@ -49,3 +50,30 @@ export interface IGetRewardSelectionQuantityParams {
|
|
|
49
50
|
* - never below 1 when the reward grants at least one item.
|
|
50
51
|
*/
|
|
51
52
|
export declare function getRewardSelectionQuantity({ offer, stats, isVoucherLinked, }: IGetRewardSelectionQuantityParams): number;
|
|
53
|
+
export interface IClaimedRewardQuantityParams {
|
|
54
|
+
/** The cart categories the caller already holds; nothing is read here. */
|
|
55
|
+
categories: readonly IOrderCategory[] | null | undefined;
|
|
56
|
+
offerId: string;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Units of `offerId`'s item reward already claimed on this cart.
|
|
60
|
+
*
|
|
61
|
+
* A claimed unit is a cart line stamped with the offer id in
|
|
62
|
+
* `item_discount_details` - `loyalty_discount_id`, or `voucher_discount_id`
|
|
63
|
+
* for a voucher-pathway claim. Pure: callers pass the categories in.
|
|
64
|
+
*/
|
|
65
|
+
export declare function getClaimedRewardQuantity({ categories, offerId }: IClaimedRewardQuantityParams): number;
|
|
66
|
+
export interface IRemainingRewardQuantityParams extends IClaimedRewardQuantityParams {
|
|
67
|
+
/** The order's total allowance, in units (not one claim's worth). */
|
|
68
|
+
allowance: number;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Units still claimable for `offerId` on this cart.
|
|
72
|
+
*
|
|
73
|
+
* `remaining = allowance - already claimed`, floored at 0. Passing one claim's
|
|
74
|
+
* `num_of_items` as the allowance is what capped repeatable rewards at a single
|
|
75
|
+
* claim; the caller must pass the order's total (the offer stats'
|
|
76
|
+
* `applicable_quantity`), or `num_of_items` only when the claim is one claim's
|
|
77
|
+
* worth (a voucher code).
|
|
78
|
+
*/
|
|
79
|
+
export declare function getRemainingRewardQuantity({ categories, offerId, allowance }: IRemainingRewardQuantityParams): number;
|
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.resolveFirstAvailableRewardItem = resolveFirstAvailableRewardItem;
|
|
4
4
|
exports.getRewardSelectionQuantity = getRewardSelectionQuantity;
|
|
5
|
+
exports.getClaimedRewardQuantity = getClaimedRewardQuantity;
|
|
6
|
+
exports.getRemainingRewardQuantity = getRemainingRewardQuantity;
|
|
5
7
|
const constants_1 = require("../../constants");
|
|
6
8
|
const defaultIsSoldOut = (item) => item?.sold_out === true;
|
|
7
9
|
/**
|
|
@@ -49,3 +51,42 @@ function getRewardSelectionQuantity({ offer, stats, isVoucherLinked = false, })
|
|
|
49
51
|
}
|
|
50
52
|
return Math.max(1, rewardNumOfItems ?? 1);
|
|
51
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Units of `offerId`'s item reward already claimed on this cart.
|
|
56
|
+
*
|
|
57
|
+
* A claimed unit is a cart line stamped with the offer id in
|
|
58
|
+
* `item_discount_details` - `loyalty_discount_id`, or `voucher_discount_id`
|
|
59
|
+
* for a voucher-pathway claim. Pure: callers pass the categories in.
|
|
60
|
+
*/
|
|
61
|
+
function getClaimedRewardQuantity({ categories, offerId }) {
|
|
62
|
+
let totalQuantity = 0;
|
|
63
|
+
for (let c = 0; c < (categories?.length ?? 0); c++) {
|
|
64
|
+
const items = categories?.[c]?.items ?? [];
|
|
65
|
+
for (let i = 0; i < items.length; i++) {
|
|
66
|
+
const discountDetails = items[i].item_discount_details;
|
|
67
|
+
if (discountDetails == null)
|
|
68
|
+
continue;
|
|
69
|
+
if (discountDetails.loyalty_discount_id !== offerId && discountDetails.voucher_discount_id !== offerId) {
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
const quantity = Number(items[i].quantity ?? 0);
|
|
73
|
+
if (Number.isFinite(quantity) && quantity > 0) {
|
|
74
|
+
totalQuantity += quantity;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return totalQuantity;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Units still claimable for `offerId` on this cart.
|
|
82
|
+
*
|
|
83
|
+
* `remaining = allowance - already claimed`, floored at 0. Passing one claim's
|
|
84
|
+
* `num_of_items` as the allowance is what capped repeatable rewards at a single
|
|
85
|
+
* claim; the caller must pass the order's total (the offer stats'
|
|
86
|
+
* `applicable_quantity`), or `num_of_items` only when the claim is one claim's
|
|
87
|
+
* worth (a voucher code).
|
|
88
|
+
*/
|
|
89
|
+
function getRemainingRewardQuantity({ categories, offerId, allowance }) {
|
|
90
|
+
const claimed = getClaimedRewardQuantity({ categories, offerId });
|
|
91
|
+
return Math.max(0, allowance - claimed);
|
|
92
|
+
}
|
|
@@ -97,3 +97,63 @@ describe('getRewardSelectionQuantity', () => {
|
|
|
97
97
|
expect((0, offer_item_resolution_1.getRewardSelectionQuantity)({ offer: noItems, stats: undefined })).toBe(1);
|
|
98
98
|
});
|
|
99
99
|
});
|
|
100
|
+
describe('getClaimedRewardQuantity / getRemainingRewardQuantity', () => {
|
|
101
|
+
const categories = (items) => [
|
|
102
|
+
{ category_id: 'c1', items: items.slice(0, 2) },
|
|
103
|
+
{ category_id: 'c2', items: items.slice(2) },
|
|
104
|
+
];
|
|
105
|
+
const stamped = (quantity, ids = {}) => ({
|
|
106
|
+
quantity,
|
|
107
|
+
item_discount_details: ids.loyalty == null && ids.voucher == null
|
|
108
|
+
? null
|
|
109
|
+
: {
|
|
110
|
+
loyalty_discount_id: ids.loyalty ?? null,
|
|
111
|
+
voucher_discount_id: ids.voucher ?? null,
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
it('returns 0 for empty or missing categories', () => {
|
|
115
|
+
expect((0, offer_item_resolution_1.getClaimedRewardQuantity)({ categories: [], offerId: 'a' })).toBe(0);
|
|
116
|
+
expect((0, offer_item_resolution_1.getClaimedRewardQuantity)({ categories: undefined, offerId: 'a' })).toBe(0);
|
|
117
|
+
expect((0, offer_item_resolution_1.getClaimedRewardQuantity)({ categories: null, offerId: 'a' })).toBe(0);
|
|
118
|
+
});
|
|
119
|
+
it('returns 0 when no line is stamped with the offer id', () => {
|
|
120
|
+
const result = (0, offer_item_resolution_1.getClaimedRewardQuantity)({
|
|
121
|
+
categories: categories([
|
|
122
|
+
stamped(2, { loyalty: 'other' }),
|
|
123
|
+
stamped(3),
|
|
124
|
+
stamped(4, { voucher: 'another' }),
|
|
125
|
+
stamped(5, { loyalty: 'a' }),
|
|
126
|
+
]),
|
|
127
|
+
offerId: 'a',
|
|
128
|
+
});
|
|
129
|
+
expect(result).toBe(5);
|
|
130
|
+
});
|
|
131
|
+
it('sums loyalty and voucher stamps across categories', () => {
|
|
132
|
+
const result = (0, offer_item_resolution_1.getClaimedRewardQuantity)({
|
|
133
|
+
categories: categories([
|
|
134
|
+
stamped(2, { loyalty: 'a' }),
|
|
135
|
+
stamped(1, { voucher: 'a' }),
|
|
136
|
+
stamped(3, { loyalty: 'a' }),
|
|
137
|
+
stamped(9, { loyalty: 'b' }),
|
|
138
|
+
]),
|
|
139
|
+
offerId: 'a',
|
|
140
|
+
});
|
|
141
|
+
expect(result).toBe(6);
|
|
142
|
+
});
|
|
143
|
+
it('subtracts claimed units from the order allowance and never goes negative', () => {
|
|
144
|
+
const cart = categories([
|
|
145
|
+
stamped(2, { loyalty: 'a' }),
|
|
146
|
+
stamped(1, { voucher: 'a' }),
|
|
147
|
+
]);
|
|
148
|
+
expect((0, offer_item_resolution_1.getRemainingRewardQuantity)({ categories: cart, offerId: 'a', allowance: 6 })).toBe(3);
|
|
149
|
+
expect((0, offer_item_resolution_1.getRemainingRewardQuantity)({ categories: cart, offerId: 'a', allowance: 3 })).toBe(0);
|
|
150
|
+
expect((0, offer_item_resolution_1.getRemainingRewardQuantity)({ categories: cart, offerId: 'a', allowance: 1 })).toBe(0);
|
|
151
|
+
});
|
|
152
|
+
it('returns the full allowance for a cart with no claim of this offer', () => {
|
|
153
|
+
expect((0, offer_item_resolution_1.getRemainingRewardQuantity)({
|
|
154
|
+
categories: categories([stamped(2, { loyalty: 'other' })]),
|
|
155
|
+
offerId: 'a',
|
|
156
|
+
allowance: 6,
|
|
157
|
+
})).toBe(6);
|
|
158
|
+
});
|
|
159
|
+
});
|
package/package.json
CHANGED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import { Schema } from 'mongoose';
|
|
2
|
-
import { ALL_DISCOUNT_APPLICABILITY_TYPES, ALL_DISCOUNT_TYPES, ALL_PLATFORMS } from '../../constants';
|
|
3
|
-
export const AppliedOrderDiscountsSchema = new Schema({
|
|
4
|
-
_id: {
|
|
5
|
-
type: Schema.Types.ObjectId,
|
|
6
|
-
required: true,
|
|
7
|
-
immutable: true,
|
|
8
|
-
},
|
|
9
|
-
tenant_id: {
|
|
10
|
-
type: String,
|
|
11
|
-
required: true,
|
|
12
|
-
immutable: true,
|
|
13
|
-
ref: 'tenants',
|
|
14
|
-
},
|
|
15
|
-
order_id: {
|
|
16
|
-
type: Schema.Types.ObjectId,
|
|
17
|
-
required: true,
|
|
18
|
-
immutable: true,
|
|
19
|
-
},
|
|
20
|
-
discount_id: {
|
|
21
|
-
type: String,
|
|
22
|
-
required: true,
|
|
23
|
-
},
|
|
24
|
-
type: {
|
|
25
|
-
type: String,
|
|
26
|
-
required: true,
|
|
27
|
-
enum: ALL_DISCOUNT_TYPES,
|
|
28
|
-
},
|
|
29
|
-
applicability: {
|
|
30
|
-
type: String,
|
|
31
|
-
required: true,
|
|
32
|
-
enum: ALL_DISCOUNT_APPLICABILITY_TYPES,
|
|
33
|
-
},
|
|
34
|
-
platform: {
|
|
35
|
-
type: String,
|
|
36
|
-
enum: ALL_PLATFORMS,
|
|
37
|
-
required: true,
|
|
38
|
-
},
|
|
39
|
-
name: {
|
|
40
|
-
type: String,
|
|
41
|
-
required: true,
|
|
42
|
-
},
|
|
43
|
-
order_amount: {
|
|
44
|
-
type: Number,
|
|
45
|
-
required: true,
|
|
46
|
-
min: 0,
|
|
47
|
-
},
|
|
48
|
-
discount_amount: {
|
|
49
|
-
type: Number,
|
|
50
|
-
required: true,
|
|
51
|
-
min: 0,
|
|
52
|
-
},
|
|
53
|
-
discount_rate: {
|
|
54
|
-
type: Number,
|
|
55
|
-
required: false,
|
|
56
|
-
default: null,
|
|
57
|
-
},
|
|
58
|
-
reason: {
|
|
59
|
-
type: String,
|
|
60
|
-
required: true,
|
|
61
|
-
},
|
|
62
|
-
reference_number: {
|
|
63
|
-
type: String,
|
|
64
|
-
required: false,
|
|
65
|
-
},
|
|
66
|
-
staff_id: {
|
|
67
|
-
type: String,
|
|
68
|
-
required: true,
|
|
69
|
-
},
|
|
70
|
-
staff_phone: {
|
|
71
|
-
type: String,
|
|
72
|
-
required: true,
|
|
73
|
-
},
|
|
74
|
-
staff_name: {
|
|
75
|
-
type: String,
|
|
76
|
-
required: true,
|
|
77
|
-
},
|
|
78
|
-
createdAt_mins: {
|
|
79
|
-
type: Number,
|
|
80
|
-
required: true,
|
|
81
|
-
immutable: true,
|
|
82
|
-
default: () => {
|
|
83
|
-
const date = new Date();
|
|
84
|
-
return Math.floor(date.getHours() * 60 + date.getMinutes());
|
|
85
|
-
},
|
|
86
|
-
},
|
|
87
|
-
createdAt: {
|
|
88
|
-
type: Schema.Types.Date,
|
|
89
|
-
required: true,
|
|
90
|
-
default: () => new Date(),
|
|
91
|
-
immutable: true,
|
|
92
|
-
},
|
|
93
|
-
updatedAt: {
|
|
94
|
-
type: Schema.Types.Date,
|
|
95
|
-
required: true,
|
|
96
|
-
default: () => new Date(),
|
|
97
|
-
},
|
|
98
|
-
}, {
|
|
99
|
-
timestamps: true,
|
|
100
|
-
versionKey: false,
|
|
101
|
-
minimize: false,
|
|
102
|
-
});
|
|
103
|
-
export const APPLIED_ORDER_DISCOUNTS_COLLECTION_NAME = 'applied_order_discounts';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { IOrderPayment } from '../../schemas/types/orders';
|
|
2
|
-
/**
|
|
3
|
-
* Server-owned `collected_at` stamping for order payments.
|
|
4
|
-
*
|
|
5
|
-
* A payment line is the unit a credit bill lives on: the sale is issued on the
|
|
6
|
-
* punch day, and money may arrive later (full or partial). `order_settled_at`
|
|
7
|
-
* only moves on FULL settlement, so it cannot anchor a partial collection.
|
|
8
|
-
* This helper records the LAST collection moment on the payment itself,
|
|
9
|
-
* derived by comparing `amount_paid` with the stored previous version.
|
|
10
|
-
*
|
|
11
|
-
* Rules:
|
|
12
|
-
* - never trust the incoming `collected_at` (clients may send anything);
|
|
13
|
-
* - a NEW payment line with money in (`amount_paid > 0`) gets `now`;
|
|
14
|
-
* - an EXISTING line whose `amount_paid` increased gets `now`;
|
|
15
|
-
* - unchanged / decreased / zero-amount lines keep the stored timestamp
|
|
16
|
-
* (`null` when unknown — legacy rows are not backfilled here).
|
|
17
|
-
*
|
|
18
|
-
* Pure and never throws. `previousPayments` must be the SERVER copy of the
|
|
19
|
-
* order's payments before the write (pass none on create).
|
|
20
|
-
*/
|
|
21
|
-
export interface IStampPaymentCollectionTimestampsOptions {
|
|
22
|
-
/** Override for the collection instant (tests / offline replays). */
|
|
23
|
-
now?: Date | string | null;
|
|
24
|
-
}
|
|
25
|
-
export declare function stampPaymentCollectionTimestamps(payments: IOrderPayment[] | null | undefined, previousPayments?: IOrderPayment[] | null | undefined, options?: IStampPaymentCollectionTimestampsOptions): IOrderPayment[];
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.stampPaymentCollectionTimestamps = stampPaymentCollectionTimestamps;
|
|
4
|
-
const constants_1 = require("../../constants");
|
|
5
|
-
const toValidDate = (value) => {
|
|
6
|
-
if (value == null)
|
|
7
|
-
return null;
|
|
8
|
-
const date = value instanceof Date ? value : new Date(value);
|
|
9
|
-
return Number.isNaN(date.getTime()) ? null : date;
|
|
10
|
-
};
|
|
11
|
-
/** Stable identity across a merge: subdoc `_id` first, then `payment_id`. */
|
|
12
|
-
const identityOf = (payment) => {
|
|
13
|
-
if (payment._id != null && String(payment._id).trim() !== '') {
|
|
14
|
-
return `id:${String(payment._id)}`;
|
|
15
|
-
}
|
|
16
|
-
if (typeof payment.payment_id === 'string' && payment.payment_id.trim() !== '') {
|
|
17
|
-
return `payment:${payment.payment_id}`;
|
|
18
|
-
}
|
|
19
|
-
return null;
|
|
20
|
-
};
|
|
21
|
-
function stampPaymentCollectionTimestamps(payments, previousPayments = null, options = {}) {
|
|
22
|
-
if (!Array.isArray(payments))
|
|
23
|
-
return [];
|
|
24
|
-
const now = toValidDate(options.now) ?? new Date();
|
|
25
|
-
const previousByIdentity = new Map();
|
|
26
|
-
for (const previous of Array.isArray(previousPayments) ? previousPayments : []) {
|
|
27
|
-
if (previous == null)
|
|
28
|
-
continue;
|
|
29
|
-
const identity = identityOf(previous);
|
|
30
|
-
if (identity != null)
|
|
31
|
-
previousByIdentity.set(identity, previous);
|
|
32
|
-
}
|
|
33
|
-
return payments.map((payment) => {
|
|
34
|
-
if (payment == null)
|
|
35
|
-
return payment;
|
|
36
|
-
const identity = identityOf(payment);
|
|
37
|
-
const previous = identity != null ? previousByIdentity.get(identity) : undefined;
|
|
38
|
-
const previousCollectedAt = toValidDate(previous?.collected_at);
|
|
39
|
-
const amountPaid = Number(payment.amount_paid ?? 0);
|
|
40
|
-
const previousAmountPaid = Number(previous?.amount_paid ?? 0);
|
|
41
|
-
const isFailed = payment.payment_status === constants_1.PAYMENT_STATUS_ENUM.FAILED;
|
|
42
|
-
const isNewCollection = !isFailed &&
|
|
43
|
-
Number.isFinite(amountPaid) &&
|
|
44
|
-
amountPaid > 0 &&
|
|
45
|
-
(previous == null || amountPaid > previousAmountPaid);
|
|
46
|
-
return {
|
|
47
|
-
...payment,
|
|
48
|
-
collected_at: isNewCollection ? now : previousCollectedAt,
|
|
49
|
-
};
|
|
50
|
-
});
|
|
51
|
-
}
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { Schema } from 'mongoose';
|
|
2
|
-
export declare const AppliedOrderDiscountsSchema: Schema<any, import("mongoose").Model<any, any, any, any, any, any>, {}, {}, {}, {}, {
|
|
3
|
-
timestamps: true;
|
|
4
|
-
versionKey: false;
|
|
5
|
-
minimize: false;
|
|
6
|
-
}, {
|
|
7
|
-
createdAt: NativeDate;
|
|
8
|
-
updatedAt: NativeDate;
|
|
9
|
-
} & {
|
|
10
|
-
_id: import("mongoose").Types.ObjectId;
|
|
11
|
-
type: import("../../constants").DISCOUNT_TYPE_ENUM;
|
|
12
|
-
name: string;
|
|
13
|
-
tenant_id: string;
|
|
14
|
-
order_id: import("mongoose").Types.ObjectId;
|
|
15
|
-
discount_id: string;
|
|
16
|
-
applicability: import("../../constants").DISCOUNT_APPLICABILITY_ENUM;
|
|
17
|
-
platform: import("../../constants").PLATFORMS_ENUM;
|
|
18
|
-
order_amount: number;
|
|
19
|
-
discount_amount: number;
|
|
20
|
-
reason: string;
|
|
21
|
-
staff_id: string;
|
|
22
|
-
staff_phone: string;
|
|
23
|
-
staff_name: string;
|
|
24
|
-
createdAt_mins: number;
|
|
25
|
-
createdAt: NativeDate;
|
|
26
|
-
updatedAt: NativeDate;
|
|
27
|
-
discount_rate?: number | null | undefined;
|
|
28
|
-
reference_number?: string | null | undefined;
|
|
29
|
-
}, import("mongoose").Document<unknown, {}, import("mongoose").FlatRecord<{
|
|
30
|
-
createdAt: NativeDate;
|
|
31
|
-
updatedAt: NativeDate;
|
|
32
|
-
} & {
|
|
33
|
-
_id: import("mongoose").Types.ObjectId;
|
|
34
|
-
type: import("../../constants").DISCOUNT_TYPE_ENUM;
|
|
35
|
-
name: string;
|
|
36
|
-
tenant_id: string;
|
|
37
|
-
order_id: import("mongoose").Types.ObjectId;
|
|
38
|
-
discount_id: string;
|
|
39
|
-
applicability: import("../../constants").DISCOUNT_APPLICABILITY_ENUM;
|
|
40
|
-
platform: import("../../constants").PLATFORMS_ENUM;
|
|
41
|
-
order_amount: number;
|
|
42
|
-
discount_amount: number;
|
|
43
|
-
reason: string;
|
|
44
|
-
staff_id: string;
|
|
45
|
-
staff_phone: string;
|
|
46
|
-
staff_name: string;
|
|
47
|
-
createdAt_mins: number;
|
|
48
|
-
createdAt: NativeDate;
|
|
49
|
-
updatedAt: NativeDate;
|
|
50
|
-
discount_rate?: number | null | undefined;
|
|
51
|
-
reference_number?: string | null | undefined;
|
|
52
|
-
}>> & import("mongoose").FlatRecord<{
|
|
53
|
-
createdAt: NativeDate;
|
|
54
|
-
updatedAt: NativeDate;
|
|
55
|
-
} & {
|
|
56
|
-
_id: import("mongoose").Types.ObjectId;
|
|
57
|
-
type: import("../../constants").DISCOUNT_TYPE_ENUM;
|
|
58
|
-
name: string;
|
|
59
|
-
tenant_id: string;
|
|
60
|
-
order_id: import("mongoose").Types.ObjectId;
|
|
61
|
-
discount_id: string;
|
|
62
|
-
applicability: import("../../constants").DISCOUNT_APPLICABILITY_ENUM;
|
|
63
|
-
platform: import("../../constants").PLATFORMS_ENUM;
|
|
64
|
-
order_amount: number;
|
|
65
|
-
discount_amount: number;
|
|
66
|
-
reason: string;
|
|
67
|
-
staff_id: string;
|
|
68
|
-
staff_phone: string;
|
|
69
|
-
staff_name: string;
|
|
70
|
-
createdAt_mins: number;
|
|
71
|
-
createdAt: NativeDate;
|
|
72
|
-
updatedAt: NativeDate;
|
|
73
|
-
discount_rate?: number | null | undefined;
|
|
74
|
-
reference_number?: string | null | undefined;
|
|
75
|
-
}> & Required<{
|
|
76
|
-
_id: import("mongoose").Types.ObjectId;
|
|
77
|
-
}> & {
|
|
78
|
-
__v: number;
|
|
79
|
-
}>;
|
|
80
|
-
export declare const APPLIED_ORDER_DISCOUNTS_COLLECTION_NAME = "applied_order_discounts";
|
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.APPLIED_ORDER_DISCOUNTS_COLLECTION_NAME = exports.AppliedOrderDiscountsSchema = void 0;
|
|
4
|
-
const mongoose_1 = require("mongoose");
|
|
5
|
-
const constants_1 = require("../../constants");
|
|
6
|
-
exports.AppliedOrderDiscountsSchema = new mongoose_1.Schema({
|
|
7
|
-
_id: {
|
|
8
|
-
type: mongoose_1.Schema.Types.ObjectId,
|
|
9
|
-
required: true,
|
|
10
|
-
immutable: true,
|
|
11
|
-
},
|
|
12
|
-
tenant_id: {
|
|
13
|
-
type: String,
|
|
14
|
-
required: true,
|
|
15
|
-
immutable: true,
|
|
16
|
-
ref: 'tenants',
|
|
17
|
-
},
|
|
18
|
-
order_id: {
|
|
19
|
-
type: mongoose_1.Schema.Types.ObjectId,
|
|
20
|
-
required: true,
|
|
21
|
-
immutable: true,
|
|
22
|
-
},
|
|
23
|
-
discount_id: {
|
|
24
|
-
type: String,
|
|
25
|
-
required: true,
|
|
26
|
-
},
|
|
27
|
-
type: {
|
|
28
|
-
type: String,
|
|
29
|
-
required: true,
|
|
30
|
-
enum: constants_1.ALL_DISCOUNT_TYPES,
|
|
31
|
-
},
|
|
32
|
-
applicability: {
|
|
33
|
-
type: String,
|
|
34
|
-
required: true,
|
|
35
|
-
enum: constants_1.ALL_DISCOUNT_APPLICABILITY_TYPES,
|
|
36
|
-
},
|
|
37
|
-
platform: {
|
|
38
|
-
type: String,
|
|
39
|
-
enum: constants_1.ALL_PLATFORMS,
|
|
40
|
-
required: true,
|
|
41
|
-
},
|
|
42
|
-
name: {
|
|
43
|
-
type: String,
|
|
44
|
-
required: true,
|
|
45
|
-
},
|
|
46
|
-
order_amount: {
|
|
47
|
-
type: Number,
|
|
48
|
-
required: true,
|
|
49
|
-
min: 0,
|
|
50
|
-
},
|
|
51
|
-
discount_amount: {
|
|
52
|
-
type: Number,
|
|
53
|
-
required: true,
|
|
54
|
-
min: 0,
|
|
55
|
-
},
|
|
56
|
-
discount_rate: {
|
|
57
|
-
type: Number,
|
|
58
|
-
required: false,
|
|
59
|
-
default: null,
|
|
60
|
-
},
|
|
61
|
-
reason: {
|
|
62
|
-
type: String,
|
|
63
|
-
required: true,
|
|
64
|
-
},
|
|
65
|
-
reference_number: {
|
|
66
|
-
type: String,
|
|
67
|
-
required: false,
|
|
68
|
-
},
|
|
69
|
-
staff_id: {
|
|
70
|
-
type: String,
|
|
71
|
-
required: true,
|
|
72
|
-
},
|
|
73
|
-
staff_phone: {
|
|
74
|
-
type: String,
|
|
75
|
-
required: true,
|
|
76
|
-
},
|
|
77
|
-
staff_name: {
|
|
78
|
-
type: String,
|
|
79
|
-
required: true,
|
|
80
|
-
},
|
|
81
|
-
createdAt_mins: {
|
|
82
|
-
type: Number,
|
|
83
|
-
required: true,
|
|
84
|
-
immutable: true,
|
|
85
|
-
default: () => {
|
|
86
|
-
const date = new Date();
|
|
87
|
-
return Math.floor(date.getHours() * 60 + date.getMinutes());
|
|
88
|
-
},
|
|
89
|
-
},
|
|
90
|
-
createdAt: {
|
|
91
|
-
type: mongoose_1.Schema.Types.Date,
|
|
92
|
-
required: true,
|
|
93
|
-
default: () => new Date(),
|
|
94
|
-
immutable: true,
|
|
95
|
-
},
|
|
96
|
-
updatedAt: {
|
|
97
|
-
type: mongoose_1.Schema.Types.Date,
|
|
98
|
-
required: true,
|
|
99
|
-
default: () => new Date(),
|
|
100
|
-
},
|
|
101
|
-
}, {
|
|
102
|
-
timestamps: true,
|
|
103
|
-
versionKey: false,
|
|
104
|
-
minimize: false,
|
|
105
|
-
});
|
|
106
|
-
exports.APPLIED_ORDER_DISCOUNTS_COLLECTION_NAME = 'applied_order_discounts';
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { DISCOUNT_APPLICABILITY_ENUM, DISCOUNT_TYPE_ENUM, PLATFORMS_ENUM } from '../../constants';
|
|
2
|
-
export interface IAppliedOrderDiscount {
|
|
3
|
-
_id: string;
|
|
4
|
-
tenant_id: string;
|
|
5
|
-
order_id: string;
|
|
6
|
-
discount_id: string;
|
|
7
|
-
type: DISCOUNT_TYPE_ENUM;
|
|
8
|
-
applicability: DISCOUNT_APPLICABILITY_ENUM;
|
|
9
|
-
platform: PLATFORMS_ENUM;
|
|
10
|
-
name: string;
|
|
11
|
-
order_amount: number;
|
|
12
|
-
discount_amount: number;
|
|
13
|
-
discount_rate: number | null;
|
|
14
|
-
reason: string;
|
|
15
|
-
reference_number: string | null;
|
|
16
|
-
staff_id: string;
|
|
17
|
-
staff_phone: string;
|
|
18
|
-
staff_name: string;
|
|
19
|
-
createdAt_mins: number;
|
|
20
|
-
createdAt: Date;
|
|
21
|
-
updatedAt: Date;
|
|
22
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const constants_1 = require("../constants");
|
|
4
|
-
const payment_collection_1 = require("../helpers/order/payment-collection");
|
|
5
|
-
const NOW = new Date('2026-10-20T14:00:00.000Z');
|
|
6
|
-
const EARLIER = new Date('2026-10-19T10:00:00.000Z');
|
|
7
|
-
const payment = (over = {}) => ({
|
|
8
|
-
_id: '5f0000000000000000000001',
|
|
9
|
-
payment_type: constants_1.PAYMENT_TYPE_ENUM.CASH,
|
|
10
|
-
payment_status: constants_1.PAYMENT_STATUS_ENUM.SETTLED,
|
|
11
|
-
card_details: null,
|
|
12
|
-
amount_paid: 1000,
|
|
13
|
-
amount_due: 1000,
|
|
14
|
-
amount_refunded: 0,
|
|
15
|
-
payment_id: 'pay-1',
|
|
16
|
-
...over,
|
|
17
|
-
});
|
|
18
|
-
describe('stampPaymentCollectionTimestamps', () => {
|
|
19
|
-
it('stamps new collected money and leaves unpaid PAY_LATER empty', () => {
|
|
20
|
-
const [paid, credit] = (0, payment_collection_1.stampPaymentCollectionTimestamps)([
|
|
21
|
-
payment(),
|
|
22
|
-
payment({
|
|
23
|
-
_id: '5f0000000000000000000002',
|
|
24
|
-
payment_id: 'pay-2',
|
|
25
|
-
payment_type: constants_1.PAYMENT_TYPE_ENUM.PAY_LATER,
|
|
26
|
-
payment_status: constants_1.PAYMENT_STATUS_ENUM.PENDING,
|
|
27
|
-
amount_paid: 0,
|
|
28
|
-
amount_due: 5000,
|
|
29
|
-
}),
|
|
30
|
-
], null, { now: NOW });
|
|
31
|
-
expect(paid.collected_at).toBe(NOW);
|
|
32
|
-
expect(credit.collected_at).toBeNull();
|
|
33
|
-
});
|
|
34
|
-
it('ignores a client-supplied collected_at on create', () => {
|
|
35
|
-
const [paid] = (0, payment_collection_1.stampPaymentCollectionTimestamps)([payment({ collected_at: EARLIER })], null, { now: NOW });
|
|
36
|
-
expect(paid.collected_at).toBe(NOW);
|
|
37
|
-
});
|
|
38
|
-
it('preserves the stored timestamp when amount_paid did not increase', () => {
|
|
39
|
-
const previous = payment({ collected_at: EARLIER });
|
|
40
|
-
const [unchanged] = (0, payment_collection_1.stampPaymentCollectionTimestamps)([payment({ collected_at: null })], [previous], { now: NOW });
|
|
41
|
-
expect(unchanged.collected_at).toBe(EARLIER);
|
|
42
|
-
});
|
|
43
|
-
it('stamps the latest collection when a partial amount is collected', () => {
|
|
44
|
-
const previous = payment({ amount_paid: 400, collected_at: EARLIER });
|
|
45
|
-
const [partial] = (0, payment_collection_1.stampPaymentCollectionTimestamps)([payment({ amount_paid: 700 })], [previous], { now: NOW });
|
|
46
|
-
expect(partial.collected_at).toBe(NOW);
|
|
47
|
-
});
|
|
48
|
-
it('does not rewrite the timestamp on a refund / amount decrease', () => {
|
|
49
|
-
const previous = payment({ amount_paid: 1000, collected_at: EARLIER });
|
|
50
|
-
const [refunded] = (0, payment_collection_1.stampPaymentCollectionTimestamps)([payment({ amount_paid: 0, amount_refunded: 1000, payment_status: constants_1.PAYMENT_STATUS_ENUM.REFUNDED })], [previous], { now: NOW });
|
|
51
|
-
expect(refunded.collected_at).toBe(EARLIER);
|
|
52
|
-
});
|
|
53
|
-
it('matches legacy rows by _id when payment_id is null', () => {
|
|
54
|
-
const previous = payment({ payment_id: null, collected_at: EARLIER });
|
|
55
|
-
const [updated] = (0, payment_collection_1.stampPaymentCollectionTimestamps)([payment({ payment_id: null, amount_paid: 1200 })], [previous], { now: NOW });
|
|
56
|
-
expect(updated.collected_at).toBe(NOW);
|
|
57
|
-
});
|
|
58
|
-
it('preserves the stamp when the server reassigns payment_id but _id is stable', () => {
|
|
59
|
-
const previous = payment({ payment_id: 'pay-old', collected_at: EARLIER });
|
|
60
|
-
const [updated] = (0, payment_collection_1.stampPaymentCollectionTimestamps)([payment({ payment_id: 'pay-new' })], [previous], { now: NOW });
|
|
61
|
-
expect(updated.collected_at).toBe(EARLIER);
|
|
62
|
-
});
|
|
63
|
-
it('does not stamp a failed payment', () => {
|
|
64
|
-
const [failed] = (0, payment_collection_1.stampPaymentCollectionTimestamps)([payment({ payment_status: constants_1.PAYMENT_STATUS_ENUM.FAILED })], null, { now: NOW });
|
|
65
|
-
expect(failed.collected_at).toBeNull();
|
|
66
|
-
});
|
|
67
|
-
it('returns an empty array for empty / invalid input', () => {
|
|
68
|
-
expect((0, payment_collection_1.stampPaymentCollectionTimestamps)(null)).toEqual([]);
|
|
69
|
-
expect((0, payment_collection_1.stampPaymentCollectionTimestamps)(undefined)).toEqual([]);
|
|
70
|
-
});
|
|
71
|
-
});
|