@instockng/api-client 1.0.5 → 1.0.6

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 (186) hide show
  1. package/dist/enum-types.d.ts +8 -0
  2. package/dist/enum-types.js +5 -0
  3. package/dist/fetchers/carts.js +5 -0
  4. package/dist/hooks/admin/abandoned-carts.js +12 -8
  5. package/dist/hooks/admin/brands.js +15 -10
  6. package/dist/hooks/admin/customers.js +3 -2
  7. package/dist/hooks/admin/delivery-zones.js +24 -16
  8. package/dist/hooks/admin/discount-codes.js +24 -16
  9. package/dist/hooks/admin/inventory.js +15 -10
  10. package/dist/hooks/admin/orders.js +18 -12
  11. package/dist/hooks/admin/products.js +15 -10
  12. package/dist/hooks/admin/stats.js +3 -2
  13. package/dist/hooks/admin/variants.js +18 -12
  14. package/dist/hooks/admin/warehouses.js +15 -10
  15. package/dist/hooks/useApiConfig.d.ts +2 -1
  16. package/dist/hooks/useApiConfig.js +2 -2
  17. package/dist/provider.d.ts +7 -4
  18. package/dist/provider.js +5 -3
  19. package/dist/types.d.ts +1 -0
  20. package/package.json +1 -1
  21. package/dist/apps/backend/src/generated/zod/index.d.ts +0 -1114
  22. package/dist/apps/backend/src/generated/zod/index.js +0 -670
  23. package/dist/apps/backend/src/http-app.d.ts +0 -40
  24. package/dist/apps/backend/src/http-app.js +0 -106
  25. package/dist/apps/backend/src/lib/brand-response.d.ts +0 -14
  26. package/dist/apps/backend/src/lib/brand-response.js +0 -8
  27. package/dist/apps/backend/src/lib/cart-helpers.d.ts +0 -280
  28. package/dist/apps/backend/src/lib/cart-helpers.js +0 -93
  29. package/dist/apps/backend/src/lib/cart-recovery.d.ts +0 -30
  30. package/dist/apps/backend/src/lib/cart-recovery.js +0 -147
  31. package/dist/apps/backend/src/lib/cart-response.d.ts +0 -121
  32. package/dist/apps/backend/src/lib/cart-response.js +0 -150
  33. package/dist/apps/backend/src/lib/clerk.d.ts +0 -18
  34. package/dist/apps/backend/src/lib/clerk.js +0 -167
  35. package/dist/apps/backend/src/lib/delivery-zone-response.d.ts +0 -62
  36. package/dist/apps/backend/src/lib/delivery-zone-response.js +0 -24
  37. package/dist/apps/backend/src/lib/discount-code-response.d.ts +0 -42
  38. package/dist/apps/backend/src/lib/discount-code-response.js +0 -19
  39. package/dist/apps/backend/src/lib/discount.d.ts +0 -20
  40. package/dist/apps/backend/src/lib/discount.js +0 -35
  41. package/dist/apps/backend/src/lib/inventory.d.ts +0 -26
  42. package/dist/apps/backend/src/lib/inventory.js +0 -160
  43. package/dist/apps/backend/src/lib/meta-capi.d.ts +0 -48
  44. package/dist/apps/backend/src/lib/meta-capi.js +0 -120
  45. package/dist/apps/backend/src/lib/openapi.d.ts +0 -36
  46. package/dist/apps/backend/src/lib/openapi.js +0 -69
  47. package/dist/apps/backend/src/lib/order-recovery.d.ts +0 -367
  48. package/dist/apps/backend/src/lib/order-recovery.js +0 -373
  49. package/dist/apps/backend/src/lib/order-response.d.ts +0 -136
  50. package/dist/apps/backend/src/lib/order-response.js +0 -61
  51. package/dist/apps/backend/src/lib/pricing.d.ts +0 -39
  52. package/dist/apps/backend/src/lib/pricing.js +0 -62
  53. package/dist/apps/backend/src/lib/prisma.d.ts +0 -9
  54. package/dist/apps/backend/src/lib/prisma.js +0 -30
  55. package/dist/apps/backend/src/lib/product-response.d.ts +0 -82
  56. package/dist/apps/backend/src/lib/product-response.js +0 -29
  57. package/dist/apps/backend/src/lib/utils.d.ts +0 -32
  58. package/dist/apps/backend/src/lib/utils.js +0 -63
  59. package/dist/apps/backend/src/middleware/clerk-auth.d.ts +0 -8
  60. package/dist/apps/backend/src/middleware/clerk-auth.js +0 -89
  61. package/dist/apps/backend/src/middleware/cors.d.ts +0 -8
  62. package/dist/apps/backend/src/middleware/cors.js +0 -11
  63. package/dist/apps/backend/src/notifications/producers/meta-capi-producer.d.ts +0 -55
  64. package/dist/apps/backend/src/notifications/producers/meta-capi-producer.js +0 -125
  65. package/dist/apps/backend/src/notifications/producers/order-notification.d.ts +0 -9
  66. package/dist/apps/backend/src/notifications/producers/order-notification.js +0 -18
  67. package/dist/apps/backend/src/notifications/producers/prospect-recovery-notification.d.ts +0 -10
  68. package/dist/apps/backend/src/notifications/producers/prospect-recovery-notification.js +0 -11
  69. package/dist/apps/backend/src/routes/admin/abandoned-carts.d.ts +0 -605
  70. package/dist/apps/backend/src/routes/admin/abandoned-carts.js +0 -194
  71. package/dist/apps/backend/src/routes/admin/brands.d.ts +0 -175
  72. package/dist/apps/backend/src/routes/admin/brands.js +0 -118
  73. package/dist/apps/backend/src/routes/admin/customers.d.ts +0 -306
  74. package/dist/apps/backend/src/routes/admin/customers.js +0 -39
  75. package/dist/apps/backend/src/routes/admin/delivery-zones.d.ts +0 -438
  76. package/dist/apps/backend/src/routes/admin/delivery-zones.js +0 -300
  77. package/dist/apps/backend/src/routes/admin/discount-codes.d.ts +0 -478
  78. package/dist/apps/backend/src/routes/admin/discount-codes.js +0 -418
  79. package/dist/apps/backend/src/routes/admin/inventory.d.ts +0 -273
  80. package/dist/apps/backend/src/routes/admin/inventory.js +0 -189
  81. package/dist/apps/backend/src/routes/admin/orders.d.ts +0 -1478
  82. package/dist/apps/backend/src/routes/admin/orders.js +0 -503
  83. package/dist/apps/backend/src/routes/admin/products.d.ts +0 -860
  84. package/dist/apps/backend/src/routes/admin/products.js +0 -107
  85. package/dist/apps/backend/src/routes/admin/stats.d.ts +0 -288
  86. package/dist/apps/backend/src/routes/admin/stats.js +0 -55
  87. package/dist/apps/backend/src/routes/admin/variants.d.ts +0 -239
  88. package/dist/apps/backend/src/routes/admin/variants.js +0 -173
  89. package/dist/apps/backend/src/routes/admin/warehouses.d.ts +0 -373
  90. package/dist/apps/backend/src/routes/admin/warehouses.js +0 -123
  91. package/dist/apps/backend/src/routes/public/brands.d.ts +0 -40
  92. package/dist/apps/backend/src/routes/public/brands.js +0 -38
  93. package/dist/apps/backend/src/routes/public/carts.d.ts +0 -2655
  94. package/dist/apps/backend/src/routes/public/carts.js +0 -631
  95. package/dist/apps/backend/src/routes/public/delivery-zones.d.ts +0 -35
  96. package/dist/apps/backend/src/routes/public/delivery-zones.js +0 -62
  97. package/dist/apps/backend/src/routes/public/orders.d.ts +0 -323
  98. package/dist/apps/backend/src/routes/public/orders.js +0 -160
  99. package/dist/apps/backend/src/routes/public/products.d.ts +0 -449
  100. package/dist/apps/backend/src/routes/public/products.js +0 -133
  101. package/dist/apps/backend/src/types/index.d.ts +0 -42
  102. package/dist/apps/backend/src/types/index.js +0 -2
  103. package/dist/apps/backend/src/validators/brand.d.ts +0 -17
  104. package/dist/apps/backend/src/validators/brand.js +0 -15
  105. package/dist/apps/backend/src/validators/delivery-zone.d.ts +0 -31
  106. package/dist/apps/backend/src/validators/delivery-zone.js +0 -51
  107. package/dist/apps/backend/src/validators/discount-code.d.ts +0 -74
  108. package/dist/apps/backend/src/validators/discount-code.js +0 -50
  109. package/dist/apps/backend/src/validators/inventory.d.ts +0 -20
  110. package/dist/apps/backend/src/validators/inventory.js +0 -15
  111. package/dist/apps/backend/src/validators/order.d.ts +0 -87
  112. package/dist/apps/backend/src/validators/order.js +0 -61
  113. package/dist/apps/backend/src/validators/product.d.ts +0 -18
  114. package/dist/apps/backend/src/validators/product.js +0 -19
  115. package/dist/apps/backend/src/validators/variant.d.ts +0 -19
  116. package/dist/apps/backend/src/validators/variant.js +0 -19
  117. package/dist/apps/backend/src/validators/warehouse.d.ts +0 -15
  118. package/dist/apps/backend/src/validators/warehouse.js +0 -15
  119. package/dist/packages/api-client/src/backend-types.d.ts +0 -10
  120. package/dist/packages/api-client/src/backend-types.js +0 -10
  121. package/dist/packages/api-client/src/client.d.ts +0 -20
  122. package/dist/packages/api-client/src/client.js +0 -40
  123. package/dist/packages/api-client/src/fetchers/brands.d.ts +0 -25
  124. package/dist/packages/api-client/src/fetchers/brands.js +0 -26
  125. package/dist/packages/api-client/src/fetchers/carts.d.ts +0 -2335
  126. package/dist/packages/api-client/src/fetchers/carts.js +0 -169
  127. package/dist/packages/api-client/src/fetchers/delivery-zones.d.ts +0 -28
  128. package/dist/packages/api-client/src/fetchers/delivery-zones.js +0 -26
  129. package/dist/packages/api-client/src/fetchers/index.d.ts +0 -22
  130. package/dist/packages/api-client/src/fetchers/index.js +0 -22
  131. package/dist/packages/api-client/src/fetchers/orders.d.ts +0 -283
  132. package/dist/packages/api-client/src/fetchers/orders.js +0 -44
  133. package/dist/packages/api-client/src/fetchers/products.d.ts +0 -386
  134. package/dist/packages/api-client/src/fetchers/products.js +0 -42
  135. package/dist/packages/api-client/src/hooks/admin/abandoned-carts.d.ts +0 -535
  136. package/dist/packages/api-client/src/hooks/admin/abandoned-carts.js +0 -83
  137. package/dist/packages/api-client/src/hooks/admin/brands.d.ts +0 -79
  138. package/dist/packages/api-client/src/hooks/admin/brands.js +0 -108
  139. package/dist/packages/api-client/src/hooks/admin/customers.d.ts +0 -278
  140. package/dist/packages/api-client/src/hooks/admin/customers.js +0 -26
  141. package/dist/packages/api-client/src/hooks/admin/delivery-zones.d.ts +0 -270
  142. package/dist/packages/api-client/src/hooks/admin/delivery-zones.js +0 -176
  143. package/dist/packages/api-client/src/hooks/admin/discount-codes.d.ts +0 -299
  144. package/dist/packages/api-client/src/hooks/admin/discount-codes.js +0 -165
  145. package/dist/packages/api-client/src/hooks/admin/index.d.ts +0 -16
  146. package/dist/packages/api-client/src/hooks/admin/index.js +0 -16
  147. package/dist/packages/api-client/src/hooks/admin/inventory.d.ts +0 -224
  148. package/dist/packages/api-client/src/hooks/admin/inventory.js +0 -107
  149. package/dist/packages/api-client/src/hooks/admin/orders.d.ts +0 -1380
  150. package/dist/packages/api-client/src/hooks/admin/orders.js +0 -175
  151. package/dist/packages/api-client/src/hooks/admin/products.d.ts +0 -374
  152. package/dist/packages/api-client/src/hooks/admin/products.js +0 -89
  153. package/dist/packages/api-client/src/hooks/admin/stats.d.ts +0 -277
  154. package/dist/packages/api-client/src/hooks/admin/stats.js +0 -25
  155. package/dist/packages/api-client/src/hooks/admin/variants.d.ts +0 -115
  156. package/dist/packages/api-client/src/hooks/admin/variants.js +0 -127
  157. package/dist/packages/api-client/src/hooks/admin/warehouses.d.ts +0 -277
  158. package/dist/packages/api-client/src/hooks/admin/warehouses.js +0 -108
  159. package/dist/packages/api-client/src/hooks/public/brands.d.ts +0 -33
  160. package/dist/packages/api-client/src/hooks/public/brands.js +0 -30
  161. package/dist/packages/api-client/src/hooks/public/carts.d.ts +0 -2405
  162. package/dist/packages/api-client/src/hooks/public/carts.js +0 -213
  163. package/dist/packages/api-client/src/hooks/public/delivery-zones.d.ts +0 -34
  164. package/dist/packages/api-client/src/hooks/public/delivery-zones.js +0 -28
  165. package/dist/packages/api-client/src/hooks/public/index.d.ts +0 -10
  166. package/dist/packages/api-client/src/hooks/public/index.js +0 -10
  167. package/dist/packages/api-client/src/hooks/public/orders.d.ts +0 -302
  168. package/dist/packages/api-client/src/hooks/public/orders.js +0 -50
  169. package/dist/packages/api-client/src/hooks/public/products.d.ts +0 -398
  170. package/dist/packages/api-client/src/hooks/public/products.js +0 -47
  171. package/dist/packages/api-client/src/hooks/use-query-unwrapped.d.ts +0 -20
  172. package/dist/packages/api-client/src/hooks/use-query-unwrapped.js +0 -22
  173. package/dist/packages/api-client/src/hooks/useApiConfig.d.ts +0 -12
  174. package/dist/packages/api-client/src/hooks/useApiConfig.js +0 -14
  175. package/dist/packages/api-client/src/index.d.ts +0 -20
  176. package/dist/packages/api-client/src/index.js +0 -25
  177. package/dist/packages/api-client/src/provider.d.ts +0 -36
  178. package/dist/packages/api-client/src/provider.js +0 -54
  179. package/dist/packages/api-client/src/rpc-client.d.ts +0 -9035
  180. package/dist/packages/api-client/src/rpc-client.js +0 -78
  181. package/dist/packages/api-client/src/rpc-types.d.ts +0 -76
  182. package/dist/packages/api-client/src/rpc-types.js +0 -7
  183. package/dist/packages/api-client/src/types.d.ts +0 -37
  184. package/dist/packages/api-client/src/types.js +0 -16
  185. package/dist/packages/api-client/src/utils/query-keys.d.ts +0 -106
  186. package/dist/packages/api-client/src/utils/query-keys.js +0 -108
@@ -1,147 +0,0 @@
1
- /**
2
- * Abandoned cart recovery scheduler
3
- * Handles automatic sending of recovery WhatsApp messages at different intervals
4
- */
5
- import { getRecoveryDiscountPercentage, generateDiscountCode } from './discount';
6
- /**
7
- * Recovery schedule configuration
8
- */
9
- export const RECOVERY_SCHEDULE = [
10
- { attemptNumber: 1, hoursAfterAbandonment: 1, discountPercent: 0 },
11
- { attemptNumber: 2, hoursAfterAbandonment: 24, discountPercent: 5 },
12
- { attemptNumber: 3, hoursAfterAbandonment: 72, discountPercent: 10 },
13
- ];
14
- /**
15
- * Find carts eligible for recovery message at specific attempt
16
- */
17
- export async function findCartsForRecovery(prisma, attemptNumber) {
18
- const schedule = RECOVERY_SCHEDULE.find((s) => s.attemptNumber === attemptNumber);
19
- if (!schedule)
20
- return [];
21
- const now = new Date();
22
- const cutoffTime = new Date(now.getTime() - schedule.hoursAfterAbandonment * 60 * 60 * 1000);
23
- // Buffer window: send messages for carts abandoned between cutoffTime and cutoffTime - 1 hour
24
- const bufferTime = new Date(cutoffTime.getTime() - 60 * 60 * 1000);
25
- // Don't message anyone who has received a recovery attempt in the last 22 hours
26
- const lastAttemptCutoff = new Date(now.getTime() - 22 * 60 * 60 * 1000);
27
- const carts = await prisma.cart.findMany({
28
- where: {
29
- OR: [{ customerPhone: { not: null } }, { customerEmail: { not: null } }],
30
- updatedAt: {
31
- gte: bufferTime,
32
- lte: cutoffTime,
33
- },
34
- AND: [
35
- {
36
- OR: [
37
- { lastRecoveryAttemptAt: null },
38
- { lastRecoveryAttemptAt: { lte: lastAttemptCutoff } },
39
- ],
40
- },
41
- ],
42
- expiresAt: { gt: now },
43
- items: { some: {} },
44
- recoveryAttempts: attemptNumber - 1, // Only send to carts at this attempt
45
- convertedToOrderId: null, // Not already converted
46
- },
47
- include: {
48
- brand: true,
49
- items: {
50
- include: {
51
- variant: {
52
- include: {
53
- product: true,
54
- },
55
- },
56
- },
57
- },
58
- },
59
- });
60
- return carts;
61
- }
62
- /**
63
- * Prepares a cart for a recovery attempt.
64
- * This is the key atomic operation that:
65
- * 1. Updates the recovery attempt number to prevent duplicate messages
66
- * 2. Creates/updates the recovery discount code
67
- * 3. Applies the discount to the cart
68
- * This should be called *before* queueing any notifications.
69
- */
70
- export async function prepareCartForRecoveryAttempt(prisma, cart, attemptNumber) {
71
- // 1. Atomically update the attempt number to "claim" this cart for processing
72
- const updateResult = await prisma.cart.updateMany({
73
- where: { id: cart.id, recoveryAttempts: attemptNumber - 1 },
74
- data: { recoveryAttempts: attemptNumber, lastRecoveryAttemptAt: new Date() },
75
- });
76
- // If the update failed, it means another process already handled this cart.
77
- if (updateResult.count === 0) {
78
- console.log(`[RECOVERY] Cart ${cart.id} already processed for attempt ${attemptNumber}. Skipping.`);
79
- return { success: false };
80
- }
81
- // 2. Create or update the discount code for this recovery attempt, if applicable
82
- const discountPercent = getRecoveryDiscountPercentage(attemptNumber);
83
- if (discountPercent > 0) {
84
- let recoveryCode = null;
85
- if (cart.recoveryDiscountCodeId) {
86
- recoveryCode = await prisma.discountCode.findUnique({ where: { id: cart.recoveryDiscountCodeId } });
87
- if (!recoveryCode) {
88
- cart.recoveryDiscountCodeId = null; // Stale ID, will create a new one
89
- }
90
- else {
91
- const currentValue = parseFloat(recoveryCode.value.toString());
92
- // If the existing code is not a percentage or has a lower value, update it
93
- if (recoveryCode.type !== 'percentage' || currentValue < discountPercent) {
94
- recoveryCode = await prisma.discountCode.update({
95
- where: { id: recoveryCode.id },
96
- data: {
97
- type: 'percentage',
98
- value: discountPercent,
99
- category: 'recovery',
100
- isActive: true,
101
- description: `Cart recovery ${discountPercent}%`,
102
- },
103
- });
104
- }
105
- }
106
- }
107
- // If no valid recovery code exists, create one
108
- if (!cart.recoveryDiscountCodeId) {
109
- const codeStr = generateDiscountCode();
110
- recoveryCode = await prisma.discountCode.create({
111
- data: {
112
- code: codeStr,
113
- brandId: cart.brandId,
114
- type: 'percentage',
115
- value: discountPercent,
116
- category: 'recovery',
117
- isActive: true,
118
- description: `Cart recovery ${discountPercent}%`,
119
- },
120
- });
121
- await prisma.cart.update({ where: { id: cart.id }, data: { recoveryDiscountCodeId: recoveryCode.id } });
122
- }
123
- // 3. Apply the recovery discount to the cart so it's ready for the customer
124
- await prisma.cart.update({ where: { id: cart.id }, data: { appliedDiscountCodeId: recoveryCode.id } });
125
- }
126
- else {
127
- // No discount for this attempt, ensure no discount is applied
128
- await prisma.cart.update({ where: { id: cart.id }, data: { appliedDiscountCodeId: null } });
129
- }
130
- // 4. Return the fully updated cart for the producer to use
131
- const updatedCart = (await prisma.cart.findUnique({
132
- where: { id: cart.id },
133
- include: {
134
- brand: true,
135
- items: {
136
- include: {
137
- variant: {
138
- include: {
139
- product: true,
140
- },
141
- },
142
- },
143
- },
144
- },
145
- }));
146
- return { success: true, cartWithDiscount: updatedCart };
147
- }
@@ -1,121 +0,0 @@
1
- import { Prisma, PrismaClient } from '@prisma/client';
2
- export type DiscountInfo = {
3
- code: string;
4
- type: 'percentage' | 'fixed';
5
- value: number;
6
- amount: number;
7
- description: string | null;
8
- };
9
- export type CartWithItemsAndBrand = Prisma.CartGetPayload<{
10
- include: {
11
- recoveryDiscountCode: true;
12
- deliveryZone: true;
13
- brand: true;
14
- items: {
15
- include: {
16
- variant: {
17
- include: {
18
- product: true;
19
- };
20
- };
21
- };
22
- };
23
- };
24
- }>;
25
- export type CartResponse = Awaited<ReturnType<typeof buildCartResponseWithPricing>>;
26
- export declare function toPricingItems(cart: CartWithItemsAndBrand): import('./pricing').CartItemForCalculation[];
27
- export declare function buildCartResponseWithPricing(prisma: PrismaClient, cart: CartWithItemsAndBrand): Promise<{
28
- id: string;
29
- brand: {
30
- createdAt: string;
31
- updatedAt: string;
32
- deletedAt: string;
33
- name: string;
34
- id: string;
35
- slug: string;
36
- logoUrl: string | null;
37
- siteUrl: string;
38
- domain: string;
39
- metaPixelId: string | null;
40
- };
41
- customerPhone: string;
42
- customerEmail: string;
43
- customerFirstName: string;
44
- customerLastName: string;
45
- availablePaymentMethods: import("@prisma/client").$Enums.PaymentMethod[];
46
- deliveryZone: {
47
- id: string;
48
- name: string;
49
- };
50
- recoveryAttempts: number;
51
- lastRecoveryAttemptAt: Date;
52
- recoveryDiscountCode: {
53
- id: string;
54
- createdAt: Date;
55
- updatedAt: Date;
56
- deletedAt: Date | null;
57
- brandId: string | null;
58
- isActive: boolean;
59
- code: string;
60
- type: string;
61
- value: Prisma.Decimal;
62
- minPurchase: Prisma.Decimal | null;
63
- maxDiscount: Prisma.Decimal | null;
64
- usageLimit: number | null;
65
- usageCount: number;
66
- perCustomerLimit: number | null;
67
- validFrom: Date;
68
- validUntil: Date | null;
69
- isAutoApply: boolean;
70
- description: string | null;
71
- category: string;
72
- createdBy: string | null;
73
- };
74
- items: {
75
- id: string;
76
- variant: {
77
- price: number;
78
- product: {
79
- name: string;
80
- id: string;
81
- slug: string;
82
- createdAt: Date;
83
- updatedAt: Date;
84
- deletedAt: Date | null;
85
- brandId: string;
86
- isActive: boolean;
87
- description: string | null;
88
- thumbnailUrl: string | null;
89
- quantityDiscounts: Prisma.JsonValue | null;
90
- };
91
- name: string | null;
92
- id: string;
93
- createdAt: Date;
94
- updatedAt: Date;
95
- deletedAt: Date | null;
96
- isActive: boolean;
97
- thumbnailUrl: string | null;
98
- productId: string;
99
- sku: string;
100
- trackInventory: boolean;
101
- lowStockThreshold: number | null;
102
- };
103
- quantity: number;
104
- basePrice: number;
105
- discountPercent: number;
106
- finalPrice: number;
107
- subtotal: number;
108
- }[];
109
- pricing: {
110
- subtotal: number;
111
- deliveryCharge: number;
112
- discount: DiscountInfo;
113
- total: number;
114
- };
115
- createdAt: string;
116
- updatedAt: string;
117
- expiresAt: string;
118
- convertedToOrderId: string;
119
- wasRecovered: boolean;
120
- recoveryUrl: string;
121
- }>;
@@ -1,150 +0,0 @@
1
- import { calculateCartCost } from './pricing';
2
- function toNumber(value) {
3
- if (typeof value === 'number')
4
- return value;
5
- return parseFloat(value.toString());
6
- }
7
- function round(value) {
8
- return Math.round(value * 100) / 100;
9
- }
10
- export function toPricingItems(cart) {
11
- return cart.items.map((item) => {
12
- const rawQtyDiscounts = item.variant.product.quantityDiscounts;
13
- let quantityDiscounts = null;
14
- if (rawQtyDiscounts && typeof rawQtyDiscounts === 'object' && !Array.isArray(rawQtyDiscounts)) {
15
- quantityDiscounts = rawQtyDiscounts;
16
- }
17
- return {
18
- variantId: item.variantId,
19
- quantity: item.quantity,
20
- variant: {
21
- price: item.variant.price,
22
- product: {
23
- id: item.variant.product.id,
24
- quantityDiscounts,
25
- },
26
- },
27
- };
28
- });
29
- }
30
- async function computePricingWithZone(prisma, cart) {
31
- const subtotalOnly = calculateCartCost(toPricingItems(cart)).subtotal;
32
- let deliveryCharge = 0;
33
- let estimatedDays = null;
34
- if (cart.deliveryZoneId) {
35
- const zone = await prisma.deliveryZone.findFirst({
36
- where: { id: cart.deliveryZoneId, isActive: true, deletedAt: null },
37
- });
38
- if (zone) {
39
- const threshold = zone.freeShippingThreshold ? Number(zone.freeShippingThreshold) : null;
40
- const base = Number(zone.deliveryCost);
41
- deliveryCharge = threshold !== null && subtotalOnly >= threshold ? 0 : base;
42
- estimatedDays = zone.estimatedDays ?? null;
43
- }
44
- }
45
- const pricing = calculateCartCost(toPricingItems(cart), deliveryCharge);
46
- return { pricing, estimatedDays };
47
- }
48
- async function computeAppliedDiscount(prisma, cart, subtotal) {
49
- if (!cart.appliedDiscountCodeId)
50
- return { discountAmount: 0, discountInfo: null };
51
- const discountCode = await prisma.discountCode.findUnique({ where: { id: cart.appliedDiscountCodeId } });
52
- if (!discountCode || !discountCode.isActive) {
53
- return { discountAmount: 0, discountInfo: null };
54
- }
55
- const now = new Date();
56
- if (discountCode.validFrom && discountCode.validFrom > now)
57
- return { discountAmount: 0, discountInfo: null };
58
- if (discountCode.validUntil && discountCode.validUntil <= now)
59
- return { discountAmount: 0, discountInfo: null };
60
- if (discountCode.brandId && discountCode.brandId !== cart.brandId)
61
- return { discountAmount: 0, discountInfo: null };
62
- let discountAmount = 0;
63
- if (discountCode.type === 'percentage') {
64
- discountAmount = round(subtotal * (toNumber(discountCode.value) / 100));
65
- if (discountCode.maxDiscount) {
66
- const maxDiscountNum = toNumber(discountCode.maxDiscount);
67
- if (discountAmount > maxDiscountNum)
68
- discountAmount = maxDiscountNum;
69
- }
70
- }
71
- else {
72
- discountAmount = Math.min(toNumber(discountCode.value), subtotal);
73
- }
74
- const discountType = discountCode.type === 'percentage' ? 'percentage' : 'fixed';
75
- const discountInfo = {
76
- code: discountCode.code,
77
- type: discountType,
78
- value: toNumber(discountCode.value),
79
- amount: discountAmount,
80
- description: discountCode.description,
81
- };
82
- return { discountAmount, discountInfo };
83
- }
84
- function formatCartResponse(cart, pricing) {
85
- const pricingMap = new Map(pricing.items.map((p) => [p.variantId, p]));
86
- const { id, brand, customerPhone, customerEmail, customerFirstName, customerLastName, availablePaymentMethods, deliveryZone, recoveryAttempts, lastRecoveryAttemptAt, recoveryDiscountCode, convertedToOrderId, wasRecovered, expiresAt } = cart;
87
- return {
88
- id,
89
- brand: {
90
- ...brand,
91
- createdAt: brand.createdAt.toISOString(),
92
- updatedAt: brand.updatedAt.toISOString(),
93
- deletedAt: brand.deletedAt?.toISOString() ?? null,
94
- },
95
- customerPhone,
96
- customerEmail,
97
- customerFirstName,
98
- customerLastName,
99
- availablePaymentMethods,
100
- deliveryZone: deliveryZone ? {
101
- id: deliveryZone.id,
102
- name: deliveryZone.name,
103
- } : null,
104
- // recovery
105
- recoveryAttempts,
106
- lastRecoveryAttemptAt,
107
- recoveryDiscountCode,
108
- items: cart.items.map((item) => {
109
- const itemPricing = pricingMap.get(item.variantId);
110
- const ip = itemPricing;
111
- return {
112
- id: item.id,
113
- variant: {
114
- ...item.variant,
115
- price: toNumber(item.variant.price),
116
- product: item.variant.product,
117
- },
118
- quantity: item.quantity,
119
- basePrice: ip ? ip.basePrice : 0,
120
- discountPercent: ip ? ip.discountPercent : 0,
121
- finalPrice: ip ? ip.finalPrice : 0,
122
- subtotal: ip ? ip.subtotal : 0,
123
- };
124
- }),
125
- pricing: {
126
- subtotal: pricing.subtotal,
127
- deliveryCharge: pricing.deliveryCharge,
128
- discount: pricing.discount ?? null,
129
- total: pricing.total,
130
- },
131
- createdAt: cart.createdAt.toISOString(),
132
- updatedAt: cart.updatedAt.toISOString(),
133
- expiresAt: expiresAt?.toISOString() ?? null,
134
- convertedToOrderId,
135
- wasRecovered,
136
- recoveryUrl: `${cart.brand.siteUrl}?cartId=${id}`
137
- };
138
- }
139
- export async function buildCartResponseWithPricing(prisma, cart) {
140
- const { pricing } = await computePricingWithZone(prisma, cart);
141
- const { discountAmount, discountInfo } = await computeAppliedDiscount(prisma, cart, pricing.subtotal);
142
- const finalSubtotal = round(Math.max(0, pricing.subtotal - discountAmount));
143
- const pricingWithDiscount = {
144
- ...pricing,
145
- discount: discountInfo,
146
- finalSubtotal,
147
- total: round(Math.max(0, finalSubtotal + pricing.deliveryCharge)),
148
- };
149
- return formatCartResponse(cart, pricingWithDiscount);
150
- }
@@ -1,18 +0,0 @@
1
- export interface ClerkUser {
2
- id: string;
3
- email: string;
4
- name?: string;
5
- }
6
- export interface ClerkUserData {
7
- id: string;
8
- first_name?: string;
9
- last_name?: string;
10
- email_addresses?: Array<{
11
- email_address: string;
12
- id: string;
13
- }>;
14
- primary_email_address_id?: string;
15
- primary_email_address?: string;
16
- }
17
- export declare function verifyClerkToken(token: string, publishableKey: string): Promise<ClerkUser | null>;
18
- export declare function extractBearerToken(authHeader?: string): string | null;
@@ -1,167 +0,0 @@
1
- // In-memory cache for JWKS (in production, consider using Workers KV or Durable Objects)
2
- let jwksCache = null;
3
- const JWKS_CACHE_TTL = 3600000; // 1 hour in milliseconds
4
- // Decode base64url (used in JWT)
5
- function base64UrlDecode(str) {
6
- // Replace URL-safe characters
7
- let base64 = str.replace(/-/g, '+').replace(/_/g, '/');
8
- // Pad with '=' to make length multiple of 4
9
- while (base64.length % 4 !== 0) {
10
- base64 += '=';
11
- }
12
- const binary = atob(base64);
13
- const bytes = new Uint8Array(binary.length);
14
- for (let i = 0; i < binary.length; i++) {
15
- bytes[i] = binary.charCodeAt(i);
16
- }
17
- return bytes;
18
- }
19
- // Decode base64url to string
20
- function base64UrlDecodeString(str) {
21
- const bytes = base64UrlDecode(str);
22
- return new TextDecoder().decode(bytes);
23
- }
24
- // Fetch Clerk's JWKS
25
- async function fetchJWKS(publishableKey) {
26
- // Check cache first
27
- if (jwksCache && Date.now() - jwksCache.timestamp < JWKS_CACHE_TTL) {
28
- console.log('Using cached JWKS with', jwksCache.keys.length, 'keys');
29
- return jwksCache.keys;
30
- }
31
- try {
32
- // Decode the publishable key to get the frontend API domain
33
- // Format: pk_test_<base64> or pk_live_<base64>
34
- const parts = publishableKey.split('_');
35
- if (parts.length < 3) {
36
- console.error('Invalid publishable key format');
37
- return [];
38
- }
39
- const base64Part = parts.slice(2).join('_');
40
- const decoded = atob(base64Part);
41
- // The decoded value should be the domain (e.g., "climbing-oyster-86.clerk.accounts.dev$")
42
- // Remove trailing $ if present
43
- const domain = decoded.replace(/\$+$/, '');
44
- console.log('Decoded Clerk domain:', domain);
45
- // Clerk JWKS endpoint format: https://<frontend-api>/.well-known/jwks.json
46
- const jwksUrl = `https://${domain}/.well-known/jwks.json`;
47
- console.log('Fetching JWKS from:', jwksUrl);
48
- const response = await fetch(jwksUrl);
49
- if (!response.ok) {
50
- const errorText = await response.text();
51
- console.error('Failed to fetch JWKS:', response.status, errorText);
52
- return [];
53
- }
54
- const jwks = await response.json();
55
- console.log('Successfully fetched', jwks.keys?.length || 0, 'JWKS keys');
56
- // Log the key IDs for debugging
57
- if (jwks.keys) {
58
- console.log('Available key IDs:', jwks.keys.map(k => k.kid).join(', '));
59
- }
60
- // Update cache
61
- jwksCache = {
62
- keys: jwks.keys,
63
- timestamp: Date.now(),
64
- };
65
- return jwks.keys;
66
- }
67
- catch (error) {
68
- console.error('Error fetching JWKS:', error);
69
- return [];
70
- }
71
- }
72
- // Convert base64url-encoded RSA components to CryptoKey
73
- async function importRSAKey(jwk) {
74
- try {
75
- const keyData = {
76
- kty: jwk.kty,
77
- n: jwk.n,
78
- e: jwk.e,
79
- alg: jwk.alg,
80
- ext: true,
81
- key_ops: ['verify'],
82
- };
83
- return await crypto.subtle.importKey('jwk', keyData, {
84
- name: 'RSASSA-PKCS1-v1_5',
85
- hash: 'SHA-256',
86
- }, false, ['verify']);
87
- }
88
- catch (error) {
89
- console.error('Error importing RSA key:', error);
90
- return null;
91
- }
92
- }
93
- // Verify JWT signature
94
- async function verifySignature(token, publicKey) {
95
- try {
96
- const parts = token.split('.');
97
- const signature = base64UrlDecode(parts[2]);
98
- const data = new TextEncoder().encode(`${parts[0]}.${parts[1]}`);
99
- return await crypto.subtle.verify('RSASSA-PKCS1-v1_5', publicKey, signature, data);
100
- }
101
- catch (error) {
102
- console.error('Error verifying signature:', error);
103
- return false;
104
- }
105
- }
106
- // Verify JWT token using Clerk's JWKS
107
- export async function verifyClerkToken(token, publishableKey) {
108
- try {
109
- // Parse JWT to get header and payload
110
- const parts = token.split('.');
111
- if (parts.length !== 3) {
112
- console.error('Invalid JWT format');
113
- return null;
114
- }
115
- const header = JSON.parse(base64UrlDecodeString(parts[0]));
116
- const payload = JSON.parse(base64UrlDecodeString(parts[1]));
117
- console.log('JWT Header kid:', header.kid);
118
- console.log('JWT Payload sub:', payload.sub);
119
- // Check if token is expired
120
- if (payload.exp && payload.exp < Math.floor(Date.now() / 1000)) {
121
- console.error('Token expired');
122
- return null;
123
- }
124
- // Verify required claims
125
- if (!payload.sub) {
126
- console.error('Missing sub claim');
127
- return null;
128
- }
129
- // Fetch JWKS and find the matching key
130
- const jwks = await fetchJWKS(publishableKey);
131
- console.log('Looking for kid:', header.kid, 'in', jwks.length, 'keys');
132
- const jwk = jwks.find((key) => key.kid === header.kid);
133
- if (!jwk) {
134
- console.error('No matching JWK found for kid:', header.kid);
135
- console.error('Available kids:', jwks.map(k => k.kid).join(', '));
136
- return null;
137
- }
138
- // Import the public key
139
- const publicKey = await importRSAKey(jwk);
140
- if (!publicKey) {
141
- console.error('Failed to import public key');
142
- return null;
143
- }
144
- // Verify the signature
145
- const isValid = await verifySignature(token, publicKey);
146
- if (!isValid) {
147
- console.error('Invalid signature');
148
- return null;
149
- }
150
- // Token is valid, return user info
151
- return {
152
- id: payload.sub,
153
- email: payload.email || payload.primary_email_address_id || '',
154
- name: payload.name || payload.first_name || payload.given_name || '',
155
- };
156
- }
157
- catch (error) {
158
- console.error('Clerk token verification failed:', error);
159
- return null;
160
- }
161
- }
162
- export function extractBearerToken(authHeader) {
163
- if (!authHeader || !authHeader.startsWith('Bearer ')) {
164
- return null;
165
- }
166
- return authHeader.substring(7);
167
- }
@@ -1,62 +0,0 @@
1
- import type { Prisma } from '@prisma/client';
2
- export type DeliveryZoneDBResponse = Prisma.DeliveryZoneGetPayload<{
3
- include: {
4
- brand: true;
5
- state: true;
6
- };
7
- }>;
8
- export type StateDBResponse = Prisma.StateGetPayload<{
9
- include?: {
10
- deliveryZones?: true;
11
- };
12
- }>;
13
- export declare function formatStateResponse(state: StateDBResponse & {
14
- deliveryZones?: any[];
15
- }): {
16
- zonesCount: number;
17
- createdAt: string;
18
- updatedAt: string;
19
- deletedAt: string;
20
- name: string;
21
- id: string;
22
- isActive: boolean;
23
- deliveryZones?: any[];
24
- };
25
- export declare function formatDeliveryZoneResponse(deliveryZone: DeliveryZoneDBResponse): {
26
- deliveryCost: number;
27
- freeShippingThreshold: number;
28
- createdAt: string;
29
- updatedAt: string;
30
- deletedAt: string;
31
- brand: {
32
- createdAt: string;
33
- updatedAt: string;
34
- deletedAt: string;
35
- name: string;
36
- id: string;
37
- slug: string;
38
- logoUrl: string | null;
39
- siteUrl: string;
40
- domain: string;
41
- metaPixelId: string | null;
42
- };
43
- stateName: string;
44
- brandName: string;
45
- state: {
46
- name: string;
47
- id: string;
48
- createdAt: Date;
49
- updatedAt: Date;
50
- deletedAt: Date | null;
51
- isActive: boolean;
52
- };
53
- name: string;
54
- id: string;
55
- brandId: string | null;
56
- stateId: string;
57
- allowCOD: boolean;
58
- allowOnline: boolean;
59
- waybillOnly: boolean;
60
- estimatedDays: number | null;
61
- isActive: boolean;
62
- };
@@ -1,24 +0,0 @@
1
- import { formatBrandResponse } from "./brand-response";
2
- import { toNumber } from "./utils";
3
- export function formatStateResponse(state) {
4
- return {
5
- ...state,
6
- zonesCount: state.deliveryZones?.length || 0,
7
- createdAt: state.createdAt.toISOString(),
8
- updatedAt: state.updatedAt.toISOString(),
9
- deletedAt: state.deletedAt ? state.deletedAt.toISOString() : null,
10
- };
11
- }
12
- export function formatDeliveryZoneResponse(deliveryZone) {
13
- return {
14
- ...deliveryZone,
15
- deliveryCost: toNumber(deliveryZone.deliveryCost),
16
- freeShippingThreshold: deliveryZone.freeShippingThreshold ? toNumber(deliveryZone.freeShippingThreshold) : null,
17
- createdAt: deliveryZone.createdAt.toISOString(),
18
- updatedAt: deliveryZone.updatedAt.toISOString(),
19
- deletedAt: deliveryZone.deletedAt ? deliveryZone.deletedAt.toISOString() : null,
20
- brand: deliveryZone.brand ? formatBrandResponse(deliveryZone.brand) : null,
21
- stateName: deliveryZone.state?.name || '',
22
- brandName: deliveryZone.brand?.name || null,
23
- };
24
- }