@managesome/eposmatic-toolkit 0.4.19 → 0.4.20
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.
|
@@ -0,0 +1,25 @@
|
|
|
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[];
|
|
@@ -0,0 +1,51 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,71 @@
|
|
|
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
|
+
});
|