@jaicome/contracts 0.0.73

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,15 @@
1
+ # api
2
+
3
+ To install dependencies:
4
+
5
+ ```bash
6
+ bun install
7
+ ```
8
+
9
+ To run:
10
+
11
+ ```bash
12
+ bun run index.ts
13
+ ```
14
+
15
+ This project was created using `bun init` in bun v1.2.18. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
@@ -0,0 +1,129 @@
1
+ import { i as FulfillmentState$1 } from "./orders-yFOPRYZw.mjs";
2
+
3
+ //#region src/fulfillment-states.d.ts
4
+
5
+ type FulfillmentState = FulfillmentState$1;
6
+ /**
7
+ * All valid fulfillment states in order
8
+ *
9
+ * State Definitions:
10
+ * - NEW: Customer placed & paid order
11
+ * - AWAITING_DELIVERY: Merchant accepted delivery order, waiting for delivery provider acceptance
12
+ * - CONFIRMED: Provider accepted delivery OR non-delivery order accepted by merchant
13
+ * - READY: [OPTIONAL] Merchant marked order as ready for pickup/delivery
14
+ * - ON_THE_WAY: Order is in transit (delivery only)
15
+ * - COMPLETED: Order delivered or picked up
16
+ * - CANCELLED: Cancelled before fulfillment (terminal)
17
+ * - FAILED: Fulfillment attempt failed (terminal)
18
+ * - REFUNDED: Order payment has been refunded (terminal)
19
+ */
20
+ declare const FulfillmentStateValues: readonly ["NEW", "AWAITING_DELIVERY", "CONFIRMED", "READY", "ON_THE_WAY", "COMPLETED", "CANCELLED", "FAILED", "REFUNDED"];
21
+ /**
22
+ * Terminal states that cannot transition to any other state
23
+ */
24
+ declare const TerminalStates: ReadonlySet<FulfillmentState>;
25
+ /**
26
+ * Actor type for determining which transitions are allowed
27
+ * - "user": User-initiated actions (merchants, admins)
28
+ * - "system": System-initiated actions (automated processes, background jobs)
29
+ */
30
+ type Actor = "user" | "system";
31
+ /**
32
+ * Checks if a fulfillment state transition is allowed
33
+ *
34
+ * @param params - Transition parameters
35
+ * @param params.from - Current state (null treated as NEW)
36
+ * @param params.to - Target state to transition to
37
+ * @param params.actor - Actor type: "user" (default) or "system"
38
+ * @returns true if transition is allowed, false otherwise
39
+ *
40
+ * @example
41
+ * ```ts
42
+ * canTransition({ from: "NEW", to: "CONFIRMED", actor: "user" }) // true
43
+ * canTransition({ from: "NEW", to: "FAILED", actor: "user" }) // false (users cannot set FAILED)
44
+ * canTransition({ from: "NEW", to: "FAILED", actor: "system" }) // true
45
+ * canTransition({ from: "COMPLETED", to: "NEW" }) // false (COMPLETED is terminal)
46
+ * canTransition({ from: null, to: "CONFIRMED" }) // true (null treated as NEW)
47
+ * ```
48
+ */
49
+ declare function canTransition(params: {
50
+ from: FulfillmentState | null;
51
+ to: FulfillmentState;
52
+ actor?: Actor;
53
+ }): boolean;
54
+ /**
55
+ * Asserts that a fulfillment state transition is allowed
56
+ * Throws an error if the transition is not allowed
57
+ *
58
+ * @param params - Transition parameters (same as canTransition)
59
+ * @throws Error if transition is not allowed
60
+ *
61
+ * @example
62
+ * ```ts
63
+ * assertCanTransition({ from: "NEW", to: "CONFIRMED" }) // ok
64
+ * assertCanTransition({ from: "COMPLETED", to: "NEW" }) // throws Error
65
+ * ```
66
+ */
67
+ declare function assertCanTransition(params: {
68
+ from: FulfillmentState | null;
69
+ to: FulfillmentState;
70
+ actor?: Actor;
71
+ }): void;
72
+ /**
73
+ * Gets all allowed transitions from a given state for a specific actor
74
+ *
75
+ * @param from - Current state (null treated as NEW)
76
+ * @param actor - Actor type (defaults to "user")
77
+ * @returns Array of allowed next states
78
+ *
79
+ * @example
80
+ * ```ts
81
+ * getAllowedTransitions("NEW", "user") // ["AWAITING_DELIVERY", "CONFIRMED", "READY", "ON_THE_WAY", "COMPLETED", "CANCELLED"]
82
+ * getAllowedTransitions("NEW", "system") // ["AWAITING_DELIVERY", "CONFIRMED", "READY", "ON_THE_WAY", "COMPLETED", "CANCELLED", "FAILED"]
83
+ * getAllowedTransitions("COMPLETED") // ["REFUNDED"]
84
+ * getAllowedTransitions("CANCELLED") // []
85
+ * ```
86
+ */
87
+ declare function getAllowedTransitions(from: FulfillmentState | null, actor?: Actor): FulfillmentState[];
88
+ /**
89
+ * Gets all allowed transitions for a given state for the user actor
90
+ * Convenience function that calls getAllowedTransitions with actor="user"
91
+ *
92
+ * @param from - Current state (null treated as NEW)
93
+ * @returns Array of allowed next states for user-initiated actions
94
+ *
95
+ * @example
96
+ * ```ts
97
+ * getAllowedUserTransitions("CONFIRMED") // ["READY", "ON_THE_WAY", "COMPLETED", "REFUNDED"]
98
+ * ```
99
+ */
100
+ declare function getAllowedUserTransitions(from: FulfillmentState | null): FulfillmentState[];
101
+ /**
102
+ * Checks if a state is a terminal state
103
+ *
104
+ * @param state - The state to check
105
+ * @returns true if the state is terminal (no further transitions possible)
106
+ *
107
+ * @example
108
+ * ```ts
109
+ * isTerminalState("COMPLETED") // false
110
+ * isTerminalState("CANCELLED") // true
111
+ * isTerminalState("REFUNDED") // true
112
+ * ```
113
+ */
114
+ declare function isTerminalState(state: FulfillmentState): boolean;
115
+ /**
116
+ * Namespace containing all fulfillment state machine operations
117
+ * Provides a unified API for state machine functionality
118
+ */
119
+ declare const FulfillmentStateMachine: {
120
+ readonly canTransition: typeof canTransition;
121
+ readonly assertCanTransition: typeof assertCanTransition;
122
+ readonly getAllowedTransitions: typeof getAllowedTransitions;
123
+ readonly getAllowedUserTransitions: typeof getAllowedUserTransitions;
124
+ readonly isTerminalState: typeof isTerminalState;
125
+ readonly values: readonly ["NEW", "AWAITING_DELIVERY", "CONFIRMED", "READY", "ON_THE_WAY", "COMPLETED", "CANCELLED", "FAILED", "REFUNDED"];
126
+ readonly terminalStates: ReadonlySet<"NEW" | "AWAITING_DELIVERY" | "CONFIRMED" | "READY" | "ON_THE_WAY" | "COMPLETED" | "CANCELLED" | "FAILED" | "REFUNDED">;
127
+ };
128
+ //#endregion
129
+ export { Actor, FulfillmentState, FulfillmentStateMachine, FulfillmentStateValues, TerminalStates, assertCanTransition, canTransition, getAllowedTransitions, getAllowedUserTransitions, isTerminalState };
@@ -0,0 +1,225 @@
1
+ //#region src/fulfillment-states.ts
2
+ /**
3
+ * All valid fulfillment states in order
4
+ *
5
+ * State Definitions:
6
+ * - NEW: Customer placed & paid order
7
+ * - AWAITING_DELIVERY: Merchant accepted delivery order, waiting for delivery provider acceptance
8
+ * - CONFIRMED: Provider accepted delivery OR non-delivery order accepted by merchant
9
+ * - READY: [OPTIONAL] Merchant marked order as ready for pickup/delivery
10
+ * - ON_THE_WAY: Order is in transit (delivery only)
11
+ * - COMPLETED: Order delivered or picked up
12
+ * - CANCELLED: Cancelled before fulfillment (terminal)
13
+ * - FAILED: Fulfillment attempt failed (terminal)
14
+ * - REFUNDED: Order payment has been refunded (terminal)
15
+ */
16
+ const FulfillmentStateValues = [
17
+ "NEW",
18
+ "AWAITING_DELIVERY",
19
+ "CONFIRMED",
20
+ "READY",
21
+ "ON_THE_WAY",
22
+ "COMPLETED",
23
+ "CANCELLED",
24
+ "FAILED",
25
+ "REFUNDED"
26
+ ];
27
+ /**
28
+ * Terminal states that cannot transition to any other state
29
+ */
30
+ const TerminalStates = new Set([
31
+ "CANCELLED",
32
+ "FAILED",
33
+ "REFUNDED"
34
+ ]);
35
+ /**
36
+ * Allowed forward transitions for user-initiated actions
37
+ * Users cannot directly set FAILED state
38
+ */
39
+ const AllowedUserTransitions = {
40
+ NEW: new Set([
41
+ "AWAITING_DELIVERY",
42
+ "CONFIRMED",
43
+ "READY",
44
+ "ON_THE_WAY",
45
+ "COMPLETED",
46
+ "CANCELLED"
47
+ ]),
48
+ AWAITING_DELIVERY: new Set([
49
+ "CONFIRMED",
50
+ "READY",
51
+ "ON_THE_WAY",
52
+ "COMPLETED",
53
+ "CANCELLED"
54
+ ]),
55
+ CONFIRMED: new Set([
56
+ "READY",
57
+ "ON_THE_WAY",
58
+ "COMPLETED",
59
+ "REFUNDED"
60
+ ]),
61
+ READY: new Set(["ON_THE_WAY", "COMPLETED"]),
62
+ ON_THE_WAY: new Set(["COMPLETED", "REFUNDED"]),
63
+ COMPLETED: new Set(["REFUNDED"]),
64
+ CANCELLED: /* @__PURE__ */ new Set(),
65
+ FAILED: /* @__PURE__ */ new Set(),
66
+ REFUNDED: /* @__PURE__ */ new Set()
67
+ };
68
+ /**
69
+ * Allowed forward transitions for system-initiated actions
70
+ * System can additionally set FAILED at any non-terminal state
71
+ */
72
+ const AllowedSystemTransitions = {
73
+ NEW: new Set([
74
+ "AWAITING_DELIVERY",
75
+ "CONFIRMED",
76
+ "READY",
77
+ "ON_THE_WAY",
78
+ "COMPLETED",
79
+ "CANCELLED",
80
+ "FAILED"
81
+ ]),
82
+ AWAITING_DELIVERY: new Set([
83
+ "CONFIRMED",
84
+ "READY",
85
+ "ON_THE_WAY",
86
+ "COMPLETED",
87
+ "CANCELLED",
88
+ "FAILED"
89
+ ]),
90
+ CONFIRMED: new Set([
91
+ "READY",
92
+ "ON_THE_WAY",
93
+ "COMPLETED",
94
+ "REFUNDED",
95
+ "FAILED"
96
+ ]),
97
+ READY: new Set([
98
+ "ON_THE_WAY",
99
+ "COMPLETED",
100
+ "FAILED"
101
+ ]),
102
+ ON_THE_WAY: new Set([
103
+ "COMPLETED",
104
+ "REFUNDED",
105
+ "FAILED"
106
+ ]),
107
+ COMPLETED: new Set(["REFUNDED"]),
108
+ CANCELLED: /* @__PURE__ */ new Set(),
109
+ FAILED: /* @__PURE__ */ new Set(),
110
+ REFUNDED: /* @__PURE__ */ new Set()
111
+ };
112
+ /**
113
+ * Checks if a fulfillment state transition is allowed
114
+ *
115
+ * @param params - Transition parameters
116
+ * @param params.from - Current state (null treated as NEW)
117
+ * @param params.to - Target state to transition to
118
+ * @param params.actor - Actor type: "user" (default) or "system"
119
+ * @returns true if transition is allowed, false otherwise
120
+ *
121
+ * @example
122
+ * ```ts
123
+ * canTransition({ from: "NEW", to: "CONFIRMED", actor: "user" }) // true
124
+ * canTransition({ from: "NEW", to: "FAILED", actor: "user" }) // false (users cannot set FAILED)
125
+ * canTransition({ from: "NEW", to: "FAILED", actor: "system" }) // true
126
+ * canTransition({ from: "COMPLETED", to: "NEW" }) // false (COMPLETED is terminal)
127
+ * canTransition({ from: null, to: "CONFIRMED" }) // true (null treated as NEW)
128
+ * ```
129
+ */
130
+ function canTransition(params) {
131
+ const { from, to, actor = "user" } = params;
132
+ if (from && from === to) return false;
133
+ const current = from ?? "NEW";
134
+ if (TerminalStates.has(current)) return false;
135
+ if (actor === "user" && to === "FAILED") return false;
136
+ return (actor === "system" ? AllowedSystemTransitions : AllowedUserTransitions)[current].has(to);
137
+ }
138
+ /**
139
+ * Asserts that a fulfillment state transition is allowed
140
+ * Throws an error if the transition is not allowed
141
+ *
142
+ * @param params - Transition parameters (same as canTransition)
143
+ * @throws Error if transition is not allowed
144
+ *
145
+ * @example
146
+ * ```ts
147
+ * assertCanTransition({ from: "NEW", to: "CONFIRMED" }) // ok
148
+ * assertCanTransition({ from: "COMPLETED", to: "NEW" }) // throws Error
149
+ * ```
150
+ */
151
+ function assertCanTransition(params) {
152
+ if (!canTransition(params)) {
153
+ const { from, to, actor = "user" } = params;
154
+ const readableFrom = from ?? "NEW";
155
+ throw new Error(`Invalid fulfillment transition: ${readableFrom} -> ${to} (actor=${actor})`);
156
+ }
157
+ }
158
+ /**
159
+ * Gets all allowed transitions from a given state for a specific actor
160
+ *
161
+ * @param from - Current state (null treated as NEW)
162
+ * @param actor - Actor type (defaults to "user")
163
+ * @returns Array of allowed next states
164
+ *
165
+ * @example
166
+ * ```ts
167
+ * getAllowedTransitions("NEW", "user") // ["AWAITING_DELIVERY", "CONFIRMED", "READY", "ON_THE_WAY", "COMPLETED", "CANCELLED"]
168
+ * getAllowedTransitions("NEW", "system") // ["AWAITING_DELIVERY", "CONFIRMED", "READY", "ON_THE_WAY", "COMPLETED", "CANCELLED", "FAILED"]
169
+ * getAllowedTransitions("COMPLETED") // ["REFUNDED"]
170
+ * getAllowedTransitions("CANCELLED") // []
171
+ * ```
172
+ */
173
+ function getAllowedTransitions(from, actor) {
174
+ const current = from ?? "NEW";
175
+ if (TerminalStates.has(current)) return [];
176
+ const allowedNext = (actor === "system" ? AllowedSystemTransitions : AllowedUserTransitions)[current];
177
+ return Array.from(allowedNext);
178
+ }
179
+ /**
180
+ * Gets all allowed transitions for a given state for the user actor
181
+ * Convenience function that calls getAllowedTransitions with actor="user"
182
+ *
183
+ * @param from - Current state (null treated as NEW)
184
+ * @returns Array of allowed next states for user-initiated actions
185
+ *
186
+ * @example
187
+ * ```ts
188
+ * getAllowedUserTransitions("CONFIRMED") // ["READY", "ON_THE_WAY", "COMPLETED", "REFUNDED"]
189
+ * ```
190
+ */
191
+ function getAllowedUserTransitions(from) {
192
+ return getAllowedTransitions(from, "user");
193
+ }
194
+ /**
195
+ * Checks if a state is a terminal state
196
+ *
197
+ * @param state - The state to check
198
+ * @returns true if the state is terminal (no further transitions possible)
199
+ *
200
+ * @example
201
+ * ```ts
202
+ * isTerminalState("COMPLETED") // false
203
+ * isTerminalState("CANCELLED") // true
204
+ * isTerminalState("REFUNDED") // true
205
+ * ```
206
+ */
207
+ function isTerminalState(state) {
208
+ return TerminalStates.has(state);
209
+ }
210
+ /**
211
+ * Namespace containing all fulfillment state machine operations
212
+ * Provides a unified API for state machine functionality
213
+ */
214
+ const FulfillmentStateMachine = {
215
+ canTransition,
216
+ assertCanTransition,
217
+ getAllowedTransitions,
218
+ getAllowedUserTransitions,
219
+ isTerminalState,
220
+ values: FulfillmentStateValues,
221
+ terminalStates: TerminalStates
222
+ };
223
+
224
+ //#endregion
225
+ export { FulfillmentStateMachine, FulfillmentStateValues, TerminalStates, assertCanTransition, canTransition, getAllowedTransitions, getAllowedUserTransitions, isTerminalState };