@tallyui/core 2.0.0 → 3.0.0-next.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 (43) hide show
  1. package/dist/chunk-C4PWPOKO.js +18 -0
  2. package/dist/commands-BW4YYgRG.d.ts +306 -0
  3. package/dist/index.d.ts +226 -130
  4. package/dist/index.js +193 -19
  5. package/dist/rxdb/index.d.ts +5 -9
  6. package/dist/server/index.d.ts +213 -0
  7. package/dist/server/index.js +591 -0
  8. package/package.json +8 -2
  9. package/src/commands.ts +1 -1
  10. package/src/context/connector-context.tsx +30 -1
  11. package/src/index.ts +17 -5
  12. package/src/known-warnings.ts +77 -0
  13. package/src/replication/combine.ts +7 -3
  14. package/src/replication/reconcile-feed.ts +57 -20
  15. package/src/rxdb/read-fresh.ts +3 -7
  16. package/src/rxdb/watch-fresh.ts +2 -2
  17. package/src/rxdb-remote-version-mismatch.ts +30 -0
  18. package/src/server/__fixtures__/fingerprint-vectors.json +139 -0
  19. package/src/server/__fixtures__/order-create-v3.json +97 -0
  20. package/src/server/batch-outcome.ts +19 -0
  21. package/src/server/batch.ts +112 -0
  22. package/src/server/command-result.ts +138 -0
  23. package/src/server/fingerprint.ts +18 -0
  24. package/src/server/fiscal-figures.ts +84 -0
  25. package/src/server/index.ts +20 -0
  26. package/src/server/money.ts +34 -0
  27. package/src/server/order-payload-shape.ts +112 -0
  28. package/src/server/register-figures.ts +32 -0
  29. package/src/server/register-outcome.ts +12 -0
  30. package/src/server/register-payload-shape.ts +86 -0
  31. package/src/server/rejection-codes.ts +49 -0
  32. package/src/server/total-warnings.ts +10 -0
  33. package/src/server/versions.ts +9 -0
  34. package/src/sync-notice.ts +26 -0
  35. package/src/types/commands.ts +138 -9
  36. package/src/types/connector.ts +58 -1
  37. package/src/types/customers.ts +26 -0
  38. package/src/types/index.ts +9 -2
  39. package/src/types/reconcile.ts +30 -0
  40. package/src/types/replication.ts +6 -0
  41. package/src/types/store-settings.ts +4 -0
  42. package/src/types/traits/product.ts +7 -0
  43. package/src/unauthorized.ts +13 -0
@@ -0,0 +1,18 @@
1
+ // src/commands.ts
2
+ var COMMANDS_PATH = "/tally/v1/commands";
3
+ var PROTOCOL_HEADER = "X-Tally-Protocol";
4
+ var PROTOCOL_VERSION = 1;
5
+ var MAX_COMMANDS_PER_BATCH = 50;
6
+ function isCommandBatchResponse(value) {
7
+ return typeof value === "object" && value !== null && "results" in value && Array.isArray(value.results) && value.results.every(
8
+ (result) => typeof result === "object" && result !== null && "id" in result && typeof result.id === "string" && "status" in result && (result.status === "applied" || result.status === "duplicate" || result.status === "rejected")
9
+ );
10
+ }
11
+
12
+ export {
13
+ COMMANDS_PATH,
14
+ PROTOCOL_HEADER,
15
+ PROTOCOL_VERSION,
16
+ MAX_COMMANDS_PER_BATCH,
17
+ isCommandBatchResponse
18
+ };
@@ -0,0 +1,306 @@
1
+ /** Supported command operation. */
2
+ type CommandType = 'order.create';
3
+ /** Client command with a stable idempotency key and typed payload. */
4
+ interface CommandEnvelope<P = unknown> {
5
+ id: string;
6
+ type: CommandType;
7
+ /** 3 when the order carries ADR-065's `display` and `taxByRate` (the store accepts 3), else 2 when discounted, else 1;
8
+ * 4 is 3 with every `discountMinor` tax-exclusive, built only when capped at 4 or more (#286). */
9
+ version: 1 | 2 | 3 | 4;
10
+ payload: P;
11
+ createdAt: string;
12
+ deviceId: string;
13
+ attempt: number;
14
+ }
15
+ /** The register commands (ADR-068); each versions on its own, from 1. */
16
+ type RegisterCommandType = 'register.session.open' | 'register.session.transition' | 'register.movement.record' | 'register.movement.void' | 'register.closure.submit';
17
+ /** A register command's envelope: the same fields as CommandEnvelope, with its own type and a numeric version. */
18
+ type RegisterCommandEnvelope<P = Record<string, unknown>> = Omit<CommandEnvelope<P>, 'type' | 'version'> & {
19
+ type: RegisterCommandType;
20
+ version: number;
21
+ };
22
+ /** Any command a transport can carry. */
23
+ type AnyCommandEnvelope = CommandEnvelope<unknown> | RegisterCommandEnvelope<unknown>;
24
+ /** An order.create envelope: its version is 1 | 2 | 3 | 4 (ADR-062, ADR-065, #286). */
25
+ type OrderCreateEnvelope = CommandEnvelope<OrderCreatePayload> & {
26
+ type: 'order.create';
27
+ version: 1 | 2 | 3 | 4;
28
+ };
29
+ /** Outcome of processing a command. */
30
+ type CommandStatus = 'applied' | 'duplicate' | 'rejected';
31
+ /** Server order identifiers and authoritative total. */
32
+ interface CommandServerRefs {
33
+ orderId: string;
34
+ displayId?: string;
35
+ totalMinor: number;
36
+ }
37
+ /**
38
+ * Non-fatal discrepancies reported while processing a command. A server may send codes not
39
+ * listed here (a newer contract); readers go through `knownWarnings`, which drops them, and a
40
+ * warning never rejects a sale.
41
+ */
42
+ type CommandWarning = {
43
+ code: 'total_mismatch';
44
+ expectedMinor: number;
45
+ serverMinor: number;
46
+ /**
47
+ * Present if and only if the server added an untaxed rounding surcharge so its total
48
+ * matches the till's; signed, in minor units, `expectedMinor - serverMinor`, where
49
+ * `serverMinor` is the platform's total before the bridge.
50
+ */
51
+ bridgeMinor?: number;
52
+ } | {
53
+ code: 'insufficient_stock';
54
+ variantId: string;
55
+ quantity: number;
56
+ } | {
57
+ /**
58
+ * One warning per tax rate whose tax differs by more than the server's rounding tolerance
59
+ * (order.create v3 only); `expectedMinor` is the till's `taxByRate[].taxMinor` for that
60
+ * rate and `serverMinor` the platform's tax for it.
61
+ */
62
+ code: 'tax_rate_mismatch';
63
+ ratePpm: number;
64
+ expectedMinor: number;
65
+ serverMinor: number;
66
+ }
67
+ /**
68
+ * The sale's `customerId` (1 to 64 characters) was unknown, deleted or in another channel, so
69
+ * the sale was kept as a guest sale rather than held (ADR-070).
70
+ */
71
+ | {
72
+ code: 'customer_ignored';
73
+ customerId: string;
74
+ }
75
+ /**
76
+ * One warning per sale, never a refusal: each of the till's figures that differs from the
77
+ * server's own computation, once, with both values (#257). `total_mismatch` stays separate.
78
+ * `parseCommandResult` refuses a `field` it doesn't know; `knownWarnings` keeps it (a newer
79
+ * store's figure), so the type admits any non-empty string besides the three names.
80
+ */
81
+ | {
82
+ code: 'figures_mismatch';
83
+ fields: Array<{
84
+ field: 'subtotalMinor' | 'taxMinor' | 'discountMinor' | (string & {});
85
+ tillMinor: number;
86
+ serverMinor: number;
87
+ }>;
88
+ };
89
+ /** Error reported when a command is rejected. */
90
+ interface CommandError {
91
+ code: string;
92
+ message: string;
93
+ /** A refusal's details, e.g. the server's supported version. */
94
+ data?: Record<string, unknown>;
95
+ }
96
+ /** Server result for a single command. */
97
+ interface CommandResult {
98
+ id: string;
99
+ status: CommandStatus;
100
+ serverRefs?: CommandServerRefs;
101
+ warnings?: CommandWarning[];
102
+ error?: CommandError;
103
+ register?: RegisterCommandResult;
104
+ }
105
+ /** A register command's server figures (registers c2b applies them). */
106
+ interface RegisterCommandResult {
107
+ /** The session's server state after this command. `expected` is absent when the server redacts it (blind). */
108
+ session?: {
109
+ id: string;
110
+ status: 'open' | 'counting' | 'closed';
111
+ expected?: Record<string, number>;
112
+ salesCount?: number;
113
+ };
114
+ /** The register's counters: a floor for the till's own, never lowered. */
115
+ counters?: {
116
+ lastClosureNumber: number;
117
+ perpetualSalesTotalMinor: number;
118
+ perpetualRefundsTotalMinor: number;
119
+ };
120
+ /** `register.closure.submit` only. */
121
+ closure?: {
122
+ serverClosureId: string;
123
+ number: number;
124
+ expected?: Record<string, number>;
125
+ variance?: Record<string, number>;
126
+ };
127
+ }
128
+ interface RegisterSessionOpenPayload {
129
+ sessionId: string;
130
+ registerId: string;
131
+ storeKey?: string;
132
+ businessDay?: string;
133
+ openedAt: string;
134
+ openedBy?: string;
135
+ expectedFloatMinor?: number;
136
+ countedFloatMinor: number;
137
+ openingVarianceMinor?: number;
138
+ }
139
+ interface RegisterSessionTransitionPayload {
140
+ sessionId: string;
141
+ status: 'open' | 'counting' | 'closed';
142
+ at: string;
143
+ /** Closing only. */ counted?: Record<string, number>;
144
+ closedBy?: string;
145
+ approvedBy?: string;
146
+ }
147
+ interface RegisterMovementRecordPayload {
148
+ movementId: string;
149
+ sessionId: string;
150
+ type: 'paid_in' | 'paid_out' | 'no_sale';
151
+ amountMinor: number;
152
+ reason: string;
153
+ createdAt: string;
154
+ createdBy?: string;
155
+ }
156
+ interface RegisterMovementVoidPayload {
157
+ movementId: string;
158
+ sessionId: string;
159
+ voids: string;
160
+ createdAt: string;
161
+ createdBy?: string;
162
+ }
163
+ interface RegisterClosureSubmitPayload {
164
+ closureId: string;
165
+ sessionId: string;
166
+ registerId: string;
167
+ number: number;
168
+ businessDay?: string;
169
+ openedAt: string;
170
+ closedAt: string;
171
+ closedBy?: string;
172
+ approvedBy?: string;
173
+ tillExpected: Record<string, number>;
174
+ counted: Record<string, number>;
175
+ periodSalesTotalMinor: number;
176
+ periodRefundsTotalMinor: number;
177
+ perpetualSalesTotalMinor: number;
178
+ perpetualRefundsTotalMinor: number;
179
+ unsyncedCount: number;
180
+ unsyncedTotalMinor: number;
181
+ softwareVersion: string;
182
+ orderIds: string[];
183
+ movementIds: string[];
184
+ }
185
+ /** Order line with client identity and price in minor units. */
186
+ interface OrderCreateLine {
187
+ clientLineId: string;
188
+ variantId: string;
189
+ title?: string;
190
+ quantity: number;
191
+ unitPriceMinor: number;
192
+ /**
193
+ * This line's own tax mode, when it differs from the order's `pricesIncludeTax`
194
+ * (a price that carries its own flag, D2c). Absent means the order's flag, so
195
+ * older clients and single-mode orders are unchanged.
196
+ */
197
+ taxInclusive?: boolean;
198
+ /**
199
+ * Version 2 (ADR-062): this line's total discount, its own line discounts plus its allocated share of
200
+ * the order discount, in the line's own tax mode and integer minor units. The line is taxed on
201
+ * `unitPriceMinor × quantity − discountMinor`. Present only when above 0.
202
+ * Version 4 (#286): tax-exclusive (net) in every mode; an inclusive line's is `net(A) − net(A − D)`.
203
+ */
204
+ discountMinor?: number;
205
+ }
206
+ /** Supported order payment method. */
207
+ type PaymentMethodKind = 'cash' | 'external';
208
+ /** Order payment with client identity and amounts in minor units. */
209
+ interface OrderCreatePayment {
210
+ clientPaymentId: string;
211
+ method: PaymentMethodKind;
212
+ amountMinor: number;
213
+ tenderedMinor?: number;
214
+ changeMinor?: number;
215
+ reference?: string;
216
+ }
217
+ /** Version 3 (ADR-065): the receipt's display figures, integer minor units of `currency` at `exponent`. */
218
+ interface OrderCreateDisplay {
219
+ currency: string;
220
+ exponent: number;
221
+ taxInclusive: boolean;
222
+ subtotalMinor: number;
223
+ discountMinor: number;
224
+ taxMinor: number;
225
+ totalMinor: number;
226
+ orderDiscountMinor: number;
227
+ lines: Array<{
228
+ clientLineId: string;
229
+ amountMinor: number;
230
+ discounts: Array<{
231
+ discountId: string;
232
+ label?: string;
233
+ amountMinor: number;
234
+ }>;
235
+ }>;
236
+ }
237
+ /** Version 3 (ADR-065): one tax rate's net, tax and gross, as the receipt's tax summary splits them. */
238
+ interface OrderCreateTaxRate {
239
+ ratePpm: number;
240
+ code?: string;
241
+ netMinor: number;
242
+ taxMinor: number;
243
+ grossMinor: number;
244
+ }
245
+ /** Client order data submitted by an order.create command. */
246
+ interface OrderCreatePayload {
247
+ clientOrderId: string;
248
+ createdAt: string;
249
+ currency: string;
250
+ pricesIncludeTax: boolean;
251
+ lines: OrderCreateLine[];
252
+ subtotalMinor: number;
253
+ /**
254
+ * Version 2 (ADR-062): the order's total discount, equal to Σ `lines[].discountMinor`. Present only when above 0.
255
+ * Version 4 (#286) means every `discountMinor`, this and each line's, is tax-exclusive, so the sum still holds.
256
+ */
257
+ discountMinor?: number;
258
+ taxMinor: number;
259
+ totalMinor: number;
260
+ payments: OrderCreatePayment[];
261
+ /** version 3 only; both or neither. */
262
+ display?: OrderCreateDisplay;
263
+ /** version 3 only; both or neither. */
264
+ taxByRate?: OrderCreateTaxRate[];
265
+ customer?: {
266
+ email?: string;
267
+ /** Version 3 only: the platform's id for the customer picked at the till, a soft reference of at most 64 characters. Absent for a guest sale, or when no customer was picked. */
268
+ customerId?: string;
269
+ } | null;
270
+ registerId?: string;
271
+ cashierRef?: string;
272
+ locationId?: string;
273
+ /** Version 3 only: the register session the sale was taken for, stamped or late (ADR-032). A late sale names the session that refused its stamp; the server tells the two apart by the session's closure `orderIds`. Absent when the sale had no session. */
274
+ sessionId?: string;
275
+ }
276
+ /** Batch of commands submitted to the server. */
277
+ interface CommandBatchRequest<E extends AnyCommandEnvelope = CommandEnvelope> {
278
+ commands: E[];
279
+ }
280
+ /** Results returned by the server for a command batch. */
281
+ interface CommandBatchResponse {
282
+ results: CommandResult[];
283
+ }
284
+ /** The `413` body for a batch over the limit (ADR-038; Front desk ruling 18). */
285
+ type BatchTooLargeBody = {
286
+ code: 'batch_too_large';
287
+ maxCommands: number;
288
+ message: string;
289
+ };
290
+
291
+ /** Path of the command endpoint, relative to the backend's base URL. */
292
+ declare const COMMANDS_PATH = "/tally/v1/commands";
293
+ /** Header carrying the protocol version. */
294
+ declare const PROTOCOL_HEADER = "X-Tally-Protocol";
295
+ /** Current protocol version. */
296
+ declare const PROTOCOL_VERSION = 1;
297
+ /** The ADR-038 batch limit; a server answers more with 413 and a BatchTooLargeBody. */
298
+ declare const MAX_COMMANDS_PER_BATCH = 50;
299
+ /**
300
+ * True if `value` has the shape of a CommandBatchResponse: an object with a
301
+ * `results` array whose every entry has a string `id` and a `status` of
302
+ * 'applied' | 'duplicate' | 'rejected'. Optional fields are not deep-checked.
303
+ */
304
+ declare function isCommandBatchResponse(value: unknown): value is CommandBatchResponse;
305
+
306
+ export { type AnyCommandEnvelope as A, type BatchTooLargeBody as B, type CommandWarning as C, MAX_COMMANDS_PER_BATCH as M, type OrderCreateDisplay as O, PROTOCOL_HEADER as P, type RegisterClosureSubmitPayload as R, COMMANDS_PATH as a, type CommandBatchRequest as b, type CommandBatchResponse as c, type CommandEnvelope as d, type CommandError as e, type CommandResult as f, type CommandServerRefs as g, type CommandStatus as h, type CommandType as i, type OrderCreateEnvelope as j, type OrderCreateLine as k, type OrderCreatePayload as l, type OrderCreatePayment as m, type OrderCreateTaxRate as n, PROTOCOL_VERSION as o, type PaymentMethodKind as p, type RegisterCommandEnvelope as q, type RegisterCommandResult as r, type RegisterCommandType as s, type RegisterMovementRecordPayload as t, type RegisterMovementVoidPayload as u, type RegisterSessionOpenPayload as v, type RegisterSessionTransitionPayload as w, isCommandBatchResponse as x };