@classytic/pos 0.1.1
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 +17 -0
- package/LICENSE +21 -0
- package/README.md +152 -0
- package/dist/domain/enums/cash-movement-reason.enum.d.mts +12 -0
- package/dist/domain/enums/cash-movement-reason.enum.mjs +19 -0
- package/dist/domain/enums/closed-by.enum.d.mts +13 -0
- package/dist/domain/enums/closed-by.enum.mjs +16 -0
- package/dist/domain/enums/payment-method.enum.d.mts +14 -0
- package/dist/domain/enums/payment-method.enum.mjs +18 -0
- package/dist/domain/enums/shift-state.enum.d.mts +26 -0
- package/dist/domain/enums/shift-state.enum.mjs +39 -0
- package/dist/domain/errors/pos.errors.d.mts +37 -0
- package/dist/domain/errors/pos.errors.mjs +64 -0
- package/dist/domain/index.d.mts +14 -0
- package/dist/domain/index.mjs +9 -0
- package/dist/domain/ports/common.d.mts +9 -0
- package/dist/domain/ports/ledger.bridge.d.mts +18 -0
- package/dist/domain/ports/notification.bridge.d.mts +12 -0
- package/dist/domain/ports/policy.bridge.d.mts +13 -0
- package/dist/domain/state-machines/shift.fsm.d.mts +14 -0
- package/dist/domain/state-machines/shift.fsm.mjs +41 -0
- package/dist/domain/value-objects/cash-movement.vo.d.mts +19 -0
- package/dist/domain/value-objects/payment-breakdown.vo.d.mts +24 -0
- package/dist/domain/value-objects/payment-breakdown.vo.mjs +11 -0
- package/dist/domain/value-objects/shift-policy.vo.d.mts +32 -0
- package/dist/domain/value-objects/shift-policy.vo.mjs +14 -0
- package/dist/engine/create-pos.d.mts +6 -0
- package/dist/engine/create-pos.mjs +65 -0
- package/dist/engine/engine-types.d.mts +73 -0
- package/dist/engine/index.d.mts +3 -0
- package/dist/engine/index.mjs +2 -0
- package/dist/events/event-constants.d.mts +21 -0
- package/dist/events/event-constants.mjs +20 -0
- package/dist/events/event-payloads.d.mts +49 -0
- package/dist/events/helpers.d.mts +6 -0
- package/dist/events/helpers.mjs +12 -0
- package/dist/events/in-process-bus.d.mts +19 -0
- package/dist/events/in-process-bus.mjs +38 -0
- package/dist/events/index.d.mts +5 -0
- package/dist/events/index.mjs +4 -0
- package/dist/index.d.mts +23 -0
- package/dist/index.mjs +15 -0
- package/dist/models/create-models.d.mts +9 -0
- package/dist/models/create-models.mjs +20 -0
- package/dist/models/inject-tenant.d.mts +2 -0
- package/dist/models/inject-tenant.mjs +48 -0
- package/dist/models/shift.model.d.mts +68 -0
- package/dist/models/shift.model.mjs +267 -0
- package/dist/repositories/repo-options.mjs +20 -0
- package/dist/repositories/shift.repository.d.mts +153 -0
- package/dist/repositories/shift.repository.mjs +401 -0
- package/dist/schemas/index.d.mts +2 -0
- package/dist/schemas/index.mjs +2 -0
- package/dist/schemas/shift.d.mts +67 -0
- package/dist/schemas/shift.mjs +52 -0
- package/dist/testing/fixtures.d.mts +9 -0
- package/dist/testing/fixtures.mjs +36 -0
- package/dist/testing/index.d.mts +2 -0
- package/dist/testing/index.mjs +2 -0
- package/package.json +110 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.1 — Initial public release
|
|
4
|
+
|
|
5
|
+
- `createPosEngine()` factory with `connection` + `defaultPolicy`
|
|
6
|
+
- `Shift` model + Mongoose schema (3 indexes, all justified)
|
|
7
|
+
- `ShiftRepository` extending mongokit `Repository<IShift>` with domain verbs:
|
|
8
|
+
`open`, `pause`, `resume`, `recordCashMovement`, `incrementSales`,
|
|
9
|
+
`blindClose`, `close`, `forceClose`
|
|
10
|
+
- 5-state FSM: `open` → `paused` ↔ `open` → `blind_closed` → `closed`;
|
|
11
|
+
any active state → `orphaned_closed` via `forceClose`
|
|
12
|
+
- Bridges: `ledger` (JE emission), `policy` (per-branch policy resolver),
|
|
13
|
+
`notification` (variance + orphan alerts)
|
|
14
|
+
- Multi-tenant scoping via mongokit `multiTenantPlugin` (default ON)
|
|
15
|
+
- 7-year soft-delete retention via `softDeletePlugin`
|
|
16
|
+
- Zod v4 body schemas at `@classytic/pos/schemas` for arc 2.11 wiring
|
|
17
|
+
- Test fixtures at `@classytic/pos/testing` for host integration tests
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Classytic
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# @classytic/pos
|
|
2
|
+
|
|
3
|
+
> Multi-vertical POS / register-session kernel for MongoDB. Shift lifecycle, cash-drawer reconciliation, payment-method breakdown, variance detection, FSM. Works for retail, restaurant, clinic, service-booking, car-wash and any vertical with a "shift opens, shift closes, money is reconciled" workflow.
|
|
4
|
+
|
|
5
|
+
Built on [@classytic/mongokit](https://npmjs.com/package/@classytic/mongokit) and [@classytic/primitives](https://npmjs.com/package/@classytic/primitives). Plugs into [@classytic/arc](https://npmjs.com/package/@classytic/arc) for HTTP — but works standalone with any framework.
|
|
6
|
+
|
|
7
|
+
## Why
|
|
8
|
+
|
|
9
|
+
Date-based aggregation ("close the day") is a pre-2010 retail pattern that hides per-shift accountability. Industry standard (Odoo, Square, Shopify POS, Lightspeed, Microsoft Dynamics 365 Retail, ARTS) is **shift-driven**: a register opens, accumulates orders, closes with a count, posts a single journal entry. Multi-cashier same-day = parallel shifts, summed by the ledger.
|
|
10
|
+
|
|
11
|
+
This package is that primitive. It has no opinion about your chart of accounts, payment methods, or accounting framework — those plug in via bridges.
|
|
12
|
+
|
|
13
|
+
## Verticals
|
|
14
|
+
|
|
15
|
+
| Vertical | Maps to |
|
|
16
|
+
|---|---|
|
|
17
|
+
| Retail (grocery, boutique, hypermarket) | One shift per register per cashier-day |
|
|
18
|
+
| Restaurant | One shift per server section, or per register; kitchen routing in extra fields |
|
|
19
|
+
| Clinic / doctor booking | One shift per front desk per day; copays + appointment fees |
|
|
20
|
+
| Service booking (salon, spa) | One shift per chair / station |
|
|
21
|
+
| Car wash | One shift per bay |
|
|
22
|
+
|
|
23
|
+
The package's only assumption: "a session opens, accumulates payments, reconciles cash, closes." How you slice that across cashiers / registers / stations is your host's choice.
|
|
24
|
+
|
|
25
|
+
## Install
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
npm install @classytic/pos
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Peer deps: `mongoose >=9.4.1`, `@classytic/mongokit >=3.11.0`, `@classytic/primitives >=0.1.0`, `@classytic/repo-core >=0.2.0`, `zod >=4.0.0`.
|
|
32
|
+
|
|
33
|
+
## Quick start
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import mongoose from 'mongoose';
|
|
37
|
+
import { createPosEngine } from '@classytic/pos';
|
|
38
|
+
|
|
39
|
+
const pos = createPosEngine({
|
|
40
|
+
connection: mongoose.connection,
|
|
41
|
+
defaultPolicy: {
|
|
42
|
+
requiredOpeningFloat: null,
|
|
43
|
+
blindCloseRequired: false,
|
|
44
|
+
varianceThresholdAbs: 100, // BDT 100
|
|
45
|
+
varianceThresholdPct: 0.5, // 0.5% of expected
|
|
46
|
+
managerOverrideRequired: true,
|
|
47
|
+
allowHandover: true,
|
|
48
|
+
requireReasonCode: true,
|
|
49
|
+
allowedReasonCodes: ['safe_drop', 'petty_cash', 'bank_deposit', 'till_top_up', 'other'],
|
|
50
|
+
allowedPaymentMethods: ['cash', 'card', 'mfs', 'bank_transfer'],
|
|
51
|
+
},
|
|
52
|
+
bridges: {
|
|
53
|
+
ledger: {
|
|
54
|
+
async onShiftClosed(shift, ctx) {
|
|
55
|
+
// Build journal entry from shift.paymentBreakdown, post via your ledger
|
|
56
|
+
// engine, return { journalEntryId }.
|
|
57
|
+
const je = await myLedger.post(buildShiftJE(shift));
|
|
58
|
+
return { journalEntryId: je._id.toString() };
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
const ctx = { organizationId: 'branch-1', actorId: 'user-1' };
|
|
65
|
+
|
|
66
|
+
// Open a shift
|
|
67
|
+
const shift = await pos.repositories.shift.open({
|
|
68
|
+
registerId: 'register-1',
|
|
69
|
+
businessDate: new Date(),
|
|
70
|
+
openingCashierId: 'cashier-1',
|
|
71
|
+
openingCashierName: 'Alice',
|
|
72
|
+
openingCash: 1000,
|
|
73
|
+
}, ctx);
|
|
74
|
+
|
|
75
|
+
// As orders post in your host, increment the breakdown:
|
|
76
|
+
await pos.repositories.shift.incrementSales({
|
|
77
|
+
shiftId: shift._id.toString(),
|
|
78
|
+
method: 'cash',
|
|
79
|
+
amount: 250,
|
|
80
|
+
}, ctx);
|
|
81
|
+
|
|
82
|
+
// At end of shift — variance check + ledger bridge + state write
|
|
83
|
+
await pos.repositories.shift.close({
|
|
84
|
+
shiftId: shift._id.toString(),
|
|
85
|
+
countedByMethod: { cash: 1245 },
|
|
86
|
+
closedBy: 'cashier',
|
|
87
|
+
}, ctx);
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## State machine
|
|
91
|
+
|
|
92
|
+
```
|
|
93
|
+
open ─► paused ─► open ─► blind_closed ─► closed
|
|
94
|
+
│ │ │
|
|
95
|
+
└─► forceClose ◄──┴────────────┴────► orphaned_closed
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
| State | Meaning |
|
|
99
|
+
|---|---|
|
|
100
|
+
| `open` | Accepting sales; drawer live |
|
|
101
|
+
| `paused` | Toast-style handover; drawer rejects new sales until `resume` |
|
|
102
|
+
| `blind_closed` | Lightspeed pattern; cashier counted, awaits manager reconcile |
|
|
103
|
+
| `closed` | Reconciled, immutable |
|
|
104
|
+
| `orphaned_closed` | EOD cron auto-closed; counts default to expected; flagged for review |
|
|
105
|
+
|
|
106
|
+
## Bridges
|
|
107
|
+
|
|
108
|
+
All optional. The package degrades gracefully without any.
|
|
109
|
+
|
|
110
|
+
- **`ledger`** — `onShiftClosed(shift, ctx) → { journalEntryId }`. Host emits the JE. Throw to roll back the close.
|
|
111
|
+
- **`policy`** — `resolvePolicy(ctx) → ShiftPolicy`. Per-branch policy override at open time. Without it, `defaultPolicy` is used.
|
|
112
|
+
- **`notification`** — `onVarianceOverride`, `onOrphanedClose`. Best-effort; failures logged not raised.
|
|
113
|
+
|
|
114
|
+
## Arc 2.11 wiring
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
import { defineResource, createMongooseAdapter } from '@classytic/arc';
|
|
118
|
+
import { openShiftBody, blindCloseBody, closeBody } from '@classytic/pos/schemas';
|
|
119
|
+
|
|
120
|
+
export default defineResource({
|
|
121
|
+
name: 'shift',
|
|
122
|
+
prefix: '/pos/shifts',
|
|
123
|
+
adapter: createMongooseAdapter(pos.models.Shift, pos.repositories.shift),
|
|
124
|
+
customSchemas: {
|
|
125
|
+
create: { body: openShiftBody },
|
|
126
|
+
},
|
|
127
|
+
actions: {
|
|
128
|
+
pause: { handler: (id, _, req) => pos.repositories.shift.pause(id, ctxFrom(req)) },
|
|
129
|
+
resume: { handler: (id, _, req) => pos.repositories.shift.resume(id, ctxFrom(req)) },
|
|
130
|
+
blind_close: { handler: (id, data, req) => pos.repositories.shift.blindClose({ shiftId: id, ...data }, ctxFrom(req)), schema: blindCloseBody },
|
|
131
|
+
close: { handler: (id, data, req) => pos.repositories.shift.close({ shiftId: id, ...data }, ctxFrom(req)), schema: closeBody },
|
|
132
|
+
force_close: { handler: (id, _, req) => pos.repositories.shift.forceClose(id, ctxFrom(req)) },
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Auto-CRUD (list/get/create/update/delete) comes from arc + mongokit. State transitions go through declarative actions. Custom routes only when you genuinely need them.
|
|
138
|
+
|
|
139
|
+
## Subpath exports
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
import { createPosEngine } from '@classytic/pos'; // engine + types
|
|
143
|
+
import { ShiftRepository, type IShift } from '@classytic/pos'; // repo + model types
|
|
144
|
+
import { POS_EVENTS, InProcessPosBus } from '@classytic/pos/events';
|
|
145
|
+
import { canTransition, ShiftPolicy } from '@classytic/pos/domain';
|
|
146
|
+
import { openShiftBody, closeBody } from '@classytic/pos/schemas';
|
|
147
|
+
import { makePolicy, makeOpenShiftInput } from '@classytic/pos/testing';
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## License
|
|
151
|
+
|
|
152
|
+
MIT — see [LICENSE](./LICENSE).
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
//#region src/domain/enums/cash-movement-reason.enum.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Reason codes for mid-shift cash movements (Shopify 2025 pattern).
|
|
4
|
+
*
|
|
5
|
+
* `requireReasonCode: true` on the policy makes one of these mandatory on
|
|
6
|
+
* every cash-in / cash-out call. Hosts can scope the allowed subset per
|
|
7
|
+
* branch via `policy.allowedReasonCodes`.
|
|
8
|
+
*/
|
|
9
|
+
declare const CASH_MOVEMENT_REASON_CODES: readonly ["safe_drop", "petty_cash", "owner_withdrawal", "correction", "bank_deposit", "till_top_up", "other"];
|
|
10
|
+
type CashMovementReasonCode = (typeof CASH_MOVEMENT_REASON_CODES)[number];
|
|
11
|
+
//#endregion
|
|
12
|
+
export { CASH_MOVEMENT_REASON_CODES, CashMovementReasonCode };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//#region src/domain/enums/cash-movement-reason.enum.ts
|
|
2
|
+
/**
|
|
3
|
+
* Reason codes for mid-shift cash movements (Shopify 2025 pattern).
|
|
4
|
+
*
|
|
5
|
+
* `requireReasonCode: true` on the policy makes one of these mandatory on
|
|
6
|
+
* every cash-in / cash-out call. Hosts can scope the allowed subset per
|
|
7
|
+
* branch via `policy.allowedReasonCodes`.
|
|
8
|
+
*/
|
|
9
|
+
const CASH_MOVEMENT_REASON_CODES = [
|
|
10
|
+
"safe_drop",
|
|
11
|
+
"petty_cash",
|
|
12
|
+
"owner_withdrawal",
|
|
13
|
+
"correction",
|
|
14
|
+
"bank_deposit",
|
|
15
|
+
"till_top_up",
|
|
16
|
+
"other"
|
|
17
|
+
];
|
|
18
|
+
//#endregion
|
|
19
|
+
export { CASH_MOVEMENT_REASON_CODES };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//#region src/domain/enums/closed-by.enum.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Identifies the actor that closed a shift — populated when state moves to
|
|
4
|
+
* `closed` or `orphaned_closed`.
|
|
5
|
+
*
|
|
6
|
+
* cashier — the cashier counted and finalized themselves
|
|
7
|
+
* manager — a manager closed (typically post blind_close reconcile)
|
|
8
|
+
* system_auto — EOD cron / forceClose (orphaned_closed)
|
|
9
|
+
*/
|
|
10
|
+
declare const CLOSED_BY_VALUES: readonly ["cashier", "manager", "system_auto"];
|
|
11
|
+
type ClosedBy = (typeof CLOSED_BY_VALUES)[number];
|
|
12
|
+
//#endregion
|
|
13
|
+
export { CLOSED_BY_VALUES, ClosedBy };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//#region src/domain/enums/closed-by.enum.ts
|
|
2
|
+
/**
|
|
3
|
+
* Identifies the actor that closed a shift — populated when state moves to
|
|
4
|
+
* `closed` or `orphaned_closed`.
|
|
5
|
+
*
|
|
6
|
+
* cashier — the cashier counted and finalized themselves
|
|
7
|
+
* manager — a manager closed (typically post blind_close reconcile)
|
|
8
|
+
* system_auto — EOD cron / forceClose (orphaned_closed)
|
|
9
|
+
*/
|
|
10
|
+
const CLOSED_BY_VALUES = [
|
|
11
|
+
"cashier",
|
|
12
|
+
"manager",
|
|
13
|
+
"system_auto"
|
|
14
|
+
];
|
|
15
|
+
//#endregion
|
|
16
|
+
export { CLOSED_BY_VALUES };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
//#region src/domain/enums/payment-method.enum.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Default payment method enum.
|
|
4
|
+
*
|
|
5
|
+
* Hosts can extend the set via `schemaOptions.shift.extraFields` if they
|
|
6
|
+
* accept methods beyond this baseline (e.g. crypto, gift card, on-account).
|
|
7
|
+
* The package treats payment methods as opaque keys for the breakdown
|
|
8
|
+
* accumulator — the actual GL mapping and tax routing live in the host's
|
|
9
|
+
* shift→JE contract.
|
|
10
|
+
*/
|
|
11
|
+
declare const SHIFT_PAYMENT_METHODS: readonly ["cash", "card", "mfs", "bank_transfer"];
|
|
12
|
+
type ShiftPaymentMethod = (typeof SHIFT_PAYMENT_METHODS)[number];
|
|
13
|
+
//#endregion
|
|
14
|
+
export { SHIFT_PAYMENT_METHODS, ShiftPaymentMethod };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//#region src/domain/enums/payment-method.enum.ts
|
|
2
|
+
/**
|
|
3
|
+
* Default payment method enum.
|
|
4
|
+
*
|
|
5
|
+
* Hosts can extend the set via `schemaOptions.shift.extraFields` if they
|
|
6
|
+
* accept methods beyond this baseline (e.g. crypto, gift card, on-account).
|
|
7
|
+
* The package treats payment methods as opaque keys for the breakdown
|
|
8
|
+
* accumulator — the actual GL mapping and tax routing live in the host's
|
|
9
|
+
* shift→JE contract.
|
|
10
|
+
*/
|
|
11
|
+
const SHIFT_PAYMENT_METHODS = [
|
|
12
|
+
"cash",
|
|
13
|
+
"card",
|
|
14
|
+
"mfs",
|
|
15
|
+
"bank_transfer"
|
|
16
|
+
];
|
|
17
|
+
//#endregion
|
|
18
|
+
export { SHIFT_PAYMENT_METHODS };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
//#region src/domain/enums/shift-state.enum.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* POS shift state enum + helpers.
|
|
4
|
+
*
|
|
5
|
+
* Modelled on Square (`CashDrawerShift`) + Toast (pause/resume) + Lightspeed
|
|
6
|
+
* (blind close). The state names match Odoo semantics where they overlap so
|
|
7
|
+
* cross-system reasoning translates 1:1.
|
|
8
|
+
*
|
|
9
|
+
* open — accepting sales; drawer live (Odoo: opened)
|
|
10
|
+
* paused — Toast-style handover; drawer rejects new sales until resume
|
|
11
|
+
* blind_closed — Lightspeed pattern; cashier counted, awaits manager reconcile
|
|
12
|
+
* (Odoo: closing_control)
|
|
13
|
+
* closed — reconciled, immutable (Odoo: closed)
|
|
14
|
+
* orphaned_closed — auto-closed by EOD cron / forceClose; counts default to
|
|
15
|
+
* expected; flagged for manager review
|
|
16
|
+
*/
|
|
17
|
+
declare const SHIFT_STATES: readonly ["open", "paused", "blind_closed", "closed", "orphaned_closed"];
|
|
18
|
+
type ShiftState = (typeof SHIFT_STATES)[number];
|
|
19
|
+
/** States that block opening a new shift for the same scope. */
|
|
20
|
+
declare const ACTIVE_SHIFT_STATES: readonly ShiftState[];
|
|
21
|
+
/** States considered final — no further state mutations permitted. */
|
|
22
|
+
declare const FINAL_SHIFT_STATES: readonly ShiftState[];
|
|
23
|
+
declare function isActiveShiftState(state: ShiftState): boolean;
|
|
24
|
+
declare function isFinalShiftState(state: ShiftState): boolean;
|
|
25
|
+
//#endregion
|
|
26
|
+
export { ACTIVE_SHIFT_STATES, FINAL_SHIFT_STATES, SHIFT_STATES, ShiftState, isActiveShiftState, isFinalShiftState };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
//#region src/domain/enums/shift-state.enum.ts
|
|
2
|
+
/**
|
|
3
|
+
* POS shift state enum + helpers.
|
|
4
|
+
*
|
|
5
|
+
* Modelled on Square (`CashDrawerShift`) + Toast (pause/resume) + Lightspeed
|
|
6
|
+
* (blind close). The state names match Odoo semantics where they overlap so
|
|
7
|
+
* cross-system reasoning translates 1:1.
|
|
8
|
+
*
|
|
9
|
+
* open — accepting sales; drawer live (Odoo: opened)
|
|
10
|
+
* paused — Toast-style handover; drawer rejects new sales until resume
|
|
11
|
+
* blind_closed — Lightspeed pattern; cashier counted, awaits manager reconcile
|
|
12
|
+
* (Odoo: closing_control)
|
|
13
|
+
* closed — reconciled, immutable (Odoo: closed)
|
|
14
|
+
* orphaned_closed — auto-closed by EOD cron / forceClose; counts default to
|
|
15
|
+
* expected; flagged for manager review
|
|
16
|
+
*/
|
|
17
|
+
const SHIFT_STATES = [
|
|
18
|
+
"open",
|
|
19
|
+
"paused",
|
|
20
|
+
"blind_closed",
|
|
21
|
+
"closed",
|
|
22
|
+
"orphaned_closed"
|
|
23
|
+
];
|
|
24
|
+
/** States that block opening a new shift for the same scope. */
|
|
25
|
+
const ACTIVE_SHIFT_STATES = [
|
|
26
|
+
"open",
|
|
27
|
+
"paused",
|
|
28
|
+
"blind_closed"
|
|
29
|
+
];
|
|
30
|
+
/** States considered final — no further state mutations permitted. */
|
|
31
|
+
const FINAL_SHIFT_STATES = ["closed", "orphaned_closed"];
|
|
32
|
+
function isActiveShiftState(state) {
|
|
33
|
+
return ACTIVE_SHIFT_STATES.includes(state);
|
|
34
|
+
}
|
|
35
|
+
function isFinalShiftState(state) {
|
|
36
|
+
return FINAL_SHIFT_STATES.includes(state);
|
|
37
|
+
}
|
|
38
|
+
//#endregion
|
|
39
|
+
export { ACTIVE_SHIFT_STATES, FINAL_SHIFT_STATES, SHIFT_STATES, isActiveShiftState, isFinalShiftState };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
//#region src/domain/errors/pos.errors.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Custom error classes thrown by the POS package.
|
|
4
|
+
*
|
|
5
|
+
* Hosts catch these by `name` (or `instanceof`) and translate to HTTP status
|
|
6
|
+
* codes — the package itself stays HTTP-agnostic. Each error carries a
|
|
7
|
+
* stable `code` for log-mining and i18n.
|
|
8
|
+
*/
|
|
9
|
+
declare class PosError extends Error {
|
|
10
|
+
readonly code: string;
|
|
11
|
+
constructor(code: string, message: string);
|
|
12
|
+
}
|
|
13
|
+
declare class ShiftNotFoundError extends PosError {
|
|
14
|
+
constructor(id: string);
|
|
15
|
+
}
|
|
16
|
+
declare class IllegalShiftTransitionError extends PosError {
|
|
17
|
+
constructor(from: string, verb: string);
|
|
18
|
+
}
|
|
19
|
+
declare class ActiveShiftAlreadyOpenError extends PosError {
|
|
20
|
+
constructor(scope: string);
|
|
21
|
+
}
|
|
22
|
+
declare class VarianceExceededError extends PosError {
|
|
23
|
+
readonly difference: number;
|
|
24
|
+
readonly expected: number;
|
|
25
|
+
constructor(difference: number, expected: number);
|
|
26
|
+
}
|
|
27
|
+
declare class ShiftFinalizedError extends PosError {
|
|
28
|
+
constructor(id: string);
|
|
29
|
+
}
|
|
30
|
+
declare class CashMovementInvalidError extends PosError {
|
|
31
|
+
constructor(reason: string);
|
|
32
|
+
}
|
|
33
|
+
declare class ShiftModelCollisionError extends Error {
|
|
34
|
+
constructor(name: string);
|
|
35
|
+
}
|
|
36
|
+
//#endregion
|
|
37
|
+
export { ActiveShiftAlreadyOpenError, CashMovementInvalidError, IllegalShiftTransitionError, PosError, ShiftFinalizedError, ShiftModelCollisionError, ShiftNotFoundError, VarianceExceededError };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
//#region src/domain/errors/pos.errors.ts
|
|
2
|
+
/**
|
|
3
|
+
* Custom error classes thrown by the POS package.
|
|
4
|
+
*
|
|
5
|
+
* Hosts catch these by `name` (or `instanceof`) and translate to HTTP status
|
|
6
|
+
* codes — the package itself stays HTTP-agnostic. Each error carries a
|
|
7
|
+
* stable `code` for log-mining and i18n.
|
|
8
|
+
*/
|
|
9
|
+
var PosError = class extends Error {
|
|
10
|
+
code;
|
|
11
|
+
constructor(code, message) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.name = "PosError";
|
|
14
|
+
this.code = code;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var ShiftNotFoundError = class extends PosError {
|
|
18
|
+
constructor(id) {
|
|
19
|
+
super("SHIFT_NOT_FOUND", `Shift not found: ${id}`);
|
|
20
|
+
this.name = "ShiftNotFoundError";
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
var IllegalShiftTransitionError = class extends PosError {
|
|
24
|
+
constructor(from, verb) {
|
|
25
|
+
super("ILLEGAL_SHIFT_TRANSITION", `Cannot ${verb} a shift in state '${from}'`);
|
|
26
|
+
this.name = "IllegalShiftTransitionError";
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
var ActiveShiftAlreadyOpenError = class extends PosError {
|
|
30
|
+
constructor(scope) {
|
|
31
|
+
super("ACTIVE_SHIFT_ALREADY_OPEN", `An active shift already exists for scope '${scope}'`);
|
|
32
|
+
this.name = "ActiveShiftAlreadyOpenError";
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
var VarianceExceededError = class extends PosError {
|
|
36
|
+
difference;
|
|
37
|
+
expected;
|
|
38
|
+
constructor(difference, expected) {
|
|
39
|
+
super("VARIANCE_EXCEEDED", `Cash variance ${difference} exceeds policy threshold (expected ${expected})`);
|
|
40
|
+
this.name = "VarianceExceededError";
|
|
41
|
+
this.difference = difference;
|
|
42
|
+
this.expected = expected;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var ShiftFinalizedError = class extends PosError {
|
|
46
|
+
constructor(id) {
|
|
47
|
+
super("SHIFT_FINALIZED", `Shift ${id} is final and cannot be mutated`);
|
|
48
|
+
this.name = "ShiftFinalizedError";
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
var CashMovementInvalidError = class extends PosError {
|
|
52
|
+
constructor(reason) {
|
|
53
|
+
super("CASH_MOVEMENT_INVALID", reason);
|
|
54
|
+
this.name = "CashMovementInvalidError";
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
var ShiftModelCollisionError = class extends Error {
|
|
58
|
+
constructor(name) {
|
|
59
|
+
super(`[pos] Mongoose model "${name}" already exists on this connection. For hot-reload / test fixtures, pass \`forceRecreate: true\` to clobber. For two pos engines in one process, use two Mongoose connections (\`mongoose.createConnection(...)\`) — each has its own model registry.`);
|
|
60
|
+
this.name = "ShiftModelCollisionError";
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
//#endregion
|
|
64
|
+
export { ActiveShiftAlreadyOpenError, CashMovementInvalidError, IllegalShiftTransitionError, PosError, ShiftFinalizedError, ShiftModelCollisionError, ShiftNotFoundError, VarianceExceededError };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ACTIVE_SHIFT_STATES, FINAL_SHIFT_STATES, SHIFT_STATES, ShiftState, isActiveShiftState, isFinalShiftState } from "./enums/shift-state.enum.mjs";
|
|
2
|
+
import { SHIFT_PAYMENT_METHODS, ShiftPaymentMethod } from "./enums/payment-method.enum.mjs";
|
|
3
|
+
import { CASH_MOVEMENT_REASON_CODES, CashMovementReasonCode } from "./enums/cash-movement-reason.enum.mjs";
|
|
4
|
+
import { CLOSED_BY_VALUES, ClosedBy } from "./enums/closed-by.enum.mjs";
|
|
5
|
+
import { SHIFT_VERBS, ShiftVerb, allowedVerbs, canTransition, nextState } from "./state-machines/shift.fsm.mjs";
|
|
6
|
+
import { CashMovement } from "./value-objects/cash-movement.vo.mjs";
|
|
7
|
+
import { PaymentBreakdown, computeDifference, computeExpected } from "./value-objects/payment-breakdown.vo.mjs";
|
|
8
|
+
import { ShiftPolicy, isWithinVarianceThreshold } from "./value-objects/shift-policy.vo.mjs";
|
|
9
|
+
import { ActiveShiftAlreadyOpenError, CashMovementInvalidError, IllegalShiftTransitionError, PosError, ShiftFinalizedError, ShiftModelCollisionError, ShiftNotFoundError, VarianceExceededError } from "./errors/pos.errors.mjs";
|
|
10
|
+
import { PosContext } from "./ports/common.mjs";
|
|
11
|
+
import { LedgerBridge } from "./ports/ledger.bridge.mjs";
|
|
12
|
+
import { PolicyBridge } from "./ports/policy.bridge.mjs";
|
|
13
|
+
import { NotificationBridge } from "./ports/notification.bridge.mjs";
|
|
14
|
+
export { ACTIVE_SHIFT_STATES, ActiveShiftAlreadyOpenError, CASH_MOVEMENT_REASON_CODES, CLOSED_BY_VALUES, CashMovement, CashMovementInvalidError, CashMovementReasonCode, ClosedBy, FINAL_SHIFT_STATES, IllegalShiftTransitionError, type LedgerBridge, type NotificationBridge, PaymentBreakdown, type PolicyBridge, type PosContext, PosError, SHIFT_PAYMENT_METHODS, SHIFT_STATES, SHIFT_VERBS, ShiftFinalizedError, ShiftModelCollisionError, ShiftNotFoundError, ShiftPaymentMethod, ShiftPolicy, ShiftState, ShiftVerb, VarianceExceededError, allowedVerbs, canTransition, computeDifference, computeExpected, isActiveShiftState, isFinalShiftState, isWithinVarianceThreshold, nextState };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ActiveShiftAlreadyOpenError, CashMovementInvalidError, IllegalShiftTransitionError, PosError, ShiftFinalizedError, ShiftModelCollisionError, ShiftNotFoundError, VarianceExceededError } from "./errors/pos.errors.mjs";
|
|
2
|
+
import { CASH_MOVEMENT_REASON_CODES } from "./enums/cash-movement-reason.enum.mjs";
|
|
3
|
+
import { CLOSED_BY_VALUES } from "./enums/closed-by.enum.mjs";
|
|
4
|
+
import { SHIFT_PAYMENT_METHODS } from "./enums/payment-method.enum.mjs";
|
|
5
|
+
import { ACTIVE_SHIFT_STATES, FINAL_SHIFT_STATES, SHIFT_STATES, isActiveShiftState, isFinalShiftState } from "./enums/shift-state.enum.mjs";
|
|
6
|
+
import { SHIFT_VERBS, allowedVerbs, canTransition, nextState } from "./state-machines/shift.fsm.mjs";
|
|
7
|
+
import { computeDifference, computeExpected } from "./value-objects/payment-breakdown.vo.mjs";
|
|
8
|
+
import { isWithinVarianceThreshold } from "./value-objects/shift-policy.vo.mjs";
|
|
9
|
+
export { ACTIVE_SHIFT_STATES, ActiveShiftAlreadyOpenError, CASH_MOVEMENT_REASON_CODES, CLOSED_BY_VALUES, CashMovementInvalidError, FINAL_SHIFT_STATES, IllegalShiftTransitionError, PosError, SHIFT_PAYMENT_METHODS, SHIFT_STATES, SHIFT_VERBS, ShiftFinalizedError, ShiftModelCollisionError, ShiftNotFoundError, VarianceExceededError, allowedVerbs, canTransition, computeDifference, computeExpected, isActiveShiftState, isFinalShiftState, isWithinVarianceThreshold, nextState };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { OperationContext } from "@classytic/primitives/context";
|
|
2
|
+
|
|
3
|
+
//#region src/domain/ports/common.d.ts
|
|
4
|
+
interface PosContext extends OperationContext {
|
|
5
|
+
/** Host's local-TZ business date (YYYY-MM-DD) for shifts that derive at open. */
|
|
6
|
+
businessDate?: string;
|
|
7
|
+
}
|
|
8
|
+
//#endregion
|
|
9
|
+
export { PosContext };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { PosContext } from "./common.mjs";
|
|
2
|
+
import { ShiftDocument } from "../../models/shift.model.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/domain/ports/ledger.bridge.d.ts
|
|
5
|
+
interface LedgerBridge {
|
|
6
|
+
/**
|
|
7
|
+
* Called during the close pipeline. Return the host-side journal entry id;
|
|
8
|
+
* the package persists it onto the shift.
|
|
9
|
+
*
|
|
10
|
+
* Throw on validation / chart-of-accounts errors so the close transaction
|
|
11
|
+
* rolls back. Use a domain error class with a stable `code`.
|
|
12
|
+
*/
|
|
13
|
+
onShiftClosed(shift: ShiftDocument, ctx: PosContext): Promise<{
|
|
14
|
+
journalEntryId: string;
|
|
15
|
+
}>;
|
|
16
|
+
}
|
|
17
|
+
//#endregion
|
|
18
|
+
export { LedgerBridge };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { PosContext } from "./common.mjs";
|
|
2
|
+
import { ShiftDocument } from "../../models/shift.model.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/domain/ports/notification.bridge.d.ts
|
|
5
|
+
interface NotificationBridge {
|
|
6
|
+
/** Cash variance exceeded the policy threshold and was overridden by a manager. */
|
|
7
|
+
onVarianceOverride?(shift: ShiftDocument, ctx: PosContext): Promise<void> | void;
|
|
8
|
+
/** Cron / admin force-closed an orphaned shift. */
|
|
9
|
+
onOrphanedClose?(shift: ShiftDocument, ctx: PosContext): Promise<void> | void;
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
export { NotificationBridge };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ShiftPolicy } from "../value-objects/shift-policy.vo.mjs";
|
|
2
|
+
import { PosContext } from "./common.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/domain/ports/policy.bridge.d.ts
|
|
5
|
+
interface PolicyBridge {
|
|
6
|
+
/**
|
|
7
|
+
* Resolve the policy snapshot for a new shift. Called once during `open()`;
|
|
8
|
+
* the result is frozen onto the shift and never re-read.
|
|
9
|
+
*/
|
|
10
|
+
resolvePolicy(ctx: PosContext): Promise<ShiftPolicy> | ShiftPolicy;
|
|
11
|
+
}
|
|
12
|
+
//#endregion
|
|
13
|
+
export { PolicyBridge };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ShiftState } from "../enums/shift-state.enum.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/domain/state-machines/shift.fsm.d.ts
|
|
4
|
+
/** Domain verbs that drive transitions. */
|
|
5
|
+
declare const SHIFT_VERBS: readonly ["pause", "resume", "blindClose", "close", "forceClose"];
|
|
6
|
+
type ShiftVerb = (typeof SHIFT_VERBS)[number];
|
|
7
|
+
/** Resolve the target state for a verb from a given state, or `null` if disallowed. */
|
|
8
|
+
declare function nextState(from: ShiftState, verb: ShiftVerb): ShiftState | null;
|
|
9
|
+
/** Predicate for guards. */
|
|
10
|
+
declare function canTransition(from: ShiftState, verb: ShiftVerb): boolean;
|
|
11
|
+
/** All verbs valid from the given state — useful for UI affordance. */
|
|
12
|
+
declare function allowedVerbs(from: ShiftState): readonly ShiftVerb[];
|
|
13
|
+
//#endregion
|
|
14
|
+
export { SHIFT_VERBS, ShiftVerb, allowedVerbs, canTransition, nextState };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
//#region src/domain/state-machines/shift.fsm.ts
|
|
2
|
+
/** Domain verbs that drive transitions. */
|
|
3
|
+
const SHIFT_VERBS = [
|
|
4
|
+
"pause",
|
|
5
|
+
"resume",
|
|
6
|
+
"blindClose",
|
|
7
|
+
"close",
|
|
8
|
+
"forceClose"
|
|
9
|
+
];
|
|
10
|
+
const TRANSITIONS = {
|
|
11
|
+
open: {
|
|
12
|
+
pause: "paused",
|
|
13
|
+
blindClose: "blind_closed",
|
|
14
|
+
close: "closed",
|
|
15
|
+
forceClose: "orphaned_closed"
|
|
16
|
+
},
|
|
17
|
+
paused: {
|
|
18
|
+
resume: "open",
|
|
19
|
+
forceClose: "orphaned_closed"
|
|
20
|
+
},
|
|
21
|
+
blind_closed: {
|
|
22
|
+
close: "closed",
|
|
23
|
+
forceClose: "orphaned_closed"
|
|
24
|
+
},
|
|
25
|
+
closed: {},
|
|
26
|
+
orphaned_closed: {}
|
|
27
|
+
};
|
|
28
|
+
/** Resolve the target state for a verb from a given state, or `null` if disallowed. */
|
|
29
|
+
function nextState(from, verb) {
|
|
30
|
+
return TRANSITIONS[from][verb] ?? null;
|
|
31
|
+
}
|
|
32
|
+
/** Predicate for guards. */
|
|
33
|
+
function canTransition(from, verb) {
|
|
34
|
+
return nextState(from, verb) !== null;
|
|
35
|
+
}
|
|
36
|
+
/** All verbs valid from the given state — useful for UI affordance. */
|
|
37
|
+
function allowedVerbs(from) {
|
|
38
|
+
return Object.keys(TRANSITIONS[from]);
|
|
39
|
+
}
|
|
40
|
+
//#endregion
|
|
41
|
+
export { SHIFT_VERBS, allowedVerbs, canTransition, nextState };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { CashMovementReasonCode } from "../enums/cash-movement-reason.enum.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/domain/value-objects/cash-movement.vo.d.ts
|
|
4
|
+
interface CashMovement {
|
|
5
|
+
/** in: money added to drawer (top-up, return); out: money removed (drop, withdrawal). */
|
|
6
|
+
type: 'in' | 'out';
|
|
7
|
+
/** Always positive minor units (e.g. paisa). Direction lives in `type`. */
|
|
8
|
+
amount: number;
|
|
9
|
+
reasonCode: CashMovementReasonCode;
|
|
10
|
+
/** Free-form note. Required when reasonCode === 'other'. Host enforces. */
|
|
11
|
+
note: string;
|
|
12
|
+
/** Actor who performed the movement. */
|
|
13
|
+
cashierId: string;
|
|
14
|
+
cashierName: string;
|
|
15
|
+
/** When the movement happened — set at construction. */
|
|
16
|
+
timestamp: Date;
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
export { CashMovement };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ShiftPaymentMethod } from "../enums/payment-method.enum.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/domain/value-objects/payment-breakdown.vo.d.ts
|
|
4
|
+
interface PaymentBreakdown {
|
|
5
|
+
method: ShiftPaymentMethod;
|
|
6
|
+
openingAmount: number;
|
|
7
|
+
salesAmount: number;
|
|
8
|
+
/** Tax portion of salesAmount (subset, not additional). 0 when no tax applied. */
|
|
9
|
+
taxAmount: number;
|
|
10
|
+
refundAmount: number;
|
|
11
|
+
/** Tax portion of refundAmount (subset, not additional). 0 when no tax. */
|
|
12
|
+
refundTaxAmount: number;
|
|
13
|
+
cashInAmount: number;
|
|
14
|
+
cashOutAmount: number;
|
|
15
|
+
expectedAmount: number | null;
|
|
16
|
+
countedAmount: number | null;
|
|
17
|
+
difference: number | null;
|
|
18
|
+
}
|
|
19
|
+
/** Pure compute — `expectedAmount` from inputs. Lives in VO so tests don't need a DB. */
|
|
20
|
+
declare function computeExpected(b: Pick<PaymentBreakdown, 'openingAmount' | 'salesAmount' | 'refundAmount' | 'cashInAmount' | 'cashOutAmount'>): number;
|
|
21
|
+
/** Pure compute — variance from a counted reading. Returns null if counted is null. */
|
|
22
|
+
declare function computeDifference(expected: number, counted: number | null): number | null;
|
|
23
|
+
//#endregion
|
|
24
|
+
export { PaymentBreakdown, computeDifference, computeExpected };
|