@instockng/api-client 1.0.3 → 1.0.5

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 (170) 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 +106 -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 +280 -0
  8. package/dist/apps/backend/src/lib/cart-helpers.js +93 -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 +167 -0
  15. package/dist/apps/backend/src/lib/delivery-zone-response.d.ts +62 -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 +48 -0
  24. package/dist/apps/backend/src/lib/meta-capi.js +120 -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 +367 -0
  28. package/dist/apps/backend/src/lib/order-recovery.js +373 -0
  29. package/dist/apps/backend/src/lib/order-response.d.ts +136 -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/utils.d.ts +32 -0
  38. package/dist/apps/backend/src/lib/utils.js +63 -0
  39. package/dist/apps/backend/src/middleware/clerk-auth.d.ts +8 -0
  40. package/dist/apps/backend/src/middleware/clerk-auth.js +89 -0
  41. package/dist/apps/backend/src/middleware/cors.d.ts +8 -0
  42. package/dist/apps/backend/src/middleware/cors.js +11 -0
  43. package/dist/apps/backend/src/notifications/producers/meta-capi-producer.d.ts +55 -0
  44. package/dist/apps/backend/src/notifications/producers/meta-capi-producer.js +125 -0
  45. package/dist/apps/backend/src/notifications/producers/order-notification.d.ts +9 -0
  46. package/dist/apps/backend/src/notifications/producers/order-notification.js +18 -0
  47. package/dist/apps/backend/src/notifications/producers/prospect-recovery-notification.d.ts +10 -0
  48. package/dist/apps/backend/src/notifications/producers/prospect-recovery-notification.js +11 -0
  49. package/dist/apps/backend/src/routes/admin/abandoned-carts.d.ts +605 -0
  50. package/dist/apps/backend/src/routes/admin/abandoned-carts.js +194 -0
  51. package/dist/apps/backend/src/routes/admin/brands.d.ts +175 -0
  52. package/dist/apps/backend/src/routes/admin/brands.js +118 -0
  53. package/dist/apps/backend/src/routes/admin/customers.d.ts +306 -0
  54. package/dist/apps/backend/src/routes/admin/customers.js +39 -0
  55. package/dist/apps/backend/src/routes/admin/delivery-zones.d.ts +438 -0
  56. package/dist/apps/backend/src/routes/admin/delivery-zones.js +300 -0
  57. package/dist/apps/backend/src/routes/admin/discount-codes.d.ts +478 -0
  58. package/dist/apps/backend/src/routes/admin/discount-codes.js +418 -0
  59. package/dist/apps/backend/src/routes/admin/inventory.d.ts +273 -0
  60. package/dist/apps/backend/src/routes/admin/inventory.js +189 -0
  61. package/dist/apps/backend/src/routes/admin/orders.d.ts +1478 -0
  62. package/dist/apps/backend/src/routes/admin/orders.js +503 -0
  63. package/dist/apps/backend/src/routes/admin/products.d.ts +860 -0
  64. package/dist/apps/backend/src/routes/admin/products.js +107 -0
  65. package/dist/apps/backend/src/routes/admin/stats.d.ts +288 -0
  66. package/dist/apps/backend/src/routes/admin/stats.js +55 -0
  67. package/dist/apps/backend/src/routes/admin/variants.d.ts +239 -0
  68. package/dist/apps/backend/src/routes/admin/variants.js +173 -0
  69. package/dist/apps/backend/src/routes/admin/warehouses.d.ts +373 -0
  70. package/dist/apps/backend/src/routes/admin/warehouses.js +123 -0
  71. package/dist/apps/backend/src/routes/public/brands.d.ts +40 -0
  72. package/dist/apps/backend/src/routes/public/brands.js +38 -0
  73. package/dist/apps/backend/src/routes/public/carts.d.ts +2655 -0
  74. package/dist/apps/backend/src/routes/public/carts.js +631 -0
  75. package/dist/apps/backend/src/routes/public/delivery-zones.d.ts +35 -0
  76. package/dist/apps/backend/src/routes/public/delivery-zones.js +62 -0
  77. package/dist/apps/backend/src/routes/public/orders.d.ts +323 -0
  78. package/dist/apps/backend/src/routes/public/orders.js +160 -0
  79. package/dist/apps/backend/src/routes/public/products.d.ts +449 -0
  80. package/dist/apps/backend/src/routes/public/products.js +133 -0
  81. package/dist/apps/backend/src/types/index.d.ts +42 -0
  82. package/dist/apps/backend/src/types/index.js +2 -0
  83. package/dist/apps/backend/src/validators/brand.d.ts +17 -0
  84. package/dist/apps/backend/src/validators/brand.js +15 -0
  85. package/dist/apps/backend/src/validators/delivery-zone.d.ts +31 -0
  86. package/dist/apps/backend/src/validators/delivery-zone.js +51 -0
  87. package/dist/apps/backend/src/validators/discount-code.d.ts +74 -0
  88. package/dist/apps/backend/src/validators/discount-code.js +50 -0
  89. package/dist/apps/backend/src/validators/inventory.d.ts +20 -0
  90. package/dist/apps/backend/src/validators/inventory.js +15 -0
  91. package/dist/apps/backend/src/validators/order.d.ts +87 -0
  92. package/dist/apps/backend/src/validators/order.js +61 -0
  93. package/dist/apps/backend/src/validators/product.d.ts +18 -0
  94. package/dist/apps/backend/src/validators/product.js +19 -0
  95. package/dist/apps/backend/src/validators/variant.d.ts +19 -0
  96. package/dist/apps/backend/src/validators/variant.js +19 -0
  97. package/dist/apps/backend/src/validators/warehouse.d.ts +15 -0
  98. package/dist/apps/backend/src/validators/warehouse.js +15 -0
  99. package/dist/fetchers/carts.d.ts +746 -754
  100. package/dist/hooks/public/carts.d.ts +694 -702
  101. package/dist/packages/api-client/src/backend-types.d.ts +10 -0
  102. package/dist/packages/api-client/src/backend-types.js +10 -0
  103. package/dist/packages/api-client/src/client.d.ts +20 -0
  104. package/dist/packages/api-client/src/client.js +40 -0
  105. package/dist/packages/api-client/src/fetchers/brands.d.ts +25 -0
  106. package/dist/packages/api-client/src/fetchers/brands.js +26 -0
  107. package/dist/packages/api-client/src/fetchers/carts.d.ts +2335 -0
  108. package/dist/packages/api-client/src/fetchers/carts.js +169 -0
  109. package/dist/packages/api-client/src/fetchers/delivery-zones.d.ts +28 -0
  110. package/dist/packages/api-client/src/fetchers/delivery-zones.js +26 -0
  111. package/dist/packages/api-client/src/fetchers/index.d.ts +22 -0
  112. package/dist/packages/api-client/src/fetchers/index.js +22 -0
  113. package/dist/packages/api-client/src/fetchers/orders.d.ts +283 -0
  114. package/dist/packages/api-client/src/fetchers/orders.js +44 -0
  115. package/dist/packages/api-client/src/fetchers/products.d.ts +386 -0
  116. package/dist/packages/api-client/src/fetchers/products.js +42 -0
  117. package/dist/packages/api-client/src/hooks/admin/abandoned-carts.d.ts +535 -0
  118. package/dist/packages/api-client/src/hooks/admin/abandoned-carts.js +83 -0
  119. package/dist/packages/api-client/src/hooks/admin/brands.d.ts +79 -0
  120. package/dist/packages/api-client/src/hooks/admin/brands.js +108 -0
  121. package/dist/packages/api-client/src/hooks/admin/customers.d.ts +278 -0
  122. package/dist/packages/api-client/src/hooks/admin/customers.js +26 -0
  123. package/dist/packages/api-client/src/hooks/admin/delivery-zones.d.ts +270 -0
  124. package/dist/packages/api-client/src/hooks/admin/delivery-zones.js +176 -0
  125. package/dist/packages/api-client/src/hooks/admin/discount-codes.d.ts +299 -0
  126. package/dist/packages/api-client/src/hooks/admin/discount-codes.js +165 -0
  127. package/dist/packages/api-client/src/hooks/admin/index.d.ts +16 -0
  128. package/dist/packages/api-client/src/hooks/admin/index.js +16 -0
  129. package/dist/packages/api-client/src/hooks/admin/inventory.d.ts +224 -0
  130. package/dist/packages/api-client/src/hooks/admin/inventory.js +107 -0
  131. package/dist/packages/api-client/src/hooks/admin/orders.d.ts +1380 -0
  132. package/dist/packages/api-client/src/hooks/admin/orders.js +175 -0
  133. package/dist/packages/api-client/src/hooks/admin/products.d.ts +374 -0
  134. package/dist/packages/api-client/src/hooks/admin/products.js +89 -0
  135. package/dist/packages/api-client/src/hooks/admin/stats.d.ts +277 -0
  136. package/dist/packages/api-client/src/hooks/admin/stats.js +25 -0
  137. package/dist/packages/api-client/src/hooks/admin/variants.d.ts +115 -0
  138. package/dist/packages/api-client/src/hooks/admin/variants.js +127 -0
  139. package/dist/packages/api-client/src/hooks/admin/warehouses.d.ts +277 -0
  140. package/dist/packages/api-client/src/hooks/admin/warehouses.js +108 -0
  141. package/dist/packages/api-client/src/hooks/public/brands.d.ts +33 -0
  142. package/dist/packages/api-client/src/hooks/public/brands.js +30 -0
  143. package/dist/packages/api-client/src/hooks/public/carts.d.ts +2405 -0
  144. package/dist/packages/api-client/src/hooks/public/carts.js +213 -0
  145. package/dist/packages/api-client/src/hooks/public/delivery-zones.d.ts +34 -0
  146. package/dist/packages/api-client/src/hooks/public/delivery-zones.js +28 -0
  147. package/dist/packages/api-client/src/hooks/public/index.d.ts +10 -0
  148. package/dist/packages/api-client/src/hooks/public/index.js +10 -0
  149. package/dist/packages/api-client/src/hooks/public/orders.d.ts +302 -0
  150. package/dist/packages/api-client/src/hooks/public/orders.js +50 -0
  151. package/dist/packages/api-client/src/hooks/public/products.d.ts +398 -0
  152. package/dist/packages/api-client/src/hooks/public/products.js +47 -0
  153. package/dist/packages/api-client/src/hooks/use-query-unwrapped.d.ts +20 -0
  154. package/dist/packages/api-client/src/hooks/use-query-unwrapped.js +22 -0
  155. package/dist/packages/api-client/src/hooks/useApiConfig.d.ts +12 -0
  156. package/dist/packages/api-client/src/hooks/useApiConfig.js +14 -0
  157. package/dist/packages/api-client/src/index.d.ts +20 -0
  158. package/dist/packages/api-client/src/index.js +25 -0
  159. package/dist/packages/api-client/src/provider.d.ts +36 -0
  160. package/dist/packages/api-client/src/provider.js +54 -0
  161. package/dist/packages/api-client/src/rpc-client.d.ts +9035 -0
  162. package/dist/packages/api-client/src/rpc-client.js +78 -0
  163. package/dist/packages/api-client/src/rpc-types.d.ts +76 -0
  164. package/dist/packages/api-client/src/rpc-types.js +7 -0
  165. package/dist/packages/api-client/src/types.d.ts +37 -0
  166. package/dist/packages/api-client/src/types.js +16 -0
  167. package/dist/packages/api-client/src/utils/query-keys.d.ts +106 -0
  168. package/dist/packages/api-client/src/utils/query-keys.js +108 -0
  169. package/dist/rpc-client.d.ts +685 -693
  170. package/package.json +10 -9
@@ -0,0 +1,147 @@
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
+ }
@@ -0,0 +1,121 @@
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
+ }>;
@@ -0,0 +1,150 @@
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
+ }
@@ -0,0 +1,18 @@
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;
@@ -0,0 +1,167 @@
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
+ }
@@ -0,0 +1,62 @@
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
+ };
@@ -0,0 +1,24 @@
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
+ }