@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
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
import { computeExpected } from "../domain/value-objects/payment-breakdown.vo.mjs";
|
|
2
|
+
import { isCashRow } from "../domain/shift/shift-decisions.mjs";
|
|
3
|
+
//#region src/commands/pos-commands.ts
|
|
4
|
+
const POS_COMMAND = {
|
|
5
|
+
shiftOpen: "pos.shift.open",
|
|
6
|
+
sale: "pos.sale",
|
|
7
|
+
refund: "pos.refund",
|
|
8
|
+
shiftClose: "pos.shift.close",
|
|
9
|
+
customerUpsert: "pos.customer.upsert"
|
|
10
|
+
};
|
|
11
|
+
const docKey = (collection, id) => `${collection}/${id}`;
|
|
12
|
+
/** A server refusal. `rejection: true` + `code` is arc-sync's structural rejection contract. */
|
|
13
|
+
var PosCommandRejection = class extends Error {
|
|
14
|
+
rejection = true;
|
|
15
|
+
code;
|
|
16
|
+
constructor(code, message = code) {
|
|
17
|
+
super(message);
|
|
18
|
+
this.name = "PosCommandRejection";
|
|
19
|
+
this.code = code;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
const phoneOf = (customer) => customer.contact?.phone?.trim() || void 0;
|
|
23
|
+
/** The stock with `quantity` taken off — from nothing when the product has no stock document yet. */
|
|
24
|
+
function takeFromStock(current, productId, variantSku, quantity) {
|
|
25
|
+
const stock = current ?? {
|
|
26
|
+
_id: productId,
|
|
27
|
+
productId,
|
|
28
|
+
quantity: 0,
|
|
29
|
+
variants: []
|
|
30
|
+
};
|
|
31
|
+
const variants = stock.variants ?? [];
|
|
32
|
+
const nextVariants = variantSku ? variants.some((v) => v.sku === variantSku) ? variants.map((v) => v.sku === variantSku ? {
|
|
33
|
+
...v,
|
|
34
|
+
quantity: v.quantity - quantity
|
|
35
|
+
} : v) : [...variants, {
|
|
36
|
+
sku: variantSku,
|
|
37
|
+
quantity: -quantity
|
|
38
|
+
}] : variants;
|
|
39
|
+
const nextQuantity = stock.quantity - quantity;
|
|
40
|
+
return {
|
|
41
|
+
...stock,
|
|
42
|
+
quantity: nextQuantity,
|
|
43
|
+
inStock: nextQuantity > 0,
|
|
44
|
+
variants: nextVariants
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/** The stock documents a sale's lines leave behind, from the current ones. */
|
|
48
|
+
function takenStock(sale, current) {
|
|
49
|
+
const stock = new Map(current);
|
|
50
|
+
for (const line of sale.stock) stock.set(line.productId, takeFromStock(stock.get(line.productId), line.productId, line.variantSku, line.quantity));
|
|
51
|
+
return [...new Set(sale.stock.map((line) => line.productId))].map((id) => ({
|
|
52
|
+
collection: "stock",
|
|
53
|
+
id,
|
|
54
|
+
doc: {
|
|
55
|
+
...stock.get(id),
|
|
56
|
+
updatedAt: sale.at
|
|
57
|
+
}
|
|
58
|
+
}));
|
|
59
|
+
}
|
|
60
|
+
/** The shift with a sale's takings added: count, total, and each method's sales. */
|
|
61
|
+
function addTakings(shift, takings, at) {
|
|
62
|
+
const rows = new Map(shift.paymentBreakdown.map((row) => [row.method, { ...row }]));
|
|
63
|
+
for (const { method, methodKind, amountMinor } of takings.byMethod) {
|
|
64
|
+
const row = rows.get(method) ?? {
|
|
65
|
+
method,
|
|
66
|
+
methodKind,
|
|
67
|
+
openingAmount: 0,
|
|
68
|
+
salesAmount: 0,
|
|
69
|
+
refundAmount: 0,
|
|
70
|
+
cashInAmount: 0,
|
|
71
|
+
cashOutAmount: 0,
|
|
72
|
+
expectedAmount: null,
|
|
73
|
+
countedAmount: null,
|
|
74
|
+
difference: null
|
|
75
|
+
};
|
|
76
|
+
row.salesAmount += amountMinor;
|
|
77
|
+
rows.set(method, row);
|
|
78
|
+
}
|
|
79
|
+
const paymentBreakdown = [...rows.values()];
|
|
80
|
+
const cash = paymentBreakdown.find(isCashRow);
|
|
81
|
+
return {
|
|
82
|
+
...shift,
|
|
83
|
+
paymentBreakdown,
|
|
84
|
+
salesCount: shift.salesCount + 1,
|
|
85
|
+
salesTotal: shift.salesTotal + takings.totalMinor,
|
|
86
|
+
expectedCash: cash ? computeExpected(cash) : shift.openingCash,
|
|
87
|
+
updatedAt: at
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
/** What an order still has to refund: charged less refunded. */
|
|
91
|
+
const refundableOf = (order) => Math.max(0, order.paymentState.totalCharged.amount - order.paymentState.totalRefunded.amount);
|
|
92
|
+
/** The order with a refund recorded as the server projects one; fully refunded ⇒ `refunded`. */
|
|
93
|
+
function addRefund(order, refund) {
|
|
94
|
+
const { currency } = order.paymentState.totalRefunded;
|
|
95
|
+
const totalRefunded = order.paymentState.totalRefunded.amount + refund.amountMinor;
|
|
96
|
+
const full = totalRefunded >= order.totals.grandTotal.amount && order.totals.grandTotal.amount > 0;
|
|
97
|
+
return {
|
|
98
|
+
...order,
|
|
99
|
+
status: full ? "refunded" : order.status,
|
|
100
|
+
paymentState: {
|
|
101
|
+
...order.paymentState,
|
|
102
|
+
totalRefunded: {
|
|
103
|
+
amount: totalRefunded,
|
|
104
|
+
currency
|
|
105
|
+
},
|
|
106
|
+
transactionRefs: [...order.paymentState.transactionRefs, ...refund.byMethod.map((leg, i) => ({
|
|
107
|
+
transactionId: `refund-${refund.refundId}${refund.byMethod.length > 1 ? `-${i}` : ""}`,
|
|
108
|
+
type: "refund",
|
|
109
|
+
amount: {
|
|
110
|
+
amount: leg.amountMinor,
|
|
111
|
+
currency
|
|
112
|
+
},
|
|
113
|
+
status: "verified",
|
|
114
|
+
gateway: leg.method,
|
|
115
|
+
methodKind: leg.methodKind,
|
|
116
|
+
createdAt: refund.at,
|
|
117
|
+
metadata: {
|
|
118
|
+
source: "pos",
|
|
119
|
+
operationId: refund.refundId
|
|
120
|
+
}
|
|
121
|
+
}))]
|
|
122
|
+
},
|
|
123
|
+
metadata: {
|
|
124
|
+
...order.metadata,
|
|
125
|
+
refundedAt: refund.at,
|
|
126
|
+
refundedAmount: totalRefunded,
|
|
127
|
+
refundReason: refund.reason,
|
|
128
|
+
refundIsPartial: !full
|
|
129
|
+
},
|
|
130
|
+
updatedAt: refund.at
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
/** The shift with a refund paid out of its drawer: each method's refunds, count and total. */
|
|
134
|
+
function addRefundToShift(shift, refund) {
|
|
135
|
+
const rows = new Map(shift.paymentBreakdown.map((row) => [row.method, { ...row }]));
|
|
136
|
+
for (const { method, methodKind, amountMinor } of refund.byMethod) {
|
|
137
|
+
const row = rows.get(method) ?? {
|
|
138
|
+
method,
|
|
139
|
+
methodKind,
|
|
140
|
+
openingAmount: 0,
|
|
141
|
+
salesAmount: 0,
|
|
142
|
+
refundAmount: 0,
|
|
143
|
+
cashInAmount: 0,
|
|
144
|
+
cashOutAmount: 0,
|
|
145
|
+
expectedAmount: null,
|
|
146
|
+
countedAmount: null,
|
|
147
|
+
difference: null
|
|
148
|
+
};
|
|
149
|
+
row.refundAmount += amountMinor;
|
|
150
|
+
rows.set(method, row);
|
|
151
|
+
}
|
|
152
|
+
const paymentBreakdown = [...rows.values()];
|
|
153
|
+
const cash = paymentBreakdown.find(isCashRow);
|
|
154
|
+
return {
|
|
155
|
+
...shift,
|
|
156
|
+
paymentBreakdown,
|
|
157
|
+
refundCount: (shift.refundCount ?? 0) + 1,
|
|
158
|
+
refundTotal: (shift.refundTotal ?? 0) + refund.amountMinor,
|
|
159
|
+
expectedCash: cash ? computeExpected(cash) : shift.expectedCash,
|
|
160
|
+
updatedAt: refund.at
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Every write a recorded refund makes. The drawer is charged only while the sale's shift is open —
|
|
165
|
+
* a closed shift's count already stands, so a later refund shows as the open drawer's difference.
|
|
166
|
+
*/
|
|
167
|
+
function recordRefundWrites(refund, current) {
|
|
168
|
+
return [{
|
|
169
|
+
collection: "orders",
|
|
170
|
+
id: refund.orderId,
|
|
171
|
+
doc: addRefund(current.order, refund)
|
|
172
|
+
}, ...current.shift?.state === "open" ? [{
|
|
173
|
+
collection: "shifts",
|
|
174
|
+
id: current.shift._id,
|
|
175
|
+
doc: addRefundToShift(current.shift, refund)
|
|
176
|
+
}] : []];
|
|
177
|
+
}
|
|
178
|
+
/** Every write a recorded sale makes, from the current documents it touches. */
|
|
179
|
+
function recordSaleWrites(sale, current, options = {}) {
|
|
180
|
+
const order = options.unresolved ? {
|
|
181
|
+
...sale.order,
|
|
182
|
+
metadata: {
|
|
183
|
+
...sale.order.metadata,
|
|
184
|
+
unresolved: true
|
|
185
|
+
}
|
|
186
|
+
} : sale.order;
|
|
187
|
+
return [
|
|
188
|
+
{
|
|
189
|
+
collection: "orders",
|
|
190
|
+
id: sale.order._id,
|
|
191
|
+
doc: order
|
|
192
|
+
},
|
|
193
|
+
...takenStock(sale, current.stock),
|
|
194
|
+
{
|
|
195
|
+
collection: "shifts",
|
|
196
|
+
id: current.shift._id,
|
|
197
|
+
doc: addTakings(current.shift, sale.takings, sale.at)
|
|
198
|
+
}
|
|
199
|
+
];
|
|
200
|
+
}
|
|
201
|
+
const read = (state, collection, id) => state.get(docKey(collection, id));
|
|
202
|
+
function stockFor(state, sale) {
|
|
203
|
+
const stock = /* @__PURE__ */ new Map();
|
|
204
|
+
for (const line of sale.stock) {
|
|
205
|
+
const doc = read(state, "stock", line.productId);
|
|
206
|
+
if (doc) stock.set(line.productId, doc);
|
|
207
|
+
}
|
|
208
|
+
return stock;
|
|
209
|
+
}
|
|
210
|
+
function loadForSale(state, sale) {
|
|
211
|
+
return {
|
|
212
|
+
shift: read(state, "shifts", sale.order.metadata.shiftId),
|
|
213
|
+
stock: stockFor(state, sale)
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
/** Where a shift is open on a register, if anywhere — a server must not open a second one. */
|
|
217
|
+
function openShiftOn(state, registerId) {
|
|
218
|
+
for (const [key, doc] of state) {
|
|
219
|
+
if (!key.startsWith("shifts/")) continue;
|
|
220
|
+
const shift = doc;
|
|
221
|
+
if (shift.registerId === registerId && shift.state === "open") return shift;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
const posCommandPack = {
|
|
225
|
+
[POS_COMMAND.customerUpsert]: {
|
|
226
|
+
/** A refused customer is administrative — it stops showing; sales keep their customer snapshot. */
|
|
227
|
+
whenRefused: "suppressed",
|
|
228
|
+
apply: (_state, command) => [{
|
|
229
|
+
collection: "customers",
|
|
230
|
+
id: command.payload.customer._id,
|
|
231
|
+
doc: command.payload.customer
|
|
232
|
+
}],
|
|
233
|
+
handle(state, command) {
|
|
234
|
+
const { customer } = command.payload;
|
|
235
|
+
const phone = phoneOf(customer);
|
|
236
|
+
for (const [key, doc] of state) {
|
|
237
|
+
if (!key.startsWith("customers/") || doc._id === customer._id) continue;
|
|
238
|
+
if (phone && phoneOf(doc) === phone) return [];
|
|
239
|
+
}
|
|
240
|
+
return [{
|
|
241
|
+
collection: "customers",
|
|
242
|
+
id: customer._id,
|
|
243
|
+
doc: customer
|
|
244
|
+
}];
|
|
245
|
+
}
|
|
246
|
+
},
|
|
247
|
+
[POS_COMMAND.shiftOpen]: {
|
|
248
|
+
/** A refused open is an administrative change — it stops showing; the server's shift stands. */
|
|
249
|
+
whenRefused: "suppressed",
|
|
250
|
+
apply: (_state, command) => [{
|
|
251
|
+
collection: "shifts",
|
|
252
|
+
id: command.payload.shift._id,
|
|
253
|
+
doc: command.payload.shift
|
|
254
|
+
}],
|
|
255
|
+
handle(state, command) {
|
|
256
|
+
const existing = openShiftOn(state, command.payload.shift.registerId);
|
|
257
|
+
if (existing && existing._id !== command.payload.shift._id) throw new PosCommandRejection("pos.shift.already_open");
|
|
258
|
+
return [{
|
|
259
|
+
collection: "shifts",
|
|
260
|
+
id: command.payload.shift._id,
|
|
261
|
+
doc: command.payload.shift
|
|
262
|
+
}];
|
|
263
|
+
}
|
|
264
|
+
},
|
|
265
|
+
[POS_COMMAND.sale]: {
|
|
266
|
+
/** A refused sale is a physical fact — the goods and money moved; it stays visible, unresolved. */
|
|
267
|
+
whenRefused: "unresolved",
|
|
268
|
+
apply(state, command, unresolved) {
|
|
269
|
+
const { shift, stock } = loadForSale(state, command.payload);
|
|
270
|
+
if (!shift) return recordSaleWrites(command.payload, {
|
|
271
|
+
shift: placeholderShift(command.payload),
|
|
272
|
+
stock
|
|
273
|
+
}, { unresolved }).filter((w) => w.collection !== "shifts");
|
|
274
|
+
return recordSaleWrites(command.payload, {
|
|
275
|
+
shift,
|
|
276
|
+
stock
|
|
277
|
+
}, { unresolved });
|
|
278
|
+
},
|
|
279
|
+
/**
|
|
280
|
+
* After the server records the sale, its stock still leaves the shelf HERE until the server's own
|
|
281
|
+
* count includes it: the order's `metadata.stockIssue` reaches `posted` only after the branch's
|
|
282
|
+
* stock documents were republished. `failed` keeps the subtraction — the shelf really is emptier.
|
|
283
|
+
*/
|
|
284
|
+
pending(state, command) {
|
|
285
|
+
if (read(state, "orders", command.payload.order._id)?.metadata?.stockIssue === "posted") return [];
|
|
286
|
+
return takenStock(command.payload, stockFor(state, command.payload));
|
|
287
|
+
},
|
|
288
|
+
handle(state, command) {
|
|
289
|
+
const { shift, stock } = loadForSale(state, command.payload);
|
|
290
|
+
if (!shift) throw new PosCommandRejection("pos.shift.unknown");
|
|
291
|
+
if (shift.state !== "open") throw new PosCommandRejection("pos.shift.not_open");
|
|
292
|
+
return recordSaleWrites(command.payload, {
|
|
293
|
+
shift,
|
|
294
|
+
stock
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
},
|
|
298
|
+
[POS_COMMAND.refund]: {
|
|
299
|
+
/** A refused refund is a physical fact — the money was handed back; it stays visible, unresolved. */
|
|
300
|
+
whenRefused: "unresolved",
|
|
301
|
+
apply(state, command) {
|
|
302
|
+
const order = read(state, "orders", command.payload.orderId);
|
|
303
|
+
if (!order) return [];
|
|
304
|
+
return recordRefundWrites(command.payload, {
|
|
305
|
+
order,
|
|
306
|
+
shift: read(state, "shifts", command.payload.shiftId)
|
|
307
|
+
});
|
|
308
|
+
},
|
|
309
|
+
handle(state, command) {
|
|
310
|
+
const order = read(state, "orders", command.payload.orderId);
|
|
311
|
+
if (!order) throw new PosCommandRejection("pos.refund.order_unknown");
|
|
312
|
+
if (!(command.payload.amountMinor > 0) || command.payload.amountMinor > refundableOf(order)) throw new PosCommandRejection("pos.refund.exceeds_refundable");
|
|
313
|
+
return recordRefundWrites(command.payload, {
|
|
314
|
+
order,
|
|
315
|
+
shift: read(state, "shifts", command.payload.shiftId)
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
},
|
|
319
|
+
[POS_COMMAND.shiftClose]: {
|
|
320
|
+
/** A refused close is a physical fact — the drawer was counted; it stays visible, unresolved. */
|
|
321
|
+
whenRefused: "unresolved",
|
|
322
|
+
apply: (_state, command, unresolved) => [{
|
|
323
|
+
collection: "shifts",
|
|
324
|
+
id: command.payload.shift._id,
|
|
325
|
+
doc: unresolved ? {
|
|
326
|
+
...command.payload.shift,
|
|
327
|
+
unresolved: true
|
|
328
|
+
} : command.payload.shift
|
|
329
|
+
}],
|
|
330
|
+
handle(state, command) {
|
|
331
|
+
const closed = command.payload.shift;
|
|
332
|
+
const current = read(state, "shifts", closed._id);
|
|
333
|
+
if (!current) throw new PosCommandRejection("pos.shift.unknown");
|
|
334
|
+
if (current.state !== "open") throw new PosCommandRejection("pos.shift.not_open");
|
|
335
|
+
if (current.salesCount !== closed.salesCount || current.salesTotal !== closed.salesTotal) throw new PosCommandRejection("pos.shift.close_basis_changed");
|
|
336
|
+
return [{
|
|
337
|
+
collection: "shifts",
|
|
338
|
+
id: closed._id,
|
|
339
|
+
doc: closed
|
|
340
|
+
}];
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
function placeholderShift(sale) {
|
|
345
|
+
return {
|
|
346
|
+
_id: sale.order.metadata.shiftId,
|
|
347
|
+
registerId: sale.order.metadata.registerId,
|
|
348
|
+
state: "unknown",
|
|
349
|
+
openingCash: 0,
|
|
350
|
+
salesCount: 0,
|
|
351
|
+
salesTotal: 0,
|
|
352
|
+
expectedCash: null,
|
|
353
|
+
paymentBreakdown: []
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
//#endregion
|
|
357
|
+
export { POS_COMMAND, PosCommandRejection, addRefund, addRefundToShift, addTakings, docKey, posCommandPack, recordRefundWrites, recordSaleWrites, refundableOf, takeFromStock };
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* every cash-in / cash-out call. Hosts can scope the allowed subset per
|
|
7
7
|
* branch via `policy.allowedReasonCodes`.
|
|
8
8
|
*/
|
|
9
|
-
declare const CASH_MOVEMENT_REASON_CODES: readonly [
|
|
9
|
+
export declare const CASH_MOVEMENT_REASON_CODES: readonly [
|
|
10
10
|
/** Money moved from drawer to safe mid-shift. */
|
|
11
11
|
'safe_drop',
|
|
12
12
|
/** Operational expense paid from drawer (utility bill, ad-hoc supply). */
|
|
@@ -21,6 +21,5 @@ declare const CASH_MOVEMENT_REASON_CODES: readonly [
|
|
|
21
21
|
'till_top_up',
|
|
22
22
|
/** Free-form; note required. */
|
|
23
23
|
'other'];
|
|
24
|
-
type CashMovementReasonCode = (typeof CASH_MOVEMENT_REASON_CODES)[number];
|
|
25
|
-
//#endregion
|
|
26
|
-
export { CASH_MOVEMENT_REASON_CODES, CashMovementReasonCode };
|
|
24
|
+
export type CashMovementReasonCode = (typeof CASH_MOVEMENT_REASON_CODES)[number];
|
|
25
|
+
//#endregion
|
|
@@ -7,7 +7,6 @@
|
|
|
7
7
|
* manager — a manager closed (typically post blind_close reconcile)
|
|
8
8
|
* system_auto — EOD cron / forceClose (orphaned_closed)
|
|
9
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 };
|
|
10
|
+
export declare const CLOSED_BY_VALUES: readonly ['cashier', 'manager', 'system_auto'];
|
|
11
|
+
export type ClosedBy = (typeof CLOSED_BY_VALUES)[number];
|
|
12
|
+
//#endregion
|
|
@@ -11,18 +11,17 @@ import { PaymentMethodKind } from "@classytic/primitives/payment-method-kind";
|
|
|
11
11
|
* `SHIFT_PAYMENT_METHODS` is only the out-of-the-box DEFAULT policy + the seed
|
|
12
12
|
* for kind resolution — never a hard constraint on what a host may accept.
|
|
13
13
|
*/
|
|
14
|
-
declare const SHIFT_PAYMENT_METHODS: readonly ['cash', 'card', '
|
|
14
|
+
export declare const SHIFT_PAYMENT_METHODS: readonly ['cash', 'card', 'mobile_money', 'bank_transfer'];
|
|
15
15
|
/** A payment method CODE (open string). Baseline defaults in {@link SHIFT_PAYMENT_METHODS}. */
|
|
16
|
-
type ShiftPaymentMethod = string;
|
|
16
|
+
export type ShiftPaymentMethod = string;
|
|
17
17
|
/** Default baseline code → universal kind. */
|
|
18
|
-
declare const DEFAULT_METHOD_KINDS: Readonly<Record<string, PaymentMethodKind>>;
|
|
18
|
+
export declare const DEFAULT_METHOD_KINDS: Readonly<Record<string, PaymentMethodKind>>;
|
|
19
19
|
/** Every valid kind value — for the mongoose enum on `paymentBreakdown.methodKind`. */
|
|
20
|
-
declare const PAYMENT_METHOD_KIND_VALUES: readonly PaymentMethodKind[];
|
|
20
|
+
export declare const PAYMENT_METHOD_KIND_VALUES: readonly PaymentMethodKind[];
|
|
21
21
|
/**
|
|
22
22
|
* Resolve a code's universal kind: an explicit override wins, else the default
|
|
23
23
|
* baseline map, else `other` (the always-valid fallback). Hosts pass explicit
|
|
24
24
|
* kinds for custom codes via `ShiftPolicy.paymentMethodKinds`.
|
|
25
25
|
*/
|
|
26
|
-
declare function resolveMethodKind(code: string, explicit?: PaymentMethodKind): PaymentMethodKind;
|
|
27
|
-
//#endregion
|
|
28
|
-
export { DEFAULT_METHOD_KINDS, PAYMENT_METHOD_KIND_VALUES, SHIFT_PAYMENT_METHODS, ShiftPaymentMethod, resolveMethodKind };
|
|
26
|
+
export declare function resolveMethodKind(code: string, explicit?: PaymentMethodKind): PaymentMethodKind;
|
|
27
|
+
//#endregion
|
|
@@ -14,14 +14,14 @@ import { PAYMENT_METHOD_KIND } from "@classytic/primitives/payment-method-kind";
|
|
|
14
14
|
const SHIFT_PAYMENT_METHODS = [
|
|
15
15
|
"cash",
|
|
16
16
|
"card",
|
|
17
|
-
"
|
|
17
|
+
"mobile_money",
|
|
18
18
|
"bank_transfer"
|
|
19
19
|
];
|
|
20
20
|
/** Default baseline code → universal kind. */
|
|
21
21
|
const DEFAULT_METHOD_KINDS = {
|
|
22
22
|
cash: PAYMENT_METHOD_KIND.CASH,
|
|
23
23
|
card: PAYMENT_METHOD_KIND.CARD,
|
|
24
|
-
|
|
24
|
+
mobile_money: PAYMENT_METHOD_KIND.MOBILE_MONEY,
|
|
25
25
|
bank_transfer: PAYMENT_METHOD_KIND.BANK_TRANSFER
|
|
26
26
|
};
|
|
27
27
|
/** Every valid kind value — for the mongoose enum on `paymentBreakdown.methodKind`. */
|
|
@@ -14,13 +14,12 @@
|
|
|
14
14
|
* orphaned_closed — auto-closed by EOD cron / forceClose; counts default to
|
|
15
15
|
* expected; flagged for manager review
|
|
16
16
|
*/
|
|
17
|
-
declare const SHIFT_STATES: readonly ['open', 'paused', 'blind_closed', 'closed', 'orphaned_closed'];
|
|
18
|
-
type ShiftState = (typeof SHIFT_STATES)[number];
|
|
17
|
+
export declare const SHIFT_STATES: readonly ['open', 'paused', 'blind_closed', 'closed', 'orphaned_closed'];
|
|
18
|
+
export type ShiftState = (typeof SHIFT_STATES)[number];
|
|
19
19
|
/** States that block opening a new shift for the same scope. */
|
|
20
|
-
declare const ACTIVE_SHIFT_STATES: readonly ShiftState[];
|
|
20
|
+
export declare const ACTIVE_SHIFT_STATES: readonly ShiftState[];
|
|
21
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 };
|
|
22
|
+
export declare const FINAL_SHIFT_STATES: readonly ShiftState[];
|
|
23
|
+
export declare function isActiveShiftState(state: ShiftState): boolean;
|
|
24
|
+
export declare function isFinalShiftState(state: ShiftState): boolean;
|
|
25
|
+
//#endregion
|
|
@@ -2,15 +2,10 @@ import { HttpError } from "@classytic/repo-core/errors";
|
|
|
2
2
|
//#region src/domain/errors/pos.errors.d.ts
|
|
3
3
|
/**
|
|
4
4
|
* `status` makes every POS throwable satisfy `HttpError` from
|
|
5
|
-
* `@classytic/repo-core/errors
|
|
6
|
-
* the correct status natively — no per-host `errorMapper`. Without it every
|
|
7
|
-
* POS error reached the client as `arc.internal_error` / 500, including
|
|
8
|
-
* `ShiftNotFoundError` (404) and `ActiveShiftAlreadyOpenError` (409).
|
|
9
|
-
*
|
|
10
|
-
* Defaults to 422; subclasses override with `readonly status = N` (a subclass
|
|
5
|
+
* `@classytic/repo-core/errors`. Defaults to 422; subclasses override with `readonly status = N` (a subclass
|
|
11
6
|
* field initializer runs after this constructor, so it wins).
|
|
12
7
|
*/
|
|
13
|
-
declare class PosError extends Error implements HttpError {
|
|
8
|
+
export declare class PosError extends Error implements HttpError {
|
|
14
9
|
readonly code: string;
|
|
15
10
|
readonly status: number;
|
|
16
11
|
constructor(code: string, message: string);
|
|
@@ -20,29 +15,47 @@ declare class PosError extends Error implements HttpError {
|
|
|
20
15
|
* publishing mid-transaction leaks ghost events on rollback (§P8.1).
|
|
21
16
|
* Session + outbox is legal durable-relay-only mode.
|
|
22
17
|
*/
|
|
23
|
-
declare class UnmanagedSessionError extends PosError {
|
|
18
|
+
export declare class UnmanagedSessionError extends PosError {
|
|
24
19
|
readonly status = 500;
|
|
25
20
|
constructor();
|
|
26
21
|
}
|
|
27
|
-
declare class ShiftNotFoundError extends PosError {
|
|
22
|
+
export declare class ShiftNotFoundError extends PosError {
|
|
28
23
|
readonly status = 404;
|
|
29
24
|
constructor(id: string);
|
|
30
25
|
}
|
|
31
|
-
declare class IllegalShiftTransitionError extends PosError {
|
|
26
|
+
export declare class IllegalShiftTransitionError extends PosError {
|
|
32
27
|
readonly status = 422;
|
|
33
28
|
constructor(from: string, verb: string);
|
|
34
29
|
}
|
|
35
|
-
declare class ActiveShiftAlreadyOpenError extends PosError {
|
|
30
|
+
export declare class ActiveShiftAlreadyOpenError extends PosError {
|
|
36
31
|
readonly status = 409;
|
|
37
32
|
constructor(scope: string);
|
|
38
33
|
}
|
|
39
|
-
|
|
34
|
+
/** One open register, as offered to a till that must say which one it is. */
|
|
35
|
+
export interface ActiveRegister {
|
|
36
|
+
registerId: string | null;
|
|
37
|
+
shiftId: string;
|
|
38
|
+
state: string;
|
|
39
|
+
openedAt?: Date | undefined;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Several registers are active at the branch and the caller named none — picking one would
|
|
43
|
+
* ring the sale into another cashier's drawer. `meta.registers` lists the choices.
|
|
44
|
+
*/
|
|
45
|
+
export declare class RegisterRequiredError extends PosError {
|
|
46
|
+
readonly status = 409;
|
|
47
|
+
readonly meta: {
|
|
48
|
+
registers: ActiveRegister[];
|
|
49
|
+
};
|
|
50
|
+
constructor(registers: ActiveRegister[]);
|
|
51
|
+
}
|
|
52
|
+
export declare class VarianceExceededError extends PosError {
|
|
40
53
|
readonly status = 409;
|
|
41
54
|
readonly difference: number;
|
|
42
55
|
readonly expected: number;
|
|
43
56
|
constructor(difference: number, expected: number);
|
|
44
57
|
}
|
|
45
|
-
declare class ShiftFinalizedError extends PosError {
|
|
58
|
+
export declare class ShiftFinalizedError extends PosError {
|
|
46
59
|
readonly status = 409;
|
|
47
60
|
constructor(id: string);
|
|
48
61
|
}
|
|
@@ -52,7 +65,7 @@ declare class ShiftFinalizedError extends PosError {
|
|
|
52
65
|
* corrections and remain allowed). Distinct from {@link ShiftFinalizedError}:
|
|
53
66
|
* the shift is recoverable — resume it and retry.
|
|
54
67
|
*/
|
|
55
|
-
declare class ShiftPausedError extends PosError {
|
|
68
|
+
export declare class ShiftPausedError extends PosError {
|
|
56
69
|
readonly status = 409;
|
|
57
70
|
constructor(id: string);
|
|
58
71
|
}
|
|
@@ -64,29 +77,64 @@ declare class ShiftPausedError extends PosError {
|
|
|
64
77
|
* pass `countedByMethod` (cash always, plus any non-cash tender being
|
|
65
78
|
* reconciled) or blind-close first.
|
|
66
79
|
*/
|
|
67
|
-
declare class ShiftCountsRequiredError extends PosError {
|
|
80
|
+
export declare class ShiftCountsRequiredError extends PosError {
|
|
68
81
|
readonly status = 422;
|
|
69
82
|
constructor(id: string);
|
|
70
83
|
}
|
|
71
|
-
|
|
84
|
+
/** A cash movement under a key already booked for a DIFFERENT movement — two commands, one key. */
|
|
85
|
+
export declare class CashMovementKeyReusedError extends PosError {
|
|
86
|
+
readonly status = 409;
|
|
87
|
+
constructor(movementKey: string);
|
|
88
|
+
}
|
|
89
|
+
export declare class CashMovementInvalidError extends PosError {
|
|
72
90
|
readonly status = 422;
|
|
73
91
|
constructor(reason: string);
|
|
74
92
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
+
/**
|
|
94
|
+
* A cash-out the drawer cannot cover, refused by the write's own filter — so two
|
|
95
|
+
* concurrent cash-outs that each fit alone cannot both land.
|
|
96
|
+
*/
|
|
97
|
+
export declare class CashOutExceedsDrawerError extends PosError {
|
|
98
|
+
readonly status = 409;
|
|
99
|
+
readonly amount: number;
|
|
100
|
+
readonly drawer: number;
|
|
101
|
+
constructor(amount: number, drawer: number);
|
|
102
|
+
}
|
|
103
|
+
/** A branch policy that allows no cash-movement reason code — `[]` is refused; any-code is `'*'`. */
|
|
104
|
+
export declare class ReasonCodePolicyInvalidError extends PosError {
|
|
105
|
+
readonly status = 409;
|
|
106
|
+
constructor();
|
|
107
|
+
}
|
|
108
|
+
/** The tender does not settle the sale, and the deployment's `TenderPolicy` does not permit that shape. */
|
|
109
|
+
export declare class TenderMismatchError extends PosError {
|
|
110
|
+
readonly status = 400;
|
|
111
|
+
readonly dueMinor: number;
|
|
112
|
+
readonly tenderedMinor: number;
|
|
113
|
+
readonly details: Record<string, unknown>;
|
|
114
|
+
constructor(dueMinor: number, tenderedMinor: number, detail: string);
|
|
115
|
+
}
|
|
116
|
+
/** A line price differs from the catalog without authority (403) or without a reason (400). */
|
|
117
|
+
export declare class PriceOverrideRefusedError extends PosError {
|
|
118
|
+
readonly status: number;
|
|
119
|
+
constructor(detail: string, status?: number);
|
|
120
|
+
}
|
|
121
|
+
/** Which requirement an order-level discount failed — each is fixed by a different person. */
|
|
122
|
+
export type OrderDiscountRefusal = 'authority' | 'reason' | 'limit' | 'full-comp';
|
|
123
|
+
/** An order-level discount was refused: 400 for a missing reason, 403 for every policy denial. */
|
|
124
|
+
export declare class OrderDiscountRefusedError extends PosError {
|
|
125
|
+
readonly status: number;
|
|
126
|
+
readonly missing: OrderDiscountRefusal;
|
|
127
|
+
readonly details: Record<string, unknown>;
|
|
128
|
+
constructor(missing: OrderDiscountRefusal, detail: string, details?: Record<string, unknown>);
|
|
129
|
+
}
|
|
130
|
+
/** Why a tender method was refused. */
|
|
131
|
+
export type TenderMethodRefusal = 'not-accepted' | 'reference-required' | 'zero-amount' | 'change-not-possible';
|
|
132
|
+
/** A tender row was refused by the method policy. 400 — every case is one the till can correct. */
|
|
133
|
+
export declare class TenderMethodRefusedError extends PosError {
|
|
134
|
+
readonly status = 400;
|
|
135
|
+
readonly method: string;
|
|
136
|
+
readonly reason: TenderMethodRefusal;
|
|
137
|
+
readonly details: Record<string, unknown>;
|
|
138
|
+
constructor(method: string, reason: TenderMethodRefusal, details?: Record<string, unknown>);
|
|
139
|
+
}
|
|
140
|
+
//#endregion
|