@instockng/api-client 1.0.6 → 1.0.8

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 (178) hide show
  1. package/dist/apps/backend/src/generated/zod/index.d.ts +1114 -0
  2. package/dist/apps/backend/src/generated/zod/index.js +670 -0
  3. package/dist/apps/backend/src/http-app.d.ts +40 -0
  4. package/dist/apps/backend/src/http-app.js +134 -0
  5. package/dist/apps/backend/src/lib/brand-response.d.ts +14 -0
  6. package/dist/apps/backend/src/lib/brand-response.js +8 -0
  7. package/dist/apps/backend/src/lib/cart-helpers.d.ts +282 -0
  8. package/dist/apps/backend/src/lib/cart-helpers.js +121 -0
  9. package/dist/apps/backend/src/lib/cart-recovery.d.ts +30 -0
  10. package/dist/apps/backend/src/lib/cart-recovery.js +147 -0
  11. package/dist/apps/backend/src/lib/cart-response.d.ts +121 -0
  12. package/dist/apps/backend/src/lib/cart-response.js +150 -0
  13. package/dist/apps/backend/src/lib/clerk.d.ts +18 -0
  14. package/dist/apps/backend/src/lib/clerk.js +190 -0
  15. package/dist/apps/backend/src/lib/delivery-zone-response.d.ts +64 -0
  16. package/dist/apps/backend/src/lib/delivery-zone-response.js +24 -0
  17. package/dist/apps/backend/src/lib/discount-code-response.d.ts +42 -0
  18. package/dist/apps/backend/src/lib/discount-code-response.js +19 -0
  19. package/dist/apps/backend/src/lib/discount.d.ts +20 -0
  20. package/dist/apps/backend/src/lib/discount.js +35 -0
  21. package/dist/apps/backend/src/lib/inventory.d.ts +26 -0
  22. package/dist/apps/backend/src/lib/inventory.js +160 -0
  23. package/dist/apps/backend/src/lib/meta-capi.d.ts +53 -0
  24. package/dist/apps/backend/src/lib/meta-capi.js +151 -0
  25. package/dist/apps/backend/src/lib/openapi.d.ts +36 -0
  26. package/dist/apps/backend/src/lib/openapi.js +69 -0
  27. package/dist/apps/backend/src/lib/order-recovery.d.ts +459 -0
  28. package/dist/apps/backend/src/lib/order-recovery.js +378 -0
  29. package/dist/apps/backend/src/lib/order-response.d.ts +138 -0
  30. package/dist/apps/backend/src/lib/order-response.js +61 -0
  31. package/dist/apps/backend/src/lib/pricing.d.ts +39 -0
  32. package/dist/apps/backend/src/lib/pricing.js +62 -0
  33. package/dist/apps/backend/src/lib/prisma.d.ts +9 -0
  34. package/dist/apps/backend/src/lib/prisma.js +30 -0
  35. package/dist/apps/backend/src/lib/product-response.d.ts +82 -0
  36. package/dist/apps/backend/src/lib/product-response.js +29 -0
  37. package/dist/apps/backend/src/lib/sentry.d.ts +48 -0
  38. package/dist/apps/backend/src/lib/sentry.js +180 -0
  39. package/dist/apps/backend/src/lib/utils.d.ts +32 -0
  40. package/dist/apps/backend/src/lib/utils.js +63 -0
  41. package/dist/apps/backend/src/middleware/clerk-auth.d.ts +8 -0
  42. package/dist/apps/backend/src/middleware/clerk-auth.js +89 -0
  43. package/dist/apps/backend/src/middleware/cors.d.ts +8 -0
  44. package/dist/apps/backend/src/middleware/cors.js +11 -0
  45. package/dist/apps/backend/src/notifications/producers/meta-capi-producer.d.ts +62 -0
  46. package/dist/apps/backend/src/notifications/producers/meta-capi-producer.js +180 -0
  47. package/dist/apps/backend/src/notifications/producers/order-notification.d.ts +9 -0
  48. package/dist/apps/backend/src/notifications/producers/order-notification.js +18 -0
  49. package/dist/apps/backend/src/notifications/producers/prospect-recovery-notification.d.ts +10 -0
  50. package/dist/apps/backend/src/notifications/producers/prospect-recovery-notification.js +11 -0
  51. package/dist/apps/backend/src/routes/admin/abandoned-carts.d.ts +605 -0
  52. package/dist/apps/backend/src/routes/admin/abandoned-carts.js +194 -0
  53. package/dist/apps/backend/src/routes/admin/brands.d.ts +175 -0
  54. package/dist/apps/backend/src/routes/admin/brands.js +118 -0
  55. package/dist/apps/backend/src/routes/admin/customers.d.ts +308 -0
  56. package/dist/apps/backend/src/routes/admin/customers.js +39 -0
  57. package/dist/apps/backend/src/routes/admin/delivery-zones.d.ts +446 -0
  58. package/dist/apps/backend/src/routes/admin/delivery-zones.js +300 -0
  59. package/dist/apps/backend/src/routes/admin/discount-codes.d.ts +478 -0
  60. package/dist/apps/backend/src/routes/admin/discount-codes.js +418 -0
  61. package/dist/apps/backend/src/routes/admin/inventory.d.ts +273 -0
  62. package/dist/apps/backend/src/routes/admin/inventory.js +199 -0
  63. package/dist/apps/backend/src/routes/admin/orders.d.ts +1780 -0
  64. package/dist/apps/backend/src/routes/admin/orders.js +552 -0
  65. package/dist/apps/backend/src/routes/admin/products.d.ts +860 -0
  66. package/dist/apps/backend/src/routes/admin/products.js +126 -0
  67. package/dist/apps/backend/src/routes/admin/stats.d.ts +290 -0
  68. package/dist/apps/backend/src/routes/admin/stats.js +55 -0
  69. package/dist/apps/backend/src/routes/admin/variants.d.ts +239 -0
  70. package/dist/apps/backend/src/routes/admin/variants.js +197 -0
  71. package/dist/apps/backend/src/routes/admin/warehouses.d.ts +373 -0
  72. package/dist/apps/backend/src/routes/admin/warehouses.js +123 -0
  73. package/dist/apps/backend/src/routes/public/brands.d.ts +40 -0
  74. package/dist/apps/backend/src/routes/public/brands.js +38 -0
  75. package/dist/apps/backend/src/routes/public/carts.d.ts +2657 -0
  76. package/dist/apps/backend/src/routes/public/carts.js +778 -0
  77. package/dist/apps/backend/src/routes/public/delivery-zones.d.ts +37 -0
  78. package/dist/apps/backend/src/routes/public/delivery-zones.js +64 -0
  79. package/dist/apps/backend/src/routes/public/orders.d.ts +609 -0
  80. package/dist/apps/backend/src/routes/public/orders.js +184 -0
  81. package/dist/apps/backend/src/routes/public/products.d.ts +449 -0
  82. package/dist/apps/backend/src/routes/public/products.js +133 -0
  83. package/dist/apps/backend/src/types/index.d.ts +43 -0
  84. package/dist/apps/backend/src/types/index.js +2 -0
  85. package/dist/apps/backend/src/validators/brand.d.ts +17 -0
  86. package/dist/apps/backend/src/validators/brand.js +15 -0
  87. package/dist/apps/backend/src/validators/delivery-zone.d.ts +35 -0
  88. package/dist/apps/backend/src/validators/delivery-zone.js +55 -0
  89. package/dist/apps/backend/src/validators/discount-code.d.ts +74 -0
  90. package/dist/apps/backend/src/validators/discount-code.js +50 -0
  91. package/dist/apps/backend/src/validators/inventory.d.ts +20 -0
  92. package/dist/apps/backend/src/validators/inventory.js +15 -0
  93. package/dist/apps/backend/src/validators/order.d.ts +58 -0
  94. package/dist/apps/backend/src/validators/order.js +62 -0
  95. package/dist/apps/backend/src/validators/product.d.ts +18 -0
  96. package/dist/apps/backend/src/validators/product.js +19 -0
  97. package/dist/apps/backend/src/validators/variant.d.ts +19 -0
  98. package/dist/apps/backend/src/validators/variant.js +19 -0
  99. package/dist/apps/backend/src/validators/warehouse.d.ts +15 -0
  100. package/dist/apps/backend/src/validators/warehouse.js +15 -0
  101. package/dist/fetchers/orders.d.ts +258 -1
  102. package/dist/hooks/admin/orders.d.ts +285 -3
  103. package/dist/hooks/admin/orders.js +7 -4
  104. package/dist/hooks/public/orders.d.ts +258 -1
  105. package/dist/packages/api-client/src/backend-types.d.ts +10 -0
  106. package/dist/packages/api-client/src/backend-types.js +10 -0
  107. package/dist/packages/api-client/src/client.d.ts +20 -0
  108. package/dist/packages/api-client/src/client.js +40 -0
  109. package/dist/packages/api-client/src/enum-types.d.ts +8 -0
  110. package/dist/packages/api-client/src/enum-types.js +5 -0
  111. package/dist/packages/api-client/src/fetchers/brands.d.ts +25 -0
  112. package/dist/packages/api-client/src/fetchers/brands.js +26 -0
  113. package/dist/packages/api-client/src/fetchers/carts.d.ts +2337 -0
  114. package/dist/packages/api-client/src/fetchers/carts.js +174 -0
  115. package/dist/packages/api-client/src/fetchers/delivery-zones.d.ts +30 -0
  116. package/dist/packages/api-client/src/fetchers/delivery-zones.js +26 -0
  117. package/dist/packages/api-client/src/fetchers/index.d.ts +22 -0
  118. package/dist/packages/api-client/src/fetchers/index.js +22 -0
  119. package/dist/packages/api-client/src/fetchers/orders.d.ts +544 -0
  120. package/dist/packages/api-client/src/fetchers/orders.js +44 -0
  121. package/dist/packages/api-client/src/fetchers/products.d.ts +386 -0
  122. package/dist/packages/api-client/src/fetchers/products.js +42 -0
  123. package/dist/packages/api-client/src/hooks/admin/abandoned-carts.d.ts +535 -0
  124. package/dist/packages/api-client/src/hooks/admin/abandoned-carts.js +83 -0
  125. package/dist/packages/api-client/src/hooks/admin/brands.d.ts +79 -0
  126. package/dist/packages/api-client/src/hooks/admin/brands.js +108 -0
  127. package/dist/packages/api-client/src/hooks/admin/customers.d.ts +280 -0
  128. package/dist/packages/api-client/src/hooks/admin/customers.js +26 -0
  129. package/dist/packages/api-client/src/hooks/admin/delivery-zones.d.ts +278 -0
  130. package/dist/packages/api-client/src/hooks/admin/delivery-zones.js +176 -0
  131. package/dist/packages/api-client/src/hooks/admin/discount-codes.d.ts +299 -0
  132. package/dist/packages/api-client/src/hooks/admin/discount-codes.js +165 -0
  133. package/dist/packages/api-client/src/hooks/admin/index.d.ts +16 -0
  134. package/dist/packages/api-client/src/hooks/admin/index.js +16 -0
  135. package/dist/packages/api-client/src/hooks/admin/inventory.d.ts +224 -0
  136. package/dist/packages/api-client/src/hooks/admin/inventory.js +107 -0
  137. package/dist/packages/api-client/src/hooks/admin/orders.d.ts +1674 -0
  138. package/dist/packages/api-client/src/hooks/admin/orders.js +178 -0
  139. package/dist/packages/api-client/src/hooks/admin/products.d.ts +374 -0
  140. package/dist/packages/api-client/src/hooks/admin/products.js +89 -0
  141. package/dist/packages/api-client/src/hooks/admin/stats.d.ts +279 -0
  142. package/dist/packages/api-client/src/hooks/admin/stats.js +25 -0
  143. package/dist/packages/api-client/src/hooks/admin/variants.d.ts +115 -0
  144. package/dist/packages/api-client/src/hooks/admin/variants.js +127 -0
  145. package/dist/packages/api-client/src/hooks/admin/warehouses.d.ts +277 -0
  146. package/dist/packages/api-client/src/hooks/admin/warehouses.js +108 -0
  147. package/dist/packages/api-client/src/hooks/public/brands.d.ts +33 -0
  148. package/dist/packages/api-client/src/hooks/public/brands.js +30 -0
  149. package/dist/packages/api-client/src/hooks/public/carts.d.ts +2407 -0
  150. package/dist/packages/api-client/src/hooks/public/carts.js +213 -0
  151. package/dist/packages/api-client/src/hooks/public/delivery-zones.d.ts +36 -0
  152. package/dist/packages/api-client/src/hooks/public/delivery-zones.js +28 -0
  153. package/dist/packages/api-client/src/hooks/public/index.d.ts +10 -0
  154. package/dist/packages/api-client/src/hooks/public/index.js +10 -0
  155. package/dist/packages/api-client/src/hooks/public/orders.d.ts +563 -0
  156. package/dist/packages/api-client/src/hooks/public/orders.js +50 -0
  157. package/dist/packages/api-client/src/hooks/public/products.d.ts +398 -0
  158. package/dist/packages/api-client/src/hooks/public/products.js +47 -0
  159. package/dist/packages/api-client/src/hooks/use-query-unwrapped.d.ts +20 -0
  160. package/dist/packages/api-client/src/hooks/use-query-unwrapped.js +22 -0
  161. package/dist/packages/api-client/src/hooks/useApiConfig.d.ts +12 -0
  162. package/dist/packages/api-client/src/hooks/useApiConfig.js +14 -0
  163. package/dist/packages/api-client/src/index.d.ts +20 -0
  164. package/dist/packages/api-client/src/index.js +25 -0
  165. package/dist/packages/api-client/src/provider.d.ts +36 -0
  166. package/dist/packages/api-client/src/provider.js +54 -0
  167. package/dist/packages/api-client/src/rpc-client.d.ts +9639 -0
  168. package/dist/packages/api-client/src/rpc-client.js +78 -0
  169. package/dist/packages/api-client/src/rpc-types.d.ts +76 -0
  170. package/dist/packages/api-client/src/rpc-types.js +7 -0
  171. package/dist/packages/api-client/src/types.d.ts +34 -0
  172. package/dist/packages/api-client/src/types.js +16 -0
  173. package/dist/packages/api-client/src/utils/query-keys.d.ts +106 -0
  174. package/dist/packages/api-client/src/utils/query-keys.js +108 -0
  175. package/dist/rpc-client.d.ts +891 -319
  176. package/dist/utils/query-keys.d.ts +1 -1
  177. package/dist/utils/query-keys.js +1 -1
  178. package/package.json +1 -1
@@ -0,0 +1,40 @@
1
+ /**
2
+ * HTTP application setup
3
+ * This module is lazy-loaded only when handling HTTP requests
4
+ */
5
+ import publicOrders from './routes/public/orders';
6
+ import publicProducts from './routes/public/products';
7
+ import publicCarts from './routes/public/carts';
8
+ import publicDeliveryZones from './routes/public/delivery-zones';
9
+ import publicBrands from './routes/public/brands';
10
+ import adminOrders from './routes/admin/orders';
11
+ import adminBrands from './routes/admin/brands';
12
+ import adminProducts from './routes/admin/products';
13
+ import adminVariants from './routes/admin/variants';
14
+ import adminWarehouses from './routes/admin/warehouses';
15
+ import adminInventory from './routes/admin/inventory';
16
+ import adminCustomers from './routes/admin/customers';
17
+ import adminStats from './routes/admin/stats';
18
+ import adminAbandonedCarts from './routes/admin/abandoned-carts';
19
+ import adminDiscountCodes from './routes/admin/discount-codes';
20
+ import adminDeliveryZones from './routes/admin/delivery-zones';
21
+ export type CartsRPC = typeof publicCarts;
22
+ export type OrdersRPC = typeof publicOrders;
23
+ export type ProductsRPC = typeof publicProducts;
24
+ export type DeliveryZonesRPC = typeof publicDeliveryZones;
25
+ export type BrandsRPC = typeof publicBrands;
26
+ export type AdminOrdersRPC = typeof adminOrders;
27
+ export type AdminBrandsRPC = typeof adminBrands;
28
+ export type AdminProductsRPC = typeof adminProducts;
29
+ export type AdminVariantsRPC = typeof adminVariants;
30
+ export type AdminWarehousesRPC = typeof adminWarehouses;
31
+ export type AdminInventoryRPC = typeof adminInventory;
32
+ export type AdminCustomersRPC = typeof adminCustomers;
33
+ export type AdminStatsRPC = typeof adminStats;
34
+ export type AdminAbandonedCartsRPC = typeof adminAbandonedCarts;
35
+ export type AdminDiscountCodesRPC = typeof adminDiscountCodes;
36
+ export type AdminDeliveryZonesRPC = typeof adminDeliveryZones;
37
+ /**
38
+ * Create the HTTP application with all routes and middleware
39
+ */
40
+ export declare function createHttpApp(): import("@hono/zod-openapi").OpenAPIHono<import("./types").AppContext, {}, "/">;
@@ -0,0 +1,134 @@
1
+ /**
2
+ * HTTP application setup
3
+ * This module is lazy-loaded only when handling HTTP requests
4
+ */
5
+ import { Hono } from 'hono';
6
+ import { corsMiddleware } from './middleware/cors';
7
+ import { clerkAuthMiddleware } from './middleware/clerk-auth';
8
+ import { createOpenAPIApp, openapiConfig } from './lib/openapi';
9
+ import { swaggerUI } from '@hono/swagger-ui';
10
+ import { HTTPException } from 'hono/http-exception';
11
+ import { captureException, captureMessage } from './lib/sentry';
12
+ // Public routes
13
+ import publicOrders from './routes/public/orders';
14
+ import publicProducts from './routes/public/products';
15
+ import publicCarts from './routes/public/carts';
16
+ import publicDeliveryZones from './routes/public/delivery-zones';
17
+ import publicBrands from './routes/public/brands';
18
+ // Admin routes
19
+ import adminOrders from './routes/admin/orders';
20
+ import adminBrands from './routes/admin/brands';
21
+ import adminProducts from './routes/admin/products';
22
+ import adminVariants from './routes/admin/variants';
23
+ import adminWarehouses from './routes/admin/warehouses';
24
+ import adminInventory from './routes/admin/inventory';
25
+ import adminCustomers from './routes/admin/customers';
26
+ import adminStats from './routes/admin/stats';
27
+ import adminAbandonedCarts from './routes/admin/abandoned-carts';
28
+ import adminDiscountCodes from './routes/admin/discount-codes';
29
+ import adminDeliveryZones from './routes/admin/delivery-zones';
30
+ /**
31
+ * Create the HTTP application with all routes and middleware
32
+ */
33
+ export function createHttpApp() {
34
+ const app = createOpenAPIApp();
35
+ // Global CORS middleware
36
+ app.use('*', corsMiddleware);
37
+ // Health check
38
+ app.get('/', (c) => {
39
+ return c.json({
40
+ name: 'OMS API',
41
+ version: '1.0.0',
42
+ status: 'healthy',
43
+ });
44
+ });
45
+ app.get('/health', (c) => {
46
+ return c.json({ status: 'ok' });
47
+ });
48
+ // OpenAPI documentation
49
+ app.doc('/openapi.json', openapiConfig);
50
+ // Swagger UI
51
+ app.get('/docs', swaggerUI({ url: '/openapi.json' }));
52
+ // Admin API routes (Clerk auth protected)
53
+ // Note: Must be mounted BEFORE public routes to match /v1/admin/* before /v1/*
54
+ const adminApp = new Hono();
55
+ adminApp.use('*', clerkAuthMiddleware);
56
+ adminApp
57
+ .route('/orders', adminOrders)
58
+ .route('/brands', adminBrands)
59
+ .route('/products', adminProducts)
60
+ .route('/variants', adminVariants)
61
+ .route('/warehouses', adminWarehouses)
62
+ .route('/inventory', adminInventory)
63
+ .route('/customers', adminCustomers)
64
+ .route('/stats', adminStats)
65
+ .route('/abandoned-carts', adminAbandonedCarts)
66
+ .route('/discount-codes', adminDiscountCodes)
67
+ .route('/delivery-zones', adminDeliveryZones);
68
+ app.route('/v1/admin', adminApp);
69
+ // Public API routes (CORS protected, no API key needed)
70
+ // UUID-based resources (orders, carts) are self-authenticating
71
+ // Rate limiting and CORS provide abuse protection
72
+ const publicApi = createOpenAPIApp();
73
+ publicApi.route('/orders', publicOrders);
74
+ publicApi.route('/products', publicProducts);
75
+ publicApi.route('/carts', publicCarts);
76
+ publicApi.route('/delivery-zones', publicDeliveryZones);
77
+ publicApi.route('/brands', publicBrands);
78
+ app.route('/v1', publicApi);
79
+ // 404 handler
80
+ app.notFound((c) => {
81
+ return c.json({
82
+ error: {
83
+ code: 'NOT_FOUND',
84
+ message: 'The requested endpoint does not exist',
85
+ },
86
+ }, 404);
87
+ });
88
+ // Error handler
89
+ app.onError((err, c) => {
90
+ if (err instanceof HTTPException && err.status === 400) {
91
+ // Track validation errors with sampling
92
+ captureMessage('Malformed JSON in request body', {
93
+ level: 'warning',
94
+ tags: {
95
+ error_code: 'BAD_REQUEST',
96
+ route: c.req.path,
97
+ method: c.req.method,
98
+ },
99
+ extra: {
100
+ url: c.req.url,
101
+ },
102
+ sampleRate: 0.1, // 10% sampling
103
+ });
104
+ return c.json({
105
+ error: {
106
+ code: 'BAD_REQUEST',
107
+ message: 'Malformed JSON in request body',
108
+ },
109
+ }, 400);
110
+ }
111
+ // Capture unhandled errors - these are critical
112
+ console.error('Unhandled error:', err);
113
+ captureException(err, {
114
+ level: 'error',
115
+ tags: {
116
+ error_code: 'INTERNAL_ERROR',
117
+ route: c.req.path,
118
+ method: c.req.method,
119
+ },
120
+ extra: {
121
+ url: c.req.url,
122
+ error_message: err instanceof Error ? err.message : String(err),
123
+ },
124
+ honoContext: c,
125
+ });
126
+ return c.json({
127
+ error: {
128
+ code: 'INTERNAL_ERROR',
129
+ message: 'An unexpected error occurred',
130
+ },
131
+ }, 500);
132
+ });
133
+ return app;
134
+ }
@@ -0,0 +1,14 @@
1
+ import type { Prisma } from '@prisma/client';
2
+ export type BrandDBResponse = Prisma.BrandGetPayload<{}>;
3
+ export declare function formatBrandResponse(brand: BrandDBResponse): {
4
+ createdAt: string;
5
+ updatedAt: string;
6
+ deletedAt: string;
7
+ id: string;
8
+ name: string;
9
+ slug: string;
10
+ logoUrl: string | null;
11
+ siteUrl: string;
12
+ domain: string;
13
+ metaPixelId: string | null;
14
+ };
@@ -0,0 +1,8 @@
1
+ export function formatBrandResponse(brand) {
2
+ return {
3
+ ...brand,
4
+ createdAt: brand.createdAt.toISOString(),
5
+ updatedAt: brand.updatedAt.toISOString(),
6
+ deletedAt: brand.deletedAt ? brand.deletedAt.toISOString() : null,
7
+ };
8
+ }
@@ -0,0 +1,282 @@
1
+ import { PrismaClient } from '@prisma/client';
2
+ /**
3
+ * Standard include pattern for cart queries with all necessary relations
4
+ */
5
+ export declare const CART_INCLUDE_FULL: {
6
+ readonly brand: true;
7
+ readonly deliveryZone: true;
8
+ readonly recoveryDiscountCode: true;
9
+ readonly items: {
10
+ readonly include: {
11
+ readonly variant: {
12
+ readonly include: {
13
+ readonly product: true;
14
+ };
15
+ };
16
+ };
17
+ };
18
+ };
19
+ /**
20
+ * Include pattern for cart item queries that need the parent cart
21
+ */
22
+ export declare const CART_ITEM_WITH_CART_INCLUDE: {
23
+ readonly cart: {
24
+ readonly include: {
25
+ readonly brand: true;
26
+ };
27
+ };
28
+ };
29
+ /**
30
+ * Include pattern for order creation with discount code
31
+ */
32
+ export declare const ORDER_INCLUDE_FULL: {
33
+ readonly brand: true;
34
+ readonly deliveryZone: {
35
+ readonly include: {
36
+ readonly state: true;
37
+ };
38
+ };
39
+ readonly items: {
40
+ readonly include: {
41
+ readonly variant: {
42
+ readonly include: {
43
+ readonly product: true;
44
+ };
45
+ };
46
+ readonly warehouse: true;
47
+ };
48
+ };
49
+ };
50
+ type CartWithRelations = Awaited<ReturnType<typeof getCartWithRelations>>;
51
+ declare function getCartWithRelations(prisma: PrismaClient, cartId: string): Promise<{
52
+ brand: {
53
+ id: string;
54
+ name: string;
55
+ slug: string;
56
+ logoUrl: string | null;
57
+ siteUrl: string;
58
+ domain: string;
59
+ metaPixelId: string | null;
60
+ createdAt: Date;
61
+ updatedAt: Date;
62
+ deletedAt: Date | null;
63
+ };
64
+ deliveryZone: {
65
+ id: string;
66
+ name: string;
67
+ createdAt: Date;
68
+ updatedAt: Date;
69
+ deletedAt: Date | null;
70
+ brandId: string | null;
71
+ stateId: string;
72
+ deliveryCost: import("@prisma/client/runtime/library").Decimal;
73
+ freeShippingThreshold: import("@prisma/client/runtime/library").Decimal | null;
74
+ allowCOD: boolean;
75
+ allowOnline: boolean;
76
+ waybillOnly: boolean;
77
+ estimatedDays: number | null;
78
+ noteTitle: string | null;
79
+ noteContent: string | null;
80
+ isActive: boolean;
81
+ };
82
+ recoveryDiscountCode: {
83
+ id: string;
84
+ type: string;
85
+ value: import("@prisma/client/runtime/library").Decimal;
86
+ createdAt: Date;
87
+ updatedAt: Date;
88
+ deletedAt: Date | null;
89
+ brandId: string | null;
90
+ isActive: boolean;
91
+ code: string;
92
+ minPurchase: import("@prisma/client/runtime/library").Decimal | null;
93
+ maxDiscount: import("@prisma/client/runtime/library").Decimal | null;
94
+ usageLimit: number | null;
95
+ usageCount: number;
96
+ perCustomerLimit: number | null;
97
+ validFrom: Date;
98
+ validUntil: Date | null;
99
+ isAutoApply: boolean;
100
+ description: string | null;
101
+ category: string;
102
+ createdBy: string | null;
103
+ };
104
+ items: ({
105
+ variant: {
106
+ product: {
107
+ id: string;
108
+ name: string;
109
+ slug: string;
110
+ createdAt: Date;
111
+ updatedAt: Date;
112
+ deletedAt: Date | null;
113
+ brandId: string;
114
+ isActive: boolean;
115
+ description: string | null;
116
+ thumbnailUrl: string | null;
117
+ quantityDiscounts: import("@prisma/client/runtime/library").JsonValue | null;
118
+ };
119
+ } & {
120
+ id: string;
121
+ name: string | null;
122
+ createdAt: Date;
123
+ updatedAt: Date;
124
+ deletedAt: Date | null;
125
+ isActive: boolean;
126
+ thumbnailUrl: string | null;
127
+ productId: string;
128
+ sku: string;
129
+ price: import("@prisma/client/runtime/library").Decimal;
130
+ trackInventory: boolean;
131
+ lowStockThreshold: number | null;
132
+ };
133
+ } & {
134
+ id: string;
135
+ createdAt: Date;
136
+ updatedAt: Date;
137
+ variantId: string;
138
+ quantity: number;
139
+ cartId: string;
140
+ })[];
141
+ } & {
142
+ id: string;
143
+ createdAt: Date;
144
+ updatedAt: Date;
145
+ brandId: string;
146
+ customerPhone: string | null;
147
+ customerFirstName: string | null;
148
+ customerLastName: string | null;
149
+ expiresAt: Date;
150
+ deliveryZoneId: string | null;
151
+ deliveryZoneLockedAt: Date | null;
152
+ availablePaymentMethods: import("@prisma/client").$Enums.PaymentMethod[];
153
+ recoveryAttempts: number;
154
+ lastRecoveryAttemptAt: Date | null;
155
+ recoveryDiscountCodeId: string | null;
156
+ convertedToOrderId: string | null;
157
+ wasRecovered: boolean;
158
+ appliedDiscountCodeId: string | null;
159
+ customerEmail: string | null;
160
+ }>;
161
+ /**
162
+ * Validate cart exists and is not expired
163
+ */
164
+ export declare function validateCart(prisma: PrismaClient, cartId: string): Promise<{
165
+ cart: CartWithRelations | null;
166
+ error?: {
167
+ code: string;
168
+ message: string;
169
+ status: number;
170
+ };
171
+ }>;
172
+ /**
173
+ * Build cart response with pricing calculations
174
+ */
175
+ export declare function buildCartResponse(prisma: PrismaClient, cart: NonNullable<CartWithRelations>): Promise<{
176
+ id: string;
177
+ brand: {
178
+ createdAt: string;
179
+ updatedAt: string;
180
+ deletedAt: string;
181
+ id: string;
182
+ name: string;
183
+ slug: string;
184
+ logoUrl: string | null;
185
+ siteUrl: string;
186
+ domain: string;
187
+ metaPixelId: string | null;
188
+ };
189
+ customerPhone: string;
190
+ customerEmail: string;
191
+ customerFirstName: string;
192
+ customerLastName: string;
193
+ availablePaymentMethods: import("@prisma/client").$Enums.PaymentMethod[];
194
+ deliveryZone: {
195
+ id: string;
196
+ name: string;
197
+ };
198
+ recoveryAttempts: number;
199
+ lastRecoveryAttemptAt: Date;
200
+ recoveryDiscountCode: {
201
+ id: string;
202
+ type: string;
203
+ value: import("@prisma/client/runtime/library").Decimal;
204
+ createdAt: Date;
205
+ updatedAt: Date;
206
+ deletedAt: Date | null;
207
+ brandId: string | null;
208
+ isActive: boolean;
209
+ code: string;
210
+ minPurchase: import("@prisma/client/runtime/library").Decimal | null;
211
+ maxDiscount: import("@prisma/client/runtime/library").Decimal | null;
212
+ usageLimit: number | null;
213
+ usageCount: number;
214
+ perCustomerLimit: number | null;
215
+ validFrom: Date;
216
+ validUntil: Date | null;
217
+ isAutoApply: boolean;
218
+ description: string | null;
219
+ category: string;
220
+ createdBy: string | null;
221
+ };
222
+ items: {
223
+ id: string;
224
+ variant: {
225
+ price: number;
226
+ product: {
227
+ id: string;
228
+ name: string;
229
+ slug: string;
230
+ createdAt: Date;
231
+ updatedAt: Date;
232
+ deletedAt: Date | null;
233
+ brandId: string;
234
+ isActive: boolean;
235
+ description: string | null;
236
+ thumbnailUrl: string | null;
237
+ quantityDiscounts: import("@prisma/client/runtime/library").JsonValue | null;
238
+ };
239
+ id: string;
240
+ name: string | null;
241
+ createdAt: Date;
242
+ updatedAt: Date;
243
+ deletedAt: Date | null;
244
+ isActive: boolean;
245
+ thumbnailUrl: string | null;
246
+ productId: string;
247
+ sku: string;
248
+ trackInventory: boolean;
249
+ lowStockThreshold: number | null;
250
+ };
251
+ quantity: number;
252
+ basePrice: number;
253
+ discountPercent: number;
254
+ finalPrice: number;
255
+ subtotal: number;
256
+ }[];
257
+ pricing: {
258
+ subtotal: number;
259
+ deliveryCharge: number;
260
+ discount: import("./cart-response").DiscountInfo;
261
+ total: number;
262
+ };
263
+ createdAt: string;
264
+ updatedAt: string;
265
+ expiresAt: string;
266
+ convertedToOrderId: string;
267
+ wasRecovered: boolean;
268
+ recoveryUrl: string;
269
+ }>;
270
+ /**
271
+ * Generate ETag from date
272
+ */
273
+ export declare function etagFrom(date: Date): string;
274
+ /**
275
+ * Derive payment methods from delivery zone
276
+ */
277
+ export declare function paymentFromZone(zone: {
278
+ allowCOD: boolean;
279
+ allowOnline: boolean;
280
+ waybillOnly: boolean;
281
+ }): ('cod' | 'online')[];
282
+ export {};
@@ -0,0 +1,121 @@
1
+ import { buildCartResponseWithPricing } from './cart-response';
2
+ import { captureMessage, getSampleRate } from './sentry';
3
+ /**
4
+ * Standard include pattern for cart queries with all necessary relations
5
+ */
6
+ export const CART_INCLUDE_FULL = {
7
+ brand: true,
8
+ deliveryZone: true,
9
+ recoveryDiscountCode: true,
10
+ items: {
11
+ include: {
12
+ variant: {
13
+ include: {
14
+ product: true,
15
+ },
16
+ },
17
+ },
18
+ },
19
+ };
20
+ /**
21
+ * Include pattern for cart item queries that need the parent cart
22
+ */
23
+ export const CART_ITEM_WITH_CART_INCLUDE = {
24
+ cart: {
25
+ include: {
26
+ brand: true
27
+ }
28
+ },
29
+ };
30
+ /**
31
+ * Include pattern for order creation with discount code
32
+ */
33
+ export const ORDER_INCLUDE_FULL = {
34
+ brand: true,
35
+ deliveryZone: { include: { state: true } },
36
+ items: {
37
+ include: {
38
+ variant: {
39
+ include: {
40
+ product: true,
41
+ },
42
+ },
43
+ warehouse: true,
44
+ },
45
+ },
46
+ };
47
+ async function getCartWithRelations(prisma, cartId) {
48
+ return await prisma.cart.findUnique({
49
+ where: { id: cartId },
50
+ include: CART_INCLUDE_FULL,
51
+ });
52
+ }
53
+ /**
54
+ * Validate cart exists and is not expired
55
+ */
56
+ export async function validateCart(prisma, cartId) {
57
+ const cart = await getCartWithRelations(prisma, cartId);
58
+ if (!cart) {
59
+ // Track cart not found errors with sampling
60
+ captureMessage('Cart not found', {
61
+ level: 'info',
62
+ tags: {
63
+ error_code: 'CART_NOT_FOUND',
64
+ },
65
+ extra: {
66
+ cartId,
67
+ },
68
+ sampleRate: getSampleRate('CART_NOT_FOUND'), // 5% sampling
69
+ });
70
+ return {
71
+ cart: null,
72
+ error: { code: 'CART_NOT_FOUND', message: 'Cart not found', status: 404 }
73
+ };
74
+ }
75
+ if (cart.expiresAt < new Date()) {
76
+ // Track cart expiration with sampling
77
+ const daysSinceExpiry = Math.floor((new Date().getTime() - cart.expiresAt.getTime()) / (1000 * 60 * 60 * 24));
78
+ captureMessage('Cart expired', {
79
+ level: 'info',
80
+ tags: {
81
+ error_code: 'CART_EXPIRED',
82
+ brand: cart.brand?.slug || 'unknown',
83
+ },
84
+ extra: {
85
+ cartId,
86
+ expiresAt: cart.expiresAt.toISOString(),
87
+ daysSinceExpiry,
88
+ lastUpdated: cart.updatedAt.toISOString(),
89
+ },
90
+ sampleRate: getSampleRate('CART_EXPIRED'), // 10% sampling
91
+ });
92
+ return {
93
+ cart: null,
94
+ error: { code: 'CART_EXPIRED', message: 'Cart has expired', status: 410 }
95
+ };
96
+ }
97
+ return { cart };
98
+ }
99
+ /**
100
+ * Build cart response with pricing calculations
101
+ */
102
+ export async function buildCartResponse(prisma, cart) {
103
+ return await buildCartResponseWithPricing(prisma, cart);
104
+ }
105
+ /**
106
+ * Generate ETag from date
107
+ */
108
+ export function etagFrom(date) {
109
+ return `W/"${date.getTime()}"`;
110
+ }
111
+ /**
112
+ * Derive payment methods from delivery zone
113
+ */
114
+ export function paymentFromZone(zone) {
115
+ const out = [];
116
+ if (zone.allowOnline)
117
+ out.push('online');
118
+ if (zone.allowCOD && !zone.waybillOnly)
119
+ out.push('cod');
120
+ return out;
121
+ }
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Abandoned cart recovery scheduler
3
+ * Handles automatic sending of recovery WhatsApp messages at different intervals
4
+ */
5
+ import type { PrismaClient } from '@prisma/client';
6
+ import type { CartWithItemsAndBrand } from './cart-response';
7
+ /**
8
+ * Recovery schedule configuration
9
+ */
10
+ export declare const RECOVERY_SCHEDULE: {
11
+ attemptNumber: number;
12
+ hoursAfterAbandonment: number;
13
+ discountPercent: number;
14
+ }[];
15
+ /**
16
+ * Find carts eligible for recovery message at specific attempt
17
+ */
18
+ export declare function findCartsForRecovery(prisma: PrismaClient, attemptNumber: number): Promise<any[]>;
19
+ /**
20
+ * Prepares a cart for a recovery attempt.
21
+ * This is the key atomic operation that:
22
+ * 1. Updates the recovery attempt number to prevent duplicate messages
23
+ * 2. Creates/updates the recovery discount code
24
+ * 3. Applies the discount to the cart
25
+ * This should be called *before* queueing any notifications.
26
+ */
27
+ export declare function prepareCartForRecoveryAttempt(prisma: PrismaClient, cart: CartWithItemsAndBrand, attemptNumber: number): Promise<{
28
+ success: boolean;
29
+ cartWithDiscount?: CartWithItemsAndBrand;
30
+ }>;