@munchi_oy/cart-engine 0.1.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/README.md ADDED
@@ -0,0 +1,204 @@
1
+ # @munchi_oy/cart-engine
2
+
3
+ Headless cart engine for Munchi POS flows.
4
+
5
+ This package owns cart state, pricing, discount application, and tax summary calculation. It is designed for offline-first consumers and shared across app surfaces without depending on React, Zustand, Firebase, or UI code.
6
+
7
+ ## Scope
8
+
9
+ This package includes:
10
+
11
+ - `Cart` aggregate for cart state mutations
12
+ - Pure pricing helpers
13
+ - Pure discount helpers
14
+ - Pure tax helpers
15
+ - Shared cart-oriented types
16
+
17
+ This package does not include:
18
+
19
+ - Product mapping from app DTOs into cart items
20
+ - UI formatting
21
+ - Persistence
22
+ - Network calls
23
+ - Activity logs or staff audit logging
24
+ - Payment settlement logic
25
+
26
+ ## Core Rules
27
+
28
+ - All money is in minor units as integers
29
+ - Use `Math.floor()` for percentage-derived money calculations
30
+ - Tax rate may be decimal, such as `13.5`
31
+ - `Cart` owns state
32
+ - Pricing, discount, and tax modules stay pure and stateless
33
+ - The package relies on shared DTO contracts from `@munchi_oy/core`
34
+
35
+ ## Install
36
+
37
+ ```bash
38
+ pnpm add @munchi_oy/cart-engine @munchi_oy/core
39
+ ```
40
+
41
+ Peer dependency:
42
+
43
+ - `@munchi_oy/core`
44
+
45
+ Bundled runtime dependencies:
46
+
47
+ - `dayjs`
48
+ - `lodash`
49
+ - `@paralleldrive/cuid2`
50
+
51
+ ## Package Layout
52
+
53
+ ```text
54
+ src/
55
+ cart/
56
+ Cart.ts
57
+ discount/
58
+ calculator.ts
59
+ modulo.ts
60
+ pricing/
61
+ calculator.ts
62
+ tax/
63
+ index.ts
64
+ types/
65
+ index.ts
66
+ utils/
67
+ index.ts
68
+ version.ts
69
+ index.ts
70
+ ```
71
+
72
+ ## Public API
73
+
74
+ Main exports:
75
+
76
+ - `Cart`
77
+ - `calculateItemPrice`
78
+ - `calculateCartPriceSnapshot`
79
+ - `calculateDiscountAmount`
80
+ - `calculateSequentialDiscountTotal`
81
+ - `allocateProportionalMinorUnits`
82
+ - `splitTaxInclusiveAmount`
83
+ - `calculateOrderTaxSummary`
84
+ - `ApplyTo`
85
+ - `VERSION`
86
+
87
+ ## Cart Responsibilities
88
+
89
+ `Cart` is the aggregate root for cart state. It currently handles:
90
+
91
+ - item add, update, remove, and status changes
92
+ - item and cart discounts
93
+ - customer, invoice company, loyalty, and order metadata
94
+ - serialization through `toJSON()`
95
+ - rehydration through `fromData()`
96
+ - cloning through `clone()`
97
+
98
+ `Cart` delegates all math to the pure modules under `discount`, `pricing`, and `tax`.
99
+
100
+ ## Pricing, Discount, and Tax Boundaries
101
+
102
+ ### Pricing
103
+
104
+ Pricing helpers work on normalized cart data and produce DTO-safe totals. They do not parse raw user input.
105
+
106
+ ### Discount
107
+
108
+ `calculateDiscountAmount` expects normalized values:
109
+
110
+ - percentage discount values are percentages
111
+ - fixed discount values are already in minor units
112
+
113
+ If the app needs to parse user-entered strings such as `"10.50"`, that parsing should happen outside this package before calling the engine.
114
+
115
+ ### Tax
116
+
117
+ Tax calculations:
118
+
119
+ - use integer money math
120
+ - support decimal tax rates
121
+ - group tax summaries by rate
122
+ - allocate cart-level discounts proportionally before tax splitting
123
+
124
+ ## Development
125
+
126
+ Install dependencies:
127
+
128
+ ```bash
129
+ pnpm install
130
+ ```
131
+
132
+ Useful commands:
133
+
134
+ ```bash
135
+ pnpm typecheck
136
+ pnpm test
137
+ pnpm build
138
+ pnpm yalc:publish
139
+ pnpm yalc:push
140
+ ```
141
+
142
+ ## Testing
143
+
144
+ Tests live under `test/` and are organized by concern:
145
+
146
+ - cart item behavior
147
+ - cart pricing behavior
148
+ - cart tax behavior
149
+ - cart metadata behavior
150
+ - cart serialization behavior
151
+ - pure discount helpers
152
+ - pure pricing helpers
153
+ - pure tax helpers
154
+
155
+ Current expectations:
156
+
157
+ - cart totals must be correct for no discount, item discount, cart discount, and combined discount cases
158
+ - tax summaries must group by dynamic tax rate
159
+ - runtime fallbacks must behave safely
160
+
161
+ ## Versioning
162
+
163
+ Package version is sourced from `package.json` and synced into `src/version.ts`.
164
+
165
+ Sync version constant:
166
+
167
+ ```bash
168
+ pnpm version:sync
169
+ ```
170
+
171
+ Build always runs version sync first.
172
+
173
+ ## Release
174
+
175
+ Release script:
176
+
177
+ ```bash
178
+ pnpm release -- 0.1.1
179
+ ```
180
+
181
+ Optional GitHub release modes:
182
+
183
+ ```bash
184
+ pnpm release -- 0.1.1 --publish
185
+ pnpm release -- 0.1.1 --publish=custom
186
+ pnpm release -- 0.1.1 --publish=file
187
+ ```
188
+
189
+ The release script:
190
+
191
+ - updates `package.json`
192
+ - syncs `src/version.ts`
193
+ - runs typecheck, tests, and build
194
+ - creates a commit
195
+ - creates a `v<version>` tag
196
+ - pushes branch and tag
197
+ - optionally creates a GitHub release
198
+
199
+ ## Consumer Notes
200
+
201
+ - Rebuild before publishing or pushing with `yalc`
202
+ - If a consumer app still sees stale types, rebuild and refresh the installed package
203
+ - One cart should use one `CurrencyCode`
204
+