@classytic/pos 0.6.0 → 0.8.0
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/CHANGELOG.md +10 -350
- package/dist/commands/index.d.mts +2 -0
- package/dist/commands/index.mjs +1 -0
- package/dist/commands/pos-commands.d.mts +230 -0
- package/dist/commands/pos-commands.mjs +357 -0
- package/dist/domain/enums/cash-movement-reason.enum.d.mts +3 -4
- package/dist/domain/enums/closed-by.enum.d.mts +3 -4
- package/dist/domain/enums/payment-method.enum.d.mts +6 -7
- package/dist/domain/enums/payment-method.enum.mjs +2 -2
- package/dist/domain/enums/shift-state.enum.d.mts +7 -8
- package/dist/domain/errors/pos.errors.d.mts +82 -34
- package/dist/domain/errors/pos.errors.mjs +108 -23
- package/dist/domain/index.d.mts +11 -3
- package/dist/domain/index.mjs +10 -3
- package/dist/domain/ports/common.d.mts +2 -3
- package/dist/domain/ports/ledger.bridge.d.mts +6 -7
- package/dist/domain/ports/notification.bridge.d.mts +5 -6
- package/dist/domain/ports/policy.bridge.d.mts +2 -3
- package/dist/domain/sale/discount.d.mts +62 -0
- package/dist/domain/sale/discount.mjs +58 -0
- package/dist/domain/sale/price-override.d.mts +51 -0
- package/dist/domain/sale/price-override.mjs +51 -0
- package/dist/domain/sale/quick-cash.d.mts +10 -0
- package/dist/domain/sale/quick-cash.mjs +27 -0
- package/dist/domain/sale/service-line.d.mts +7 -0
- package/dist/domain/sale/service-line.mjs +10 -0
- package/dist/domain/sale/tender.d.mts +256 -0
- package/dist/domain/sale/tender.mjs +242 -0
- package/dist/domain/shift/shift-decisions.d.mts +51 -0
- package/dist/domain/shift/shift-decisions.mjs +93 -0
- package/dist/domain/shift/shift-report.d.mts +127 -0
- package/dist/domain/shift/shift-report.mjs +95 -0
- package/dist/domain/shift/shift.types.d.mts +107 -0
- package/dist/domain/state-machines/shift.fsm.d.mts +6 -7
- package/dist/domain/value-objects/cash-movement.vo.d.mts +7 -3
- package/dist/domain/value-objects/payment-breakdown.vo.d.mts +4 -5
- package/dist/domain/value-objects/shift-policy.vo.d.mts +12 -13
- package/dist/domain/value-objects/shift-policy.vo.mjs +13 -7
- package/dist/engine/capability.errors.d.mts +23 -0
- package/dist/engine/capability.errors.mjs +28 -0
- package/dist/engine/define-pos.d.mts +2 -3
- package/dist/engine/define-pos.mjs +14 -3
- package/dist/engine/engine-types.d.mts +24 -9
- package/dist/engine/index.d.mts +2 -1
- package/dist/engine/index.mjs +2 -1
- package/dist/events/event-catalog.d.mts +18 -14
- package/dist/events/event-catalog.mjs +10 -4
- package/dist/events/event-constants.d.mts +3 -4
- package/dist/events/event-payloads.d.mts +9 -10
- package/dist/events/helpers.d.mts +6 -3
- package/dist/events/helpers.mjs +12 -2
- package/dist/events/in-process-bus.d.mts +3 -4
- package/dist/events/index.mjs +1 -1
- package/dist/grant/till-grant.d.mts +50 -0
- package/dist/grant/till-grant.mjs +47 -0
- package/dist/index.d.mts +15 -4
- package/dist/index.mjs +13 -4
- package/dist/models/create-models.d.mts +10 -6
- package/dist/models/create-models.mjs +16 -3
- package/dist/models/register.model.d.mts +7 -6
- package/dist/models/shift-sale-dedupe.model.d.mts +4 -5
- package/dist/models/shift.model.d.mts +4 -111
- package/dist/models/shift.model.mjs +27 -3
- package/dist/models/till-grant.model.d.mts +19 -0
- package/dist/models/till-grant.model.mjs +52 -0
- package/dist/repositories/register.repository.d.mts +3 -4
- package/dist/repositories/shift.repository.d.mts +90 -60
- package/dist/repositories/shift.repository.mjs +323 -150
- package/dist/repositories/till-grant.repository.d.mts +22 -0
- package/dist/repositories/till-grant.repository.mjs +41 -0
- package/dist/schemas/index.mjs +1 -2
- package/dist/schemas/shift.d.mts +9 -10
- package/dist/testing/fixtures.d.mts +4 -5
- package/dist/testing/index.mjs +1 -2
- package/package.json +50 -19
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { buildRegisterSchema } from "./register.model.mjs";
|
|
2
2
|
import { buildShiftSaleDedupeSchema } from "./shift-sale-dedupe.model.mjs";
|
|
3
|
+
import { buildTillGrantSchema } from "./till-grant.model.mjs";
|
|
3
4
|
import { buildShiftSchema } from "./shift.model.mjs";
|
|
4
5
|
import { defineModels } from "@classytic/mongokit";
|
|
5
6
|
//#region src/models/create-models.ts
|
|
@@ -30,13 +31,15 @@ import { defineModels } from "@classytic/mongokit";
|
|
|
30
31
|
const POS_MODEL_NAMES = {
|
|
31
32
|
Shift: "Shift",
|
|
32
33
|
Register: "Register",
|
|
33
|
-
ShiftSaleDedupe: "ShiftSaleDedupe"
|
|
34
|
+
ShiftSaleDedupe: "ShiftSaleDedupe",
|
|
35
|
+
TillGrant: "TillGrant"
|
|
34
36
|
};
|
|
35
37
|
/** Single source of truth for collection names. Renaming = breaking change. */
|
|
36
38
|
const DEFAULT_COLLECTIONS = {
|
|
37
39
|
Shift: "pos_shifts",
|
|
38
40
|
Register: "pos_registers",
|
|
39
|
-
ShiftSaleDedupe: "pos_shift_sale_dedupe"
|
|
41
|
+
ShiftSaleDedupe: "pos_shift_sale_dedupe",
|
|
42
|
+
TillGrant: "pos_till_grants"
|
|
40
43
|
};
|
|
41
44
|
/**
|
|
42
45
|
* Describe the POS model set as a connection-free blueprint. Pure — builds no
|
|
@@ -74,12 +77,22 @@ function definePosModels(config) {
|
|
|
74
77
|
collection: prefix + DEFAULT_COLLECTIONS.ShiftSaleDedupe,
|
|
75
78
|
schema: () => buildShiftSaleDedupeSchema({ autoIndex }),
|
|
76
79
|
existing
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
name: POS_MODEL_NAMES.TillGrant,
|
|
83
|
+
collection: prefix + DEFAULT_COLLECTIONS.TillGrant,
|
|
84
|
+
schema: () => buildTillGrantSchema({
|
|
85
|
+
tenant,
|
|
86
|
+
autoIndex
|
|
87
|
+
}),
|
|
88
|
+
existing
|
|
77
89
|
}
|
|
78
90
|
],
|
|
79
91
|
assemble: (models) => Object.freeze({
|
|
80
92
|
Shift: models.get("Shift"),
|
|
81
93
|
Register: models.get("Register"),
|
|
82
|
-
ShiftSaleDedupe: models.get("ShiftSaleDedupe")
|
|
94
|
+
ShiftSaleDedupe: models.get("ShiftSaleDedupe"),
|
|
95
|
+
TillGrant: models.get("TillGrant")
|
|
83
96
|
})
|
|
84
97
|
});
|
|
85
98
|
}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import "./inject-tenant.mjs";
|
|
2
|
-
import { HydratedDocument, Model, Schema } from "mongoose";
|
|
2
|
+
import { HydratedDocument, Model, Schema, Types } from "mongoose";
|
|
3
3
|
//#region src/models/register.model.d.ts
|
|
4
|
-
interface IRegister {
|
|
4
|
+
export interface IRegister {
|
|
5
|
+
/** The document id — every stored register has one, so a create or read returns it. */
|
|
6
|
+
_id: Types.ObjectId;
|
|
5
7
|
/** Tenant scope (organizationId / branchId). Type set by tenant config. */
|
|
6
8
|
organizationId: unknown;
|
|
7
9
|
/**
|
|
@@ -31,7 +33,6 @@ interface IRegister {
|
|
|
31
33
|
createdAt?: Date;
|
|
32
34
|
updatedAt?: Date;
|
|
33
35
|
}
|
|
34
|
-
type RegisterDocument = HydratedDocument<IRegister>;
|
|
35
|
-
type RegisterModel = Model<IRegister>;
|
|
36
|
-
//#endregion
|
|
37
|
-
export { IRegister, RegisterDocument, RegisterModel };
|
|
36
|
+
export type RegisterDocument = HydratedDocument<IRegister>;
|
|
37
|
+
export type RegisterModel = Model<IRegister>;
|
|
38
|
+
//#endregion
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { HydratedDocument, Model, Schema } from "mongoose";
|
|
2
2
|
//#region src/models/shift-sale-dedupe.model.d.ts
|
|
3
|
-
interface IShiftSaleDedupe {
|
|
3
|
+
export interface IShiftSaleDedupe {
|
|
4
4
|
/** Target shift `_id` (stringified) — one claim namespace per shift. */
|
|
5
5
|
shiftId: string;
|
|
6
6
|
/**
|
|
@@ -10,7 +10,6 @@ interface IShiftSaleDedupe {
|
|
|
10
10
|
orderRef: string;
|
|
11
11
|
createdAt: Date;
|
|
12
12
|
}
|
|
13
|
-
type ShiftSaleDedupeDocument = HydratedDocument<IShiftSaleDedupe>;
|
|
14
|
-
type ShiftSaleDedupeModel = Model<IShiftSaleDedupe>;
|
|
15
|
-
//#endregion
|
|
16
|
-
export { IShiftSaleDedupe, ShiftSaleDedupeDocument, ShiftSaleDedupeModel };
|
|
13
|
+
export type ShiftSaleDedupeDocument = HydratedDocument<IShiftSaleDedupe>;
|
|
14
|
+
export type ShiftSaleDedupeModel = Model<IShiftSaleDedupe>;
|
|
15
|
+
//#endregion
|
|
@@ -1,114 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ClosedBy } from "../domain/enums/closed-by.enum.mjs";
|
|
3
|
-
import { CashMovement } from "../domain/value-objects/cash-movement.vo.mjs";
|
|
4
|
-
import { PaymentBreakdown } from "../domain/value-objects/payment-breakdown.vo.mjs";
|
|
5
|
-
import { ShiftPolicy } from "../domain/value-objects/shift-policy.vo.mjs";
|
|
1
|
+
import { IShift } from "../domain/shift/shift.types.mjs";
|
|
6
2
|
import "./inject-tenant.mjs";
|
|
7
3
|
import { HydratedDocument, Model, Schema } from "mongoose";
|
|
8
|
-
import { ApprovalChain } from "@classytic/primitives/approval";
|
|
9
4
|
//#region src/models/shift.model.d.ts
|
|
10
|
-
|
|
11
|
-
type
|
|
12
|
-
|
|
13
|
-
* Crash-recovery progress record for the ledger post.
|
|
14
|
-
*
|
|
15
|
-
* Written in the SAME atomic CAS that moves the shift to its terminal state,
|
|
16
|
-
* BEFORE the ledger bridge is invoked, and cleared in the write that records
|
|
17
|
-
* the resulting `journalEntryId`. Its presence therefore means exactly one
|
|
18
|
-
* thing: *a close won the CAS and the journal entry is not yet recorded.*
|
|
19
|
-
*
|
|
20
|
-
* That is the state a pod eviction between the bridge call and the final
|
|
21
|
-
* write leaves behind. Retrying `close()` / `forceClose()` CONVERGES BY STATE
|
|
22
|
-
* — it re-invokes the bridge with the same deterministic `idempotencyKey`
|
|
23
|
-
* instead of posting a second entry. Without this marker the shift would look
|
|
24
|
-
* identical to "closed, no ledger wired", and the entry would be lost (or, on
|
|
25
|
-
* the old pre-CAS ordering, re-posted on every retry forever).
|
|
26
|
-
*/
|
|
27
|
-
interface PendingLedgerPost {
|
|
28
|
-
/** Deterministic — `pos:shift:<shiftId>:close`. Never regenerated. */
|
|
29
|
-
idempotencyKey: string;
|
|
30
|
-
trigger: PendingLedgerPostTrigger;
|
|
31
|
-
/**
|
|
32
|
-
* Post attempts started. Incremented by a guarded CAS on the resume path,
|
|
33
|
-
* so two concurrent resumers cannot both enter the bridge for the same
|
|
34
|
-
* attempt (the deterministic key is the second line of defence).
|
|
35
|
-
*/
|
|
36
|
-
attempts: number;
|
|
37
|
-
stampedAt: Date;
|
|
38
|
-
lastAttemptAt: Date | null;
|
|
39
|
-
/**
|
|
40
|
-
* True once the pipeline emitted the shift's lifecycle event
|
|
41
|
-
* (`pos:shift.closed` / `pos:shift.force_closed`). The resume path checks
|
|
42
|
-
* it so converging a crashed close re-emits the event the crashed run
|
|
43
|
-
* never sent, without double-emitting for a `forceClose` whose orphan
|
|
44
|
-
* already landed and announced itself.
|
|
45
|
-
*/
|
|
46
|
-
announced: boolean;
|
|
47
|
-
}
|
|
48
|
-
interface IShift {
|
|
49
|
-
/** Tenant scope (organizationId / branchId). Type set by tenant config. */
|
|
50
|
-
organizationId: unknown;
|
|
51
|
-
/**
|
|
52
|
-
* Register / terminal id within the branch. Hosts that have only one
|
|
53
|
-
* register per branch can pass a constant string. Unique-active-shift is
|
|
54
|
-
* enforced per (organizationId, registerId).
|
|
55
|
-
*/
|
|
56
|
-
registerId: string;
|
|
57
|
-
/** Business day this shift belongs to (host's local TZ; YYYY-MM-DD UTC midnight). */
|
|
58
|
-
businessDate: Date;
|
|
59
|
-
state: ShiftState;
|
|
60
|
-
openingCashierId: string;
|
|
61
|
-
openingCashierName: string;
|
|
62
|
-
endingCashierId: string | null;
|
|
63
|
-
endingCashierName: string | null;
|
|
64
|
-
closingCashierId: string | null;
|
|
65
|
-
closingCashierName: string | null;
|
|
66
|
-
teamMemberIds: string[];
|
|
67
|
-
openedAt: Date;
|
|
68
|
-
pausedAt: Date | null;
|
|
69
|
-
resumedAt: Date | null;
|
|
70
|
-
blindClosedAt: Date | null;
|
|
71
|
-
closedAt: Date | null;
|
|
72
|
-
openingCash: number;
|
|
73
|
-
expectedCash: number | null;
|
|
74
|
-
countedCash: number | null;
|
|
75
|
-
cashDifference: number | null;
|
|
76
|
-
paymentBreakdown: PaymentBreakdown[];
|
|
77
|
-
salesCount: number;
|
|
78
|
-
salesTotal: number;
|
|
79
|
-
refundCount: number;
|
|
80
|
-
refundTotal: number;
|
|
81
|
-
cashMovements: CashMovement[];
|
|
82
|
-
/**
|
|
83
|
-
* Approval chain — primitives' `ApprovalChain` (PACKAGE_RULES §P7:
|
|
84
|
-
* canonical field name is `approvals`; never build a parallel
|
|
85
|
-
* review-workflow shape). On a Shift document this carries the
|
|
86
|
-
* **variance approval** — the single-step `ApprovalChain` constructed
|
|
87
|
-
* when a manager overrides a variance during close, or any multi-step
|
|
88
|
-
* chain a host attaches via a future `approvals` input on
|
|
89
|
-
* `CloseShiftInput`. Null when no variance override / no chain attached.
|
|
90
|
-
*
|
|
91
|
-
* Renamed from `varianceApproval` (2026-05-06) to align with P7.
|
|
92
|
-
*/
|
|
93
|
-
approvals: ApprovalChain | null;
|
|
94
|
-
/** Policy frozen at open time. Reconciliation reads from here, never live. */
|
|
95
|
-
policySnapshot: ShiftPolicy;
|
|
96
|
-
closedBy: ClosedBy | null;
|
|
97
|
-
notes: string;
|
|
98
|
-
/** Set by the close pipeline after the ledger bridge returns the JE id. */
|
|
99
|
-
journalEntryId: string | null;
|
|
100
|
-
/**
|
|
101
|
-
* Non-null ⇒ a close won the CAS and the ledger post is not yet recorded.
|
|
102
|
-
* See {@link PendingLedgerPost}. Null on every shift that never had a
|
|
103
|
-
* ledger bridge wired, and on every close whose post completed.
|
|
104
|
-
*/
|
|
105
|
-
pendingLedgerPost: PendingLedgerPost | null;
|
|
106
|
-
/** Soft-delete tombstone (softDeletePlugin). Null when live. */
|
|
107
|
-
deletedAt?: Date | null;
|
|
108
|
-
createdAt?: Date;
|
|
109
|
-
updatedAt?: Date;
|
|
110
|
-
}
|
|
111
|
-
type ShiftDocument = HydratedDocument<IShift>;
|
|
112
|
-
type ShiftModel = Model<IShift>;
|
|
113
|
-
//#endregion
|
|
114
|
-
export { IShift, PendingLedgerPost, PendingLedgerPostTrigger, ShiftDocument, ShiftModel };
|
|
5
|
+
export type ShiftDocument = HydratedDocument<IShift>;
|
|
6
|
+
export type ShiftModel = Model<IShift>;
|
|
7
|
+
//#endregion
|
|
@@ -7,6 +7,27 @@ import { SHIFT_STATES } from "../domain/enums/shift-state.enum.mjs";
|
|
|
7
7
|
import { Schema } from "mongoose";
|
|
8
8
|
import { mongooseMinorUnits } from "@classytic/validation/money";
|
|
9
9
|
//#region src/models/shift.model.ts
|
|
10
|
+
/**
|
|
11
|
+
* Shift Mongoose model — register-session document.
|
|
12
|
+
*
|
|
13
|
+
* One Shift represents a continuous period during which a register accepts
|
|
14
|
+
* sales: opens with a cash float, accumulates per-method totals as orders
|
|
15
|
+
* post, closes with a count and reconciliation. The package is vertical-
|
|
16
|
+
* agnostic — retail, restaurant, clinic, service-booking, and car-wash
|
|
17
|
+
* deployments all use the same shape.
|
|
18
|
+
*
|
|
19
|
+
* Indexing strategy:
|
|
20
|
+
* - One active shift per (organizationId, registerId) is enforced via a
|
|
21
|
+
* PARTIAL UNIQUE index whose KEY is (organizationId, registerId) and whose
|
|
22
|
+
* partial filter is `state ∈ active`. `state` is intentionally NOT in the
|
|
23
|
+
* key, so two active shifts in DIFFERENT active states (open + paused)
|
|
24
|
+
* still collide. Two cashiers on different registers in the same branch
|
|
25
|
+
* can have parallel shifts.
|
|
26
|
+
* - `(organizationId, businessDate, -openedAt)` covers the dashboard
|
|
27
|
+
* "shifts today" query.
|
|
28
|
+
* - The tenant prepend (organizationId) is injected by `injectTenantField`
|
|
29
|
+
* on the existing compound indexes; we don't repeat it manually.
|
|
30
|
+
*/
|
|
10
31
|
const cashMovementSchema = new Schema({
|
|
11
32
|
type: {
|
|
12
33
|
type: String,
|
|
@@ -39,7 +60,8 @@ const cashMovementSchema = new Schema({
|
|
|
39
60
|
timestamp: {
|
|
40
61
|
type: Date,
|
|
41
62
|
default: () => /* @__PURE__ */ new Date()
|
|
42
|
-
}
|
|
63
|
+
},
|
|
64
|
+
movementKey: { type: String }
|
|
43
65
|
}, { _id: false });
|
|
44
66
|
const paymentBreakdownSchema = new Schema({
|
|
45
67
|
method: {
|
|
@@ -50,7 +72,7 @@ const paymentBreakdownSchema = new Schema({
|
|
|
50
72
|
methodKind: {
|
|
51
73
|
type: String,
|
|
52
74
|
enum: PAYMENT_METHOD_KIND_VALUES,
|
|
53
|
-
|
|
75
|
+
required: true
|
|
54
76
|
},
|
|
55
77
|
openingAmount: {
|
|
56
78
|
type: Number,
|
|
@@ -110,6 +132,7 @@ const paymentBreakdownSchema = new Schema({
|
|
|
110
132
|
validate: mongooseMinorUnits("difference", { nullable: true })
|
|
111
133
|
}
|
|
112
134
|
}, { _id: false });
|
|
135
|
+
/** Which pipeline claimed the shift and owes the ledger post. */
|
|
113
136
|
const pendingLedgerPostSchema = new Schema({
|
|
114
137
|
idempotencyKey: {
|
|
115
138
|
type: String,
|
|
@@ -136,7 +159,8 @@ const pendingLedgerPostSchema = new Schema({
|
|
|
136
159
|
announced: {
|
|
137
160
|
type: Boolean,
|
|
138
161
|
default: false
|
|
139
|
-
}
|
|
162
|
+
},
|
|
163
|
+
durable: { type: Boolean }
|
|
140
164
|
}, { _id: false });
|
|
141
165
|
/**
|
|
142
166
|
* Build the Shift schema. Caller (definePosModels) wires the tenant adapter
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { OrderDiscountLimits } from "../domain/sale/discount.mjs";
|
|
2
|
+
import "./inject-tenant.mjs";
|
|
3
|
+
import { HydratedDocument, Model, Schema } from "mongoose";
|
|
4
|
+
//#region src/models/till-grant.model.d.ts
|
|
5
|
+
export interface ITillGrant {
|
|
6
|
+
/** The grant's `jti`. */
|
|
7
|
+
_id: string;
|
|
8
|
+
organizationId: unknown;
|
|
9
|
+
/** The till it was issued to (its sn, which is its sync stream). */
|
|
10
|
+
sn: string;
|
|
11
|
+
registerId: string;
|
|
12
|
+
issuedAt: Date;
|
|
13
|
+
expiresAt: Date;
|
|
14
|
+
discount: OrderDiscountLimits;
|
|
15
|
+
counterSaleGoodsInHand: boolean;
|
|
16
|
+
}
|
|
17
|
+
export type TillGrantDocument = HydratedDocument<ITillGrant>;
|
|
18
|
+
export type TillGrantModel = Model<ITillGrant>;
|
|
19
|
+
//#endregion
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { injectTenantField } from "./inject-tenant.mjs";
|
|
2
|
+
import { Schema } from "mongoose";
|
|
3
|
+
//#region src/models/till-grant.model.ts
|
|
4
|
+
/**
|
|
5
|
+
* An issued till grant — the server's record of what it signed, keyed by the grant's `jti`. A
|
|
6
|
+
* synced command names its grant (`actor.grantId`); the server checks the command against THIS
|
|
7
|
+
* record, never against a grant the device presents. Kept 90 days past expiry as the audit trail.
|
|
8
|
+
*/
|
|
9
|
+
const RETAIN_AFTER_EXPIRY_SECONDS = 7776e3;
|
|
10
|
+
function buildTillGrantSchema(options) {
|
|
11
|
+
const schema = new Schema({
|
|
12
|
+
_id: {
|
|
13
|
+
type: String,
|
|
14
|
+
required: true
|
|
15
|
+
},
|
|
16
|
+
sn: {
|
|
17
|
+
type: String,
|
|
18
|
+
required: true
|
|
19
|
+
},
|
|
20
|
+
registerId: {
|
|
21
|
+
type: String,
|
|
22
|
+
required: true
|
|
23
|
+
},
|
|
24
|
+
issuedAt: {
|
|
25
|
+
type: Date,
|
|
26
|
+
required: true
|
|
27
|
+
},
|
|
28
|
+
expiresAt: {
|
|
29
|
+
type: Date,
|
|
30
|
+
required: true
|
|
31
|
+
},
|
|
32
|
+
discount: {
|
|
33
|
+
type: Schema.Types.Mixed,
|
|
34
|
+
required: true
|
|
35
|
+
},
|
|
36
|
+
counterSaleGoodsInHand: {
|
|
37
|
+
type: Boolean,
|
|
38
|
+
required: true
|
|
39
|
+
}
|
|
40
|
+
}, {
|
|
41
|
+
versionKey: false,
|
|
42
|
+
...options.autoIndex !== void 0 ? { autoIndex: options.autoIndex } : {}
|
|
43
|
+
});
|
|
44
|
+
injectTenantField(schema, options.tenant);
|
|
45
|
+
schema.index({ expiresAt: 1 }, {
|
|
46
|
+
expireAfterSeconds: RETAIN_AFTER_EXPIRY_SECONDS,
|
|
47
|
+
name: "ttl_till_grant"
|
|
48
|
+
});
|
|
49
|
+
return schema;
|
|
50
|
+
}
|
|
51
|
+
//#endregion
|
|
52
|
+
export { buildTillGrantSchema };
|
|
@@ -2,7 +2,7 @@ import { IRegister } from "../models/register.model.mjs";
|
|
|
2
2
|
import { PluginType, Repository } from "@classytic/mongokit";
|
|
3
3
|
import { Model } from "mongoose";
|
|
4
4
|
//#region src/repositories/register.repository.d.ts
|
|
5
|
-
interface RegisterRepositoryConfig {
|
|
5
|
+
export interface RegisterRepositoryConfig {
|
|
6
6
|
model: Model<IRegister>;
|
|
7
7
|
/** Tenant (and any host-supplied) plugins, appended after the built-ins. */
|
|
8
8
|
plugins?: PluginType[] | undefined;
|
|
@@ -15,8 +15,7 @@ interface RegisterRepositoryConfig {
|
|
|
15
15
|
ttlDays?: number;
|
|
16
16
|
} | undefined;
|
|
17
17
|
}
|
|
18
|
-
declare class RegisterRepository extends Repository<IRegister> {
|
|
18
|
+
export declare class RegisterRepository extends Repository<IRegister> {
|
|
19
19
|
constructor(config: RegisterRepositoryConfig);
|
|
20
20
|
}
|
|
21
|
-
//#endregion
|
|
22
|
-
export { RegisterRepository, RegisterRepositoryConfig };
|
|
21
|
+
//#endregion
|
|
@@ -2,18 +2,21 @@ import { ShiftPaymentMethod } from "../domain/enums/payment-method.enum.mjs";
|
|
|
2
2
|
import { ClosedBy } from "../domain/enums/closed-by.enum.mjs";
|
|
3
3
|
import { CashMovement } from "../domain/value-objects/cash-movement.vo.mjs";
|
|
4
4
|
import { ShiftPolicy } from "../domain/value-objects/shift-policy.vo.mjs";
|
|
5
|
+
import { IShift, PendingLedgerPost } from "../domain/shift/shift.types.mjs";
|
|
5
6
|
import { PosContext } from "../domain/ports/common.mjs";
|
|
6
|
-
import { IShift, PendingLedgerPost, ShiftDocument } from "../models/shift.model.mjs";
|
|
7
7
|
import { LedgerBridge } from "../domain/ports/ledger.bridge.mjs";
|
|
8
8
|
import { PolicyBridge } from "../domain/ports/policy.bridge.mjs";
|
|
9
9
|
import { NotificationBridge } from "../domain/ports/notification.bridge.mjs";
|
|
10
10
|
import { IShiftSaleDedupe } from "../models/shift-sale-dedupe.model.mjs";
|
|
11
11
|
import { OutboxStore } from "../events/outbox-store.mjs";
|
|
12
|
+
import { ShiftDocument } from "../models/shift.model.mjs";
|
|
12
13
|
import { PluginType, Repository } from "@classytic/mongokit";
|
|
13
14
|
import { Model } from "mongoose";
|
|
14
15
|
import { EventTransport } from "@classytic/primitives/events";
|
|
15
16
|
//#region src/repositories/shift.repository.d.ts
|
|
16
|
-
interface OpenShiftInput {
|
|
17
|
+
export interface OpenShiftInput {
|
|
18
|
+
/** A caller-minted id (an offline till's shift) — kept, so the device and server name one shift. */
|
|
19
|
+
id?: string;
|
|
17
20
|
registerId: string;
|
|
18
21
|
businessDate: Date;
|
|
19
22
|
openingCashierId: string;
|
|
@@ -22,14 +25,21 @@ interface OpenShiftInput {
|
|
|
22
25
|
/** Optional override for the policy resolver. */
|
|
23
26
|
policy?: ShiftPolicy;
|
|
24
27
|
notes?: string;
|
|
28
|
+
/** When the drawer opened, if not now (a shift opened offline). */
|
|
29
|
+
openedAt?: Date;
|
|
25
30
|
}
|
|
26
|
-
interface RecordCashMovementInput {
|
|
31
|
+
export interface RecordCashMovementInput {
|
|
27
32
|
shiftId: string;
|
|
28
33
|
movement: Omit<CashMovement, 'timestamp'> & {
|
|
29
34
|
timestamp?: Date;
|
|
30
35
|
};
|
|
36
|
+
/**
|
|
37
|
+
* Refuse a cash-out the drawer cannot cover, IN the write's filter
|
|
38
|
+
* (`CashOutExceedsDrawerError`). Absent ⇒ a negative drawer is permitted.
|
|
39
|
+
*/
|
|
40
|
+
refuseOverdraw?: boolean;
|
|
31
41
|
}
|
|
32
|
-
interface BlindCloseInput {
|
|
42
|
+
export interface BlindCloseInput {
|
|
33
43
|
shiftId: string;
|
|
34
44
|
/** Cashier-counted amounts per method (cash always required; others optional). */
|
|
35
45
|
countedByMethod: Partial<Record<ShiftPaymentMethod, number>>;
|
|
@@ -37,7 +47,7 @@ interface BlindCloseInput {
|
|
|
37
47
|
closingCashierName: string;
|
|
38
48
|
notes?: string;
|
|
39
49
|
}
|
|
40
|
-
interface CloseShiftInput {
|
|
50
|
+
export interface CloseShiftInput {
|
|
41
51
|
shiftId: string;
|
|
42
52
|
/** When called after blindClose, host can pass the manager-reconciled counts. */
|
|
43
53
|
countedByMethod?: Partial<Record<ShiftPaymentMethod, number>>;
|
|
@@ -47,8 +57,13 @@ interface CloseShiftInput {
|
|
|
47
57
|
reason: string;
|
|
48
58
|
};
|
|
49
59
|
closedBy: ClosedBy;
|
|
60
|
+
/** WHO closed it. Recorded as the closing cashier unless a blind-close already named the counter. */
|
|
61
|
+
closer: {
|
|
62
|
+
id: string;
|
|
63
|
+
name: string;
|
|
64
|
+
};
|
|
50
65
|
}
|
|
51
|
-
interface IncrementSalesInput {
|
|
66
|
+
export interface IncrementSalesInput {
|
|
52
67
|
shiftId: string;
|
|
53
68
|
method: ShiftPaymentMethod;
|
|
54
69
|
/** Positive minor units (gross, tax-inclusive). */
|
|
@@ -61,21 +76,20 @@ interface IncrementSalesInput {
|
|
|
61
76
|
cashierId?: string;
|
|
62
77
|
cashierName?: string;
|
|
63
78
|
/**
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
* "idempotency is the host's responsibility" behavior.
|
|
79
|
+
* Claim-first dedupe key: the repo claims `(shiftId, dedupeRef)` in `pos_shift_sale_dedupe` before the
|
|
80
|
+
* `$inc`, so a retried call returns `{ applied: false }`. Must be retry-stable, e.g.
|
|
81
|
+
* `shift-sale:{orderNumber}:{method}`; a per-attempt id defeats it.
|
|
82
|
+
*/
|
|
83
|
+
dedupeRef: string;
|
|
84
|
+
/**
|
|
85
|
+
* Whether this call counts toward `salesCount` / `refundCount`. Default true. A sale or refund
|
|
86
|
+
* split across methods is one call per leg: pass false for every leg after the first, so the count
|
|
87
|
+
* is transactions, not legs.
|
|
74
88
|
*/
|
|
75
|
-
|
|
89
|
+
countsTransaction?: boolean;
|
|
76
90
|
}
|
|
77
91
|
/** Result of `incrementSales` — `applied: false` = dedupe hit or zero amount. */
|
|
78
|
-
interface IncrementSalesResult {
|
|
92
|
+
export interface IncrementSalesResult {
|
|
79
93
|
applied: boolean;
|
|
80
94
|
reason?: 'duplicate' | 'zero-amount';
|
|
81
95
|
}
|
|
@@ -90,18 +104,15 @@ interface IncrementSalesResult {
|
|
|
90
104
|
* post (a crash between the CAS and the ledger write);
|
|
91
105
|
* this call converged it.
|
|
92
106
|
*
|
|
93
|
-
* `already-closed` is
|
|
94
|
-
*
|
|
95
|
-
* posted its journal entry — which every host caught and logged, turning a
|
|
96
|
-
* double-post into a log line. Returning it as an outcome is what lets the
|
|
97
|
-
* lazy-close / cron / oversight triggers stop swallowing exceptions.
|
|
107
|
+
* `already-closed` is an IDEMPOTENT RESULT, not an error, so the lazy-close / cron / oversight
|
|
108
|
+
* triggers never need to swallow exceptions.
|
|
98
109
|
*
|
|
99
110
|
* **Counts submitted by a losing caller are NOT applied** — the shift was
|
|
100
111
|
* reconciled by whoever won. Check `outcome` before reporting success to a
|
|
101
112
|
* human who just typed a drawer count.
|
|
102
113
|
*/
|
|
103
|
-
type ShiftCloseOutcome = 'closed' | 'already-closed' | 'resumed';
|
|
104
|
-
interface CloseShiftResult {
|
|
114
|
+
export type ShiftCloseOutcome = 'closed' | 'already-closed' | 'resumed';
|
|
115
|
+
export interface CloseShiftResult {
|
|
105
116
|
/** The shift as persisted. Terminal in every outcome. */
|
|
106
117
|
shift: ShiftDocument;
|
|
107
118
|
outcome: ShiftCloseOutcome;
|
|
@@ -109,6 +120,11 @@ interface CloseShiftResult {
|
|
|
109
120
|
journalEntryId: string | null;
|
|
110
121
|
/** True only when THIS call invoked the ledger bridge and it succeeded. */
|
|
111
122
|
ledgerPosted: boolean;
|
|
123
|
+
/**
|
|
124
|
+
* The shift still OWES its journal entry (`pendingLedgerPost` is set): under `ledgerPost: 'deferred'` the durable
|
|
125
|
+
* event's delivery posts it after this call returns. False once the entry is recorded or none was owed.
|
|
126
|
+
*/
|
|
127
|
+
ledgerPending: boolean;
|
|
112
128
|
/**
|
|
113
129
|
* Set when the ledger bridge threw on a `forceClose` — the orphan still
|
|
114
130
|
* landed (it must), the shift keeps its `pendingLedgerPost` marker, and a
|
|
@@ -117,14 +133,10 @@ interface CloseShiftResult {
|
|
|
117
133
|
*/
|
|
118
134
|
ledgerError?: unknown;
|
|
119
135
|
}
|
|
120
|
-
interface ShiftRepositoryConfig {
|
|
136
|
+
export interface ShiftRepositoryConfig {
|
|
121
137
|
model: Model<IShift>;
|
|
122
|
-
/**
|
|
123
|
-
|
|
124
|
-
* legacy constructions without the model keep working — a `dedupeRef` call
|
|
125
|
-
* without the model throws (fail loud, never silently double-count).
|
|
126
|
-
*/
|
|
127
|
-
dedupeModel?: Model<IShiftSaleDedupe> | undefined;
|
|
138
|
+
/** Claim ledger backing `incrementSales({ dedupeRef })`. */
|
|
139
|
+
dedupeModel: Model<IShiftSaleDedupe>;
|
|
128
140
|
plugins?: PluginType[] | undefined;
|
|
129
141
|
/**
|
|
130
142
|
* Soft-delete policy. `{ ttlDays }` (default 2555 = 7yr) wires
|
|
@@ -146,11 +158,15 @@ interface ShiftRepositoryConfig {
|
|
|
146
158
|
* swallowed (the host's relay re-delivers). See PACKAGE_RULES §P8.
|
|
147
159
|
*/
|
|
148
160
|
outbox?: OutboxStore | undefined;
|
|
161
|
+
/** See `PosRuntime.ledgerPost`. Default `'inline'`. */
|
|
162
|
+
ledgerPost?: 'inline' | 'deferred' | undefined;
|
|
163
|
+
/** The engine's waiver: a deferred close falls back to the non-transactional path instead of refusing. */
|
|
164
|
+
allowNonTransactional?: boolean | undefined;
|
|
149
165
|
logger?: {
|
|
150
166
|
error(message: string, ...args: unknown[]): void;
|
|
151
167
|
} | undefined;
|
|
152
168
|
}
|
|
153
|
-
declare class ShiftRepository extends Repository<IShift> {
|
|
169
|
+
export declare class ShiftRepository extends Repository<IShift> {
|
|
154
170
|
private readonly dedupeModel;
|
|
155
171
|
private readonly defaultPolicy;
|
|
156
172
|
private readonly ledgerBridge;
|
|
@@ -158,6 +174,8 @@ declare class ShiftRepository extends Repository<IShift> {
|
|
|
158
174
|
private readonly notificationBridge;
|
|
159
175
|
private readonly eventTransport;
|
|
160
176
|
private readonly outbox;
|
|
177
|
+
private readonly ledgerPost;
|
|
178
|
+
private readonly allowNonTransactional;
|
|
161
179
|
private readonly logger;
|
|
162
180
|
constructor(config: ShiftRepositoryConfig);
|
|
163
181
|
/**
|
|
@@ -169,16 +187,17 @@ declare class ShiftRepository extends Repository<IShift> {
|
|
|
169
187
|
pause(shiftId: string, ctx: PosContext): Promise<ShiftDocument>;
|
|
170
188
|
resume(shiftId: string, ctx: PosContext): Promise<ShiftDocument>;
|
|
171
189
|
recordCashMovement(input: RecordCashMovementInput, ctx: PosContext): Promise<ShiftDocument>;
|
|
190
|
+
/**
|
|
191
|
+
* The shift, when `movement`'s key is already booked on it — the same movement, applied once.
|
|
192
|
+
* The same key on a DIFFERENT movement is two commands sharing a key, refused.
|
|
193
|
+
*/
|
|
194
|
+
private bookedMovement;
|
|
172
195
|
/**
|
|
173
196
|
* Atomically increment shift aggregates from an order-create hook in the
|
|
174
197
|
* host. Refunds pass `refund: true`.
|
|
175
198
|
*
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
* repo claims `(shiftId, dedupeRef)` insert-first in the dedupe ledger and
|
|
179
|
-
* skips the `$inc` on a duplicate claim (`{ applied: false }`). Without
|
|
180
|
-
* `dedupeRef` the legacy contract applies: idempotency is the host's
|
|
181
|
-
* responsibility (don't call twice for the same order).
|
|
199
|
+
* Exactly-once on `dedupeRef`: the repo claims `(shiftId, dedupeRef)` insert-first in the dedupe
|
|
200
|
+
* ledger and skips the `$inc` on a duplicate claim (`{ applied: false }`).
|
|
182
201
|
*/
|
|
183
202
|
incrementSales(input: IncrementSalesInput, ctx: PosContext): Promise<IncrementSalesResult>;
|
|
184
203
|
/**
|
|
@@ -216,27 +235,18 @@ declare class ShiftRepository extends Repository<IShift> {
|
|
|
216
235
|
registerId: string;
|
|
217
236
|
state: string;
|
|
218
237
|
businessDate: Date;
|
|
238
|
+
salesCount: number;
|
|
239
|
+
salesTotal: number;
|
|
219
240
|
}>>;
|
|
220
241
|
/**
|
|
221
242
|
* The currently ACTIVE shift (`open | paused | blind_closed`), or `null`.
|
|
222
243
|
*
|
|
223
|
-
*
|
|
224
|
-
*
|
|
225
|
-
*
|
|
226
|
-
*
|
|
227
|
-
* it lives here once instead of being re-derived per host.
|
|
228
|
-
*
|
|
229
|
-
* Scope:
|
|
230
|
-
* - `registerId` given → the active shift for THAT register (at most one).
|
|
231
|
-
* This is the correct scope once a branch runs multiple registers.
|
|
232
|
-
* - `registerId` omitted → the active shift for the branch. Back-compat for
|
|
233
|
-
* single-register callers; with several registers open this returns an
|
|
234
|
-
* ARBITRARY one, so multi-register call sites MUST pass `registerId`.
|
|
244
|
+
* With `registerId`: that register's shift (at most one — the
|
|
245
|
+
* `uniq_active_shift_per_register` index). Without it: the branch's only active
|
|
246
|
+
* shift, and {@link RegisterRequiredError} when there are several — returning one
|
|
247
|
+
* would ring the sale into another cashier's drawer.
|
|
235
248
|
*
|
|
236
|
-
* Routed through
|
|
237
|
-
* CASTS — the scope field. A raw `Model.findOne` here would need the host to
|
|
238
|
-
* hand-cast `organizationId` to an ObjectId (the bug that made the previous
|
|
239
|
-
* host-side shim carry its own `isValid` dance).
|
|
249
|
+
* Routed through `findAll`, so the tenant plugin injects and casts the scope.
|
|
240
250
|
*/
|
|
241
251
|
getActiveShift(ctx: PosContext, registerId?: string): Promise<ShiftDocument | null>;
|
|
242
252
|
/**
|
|
@@ -326,8 +336,7 @@ declare class ShiftRepository extends Repository<IShift> {
|
|
|
326
336
|
* on-disk state says another caller finalized the shift; returns null when
|
|
327
337
|
* the failure is a genuine illegal transition the caller should throw.
|
|
328
338
|
*
|
|
329
|
-
* The LOSER NEVER POSTS
|
|
330
|
-
* already committed its journal entry by the time it got here.
|
|
339
|
+
* The LOSER NEVER POSTS: it reaches here before any journal entry.
|
|
331
340
|
*/
|
|
332
341
|
private diagnoseLostClaim;
|
|
333
342
|
/**
|
|
@@ -335,6 +344,15 @@ declare class ShiftRepository extends Repository<IShift> {
|
|
|
335
344
|
* outstanding (crash recovery — resume it) or there is nothing to do.
|
|
336
345
|
*/
|
|
337
346
|
private convergeTerminal;
|
|
347
|
+
/**
|
|
348
|
+
* Post the journal entry a deferred close owes — the subscriber verb of `pos:shift.closed` /
|
|
349
|
+
* `pos:shift.force_closed`. Idempotent: the entry is posted under the marker's own key, a recorded entry
|
|
350
|
+
* retires the marker, and a delivery that finds no marker does nothing. Throws when the post fails (the
|
|
351
|
+
* delivery then retries under the outbox's failure policy) — it never reports a failure as a success.
|
|
352
|
+
*
|
|
353
|
+
* Cross-tenant by design (the event carries only the shift id): the scope is the shift's own branch.
|
|
354
|
+
*/
|
|
355
|
+
postPendingLedger(shiftId: string): Promise<CloseShiftResult>;
|
|
338
356
|
/**
|
|
339
357
|
* Converge a close that won the CAS but died before recording its entry.
|
|
340
358
|
*
|
|
@@ -366,7 +384,6 @@ declare class ShiftRepository extends Repository<IShift> {
|
|
|
366
384
|
* them a terminal shift is an idempotent outcome, not a failure.
|
|
367
385
|
*/
|
|
368
386
|
private requireActive;
|
|
369
|
-
private applyCounts;
|
|
370
387
|
/**
|
|
371
388
|
* Session-bound dispatch (PACKAGE_RULES §P8). Build the envelope ONCE so an
|
|
372
389
|
* outbox-relay re-publish and the live transport publish share `meta.id`
|
|
@@ -376,7 +393,20 @@ declare class ShiftRepository extends Repository<IShift> {
|
|
|
376
393
|
* than a redelivered one). Transport publish runs second; its failures are
|
|
377
394
|
* swallowed because the durable row drives re-delivery.
|
|
378
395
|
*/
|
|
396
|
+
/**
|
|
397
|
+
* One unit of work for a deferred close: the caller's session when it owns one (it commits and flushes),
|
|
398
|
+
* else a transaction this repository opens. Without transactions the engine's waiver
|
|
399
|
+
* (`allowNonTransactional`) runs the work with no session — the row is then saved after the CAS, not with it.
|
|
400
|
+
*/
|
|
401
|
+
private inUnit;
|
|
402
|
+
/**
|
|
403
|
+
* Write one event inside `unit`. Under a session the row commits with the unit through `dispatchDomainEvent`
|
|
404
|
+
* (a durable type REFUSES to write anywhere else); returns the event for {@link announceCommitted}. With no
|
|
405
|
+
* session (`allowNonTransactional`) it saves then publishes, and `null` says it already published.
|
|
406
|
+
*/
|
|
407
|
+
private dispatchInUnit;
|
|
408
|
+
/** After the unit committed: wake the transport (the relay nudge). A caller-owned session flushes for itself. */
|
|
409
|
+
private announceCommitted;
|
|
379
410
|
private dispatch;
|
|
380
411
|
}
|
|
381
|
-
//#endregion
|
|
382
|
-
export { BlindCloseInput, CloseShiftInput, CloseShiftResult, IncrementSalesInput, IncrementSalesResult, OpenShiftInput, RecordCashMovementInput, ShiftCloseOutcome, ShiftRepository, ShiftRepositoryConfig };
|
|
412
|
+
//#endregion
|