@classytic/pos 0.3.0 → 0.4.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.
Files changed (59) hide show
  1. package/CHANGELOG.md +236 -0
  2. package/LICENSE +75 -21
  3. package/README.md +11 -4
  4. package/dist/domain/enums/cash-movement-reason.enum.d.mts +15 -1
  5. package/dist/domain/enums/closed-by.enum.d.mts +1 -1
  6. package/dist/domain/enums/payment-method.enum.d.mts +23 -9
  7. package/dist/domain/enums/payment-method.enum.mjs +27 -7
  8. package/dist/domain/enums/shift-state.enum.d.mts +1 -1
  9. package/dist/domain/errors/pos.errors.d.mts +53 -10
  10. package/dist/domain/errors/pos.errors.mjs +58 -13
  11. package/dist/domain/index.d.mts +5 -5
  12. package/dist/domain/index.mjs +5 -4
  13. package/dist/domain/ports/common.d.mts +0 -1
  14. package/dist/domain/ports/ledger.bridge.d.mts +42 -8
  15. package/dist/domain/ports/ledger.bridge.mjs +14 -0
  16. package/dist/domain/ports/notification.bridge.d.mts +0 -1
  17. package/dist/domain/ports/policy.bridge.d.mts +0 -1
  18. package/dist/domain/state-machines/shift.fsm.d.mts +1 -2
  19. package/dist/domain/value-objects/cash-movement.vo.d.mts +0 -1
  20. package/dist/domain/value-objects/payment-breakdown.vo.d.mts +4 -1
  21. package/dist/domain/value-objects/shift-policy.vo.d.mts +31 -3
  22. package/dist/domain/value-objects/shift-policy.vo.mjs +35 -1
  23. package/dist/engine/define-pos.d.mts +9 -0
  24. package/dist/engine/define-pos.mjs +126 -0
  25. package/dist/engine/engine-types.d.mts +84 -36
  26. package/dist/engine/index.d.mts +4 -3
  27. package/dist/engine/index.mjs +2 -2
  28. package/dist/events/event-catalog.d.mts +6 -7
  29. package/dist/events/event-constants.d.mts +8 -8
  30. package/dist/events/event-payloads.d.mts +0 -1
  31. package/dist/events/helpers.d.mts +0 -1
  32. package/dist/events/helpers.mjs +5 -5
  33. package/dist/events/in-process-bus.d.mts +4 -15
  34. package/dist/events/in-process-bus.mjs +14 -32
  35. package/dist/events/outbox-store.d.mts +3 -19
  36. package/dist/events/outbox-store.mjs +1 -27
  37. package/dist/index.d.mts +15 -10
  38. package/dist/index.mjs +8 -6
  39. package/dist/models/create-models.d.mts +31 -4
  40. package/dist/models/create-models.mjs +80 -13
  41. package/dist/models/hide-soft-delete.mjs +11 -0
  42. package/dist/models/inject-tenant.d.mts +2 -2
  43. package/dist/models/inject-tenant.mjs +5 -11
  44. package/dist/models/register.model.d.mts +37 -0
  45. package/dist/models/register.model.mjs +82 -0
  46. package/dist/models/shift-sale-dedupe.model.d.mts +16 -0
  47. package/dist/models/shift-sale-dedupe.model.mjs +57 -0
  48. package/dist/models/shift.model.d.mts +48 -2
  49. package/dist/models/shift.model.mjs +88 -25
  50. package/dist/repositories/register.repository.d.mts +22 -0
  51. package/dist/repositories/register.repository.mjs +25 -0
  52. package/dist/repositories/shift.repository.d.mts +217 -12
  53. package/dist/repositories/shift.repository.mjs +480 -85
  54. package/dist/schemas/shift.d.mts +7 -18
  55. package/dist/schemas/shift.mjs +1 -2
  56. package/dist/testing/fixtures.d.mts +0 -1
  57. package/package.json +113 -110
  58. package/dist/engine/create-pos.d.mts +0 -6
  59. package/dist/engine/create-pos.mjs +0 -82
@@ -0,0 +1,11 @@
1
+ //#region src/models/hide-soft-delete.ts
2
+ function hideSoftDeleteField(schema) {
3
+ const transform = (_doc, ret) => {
4
+ delete ret.deletedAt;
5
+ return ret;
6
+ };
7
+ schema.set("toJSON", { transform });
8
+ schema.set("toObject", { transform });
9
+ }
10
+ //#endregion
11
+ export { hideSoftDeleteField };
@@ -1,2 +1,2 @@
1
- import { Schema } from "mongoose";
2
- import { TenantConfig } from "@classytic/repo-core/tenant";
1
+ import { TenantConfig } from "@classytic/repo-core/tenant";
2
+ import { Schema } from "mongoose";
@@ -1,5 +1,5 @@
1
- import mongoose from "mongoose";
2
1
  import { resolveTenantConfig } from "@classytic/repo-core/tenant";
2
+ import mongoose from "mongoose";
3
3
  //#region src/models/inject-tenant.ts
4
4
  /**
5
5
  * Mongoose-specific tenant adapter — wraps `resolveTenantConfig()` from
@@ -9,15 +9,9 @@ import { resolveTenantConfig } from "@classytic/repo-core/tenant";
9
9
  * mongoose dependency. Mirrors the @classytic/order convention.
10
10
  */
11
11
  function resolvePosTenant(input = {}) {
12
- if (typeof input === "boolean" || input === void 0) return resolveTenantConfig(input);
13
- if ("tenantFieldType" in input || "multiTenant" in input) {
14
- const legacy = input;
15
- return resolveTenantConfig({
16
- enabled: legacy.multiTenant !== false,
17
- fieldType: legacy.tenantFieldType ?? "objectId"
18
- });
19
- }
20
- return resolveTenantConfig(input);
12
+ const resolved = resolveTenantConfig(input);
13
+ if (resolved.strategy === "custom") throw new Error("[pos] tenant.strategy 'custom' is unsupported: POS repositories require one concrete tenant field");
14
+ return resolved;
21
15
  }
22
16
  /**
23
17
  * Inject the tenant field on a schema and prepend it to every existing
@@ -31,7 +25,6 @@ function injectTenantField(schema, tenant) {
31
25
  schema.add({ [tenant.tenantField]: {
32
26
  type: isObjectId ? mongoose.Schema.Types.ObjectId : String,
33
27
  ...tenant.enabled && tenant.required ? { required: true } : {},
34
- index: true,
35
28
  ...isObjectId && tenant.ref ? { ref: tenant.ref } : {}
36
29
  } });
37
30
  if (!tenant.enabled || !isFieldStrategy) return;
@@ -43,6 +36,7 @@ function injectTenantField(schema, tenant) {
43
36
  for (const [key, val] of Object.entries(fields)) newFields[key] = val;
44
37
  indexEntry[0] = newFields;
45
38
  }
39
+ if (!(existingIndexes ?? []).some(([fields]) => Object.keys(fields)[0] === tenant.tenantField)) schema.index({ [tenant.tenantField]: 1 });
46
40
  }
47
41
  //#endregion
48
42
  export { injectTenantField, resolvePosTenant };
@@ -0,0 +1,37 @@
1
+ import "./inject-tenant.mjs";
2
+ import { HydratedDocument, Model, Schema } from "mongoose";
3
+ //#region src/models/register.model.d.ts
4
+ interface IRegister {
5
+ /** Tenant scope (organizationId / branchId). Type set by tenant config. */
6
+ organizationId: unknown;
7
+ /**
8
+ * Short, human-facing identifier unique within the branch (e.g. `REG-01`,
9
+ * `TILL-2`, `FRONT`). This is the value a `Shift.registerId` points at
10
+ * — stable across renames of `name`.
11
+ */
12
+ code: string;
13
+ /** Display label shown in the register picker (e.g. "Front Counter"). */
14
+ name: string;
15
+ /**
16
+ * Soft enable/disable. An archived register (`isActive: false`) is hidden
17
+ * from the open-shift picker but is NOT deleted — its historical shifts and
18
+ * their journal entries keep referencing it. Prefer archiving over delete.
19
+ */
20
+ isActive: boolean;
21
+ /** Optional physical location / zone label within the store. */
22
+ location: string | null;
23
+ /**
24
+ * Optional hardware binding — a terminal / device / receipt-printer id the
25
+ * host maps this register to. Free-form; the kernel never interprets it.
26
+ */
27
+ terminalId: string | null;
28
+ notes: string;
29
+ /** Soft-delete tombstone (softDeletePlugin). Null when live. */
30
+ deletedAt?: Date | null;
31
+ createdAt?: Date;
32
+ updatedAt?: Date;
33
+ }
34
+ type RegisterDocument = HydratedDocument<IRegister>;
35
+ type RegisterModel = Model<IRegister>;
36
+ //#endregion
37
+ export { IRegister, RegisterDocument, RegisterModel };
@@ -0,0 +1,82 @@
1
+ import { hideSoftDeleteField } from "./hide-soft-delete.mjs";
2
+ import { injectTenantField } from "./inject-tenant.mjs";
3
+ import { Schema } from "mongoose";
4
+ //#region src/models/register.model.ts
5
+ /**
6
+ * Register Mongoose model — a POS register / till / terminal within a branch.
7
+ *
8
+ * A Register is the physical (or logical) point of sale a cashier signs on to.
9
+ * A branch (organizationId) may have MANY registers; each open `Shift`
10
+ * references one via `shift.registerId`, and the Shift model's
11
+ * `uniq_active_shift_per_register` partial-unique index already permits one
12
+ * active shift PER register — so two cashiers on two registers in the same
13
+ * branch run parallel shifts. This model gives that `registerId` a real,
14
+ * host-manageable identity (code, name, active flag) instead of a bare string.
15
+ *
16
+ * The package stays vertical-agnostic: retail counters, restaurant terminals,
17
+ * clinic front-desks, and car-wash bays are all just "registers".
18
+ *
19
+ * Indexing strategy:
20
+ * - `code` is UNIQUE per branch among live (non-soft-deleted) registers —
21
+ * a partial unique index filtered on `deletedAt: null` (soft-delete
22
+ * convention; a deleted register frees its code for reuse).
23
+ * - `(isActive)` covers the "list this branch's active registers" query the
24
+ * POS open screen runs.
25
+ * - The tenant prepend (organizationId) is injected by `injectTenantField`
26
+ * onto every compound index; we don't repeat it manually.
27
+ */
28
+ /**
29
+ * Build the Register schema. The caller (`definePosModels`) binds it to the
30
+ * connection with the resolved collection name — this function never calls
31
+ * `connection.model(...)` so model-collision detection lives in one place
32
+ * (PACKAGE_RULES §21).
33
+ */
34
+ function buildRegisterSchema(options) {
35
+ const schema = new Schema({
36
+ code: {
37
+ type: String,
38
+ required: true,
39
+ trim: true
40
+ },
41
+ name: {
42
+ type: String,
43
+ required: true,
44
+ trim: true
45
+ },
46
+ isActive: {
47
+ type: Boolean,
48
+ default: true
49
+ },
50
+ location: {
51
+ type: String,
52
+ default: null
53
+ },
54
+ terminalId: {
55
+ type: String,
56
+ default: null
57
+ },
58
+ notes: {
59
+ type: String,
60
+ default: ""
61
+ },
62
+ deletedAt: {
63
+ type: Date,
64
+ default: null,
65
+ select: false
66
+ }
67
+ }, {
68
+ timestamps: true,
69
+ ...options.autoIndex !== void 0 ? { autoIndex: options.autoIndex } : {}
70
+ });
71
+ schema.index({ code: 1 }, {
72
+ unique: true,
73
+ partialFilterExpression: { deletedAt: null },
74
+ name: "uniq_register_code"
75
+ });
76
+ schema.index({ isActive: 1 }, { name: "registers_by_active" });
77
+ injectTenantField(schema, options.tenant);
78
+ hideSoftDeleteField(schema);
79
+ return schema;
80
+ }
81
+ //#endregion
82
+ export { buildRegisterSchema };
@@ -0,0 +1,16 @@
1
+ import { HydratedDocument, Model, Schema } from "mongoose";
2
+ //#region src/models/shift-sale-dedupe.model.d.ts
3
+ interface IShiftSaleDedupe {
4
+ /** Target shift `_id` (stringified) — one claim namespace per shift. */
5
+ shiftId: string;
6
+ /**
7
+ * Host-supplied claim key — retry-stable order identity (e.g.
8
+ * `shift-sale:{orderNumber}:{method}`). NEVER a per-attempt `_id`.
9
+ */
10
+ orderRef: string;
11
+ createdAt: Date;
12
+ }
13
+ type ShiftSaleDedupeDocument = HydratedDocument<IShiftSaleDedupe>;
14
+ type ShiftSaleDedupeModel = Model<IShiftSaleDedupe>;
15
+ //#endregion
16
+ export { IShiftSaleDedupe, ShiftSaleDedupeDocument, ShiftSaleDedupeModel };
@@ -0,0 +1,57 @@
1
+ import { Schema } from "mongoose";
2
+ //#region src/models/shift-sale-dedupe.model.ts
3
+ /**
4
+ * ShiftSaleDedupe — tiny claim-first dedupe ledger backing
5
+ * `ShiftRepository.incrementSales({ dedupeRef })`.
6
+ *
7
+ * Why it exists: hosts fire `incrementSales` from order-create hooks that can
8
+ * legitimately re-run (Mongo transient-transaction retries re-execute the
9
+ * host's create callback — and its `after:create` hooks — while the shift
10
+ * `$inc` itself is NOT part of that transaction, so a naive re-run
11
+ * double-counts the sale). The claim is insert-first into this collection:
12
+ * exactly one caller per `(shiftId, orderRef)` wins the unique index; the
13
+ * loser's duplicate-key error IS the "already applied" signal and the `$inc`
14
+ * is skipped. No unbounded arrays on the Shift doc (PACKAGE_RULES).
15
+ *
16
+ * Intentional deviations from the package's collection rules (mirrors
17
+ * `@classytic/order`'s `order_idempotency` exemption — ephemeral claim state,
18
+ * not a domain document):
19
+ * - NO soft delete: a soft-deleted claim would fall out of the unique
20
+ * guard and re-admit a duplicate `$inc`. Rows are reaped by TTL instead.
21
+ * - NO tenant field / plugin: scoping is structural — `shiftId` is a
22
+ * globally-unique Shift `_id`, so a claim can never collide across
23
+ * branches, and reads are always key-exact.
24
+ */
25
+ function buildShiftSaleDedupeSchema(options = {}) {
26
+ const schema = new Schema({
27
+ shiftId: {
28
+ type: String,
29
+ required: true
30
+ },
31
+ orderRef: {
32
+ type: String,
33
+ required: true
34
+ },
35
+ createdAt: {
36
+ type: Date,
37
+ default: () => /* @__PURE__ */ new Date()
38
+ }
39
+ }, {
40
+ versionKey: false,
41
+ ...options.autoIndex !== void 0 ? { autoIndex: options.autoIndex } : {}
42
+ });
43
+ schema.index({
44
+ shiftId: 1,
45
+ orderRef: 1
46
+ }, {
47
+ unique: true,
48
+ name: "uniq_shift_sale_claim"
49
+ });
50
+ schema.index({ createdAt: 1 }, {
51
+ expireAfterSeconds: 2592e3,
52
+ name: "ttl_shift_sale_claim"
53
+ });
54
+ return schema;
55
+ }
56
+ //#endregion
57
+ export { buildShiftSaleDedupeSchema };
@@ -3,10 +3,48 @@ import { ClosedBy } from "../domain/enums/closed-by.enum.mjs";
3
3
  import { CashMovement } from "../domain/value-objects/cash-movement.vo.mjs";
4
4
  import { PaymentBreakdown } from "../domain/value-objects/payment-breakdown.vo.mjs";
5
5
  import { ShiftPolicy } from "../domain/value-objects/shift-policy.vo.mjs";
6
+ import "./inject-tenant.mjs";
6
7
  import { HydratedDocument, Model, Schema } from "mongoose";
7
8
  import { ApprovalChain } from "@classytic/primitives/approval";
8
-
9
9
  //#region src/models/shift.model.d.ts
10
+ /** Which pipeline claimed the shift and owes the ledger post. */
11
+ type PendingLedgerPostTrigger = 'close' | 'forceClose';
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
+ }
10
48
  interface IShift {
11
49
  /** Tenant scope (organizationId / branchId). Type set by tenant config. */
12
50
  organizationId: unknown;
@@ -59,10 +97,18 @@ interface IShift {
59
97
  notes: string;
60
98
  /** Set by the close pipeline after the ledger bridge returns the JE id. */
61
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;
62
108
  createdAt?: Date;
63
109
  updatedAt?: Date;
64
110
  }
65
111
  type ShiftDocument = HydratedDocument<IShift>;
66
112
  type ShiftModel = Model<IShift>;
67
113
  //#endregion
68
- export { IShift, ShiftDocument, ShiftModel };
114
+ export { IShift, PendingLedgerPost, PendingLedgerPostTrigger, ShiftDocument, ShiftModel };
@@ -1,9 +1,11 @@
1
+ import { hideSoftDeleteField } from "./hide-soft-delete.mjs";
2
+ import { injectTenantField } from "./inject-tenant.mjs";
1
3
  import { CASH_MOVEMENT_REASON_CODES } from "../domain/enums/cash-movement-reason.enum.mjs";
2
4
  import { CLOSED_BY_VALUES } from "../domain/enums/closed-by.enum.mjs";
3
- import { SHIFT_PAYMENT_METHODS } from "../domain/enums/payment-method.enum.mjs";
5
+ import { PAYMENT_METHOD_KIND_VALUES } from "../domain/enums/payment-method.enum.mjs";
4
6
  import { SHIFT_STATES } from "../domain/enums/shift-state.enum.mjs";
5
- import { injectTenantField } from "./inject-tenant.mjs";
6
7
  import { Schema } from "mongoose";
8
+ import { mongooseMinorUnits } from "@classytic/validation/money";
7
9
  //#region src/models/shift.model.ts
8
10
  const cashMovementSchema = new Schema({
9
11
  type: {
@@ -14,7 +16,8 @@ const cashMovementSchema = new Schema({
14
16
  amount: {
15
17
  type: Number,
16
18
  required: true,
17
- min: 0
19
+ min: 0,
20
+ validate: mongooseMinorUnits("amount")
18
21
  },
19
22
  reasonCode: {
20
23
  type: String,
@@ -41,59 +44,102 @@ const cashMovementSchema = new Schema({
41
44
  const paymentBreakdownSchema = new Schema({
42
45
  method: {
43
46
  type: String,
44
- enum: SHIFT_PAYMENT_METHODS,
45
- required: true
47
+ required: true,
48
+ minlength: 1
49
+ },
50
+ methodKind: {
51
+ type: String,
52
+ enum: PAYMENT_METHOD_KIND_VALUES,
53
+ default: "other"
46
54
  },
47
55
  openingAmount: {
48
56
  type: Number,
49
57
  default: 0,
50
- min: 0
58
+ min: 0,
59
+ validate: mongooseMinorUnits("openingAmount")
51
60
  },
52
61
  salesAmount: {
53
62
  type: Number,
54
63
  default: 0,
55
- min: 0
64
+ min: 0,
65
+ validate: mongooseMinorUnits("salesAmount")
56
66
  },
57
67
  taxAmount: {
58
68
  type: Number,
59
69
  default: 0,
60
- min: 0
70
+ min: 0,
71
+ validate: mongooseMinorUnits("taxAmount")
61
72
  },
62
73
  refundAmount: {
63
74
  type: Number,
64
75
  default: 0,
65
- min: 0
76
+ min: 0,
77
+ validate: mongooseMinorUnits("refundAmount")
66
78
  },
67
79
  refundTaxAmount: {
68
80
  type: Number,
69
81
  default: 0,
70
- min: 0
82
+ min: 0,
83
+ validate: mongooseMinorUnits("refundTaxAmount")
71
84
  },
72
85
  cashInAmount: {
73
86
  type: Number,
74
87
  default: 0,
75
- min: 0
88
+ min: 0,
89
+ validate: mongooseMinorUnits("cashInAmount")
76
90
  },
77
91
  cashOutAmount: {
78
92
  type: Number,
79
93
  default: 0,
80
- min: 0
94
+ min: 0,
95
+ validate: mongooseMinorUnits("cashOutAmount")
81
96
  },
82
97
  expectedAmount: {
83
98
  type: Number,
84
- default: null
99
+ default: null,
100
+ validate: mongooseMinorUnits("expectedAmount", { nullable: true })
85
101
  },
86
102
  countedAmount: {
87
103
  type: Number,
88
- default: null
104
+ default: null,
105
+ validate: mongooseMinorUnits("countedAmount", { nullable: true })
89
106
  },
90
107
  difference: {
91
108
  type: Number,
109
+ default: null,
110
+ validate: mongooseMinorUnits("difference", { nullable: true })
111
+ }
112
+ }, { _id: false });
113
+ const pendingLedgerPostSchema = new Schema({
114
+ idempotencyKey: {
115
+ type: String,
116
+ required: true
117
+ },
118
+ trigger: {
119
+ type: String,
120
+ enum: ["close", "forceClose"],
121
+ required: true
122
+ },
123
+ attempts: {
124
+ type: Number,
125
+ required: true,
126
+ min: 1
127
+ },
128
+ stampedAt: {
129
+ type: Date,
130
+ required: true
131
+ },
132
+ lastAttemptAt: {
133
+ type: Date,
92
134
  default: null
135
+ },
136
+ announced: {
137
+ type: Boolean,
138
+ default: false
93
139
  }
94
140
  }, { _id: false });
95
141
  /**
96
- * Build the Shift schema. Caller (createPosModels) wires the tenant adapter
142
+ * Build the Shift schema. Caller (definePosModels) wires the tenant adapter
97
143
  * + register on the connection with the resolved collection name.
98
144
  *
99
145
  * The schema does NOT call `connection.model(...)` — that's the registrar's
@@ -166,19 +212,23 @@ function buildShiftSchema(options) {
166
212
  openingCash: {
167
213
  type: Number,
168
214
  default: 0,
169
- min: 0
215
+ min: 0,
216
+ validate: mongooseMinorUnits("openingCash")
170
217
  },
171
218
  expectedCash: {
172
219
  type: Number,
173
- default: null
220
+ default: null,
221
+ validate: mongooseMinorUnits("expectedCash", { nullable: true })
174
222
  },
175
223
  countedCash: {
176
224
  type: Number,
177
- default: null
225
+ default: null,
226
+ validate: mongooseMinorUnits("countedCash", { nullable: true })
178
227
  },
179
228
  cashDifference: {
180
229
  type: Number,
181
- default: null
230
+ default: null,
231
+ validate: mongooseMinorUnits("cashDifference", { nullable: true })
182
232
  },
183
233
  paymentBreakdown: {
184
234
  type: [paymentBreakdownSchema],
@@ -192,7 +242,8 @@ function buildShiftSchema(options) {
192
242
  salesTotal: {
193
243
  type: Number,
194
244
  default: 0,
195
- min: 0
245
+ min: 0,
246
+ validate: mongooseMinorUnits("salesTotal")
196
247
  },
197
248
  refundCount: {
198
249
  type: Number,
@@ -202,7 +253,8 @@ function buildShiftSchema(options) {
202
253
  refundTotal: {
203
254
  type: Number,
204
255
  default: 0,
205
- min: 0
256
+ min: 0,
257
+ validate: mongooseMinorUnits("refundTotal")
206
258
  },
207
259
  cashMovements: {
208
260
  type: [cashMovementSchema],
@@ -228,15 +280,21 @@ function buildShiftSchema(options) {
228
280
  journalEntryId: {
229
281
  type: String,
230
282
  default: null
283
+ },
284
+ pendingLedgerPost: {
285
+ type: pendingLedgerPostSchema,
286
+ default: null
287
+ },
288
+ deletedAt: {
289
+ type: Date,
290
+ default: null,
291
+ select: false
231
292
  }
232
293
  }, {
233
294
  timestamps: true,
234
295
  ...options.autoIndex !== void 0 ? { autoIndex: options.autoIndex } : {}
235
296
  });
236
- schema.index({
237
- registerId: 1,
238
- state: 1
239
- }, {
297
+ schema.index({ registerId: 1 }, {
240
298
  unique: true,
241
299
  partialFilterExpression: { state: { $in: [
242
300
  "open",
@@ -260,7 +318,12 @@ function buildShiftSchema(options) {
260
318
  "blind_closed"
261
319
  ] } }
262
320
  });
321
+ schema.index({ "pendingLedgerPost.stampedAt": 1 }, {
322
+ name: "pending_ledger_post_sweep",
323
+ partialFilterExpression: { "pendingLedgerPost.stampedAt": { $exists: true } }
324
+ });
263
325
  injectTenantField(schema, options.tenant);
326
+ hideSoftDeleteField(schema);
264
327
  return schema;
265
328
  }
266
329
  //#endregion
@@ -0,0 +1,22 @@
1
+ import { IRegister } from "../models/register.model.mjs";
2
+ import { PluginType, Repository } from "@classytic/mongokit";
3
+ import { Model } from "mongoose";
4
+ //#region src/repositories/register.repository.d.ts
5
+ interface RegisterRepositoryConfig {
6
+ model: Model<IRegister>;
7
+ /** Tenant (and any host-supplied) plugins, appended after the built-ins. */
8
+ plugins?: PluginType[] | undefined;
9
+ /**
10
+ * Soft-delete policy. `{ ttlDays }` (default) wires `softDeletePlugin`;
11
+ * `false` disables it (hard deletes). The engine resolves this from
12
+ * `PosShape.softDelete` so one config toggles the whole POS surface.
13
+ */
14
+ softDelete?: false | {
15
+ ttlDays?: number;
16
+ } | undefined;
17
+ }
18
+ declare class RegisterRepository extends Repository<IRegister> {
19
+ constructor(config: RegisterRepositoryConfig);
20
+ }
21
+ //#endregion
22
+ export { RegisterRepository, RegisterRepositoryConfig };
@@ -0,0 +1,25 @@
1
+ import { Repository, softDeletePlugin } from "@classytic/mongokit";
2
+ //#region src/repositories/register.repository.ts
3
+ /**
4
+ * RegisterRepository — extends mongokit `Repository<IRegister>`.
5
+ *
6
+ * A Register is a plain reference entity: create / list / get / update /
7
+ * delete (soft) come straight from mongokit and are enough — there is no
8
+ * state machine, no ledger handshake, no money. Per the kernel rule "add a
9
+ * method only when it encodes domain logic", this repository adds NO verbs;
10
+ * it exists solely to wire the soft-delete + tenant plugins the way the
11
+ * engine expects, exactly like `ShiftRepository` does.
12
+ *
13
+ * Code-uniqueness within a branch is enforced by the `uniq_register_code`
14
+ * partial-unique index (see register.model.ts) — a duplicate `code` surfaces
15
+ * as a driver E11000 that the host maps to a friendly 409, mirroring how the
16
+ * shift flow maps its active-shift collision.
17
+ */
18
+ var RegisterRepository = class extends Repository {
19
+ constructor(config) {
20
+ const { model, plugins = [], softDelete = { ttlDays: 2555 } } = config;
21
+ super(model, [...softDelete === false ? [] : [softDeletePlugin({ ttlDays: softDelete.ttlDays ?? 2555 })], ...plugins]);
22
+ }
23
+ };
24
+ //#endregion
25
+ export { RegisterRepository };