@classytic/pos 0.6.0 → 0.7.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 +103 -0
- package/dist/commands/index.d.mts +2 -0
- package/dist/commands/index.mjs +1 -0
- package/dist/commands/pos-commands.d.mts +228 -0
- package/dist/commands/pos-commands.mjs +355 -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/shift-state.enum.d.mts +7 -8
- package/dist/domain/errors/pos.errors.d.mts +82 -13
- package/dist/domain/errors/pos.errors.mjs +111 -1
- 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 +52 -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 +109 -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 +11 -7
- package/dist/domain/value-objects/shift-policy.vo.mjs +12 -1
- package/dist/engine/define-pos.d.mts +2 -3
- package/dist/engine/define-pos.mjs +13 -2
- package/dist/engine/engine-types.d.mts +24 -9
- package/dist/engine/index.mjs +1 -2
- 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 +14 -4
- package/dist/index.mjs +12 -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 +26 -2
- 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 +71 -30
- package/dist/repositories/shift.repository.mjs +295 -108
- 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,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: {
|
|
@@ -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). */
|
|
@@ -75,7 +90,7 @@ interface IncrementSalesInput {
|
|
|
75
90
|
dedupeRef?: string;
|
|
76
91
|
}
|
|
77
92
|
/** Result of `incrementSales` — `applied: false` = dedupe hit or zero amount. */
|
|
78
|
-
interface IncrementSalesResult {
|
|
93
|
+
export interface IncrementSalesResult {
|
|
79
94
|
applied: boolean;
|
|
80
95
|
reason?: 'duplicate' | 'zero-amount';
|
|
81
96
|
}
|
|
@@ -100,8 +115,8 @@ interface IncrementSalesResult {
|
|
|
100
115
|
* reconciled by whoever won. Check `outcome` before reporting success to a
|
|
101
116
|
* human who just typed a drawer count.
|
|
102
117
|
*/
|
|
103
|
-
type ShiftCloseOutcome = 'closed' | 'already-closed' | 'resumed';
|
|
104
|
-
interface CloseShiftResult {
|
|
118
|
+
export type ShiftCloseOutcome = 'closed' | 'already-closed' | 'resumed';
|
|
119
|
+
export interface CloseShiftResult {
|
|
105
120
|
/** The shift as persisted. Terminal in every outcome. */
|
|
106
121
|
shift: ShiftDocument;
|
|
107
122
|
outcome: ShiftCloseOutcome;
|
|
@@ -109,6 +124,11 @@ interface CloseShiftResult {
|
|
|
109
124
|
journalEntryId: string | null;
|
|
110
125
|
/** True only when THIS call invoked the ledger bridge and it succeeded. */
|
|
111
126
|
ledgerPosted: boolean;
|
|
127
|
+
/**
|
|
128
|
+
* The shift still OWES its journal entry (`pendingLedgerPost` is set): under `ledgerPost: 'deferred'` the durable
|
|
129
|
+
* event's delivery posts it after this call returns. False once the entry is recorded or none was owed.
|
|
130
|
+
*/
|
|
131
|
+
ledgerPending: boolean;
|
|
112
132
|
/**
|
|
113
133
|
* Set when the ledger bridge threw on a `forceClose` — the orphan still
|
|
114
134
|
* landed (it must), the shift keeps its `pendingLedgerPost` marker, and a
|
|
@@ -117,7 +137,7 @@ interface CloseShiftResult {
|
|
|
117
137
|
*/
|
|
118
138
|
ledgerError?: unknown;
|
|
119
139
|
}
|
|
120
|
-
interface ShiftRepositoryConfig {
|
|
140
|
+
export interface ShiftRepositoryConfig {
|
|
121
141
|
model: Model<IShift>;
|
|
122
142
|
/**
|
|
123
143
|
* Claim ledger backing `incrementSales({ dedupeRef })`. Optional so tests /
|
|
@@ -146,11 +166,15 @@ interface ShiftRepositoryConfig {
|
|
|
146
166
|
* swallowed (the host's relay re-delivers). See PACKAGE_RULES §P8.
|
|
147
167
|
*/
|
|
148
168
|
outbox?: OutboxStore | undefined;
|
|
169
|
+
/** See `PosRuntime.ledgerPost`. Default `'inline'`. */
|
|
170
|
+
ledgerPost?: 'inline' | 'deferred' | undefined;
|
|
171
|
+
/** The engine's waiver: a deferred close falls back to the non-transactional path instead of refusing. */
|
|
172
|
+
allowNonTransactional?: boolean | undefined;
|
|
149
173
|
logger?: {
|
|
150
174
|
error(message: string, ...args: unknown[]): void;
|
|
151
175
|
} | undefined;
|
|
152
176
|
}
|
|
153
|
-
declare class ShiftRepository extends Repository<IShift> {
|
|
177
|
+
export declare class ShiftRepository extends Repository<IShift> {
|
|
154
178
|
private readonly dedupeModel;
|
|
155
179
|
private readonly defaultPolicy;
|
|
156
180
|
private readonly ledgerBridge;
|
|
@@ -158,6 +182,8 @@ declare class ShiftRepository extends Repository<IShift> {
|
|
|
158
182
|
private readonly notificationBridge;
|
|
159
183
|
private readonly eventTransport;
|
|
160
184
|
private readonly outbox;
|
|
185
|
+
private readonly ledgerPost;
|
|
186
|
+
private readonly allowNonTransactional;
|
|
161
187
|
private readonly logger;
|
|
162
188
|
constructor(config: ShiftRepositoryConfig);
|
|
163
189
|
/**
|
|
@@ -169,6 +195,11 @@ declare class ShiftRepository extends Repository<IShift> {
|
|
|
169
195
|
pause(shiftId: string, ctx: PosContext): Promise<ShiftDocument>;
|
|
170
196
|
resume(shiftId: string, ctx: PosContext): Promise<ShiftDocument>;
|
|
171
197
|
recordCashMovement(input: RecordCashMovementInput, ctx: PosContext): Promise<ShiftDocument>;
|
|
198
|
+
/**
|
|
199
|
+
* The shift, when `movement`'s key is already booked on it — the same movement, applied once.
|
|
200
|
+
* The same key on a DIFFERENT movement is two commands sharing a key, refused.
|
|
201
|
+
*/
|
|
202
|
+
private bookedMovement;
|
|
172
203
|
/**
|
|
173
204
|
* Atomically increment shift aggregates from an order-create hook in the
|
|
174
205
|
* host. Refunds pass `refund: true`.
|
|
@@ -220,23 +251,12 @@ declare class ShiftRepository extends Repository<IShift> {
|
|
|
220
251
|
/**
|
|
221
252
|
* The currently ACTIVE shift (`open | paused | blind_closed`), or `null`.
|
|
222
253
|
*
|
|
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`.
|
|
254
|
+
* With `registerId`: that register's shift (at most one — the
|
|
255
|
+
* `uniq_active_shift_per_register` index). Without it: the branch's only active
|
|
256
|
+
* shift, and {@link RegisterRequiredError} when there are several — returning one
|
|
257
|
+
* would ring the sale into another cashier's drawer.
|
|
235
258
|
*
|
|
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).
|
|
259
|
+
* Routed through `findAll`, so the tenant plugin injects and casts the scope.
|
|
240
260
|
*/
|
|
241
261
|
getActiveShift(ctx: PosContext, registerId?: string): Promise<ShiftDocument | null>;
|
|
242
262
|
/**
|
|
@@ -335,6 +355,15 @@ declare class ShiftRepository extends Repository<IShift> {
|
|
|
335
355
|
* outstanding (crash recovery — resume it) or there is nothing to do.
|
|
336
356
|
*/
|
|
337
357
|
private convergeTerminal;
|
|
358
|
+
/**
|
|
359
|
+
* Post the journal entry a deferred close owes — the subscriber verb of `pos:shift.closed` /
|
|
360
|
+
* `pos:shift.force_closed`. Idempotent: the entry is posted under the marker's own key, a recorded entry
|
|
361
|
+
* retires the marker, and a delivery that finds no marker does nothing. Throws when the post fails (the
|
|
362
|
+
* delivery then retries under the outbox's failure policy) — it never reports a failure as a success.
|
|
363
|
+
*
|
|
364
|
+
* Cross-tenant by design (the event carries only the shift id): the scope is the shift's own branch.
|
|
365
|
+
*/
|
|
366
|
+
postPendingLedger(shiftId: string): Promise<CloseShiftResult>;
|
|
338
367
|
/**
|
|
339
368
|
* Converge a close that won the CAS but died before recording its entry.
|
|
340
369
|
*
|
|
@@ -366,7 +395,6 @@ declare class ShiftRepository extends Repository<IShift> {
|
|
|
366
395
|
* them a terminal shift is an idempotent outcome, not a failure.
|
|
367
396
|
*/
|
|
368
397
|
private requireActive;
|
|
369
|
-
private applyCounts;
|
|
370
398
|
/**
|
|
371
399
|
* Session-bound dispatch (PACKAGE_RULES §P8). Build the envelope ONCE so an
|
|
372
400
|
* outbox-relay re-publish and the live transport publish share `meta.id`
|
|
@@ -376,7 +404,20 @@ declare class ShiftRepository extends Repository<IShift> {
|
|
|
376
404
|
* than a redelivered one). Transport publish runs second; its failures are
|
|
377
405
|
* swallowed because the durable row drives re-delivery.
|
|
378
406
|
*/
|
|
407
|
+
/**
|
|
408
|
+
* One unit of work for a deferred close: the caller's session when it owns one (it commits and flushes),
|
|
409
|
+
* else a transaction this repository opens. Without transactions the engine's waiver
|
|
410
|
+
* (`allowNonTransactional`) runs the work with no session — the row is then saved after the CAS, not with it.
|
|
411
|
+
*/
|
|
412
|
+
private inUnit;
|
|
413
|
+
/**
|
|
414
|
+
* Write one event inside `unit`. Under a session the row commits with the unit through `dispatchDomainEvent`
|
|
415
|
+
* (a durable type REFUSES to write anywhere else); returns the event for {@link announceCommitted}. With no
|
|
416
|
+
* session (the waived fallback) the legacy save-then-publish runs and `null` says it already published.
|
|
417
|
+
*/
|
|
418
|
+
private dispatchInUnit;
|
|
419
|
+
/** After the unit committed: wake the transport (the relay nudge). A caller-owned session flushes for itself. */
|
|
420
|
+
private announceCommitted;
|
|
379
421
|
private dispatch;
|
|
380
422
|
}
|
|
381
|
-
//#endregion
|
|
382
|
-
export { BlindCloseInput, CloseShiftInput, CloseShiftResult, IncrementSalesInput, IncrementSalesResult, OpenShiftInput, RecordCashMovementInput, ShiftCloseOutcome, ShiftRepository, ShiftRepositoryConfig };
|
|
423
|
+
//#endregion
|