@voyantjs/transactions 0.2.0 → 0.3.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/routes-offers.d.ts +885 -0
  2. package/dist/routes-offers.d.ts.map +1 -0
  3. package/dist/routes-offers.js +161 -0
  4. package/dist/routes-orders.d.ts +1056 -0
  5. package/dist/routes-orders.d.ts.map +1 -0
  6. package/dist/routes-orders.js +180 -0
  7. package/dist/routes-shared.d.ts +103 -0
  8. package/dist/routes-shared.d.ts.map +1 -0
  9. package/dist/routes-shared.js +97 -0
  10. package/dist/routes.d.ts +3 -1979
  11. package/dist/routes.d.ts.map +1 -1
  12. package/dist/routes.js +5 -604
  13. package/dist/schema-audit.d.ts +216 -0
  14. package/dist/schema-audit.d.ts.map +1 -0
  15. package/dist/schema-audit.js +22 -0
  16. package/dist/schema-offers.d.ts +1301 -0
  17. package/dist/schema-offers.d.ts.map +1 -0
  18. package/dist/schema-offers.js +120 -0
  19. package/dist/schema-orders.d.ts +1582 -0
  20. package/dist/schema-orders.d.ts.map +1 -0
  21. package/dist/schema-orders.js +149 -0
  22. package/dist/schema-relations.d.ts +42 -0
  23. package/dist/schema-relations.d.ts.map +1 -0
  24. package/dist/schema-relations.js +56 -0
  25. package/dist/schema-shared.d.ts +12 -0
  26. package/dist/schema-shared.d.ts.map +1 -0
  27. package/dist/schema-shared.js +84 -0
  28. package/dist/schema.d.ts +5 -3148
  29. package/dist/schema.d.ts.map +1 -1
  30. package/dist/schema.js +5 -420
  31. package/dist/service-offers.d.ts +447 -0
  32. package/dist/service-offers.d.ts.map +1 -0
  33. package/dist/service-offers.js +262 -0
  34. package/dist/service-orders.d.ts +443 -0
  35. package/dist/service-orders.d.ts.map +1 -0
  36. package/dist/service-orders.js +278 -0
  37. package/dist/service-shared.d.ts +86 -0
  38. package/dist/service-shared.d.ts.map +1 -0
  39. package/dist/service-shared.js +49 -0
  40. package/dist/service.d.ts +48 -927
  41. package/dist/service.d.ts.map +1 -1
  42. package/dist/service.js +48 -595
  43. package/package.json +5 -5
@@ -1 +1 @@
1
- {"version":3,"file":"routes.d.ts","sourceRoot":"","sources":["../src/routes.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAoCjE,KAAK,GAAG,GAAG;IACT,QAAQ,EAAE,OAAO,CAAC;QAChB,YAAY,EAAE,MAAM,CAAA;QACpB,WAAW,EAAE,MAAM,CAAA;QACnB,aAAa,EAAE,MAAM,CAAA;QACrB,cAAc,EAAE,MAAM,CAAA;QACtB,yBAAyB,EAAE,MAAM,CAAA;QACjC,eAAe,EAAE,MAAM,CAAA;QACvB,eAAe,EAAE,MAAM,CAAA;QACvB,gBAAgB,EAAE,MAAM,CAAA;QACxB,uBAAuB,EAAE,MAAM,CAAA;QAC/B,6BAA6B,EAAE,MAAM,CAAA;QACrC,UAAU,EAAE,MAAM,CAAA;QAClB,iBAAiB,EAAE,MAAM,CAAA;QACzB,qBAAqB,EAAE,MAAM,CAAA;QAC7B,iBAAiB,EAAE,MAAM,CAAA;QACzB,gBAAgB,EAAE,MAAM,CAAA;QACxB,qBAAqB,EAAE,MAAM,CAAA;QAC7B,2BAA2B,EAAE,MAAM,CAAA;KACpC,CAAC,CAAA;IACF,SAAS,EAAE;QACT,EAAE,EAAE,kBAAkB,CAAA;QACtB,MAAM,CAAC,EAAE,MAAM,CAAA;QACf,KAAK,CAAC,EAAE,OAAO,GAAG,UAAU,GAAG,SAAS,GAAG,UAAU,CAAA;QACrD,UAAU,CAAC,EAAE,SAAS,GAAG,SAAS,GAAG,UAAU,CAAA;QAC/C,MAAM,CAAC,EAAE,MAAM,EAAE,GAAG,IAAI,CAAA;QACxB,iBAAiB,CAAC,EAAE,OAAO,CAAA;QAC3B,uBAAuB,CAAC,EAAE,CAAC,IAAI,EAAE;YAC/B,EAAE,EAAE,kBAAkB,CAAA;YACtB,MAAM,CAAC,EAAE,M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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;2BAgpB3B,CAAA;AAEJ,MAAM,MAAM,kBAAkB,GAAG,OAAO,kBAAkB,CAAA"}
1
+ {"version":3,"file":"routes.d.ts","sourceRoot":"","sources":["../src/routes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAA;AAI3B,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,oBAAoB,CAAA;AAE7C,eAAO,MAAM,kBAAkB,kDAAkB,CAAA;AAIjD,MAAM,MAAM,kBAAkB,GAAG,OAAO,kBAAkB,CAAA"}
package/dist/routes.js CHANGED
@@ -1,605 +1,6 @@
1
- import { createKmsProviderFromEnv } from "@voyantjs/utils";
2
1
  import { Hono } from "hono";
3
- import { createTransactionPiiService } from "./pii.js";
4
- import { transactionPiiAccessLog } from "./schema.js";
5
- import { transactionsService } from "./service.js";
6
- import { insertOfferItemParticipantSchema, insertOfferItemSchema, insertOfferParticipantSchema, insertOfferSchema, insertOrderItemParticipantSchema, insertOrderItemSchema, insertOrderParticipantSchema, insertOrderSchema, insertOrderTermSchema, offerItemListQuerySchema, offerItemParticipantListQuerySchema, offerListQuerySchema, offerParticipantListQuerySchema, orderItemListQuerySchema, orderItemParticipantListQuerySchema, orderListQuerySchema, orderParticipantListQuerySchema, orderTermListQuerySchema, updateOfferItemParticipantSchema, updateOfferItemSchema, updateOfferParticipantSchema, updateOfferSchema, updateOrderItemParticipantSchema, updateOrderItemSchema, updateOrderParticipantSchema, updateOrderSchema, updateOrderTermSchema, } from "./validation.js";
7
- function getRuntimeEnv(c) {
8
- const processEnv = globalThis.process?.env ?? {};
9
- return {
10
- ...processEnv,
11
- ...(c.env ?? {}),
12
- };
13
- }
14
- function hasPiiScope(scopes, action) {
15
- if (!scopes || scopes.length === 0) {
16
- return false;
17
- }
18
- return (scopes.includes("*") ||
19
- scopes.includes("transactions-pii:*") ||
20
- scopes.includes(`transactions-pii:${action}`));
21
- }
22
- function hasParticipantIdentityInput(body) {
23
- return "dateOfBirth" in body || "nationality" in body;
24
- }
25
- async function logTransactionPiiAccess(c, input) {
26
- await c
27
- .get("db")
28
- .insert(transactionPiiAccessLog)
29
- .values({
30
- participantKind: input.participantKind,
31
- parentId: input.parentId ?? null,
32
- participantId: input.participantId ?? null,
33
- actorId: c.get("userId") ?? null,
34
- actorType: c.get("actor") ?? null,
35
- callerType: c.get("callerType") ?? null,
36
- action: input.action,
37
- outcome: input.outcome,
38
- reason: input.reason ?? null,
39
- metadata: input.metadata ?? null,
40
- });
41
- }
42
- async function authorizeTransactionPiiAccess(c, input) {
43
- if (c.get("isInternalRequest")) {
44
- return { allowed: true };
45
- }
46
- const userId = c.get("userId");
47
- if (!userId) {
48
- await logTransactionPiiAccess(c, {
49
- ...input,
50
- outcome: "denied",
51
- reason: "missing_user",
52
- });
53
- return { allowed: false, response: c.json({ error: "Unauthorized" }, 401) };
54
- }
55
- const customAuthorizer = c.get("authorizeTransactionPii");
56
- if (customAuthorizer) {
57
- const allowed = await customAuthorizer({
58
- db: c.get("db"),
59
- userId,
60
- actor: c.get("actor"),
61
- callerType: c.get("callerType"),
62
- scopes: c.get("scopes"),
63
- isInternalRequest: c.get("isInternalRequest"),
64
- ...input,
65
- });
66
- if (!allowed) {
67
- await logTransactionPiiAccess(c, {
68
- ...input,
69
- outcome: "denied",
70
- reason: "custom_policy_denied",
71
- });
72
- return { allowed: false, response: c.json({ error: "Forbidden" }, 403) };
73
- }
74
- return { allowed: true };
75
- }
76
- const allowed = hasPiiScope(c.get("scopes"), input.action) || c.get("actor") === "staff";
77
- if (!allowed) {
78
- await logTransactionPiiAccess(c, {
79
- ...input,
80
- outcome: "denied",
81
- reason: "insufficient_scope",
82
- metadata: { actor: c.get("actor") ?? null },
83
- });
84
- return { allowed: false, response: c.json({ error: "Forbidden" }, 403) };
85
- }
86
- return { allowed: true };
87
- }
88
- export const transactionsRoutes = new Hono()
89
- .get("/offers", async (c) => {
90
- const query = offerListQuerySchema.parse(Object.fromEntries(new URL(c.req.url).searchParams));
91
- return c.json(await transactionsService.listOffers(c.get("db"), query));
92
- })
93
- .post("/offers", async (c) => {
94
- return c.json({
95
- data: await transactionsService.createOffer(c.get("db"), insertOfferSchema.parse(await c.req.json())),
96
- }, 201);
97
- })
98
- .get("/offers/:id", async (c) => {
99
- const row = await transactionsService.getOfferById(c.get("db"), c.req.param("id"));
100
- if (!row)
101
- return c.json({ error: "Offer not found" }, 404);
102
- return c.json({ data: row });
103
- })
104
- .patch("/offers/:id", async (c) => {
105
- const row = await transactionsService.updateOffer(c.get("db"), c.req.param("id"), updateOfferSchema.parse(await c.req.json()));
106
- if (!row)
107
- return c.json({ error: "Offer not found" }, 404);
108
- return c.json({ data: row });
109
- })
110
- .delete("/offers/:id", async (c) => {
111
- const row = await transactionsService.deleteOffer(c.get("db"), c.req.param("id"));
112
- if (!row)
113
- return c.json({ error: "Offer not found" }, 404);
114
- return c.json({ success: true });
115
- })
116
- .get("/offer-participants", async (c) => {
117
- const query = offerParticipantListQuerySchema.parse(Object.fromEntries(new URL(c.req.url).searchParams));
118
- return c.json(await transactionsService.listOfferParticipants(c.get("db"), query));
119
- })
120
- .post("/offer-participants", async (c) => {
121
- const payload = insertOfferParticipantSchema.parse(await c.req.json());
122
- return c.json({
123
- data: await (async () => {
124
- const row = await transactionsService.createOfferParticipant(c.get("db"), payload);
125
- if (!row)
126
- return row;
127
- if (hasParticipantIdentityInput(payload)) {
128
- const pii = createTransactionPiiService({
129
- kms: createKmsProviderFromEnv(getRuntimeEnv(c)),
130
- onAudit: async (event) => {
131
- await logTransactionPiiAccess(c, {
132
- participantKind: event.participantKind,
133
- parentId: row.offerId,
134
- participantId: event.participantId,
135
- action: event.action,
136
- outcome: "allowed",
137
- });
138
- },
139
- });
140
- await pii.upsertParticipantIdentity(c.get("db"), "offer", row.id, payload, c.get("userId"));
141
- return transactionsService.getOfferParticipantById(c.get("db"), row.id);
142
- }
143
- return row;
144
- })(),
145
- }, 201);
146
- })
147
- .get("/offer-participants/:id", async (c) => {
148
- const row = await transactionsService.getOfferParticipantById(c.get("db"), c.req.param("id"));
149
- if (!row)
150
- return c.json({ error: "Offer participant not found" }, 404);
151
- return c.json({ data: row });
152
- })
153
- .patch("/offer-participants/:id", async (c) => {
154
- const payload = updateOfferParticipantSchema.parse(await c.req.json());
155
- const row = await transactionsService.updateOfferParticipant(c.get("db"), c.req.param("id"), payload);
156
- if (!row)
157
- return c.json({ error: "Offer participant not found" }, 404);
158
- if (hasParticipantIdentityInput(payload)) {
159
- const pii = createTransactionPiiService({
160
- kms: createKmsProviderFromEnv(getRuntimeEnv(c)),
161
- onAudit: async (event) => {
162
- await logTransactionPiiAccess(c, {
163
- participantKind: event.participantKind,
164
- parentId: row.offerId,
165
- participantId: event.participantId,
166
- action: event.action,
167
- outcome: "allowed",
168
- });
169
- },
170
- });
171
- await pii.upsertParticipantIdentity(c.get("db"), "offer", row.id, payload, c.get("userId"));
172
- return c.json({
173
- data: await transactionsService.getOfferParticipantById(c.get("db"), row.id),
174
- });
175
- }
176
- return c.json({ data: row });
177
- })
178
- .get("/offer-participants/:id/travel-details", async (c) => {
179
- const participant = await transactionsService.getOfferParticipantById(c.get("db"), c.req.param("id"));
180
- if (!participant)
181
- return c.json({ error: "Offer participant not found" }, 404);
182
- const auth = await authorizeTransactionPiiAccess(c, {
183
- participantKind: "offer",
184
- participantId: participant.id,
185
- parentId: participant.offerId,
186
- action: "read",
187
- });
188
- if (!auth.allowed)
189
- return auth.response;
190
- const pii = createTransactionPiiService({
191
- kms: createKmsProviderFromEnv(getRuntimeEnv(c)),
192
- onAudit: async (event) => {
193
- await logTransactionPiiAccess(c, {
194
- participantKind: event.participantKind,
195
- parentId: participant.offerId,
196
- participantId: event.participantId,
197
- action: event.action,
198
- outcome: "allowed",
199
- });
200
- },
201
- });
202
- const row = await pii.getParticipantIdentity(c.get("db"), "offer", participant.id, c.get("userId"));
203
- if (!row) {
204
- await logTransactionPiiAccess(c, {
205
- participantKind: "offer",
206
- parentId: participant.offerId,
207
- participantId: participant.id,
208
- action: "read",
209
- outcome: "denied",
210
- reason: "travel_details_not_found",
211
- });
212
- return c.json({ error: "Offer participant travel details not found" }, 404);
213
- }
214
- return c.json({ data: row });
215
- })
216
- .patch("/offer-participants/:id/travel-details", async (c) => {
217
- const participant = await transactionsService.getOfferParticipantById(c.get("db"), c.req.param("id"));
218
- if (!participant)
219
- return c.json({ error: "Offer participant not found" }, 404);
220
- const auth = await authorizeTransactionPiiAccess(c, {
221
- participantKind: "offer",
222
- participantId: participant.id,
223
- parentId: participant.offerId,
224
- action: "update",
225
- });
226
- if (!auth.allowed)
227
- return auth.response;
228
- const pii = createTransactionPiiService({
229
- kms: createKmsProviderFromEnv(getRuntimeEnv(c)),
230
- onAudit: async (event) => {
231
- await logTransactionPiiAccess(c, {
232
- participantKind: event.participantKind,
233
- parentId: participant.offerId,
234
- participantId: event.participantId,
235
- action: event.action,
236
- outcome: "allowed",
237
- });
238
- },
239
- });
240
- const row = await pii.upsertParticipantIdentity(c.get("db"), "offer", participant.id, updateOfferParticipantSchema.parse(await c.req.json()), c.get("userId"));
241
- if (!row)
242
- return c.json({ error: "Offer participant not found" }, 404);
243
- return c.json({ data: row });
244
- })
245
- .delete("/offer-participants/:id/travel-details", async (c) => {
246
- const participant = await transactionsService.getOfferParticipantById(c.get("db"), c.req.param("id"));
247
- if (!participant)
248
- return c.json({ error: "Offer participant not found" }, 404);
249
- const auth = await authorizeTransactionPiiAccess(c, {
250
- participantKind: "offer",
251
- participantId: participant.id,
252
- parentId: participant.offerId,
253
- action: "delete",
254
- });
255
- if (!auth.allowed)
256
- return auth.response;
257
- const pii = createTransactionPiiService({
258
- kms: createKmsProviderFromEnv(getRuntimeEnv(c)),
259
- onAudit: async (event) => {
260
- await logTransactionPiiAccess(c, {
261
- participantKind: event.participantKind,
262
- parentId: participant.offerId,
263
- participantId: event.participantId,
264
- action: event.action,
265
- outcome: "allowed",
266
- });
267
- },
268
- });
269
- const row = await pii.deleteParticipantIdentity(c.get("db"), "offer", participant.id, c.get("userId"));
270
- if (!row)
271
- return c.json({ error: "Offer participant travel details not found" }, 404);
272
- return c.json({ success: true });
273
- })
274
- .delete("/offer-participants/:id", async (c) => {
275
- const row = await transactionsService.deleteOfferParticipant(c.get("db"), c.req.param("id"));
276
- if (!row)
277
- return c.json({ error: "Offer participant not found" }, 404);
278
- return c.json({ success: true });
279
- })
280
- .get("/offer-items", async (c) => {
281
- const query = offerItemListQuerySchema.parse(Object.fromEntries(new URL(c.req.url).searchParams));
282
- return c.json(await transactionsService.listOfferItems(c.get("db"), query));
283
- })
284
- .post("/offer-items", async (c) => {
285
- return c.json({
286
- data: await transactionsService.createOfferItem(c.get("db"), insertOfferItemSchema.parse(await c.req.json())),
287
- }, 201);
288
- })
289
- .get("/offer-items/:id", async (c) => {
290
- const row = await transactionsService.getOfferItemById(c.get("db"), c.req.param("id"));
291
- if (!row)
292
- return c.json({ error: "Offer item not found" }, 404);
293
- return c.json({ data: row });
294
- })
295
- .patch("/offer-items/:id", async (c) => {
296
- const row = await transactionsService.updateOfferItem(c.get("db"), c.req.param("id"), updateOfferItemSchema.parse(await c.req.json()));
297
- if (!row)
298
- return c.json({ error: "Offer item not found" }, 404);
299
- return c.json({ data: row });
300
- })
301
- .delete("/offer-items/:id", async (c) => {
302
- const row = await transactionsService.deleteOfferItem(c.get("db"), c.req.param("id"));
303
- if (!row)
304
- return c.json({ error: "Offer item not found" }, 404);
305
- return c.json({ success: true });
306
- })
307
- .get("/offer-item-participants", async (c) => {
308
- const query = offerItemParticipantListQuerySchema.parse(Object.fromEntries(new URL(c.req.url).searchParams));
309
- return c.json(await transactionsService.listOfferItemParticipants(c.get("db"), query));
310
- })
311
- .post("/offer-item-participants", async (c) => {
312
- return c.json({
313
- data: await transactionsService.createOfferItemParticipant(c.get("db"), insertOfferItemParticipantSchema.parse(await c.req.json())),
314
- }, 201);
315
- })
316
- .get("/offer-item-participants/:id", async (c) => {
317
- const row = await transactionsService.getOfferItemParticipantById(c.get("db"), c.req.param("id"));
318
- if (!row)
319
- return c.json({ error: "Offer item participant not found" }, 404);
320
- return c.json({ data: row });
321
- })
322
- .patch("/offer-item-participants/:id", async (c) => {
323
- const row = await transactionsService.updateOfferItemParticipant(c.get("db"), c.req.param("id"), updateOfferItemParticipantSchema.parse(await c.req.json()));
324
- if (!row)
325
- return c.json({ error: "Offer item participant not found" }, 404);
326
- return c.json({ data: row });
327
- })
328
- .delete("/offer-item-participants/:id", async (c) => {
329
- const row = await transactionsService.deleteOfferItemParticipant(c.get("db"), c.req.param("id"));
330
- if (!row)
331
- return c.json({ error: "Offer item participant not found" }, 404);
332
- return c.json({ success: true });
333
- })
334
- .get("/orders", async (c) => {
335
- const query = orderListQuerySchema.parse(Object.fromEntries(new URL(c.req.url).searchParams));
336
- return c.json(await transactionsService.listOrders(c.get("db"), query));
337
- })
338
- .post("/orders", async (c) => {
339
- return c.json({
340
- data: await transactionsService.createOrder(c.get("db"), insertOrderSchema.parse(await c.req.json())),
341
- }, 201);
342
- })
343
- .get("/orders/:id", async (c) => {
344
- const row = await transactionsService.getOrderById(c.get("db"), c.req.param("id"));
345
- if (!row)
346
- return c.json({ error: "Order not found" }, 404);
347
- return c.json({ data: row });
348
- })
349
- .patch("/orders/:id", async (c) => {
350
- const row = await transactionsService.updateOrder(c.get("db"), c.req.param("id"), updateOrderSchema.parse(await c.req.json()));
351
- if (!row)
352
- return c.json({ error: "Order not found" }, 404);
353
- return c.json({ data: row });
354
- })
355
- .delete("/orders/:id", async (c) => {
356
- const row = await transactionsService.deleteOrder(c.get("db"), c.req.param("id"));
357
- if (!row)
358
- return c.json({ error: "Order not found" }, 404);
359
- return c.json({ success: true });
360
- })
361
- .get("/order-participants", async (c) => {
362
- const query = orderParticipantListQuerySchema.parse(Object.fromEntries(new URL(c.req.url).searchParams));
363
- return c.json(await transactionsService.listOrderParticipants(c.get("db"), query));
364
- })
365
- .post("/order-participants", async (c) => {
366
- const payload = insertOrderParticipantSchema.parse(await c.req.json());
367
- return c.json({
368
- data: await (async () => {
369
- const row = await transactionsService.createOrderParticipant(c.get("db"), payload);
370
- if (!row)
371
- return row;
372
- if (hasParticipantIdentityInput(payload)) {
373
- const pii = createTransactionPiiService({
374
- kms: createKmsProviderFromEnv(getRuntimeEnv(c)),
375
- onAudit: async (event) => {
376
- await logTransactionPiiAccess(c, {
377
- participantKind: event.participantKind,
378
- parentId: row.orderId,
379
- participantId: event.participantId,
380
- action: event.action,
381
- outcome: "allowed",
382
- });
383
- },
384
- });
385
- await pii.upsertParticipantIdentity(c.get("db"), "order", row.id, payload, c.get("userId"));
386
- return transactionsService.getOrderParticipantById(c.get("db"), row.id);
387
- }
388
- return row;
389
- })(),
390
- }, 201);
391
- })
392
- .get("/order-participants/:id", async (c) => {
393
- const row = await transactionsService.getOrderParticipantById(c.get("db"), c.req.param("id"));
394
- if (!row)
395
- return c.json({ error: "Order participant not found" }, 404);
396
- return c.json({ data: row });
397
- })
398
- .patch("/order-participants/:id", async (c) => {
399
- const payload = updateOrderParticipantSchema.parse(await c.req.json());
400
- const row = await transactionsService.updateOrderParticipant(c.get("db"), c.req.param("id"), payload);
401
- if (!row)
402
- return c.json({ error: "Order participant not found" }, 404);
403
- if (hasParticipantIdentityInput(payload)) {
404
- const pii = createTransactionPiiService({
405
- kms: createKmsProviderFromEnv(getRuntimeEnv(c)),
406
- onAudit: async (event) => {
407
- await logTransactionPiiAccess(c, {
408
- participantKind: event.participantKind,
409
- parentId: row.orderId,
410
- participantId: event.participantId,
411
- action: event.action,
412
- outcome: "allowed",
413
- });
414
- },
415
- });
416
- await pii.upsertParticipantIdentity(c.get("db"), "order", row.id, payload, c.get("userId"));
417
- return c.json({
418
- data: await transactionsService.getOrderParticipantById(c.get("db"), row.id),
419
- });
420
- }
421
- return c.json({ data: row });
422
- })
423
- .get("/order-participants/:id/travel-details", async (c) => {
424
- const participant = await transactionsService.getOrderParticipantById(c.get("db"), c.req.param("id"));
425
- if (!participant)
426
- return c.json({ error: "Order participant not found" }, 404);
427
- const auth = await authorizeTransactionPiiAccess(c, {
428
- participantKind: "order",
429
- participantId: participant.id,
430
- parentId: participant.orderId,
431
- action: "read",
432
- });
433
- if (!auth.allowed)
434
- return auth.response;
435
- const pii = createTransactionPiiService({
436
- kms: createKmsProviderFromEnv(getRuntimeEnv(c)),
437
- onAudit: async (event) => {
438
- await logTransactionPiiAccess(c, {
439
- participantKind: event.participantKind,
440
- parentId: participant.orderId,
441
- participantId: event.participantId,
442
- action: event.action,
443
- outcome: "allowed",
444
- });
445
- },
446
- });
447
- const row = await pii.getParticipantIdentity(c.get("db"), "order", participant.id, c.get("userId"));
448
- if (!row) {
449
- await logTransactionPiiAccess(c, {
450
- participantKind: "order",
451
- parentId: participant.orderId,
452
- participantId: participant.id,
453
- action: "read",
454
- outcome: "denied",
455
- reason: "travel_details_not_found",
456
- });
457
- return c.json({ error: "Order participant travel details not found" }, 404);
458
- }
459
- return c.json({ data: row });
460
- })
461
- .patch("/order-participants/:id/travel-details", async (c) => {
462
- const participant = await transactionsService.getOrderParticipantById(c.get("db"), c.req.param("id"));
463
- if (!participant)
464
- return c.json({ error: "Order participant not found" }, 404);
465
- const auth = await authorizeTransactionPiiAccess(c, {
466
- participantKind: "order",
467
- participantId: participant.id,
468
- parentId: participant.orderId,
469
- action: "update",
470
- });
471
- if (!auth.allowed)
472
- return auth.response;
473
- const pii = createTransactionPiiService({
474
- kms: createKmsProviderFromEnv(getRuntimeEnv(c)),
475
- onAudit: async (event) => {
476
- await logTransactionPiiAccess(c, {
477
- participantKind: event.participantKind,
478
- parentId: participant.orderId,
479
- participantId: event.participantId,
480
- action: event.action,
481
- outcome: "allowed",
482
- });
483
- },
484
- });
485
- const row = await pii.upsertParticipantIdentity(c.get("db"), "order", participant.id, updateOrderParticipantSchema.parse(await c.req.json()), c.get("userId"));
486
- if (!row)
487
- return c.json({ error: "Order participant not found" }, 404);
488
- return c.json({ data: row });
489
- })
490
- .delete("/order-participants/:id/travel-details", async (c) => {
491
- const participant = await transactionsService.getOrderParticipantById(c.get("db"), c.req.param("id"));
492
- if (!participant)
493
- return c.json({ error: "Order participant not found" }, 404);
494
- const auth = await authorizeTransactionPiiAccess(c, {
495
- participantKind: "order",
496
- participantId: participant.id,
497
- parentId: participant.orderId,
498
- action: "delete",
499
- });
500
- if (!auth.allowed)
501
- return auth.response;
502
- const pii = createTransactionPiiService({
503
- kms: createKmsProviderFromEnv(getRuntimeEnv(c)),
504
- onAudit: async (event) => {
505
- await logTransactionPiiAccess(c, {
506
- participantKind: event.participantKind,
507
- parentId: participant.orderId,
508
- participantId: event.participantId,
509
- action: event.action,
510
- outcome: "allowed",
511
- });
512
- },
513
- });
514
- const row = await pii.deleteParticipantIdentity(c.get("db"), "order", participant.id, c.get("userId"));
515
- if (!row)
516
- return c.json({ error: "Order participant travel details not found" }, 404);
517
- return c.json({ success: true });
518
- })
519
- .delete("/order-participants/:id", async (c) => {
520
- const row = await transactionsService.deleteOrderParticipant(c.get("db"), c.req.param("id"));
521
- if (!row)
522
- return c.json({ error: "Order participant not found" }, 404);
523
- return c.json({ success: true });
524
- })
525
- .get("/order-items", async (c) => {
526
- const query = orderItemListQuerySchema.parse(Object.fromEntries(new URL(c.req.url).searchParams));
527
- return c.json(await transactionsService.listOrderItems(c.get("db"), query));
528
- })
529
- .post("/order-items", async (c) => {
530
- return c.json({
531
- data: await transactionsService.createOrderItem(c.get("db"), insertOrderItemSchema.parse(await c.req.json())),
532
- }, 201);
533
- })
534
- .get("/order-items/:id", async (c) => {
535
- const row = await transactionsService.getOrderItemById(c.get("db"), c.req.param("id"));
536
- if (!row)
537
- return c.json({ error: "Order item not found" }, 404);
538
- return c.json({ data: row });
539
- })
540
- .patch("/order-items/:id", async (c) => {
541
- const row = await transactionsService.updateOrderItem(c.get("db"), c.req.param("id"), updateOrderItemSchema.parse(await c.req.json()));
542
- if (!row)
543
- return c.json({ error: "Order item not found" }, 404);
544
- return c.json({ data: row });
545
- })
546
- .delete("/order-items/:id", async (c) => {
547
- const row = await transactionsService.deleteOrderItem(c.get("db"), c.req.param("id"));
548
- if (!row)
549
- return c.json({ error: "Order item not found" }, 404);
550
- return c.json({ success: true });
551
- })
552
- .get("/order-item-participants", async (c) => {
553
- const query = orderItemParticipantListQuerySchema.parse(Object.fromEntries(new URL(c.req.url).searchParams));
554
- return c.json(await transactionsService.listOrderItemParticipants(c.get("db"), query));
555
- })
556
- .post("/order-item-participants", async (c) => {
557
- return c.json({
558
- data: await transactionsService.createOrderItemParticipant(c.get("db"), insertOrderItemParticipantSchema.parse(await c.req.json())),
559
- }, 201);
560
- })
561
- .get("/order-item-participants/:id", async (c) => {
562
- const row = await transactionsService.getOrderItemParticipantById(c.get("db"), c.req.param("id"));
563
- if (!row)
564
- return c.json({ error: "Order item participant not found" }, 404);
565
- return c.json({ data: row });
566
- })
567
- .patch("/order-item-participants/:id", async (c) => {
568
- const row = await transactionsService.updateOrderItemParticipant(c.get("db"), c.req.param("id"), updateOrderItemParticipantSchema.parse(await c.req.json()));
569
- if (!row)
570
- return c.json({ error: "Order item participant not found" }, 404);
571
- return c.json({ data: row });
572
- })
573
- .delete("/order-item-participants/:id", async (c) => {
574
- const row = await transactionsService.deleteOrderItemParticipant(c.get("db"), c.req.param("id"));
575
- if (!row)
576
- return c.json({ error: "Order item participant not found" }, 404);
577
- return c.json({ success: true });
578
- })
579
- .get("/order-terms", async (c) => {
580
- const query = orderTermListQuerySchema.parse(Object.fromEntries(new URL(c.req.url).searchParams));
581
- return c.json(await transactionsService.listOrderTerms(c.get("db"), query));
582
- })
583
- .post("/order-terms", async (c) => {
584
- return c.json({
585
- data: await transactionsService.createOrderTerm(c.get("db"), insertOrderTermSchema.parse(await c.req.json())),
586
- }, 201);
587
- })
588
- .get("/order-terms/:id", async (c) => {
589
- const row = await transactionsService.getOrderTermById(c.get("db"), c.req.param("id"));
590
- if (!row)
591
- return c.json({ error: "Order term not found" }, 404);
592
- return c.json({ data: row });
593
- })
594
- .patch("/order-terms/:id", async (c) => {
595
- const row = await transactionsService.updateOrderTerm(c.get("db"), c.req.param("id"), updateOrderTermSchema.parse(await c.req.json()));
596
- if (!row)
597
- return c.json({ error: "Order term not found" }, 404);
598
- return c.json({ data: row });
599
- })
600
- .delete("/order-terms/:id", async (c) => {
601
- const row = await transactionsService.deleteOrderTerm(c.get("db"), c.req.param("id"));
602
- if (!row)
603
- return c.json({ error: "Order term not found" }, 404);
604
- return c.json({ success: true });
605
- });
2
+ import { transactionOfferRoutes } from "./routes-offers.js";
3
+ import { transactionOrderRoutes } from "./routes-orders.js";
4
+ export const transactionsRoutes = new Hono();
5
+ transactionsRoutes.route("/", transactionOfferRoutes);
6
+ transactionsRoutes.route("/", transactionOrderRoutes);