@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.
- package/dist/apps/backend/src/generated/zod/index.d.ts +1114 -0
- package/dist/apps/backend/src/generated/zod/index.js +670 -0
- package/dist/apps/backend/src/http-app.d.ts +40 -0
- package/dist/apps/backend/src/http-app.js +106 -0
- package/dist/apps/backend/src/lib/brand-response.d.ts +14 -0
- package/dist/apps/backend/src/lib/brand-response.js +8 -0
- package/dist/apps/backend/src/lib/cart-helpers.d.ts +280 -0
- package/dist/apps/backend/src/lib/cart-helpers.js +93 -0
- package/dist/apps/backend/src/lib/cart-recovery.d.ts +30 -0
- package/dist/apps/backend/src/lib/cart-recovery.js +147 -0
- package/dist/apps/backend/src/lib/cart-response.d.ts +121 -0
- package/dist/apps/backend/src/lib/cart-response.js +150 -0
- package/dist/apps/backend/src/lib/clerk.d.ts +18 -0
- package/dist/apps/backend/src/lib/clerk.js +167 -0
- package/dist/apps/backend/src/lib/delivery-zone-response.d.ts +62 -0
- package/dist/apps/backend/src/lib/delivery-zone-response.js +24 -0
- package/dist/apps/backend/src/lib/discount-code-response.d.ts +42 -0
- package/dist/apps/backend/src/lib/discount-code-response.js +19 -0
- package/dist/apps/backend/src/lib/discount.d.ts +20 -0
- package/dist/apps/backend/src/lib/discount.js +35 -0
- package/dist/apps/backend/src/lib/inventory.d.ts +26 -0
- package/dist/apps/backend/src/lib/inventory.js +160 -0
- package/dist/apps/backend/src/lib/meta-capi.d.ts +48 -0
- package/dist/apps/backend/src/lib/meta-capi.js +120 -0
- package/dist/apps/backend/src/lib/openapi.d.ts +36 -0
- package/dist/apps/backend/src/lib/openapi.js +69 -0
- package/dist/apps/backend/src/lib/order-recovery.d.ts +367 -0
- package/dist/apps/backend/src/lib/order-recovery.js +373 -0
- package/dist/apps/backend/src/lib/order-response.d.ts +136 -0
- package/dist/apps/backend/src/lib/order-response.js +61 -0
- package/dist/apps/backend/src/lib/pricing.d.ts +39 -0
- package/dist/apps/backend/src/lib/pricing.js +62 -0
- package/dist/apps/backend/src/lib/prisma.d.ts +9 -0
- package/dist/apps/backend/src/lib/prisma.js +30 -0
- package/dist/apps/backend/src/lib/product-response.d.ts +82 -0
- package/dist/apps/backend/src/lib/product-response.js +29 -0
- package/dist/apps/backend/src/lib/utils.d.ts +32 -0
- package/dist/apps/backend/src/lib/utils.js +63 -0
- package/dist/apps/backend/src/middleware/clerk-auth.d.ts +8 -0
- package/dist/apps/backend/src/middleware/clerk-auth.js +89 -0
- package/dist/apps/backend/src/middleware/cors.d.ts +8 -0
- package/dist/apps/backend/src/middleware/cors.js +11 -0
- package/dist/apps/backend/src/notifications/producers/meta-capi-producer.d.ts +55 -0
- package/dist/apps/backend/src/notifications/producers/meta-capi-producer.js +125 -0
- package/dist/apps/backend/src/notifications/producers/order-notification.d.ts +9 -0
- package/dist/apps/backend/src/notifications/producers/order-notification.js +18 -0
- package/dist/apps/backend/src/notifications/producers/prospect-recovery-notification.d.ts +10 -0
- package/dist/apps/backend/src/notifications/producers/prospect-recovery-notification.js +11 -0
- package/dist/apps/backend/src/routes/admin/abandoned-carts.d.ts +605 -0
- package/dist/apps/backend/src/routes/admin/abandoned-carts.js +194 -0
- package/dist/apps/backend/src/routes/admin/brands.d.ts +175 -0
- package/dist/apps/backend/src/routes/admin/brands.js +118 -0
- package/dist/apps/backend/src/routes/admin/customers.d.ts +306 -0
- package/dist/apps/backend/src/routes/admin/customers.js +39 -0
- package/dist/apps/backend/src/routes/admin/delivery-zones.d.ts +438 -0
- package/dist/apps/backend/src/routes/admin/delivery-zones.js +300 -0
- package/dist/apps/backend/src/routes/admin/discount-codes.d.ts +478 -0
- package/dist/apps/backend/src/routes/admin/discount-codes.js +418 -0
- package/dist/apps/backend/src/routes/admin/inventory.d.ts +273 -0
- package/dist/apps/backend/src/routes/admin/inventory.js +189 -0
- package/dist/apps/backend/src/routes/admin/orders.d.ts +1478 -0
- package/dist/apps/backend/src/routes/admin/orders.js +503 -0
- package/dist/apps/backend/src/routes/admin/products.d.ts +860 -0
- package/dist/apps/backend/src/routes/admin/products.js +107 -0
- package/dist/apps/backend/src/routes/admin/stats.d.ts +288 -0
- package/dist/apps/backend/src/routes/admin/stats.js +55 -0
- package/dist/apps/backend/src/routes/admin/variants.d.ts +239 -0
- package/dist/apps/backend/src/routes/admin/variants.js +173 -0
- package/dist/apps/backend/src/routes/admin/warehouses.d.ts +373 -0
- package/dist/apps/backend/src/routes/admin/warehouses.js +123 -0
- package/dist/apps/backend/src/routes/public/brands.d.ts +40 -0
- package/dist/apps/backend/src/routes/public/brands.js +38 -0
- package/dist/apps/backend/src/routes/public/carts.d.ts +2655 -0
- package/dist/apps/backend/src/routes/public/carts.js +631 -0
- package/dist/apps/backend/src/routes/public/delivery-zones.d.ts +35 -0
- package/dist/apps/backend/src/routes/public/delivery-zones.js +62 -0
- package/dist/apps/backend/src/routes/public/orders.d.ts +323 -0
- package/dist/apps/backend/src/routes/public/orders.js +160 -0
- package/dist/apps/backend/src/routes/public/products.d.ts +449 -0
- package/dist/apps/backend/src/routes/public/products.js +133 -0
- package/dist/apps/backend/src/types/index.d.ts +42 -0
- package/dist/apps/backend/src/types/index.js +2 -0
- package/dist/apps/backend/src/validators/brand.d.ts +17 -0
- package/dist/apps/backend/src/validators/brand.js +15 -0
- package/dist/apps/backend/src/validators/delivery-zone.d.ts +31 -0
- package/dist/apps/backend/src/validators/delivery-zone.js +51 -0
- package/dist/apps/backend/src/validators/discount-code.d.ts +74 -0
- package/dist/apps/backend/src/validators/discount-code.js +50 -0
- package/dist/apps/backend/src/validators/inventory.d.ts +20 -0
- package/dist/apps/backend/src/validators/inventory.js +15 -0
- package/dist/apps/backend/src/validators/order.d.ts +87 -0
- package/dist/apps/backend/src/validators/order.js +61 -0
- package/dist/apps/backend/src/validators/product.d.ts +18 -0
- package/dist/apps/backend/src/validators/product.js +19 -0
- package/dist/apps/backend/src/validators/variant.d.ts +19 -0
- package/dist/apps/backend/src/validators/variant.js +19 -0
- package/dist/apps/backend/src/validators/warehouse.d.ts +15 -0
- package/dist/apps/backend/src/validators/warehouse.js +15 -0
- package/dist/fetchers/carts.d.ts +746 -754
- package/dist/hooks/public/carts.d.ts +694 -702
- package/dist/packages/api-client/src/backend-types.d.ts +10 -0
- package/dist/packages/api-client/src/backend-types.js +10 -0
- package/dist/packages/api-client/src/client.d.ts +20 -0
- package/dist/packages/api-client/src/client.js +40 -0
- package/dist/packages/api-client/src/fetchers/brands.d.ts +25 -0
- package/dist/packages/api-client/src/fetchers/brands.js +26 -0
- package/dist/packages/api-client/src/fetchers/carts.d.ts +2335 -0
- package/dist/packages/api-client/src/fetchers/carts.js +169 -0
- package/dist/packages/api-client/src/fetchers/delivery-zones.d.ts +28 -0
- package/dist/packages/api-client/src/fetchers/delivery-zones.js +26 -0
- package/dist/packages/api-client/src/fetchers/index.d.ts +22 -0
- package/dist/packages/api-client/src/fetchers/index.js +22 -0
- package/dist/packages/api-client/src/fetchers/orders.d.ts +283 -0
- package/dist/packages/api-client/src/fetchers/orders.js +44 -0
- package/dist/packages/api-client/src/fetchers/products.d.ts +386 -0
- package/dist/packages/api-client/src/fetchers/products.js +42 -0
- package/dist/packages/api-client/src/hooks/admin/abandoned-carts.d.ts +535 -0
- package/dist/packages/api-client/src/hooks/admin/abandoned-carts.js +83 -0
- package/dist/packages/api-client/src/hooks/admin/brands.d.ts +79 -0
- package/dist/packages/api-client/src/hooks/admin/brands.js +108 -0
- package/dist/packages/api-client/src/hooks/admin/customers.d.ts +278 -0
- package/dist/packages/api-client/src/hooks/admin/customers.js +26 -0
- package/dist/packages/api-client/src/hooks/admin/delivery-zones.d.ts +270 -0
- package/dist/packages/api-client/src/hooks/admin/delivery-zones.js +176 -0
- package/dist/packages/api-client/src/hooks/admin/discount-codes.d.ts +299 -0
- package/dist/packages/api-client/src/hooks/admin/discount-codes.js +165 -0
- package/dist/packages/api-client/src/hooks/admin/index.d.ts +16 -0
- package/dist/packages/api-client/src/hooks/admin/index.js +16 -0
- package/dist/packages/api-client/src/hooks/admin/inventory.d.ts +224 -0
- package/dist/packages/api-client/src/hooks/admin/inventory.js +107 -0
- package/dist/packages/api-client/src/hooks/admin/orders.d.ts +1380 -0
- package/dist/packages/api-client/src/hooks/admin/orders.js +175 -0
- package/dist/packages/api-client/src/hooks/admin/products.d.ts +374 -0
- package/dist/packages/api-client/src/hooks/admin/products.js +89 -0
- package/dist/packages/api-client/src/hooks/admin/stats.d.ts +277 -0
- package/dist/packages/api-client/src/hooks/admin/stats.js +25 -0
- package/dist/packages/api-client/src/hooks/admin/variants.d.ts +115 -0
- package/dist/packages/api-client/src/hooks/admin/variants.js +127 -0
- package/dist/packages/api-client/src/hooks/admin/warehouses.d.ts +277 -0
- package/dist/packages/api-client/src/hooks/admin/warehouses.js +108 -0
- package/dist/packages/api-client/src/hooks/public/brands.d.ts +33 -0
- package/dist/packages/api-client/src/hooks/public/brands.js +30 -0
- package/dist/packages/api-client/src/hooks/public/carts.d.ts +2405 -0
- package/dist/packages/api-client/src/hooks/public/carts.js +213 -0
- package/dist/packages/api-client/src/hooks/public/delivery-zones.d.ts +34 -0
- package/dist/packages/api-client/src/hooks/public/delivery-zones.js +28 -0
- package/dist/packages/api-client/src/hooks/public/index.d.ts +10 -0
- package/dist/packages/api-client/src/hooks/public/index.js +10 -0
- package/dist/packages/api-client/src/hooks/public/orders.d.ts +302 -0
- package/dist/packages/api-client/src/hooks/public/orders.js +50 -0
- package/dist/packages/api-client/src/hooks/public/products.d.ts +398 -0
- package/dist/packages/api-client/src/hooks/public/products.js +47 -0
- package/dist/packages/api-client/src/hooks/use-query-unwrapped.d.ts +20 -0
- package/dist/packages/api-client/src/hooks/use-query-unwrapped.js +22 -0
- package/dist/packages/api-client/src/hooks/useApiConfig.d.ts +12 -0
- package/dist/packages/api-client/src/hooks/useApiConfig.js +14 -0
- package/dist/packages/api-client/src/index.d.ts +20 -0
- package/dist/packages/api-client/src/index.js +25 -0
- package/dist/packages/api-client/src/provider.d.ts +36 -0
- package/dist/packages/api-client/src/provider.js +54 -0
- package/dist/packages/api-client/src/rpc-client.d.ts +9035 -0
- package/dist/packages/api-client/src/rpc-client.js +78 -0
- package/dist/packages/api-client/src/rpc-types.d.ts +76 -0
- package/dist/packages/api-client/src/rpc-types.js +7 -0
- package/dist/packages/api-client/src/types.d.ts +37 -0
- package/dist/packages/api-client/src/types.js +16 -0
- package/dist/packages/api-client/src/utils/query-keys.d.ts +106 -0
- package/dist/packages/api-client/src/utils/query-keys.js +108 -0
- package/dist/rpc-client.d.ts +685 -693
- package/package.json +10 -9
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { Hono } from 'hono';
|
|
2
|
+
import { getPrismaClient } from '../../lib/prisma';
|
|
3
|
+
import { calculateCartCost } from '../../lib/pricing';
|
|
4
|
+
import { buildCartResponseWithPricing, toPricingItems } from '../../lib/cart-response';
|
|
5
|
+
import { CART_INCLUDE_FULL } from '../../lib/cart-helpers';
|
|
6
|
+
const app = new Hono()
|
|
7
|
+
// Note: Abandoned cart utility functions are not used here
|
|
8
|
+
// because they rely on a shared prisma instance which doesn't work in Workers
|
|
9
|
+
// We'll inline the logic in each endpoint instead
|
|
10
|
+
// List abandoned carts
|
|
11
|
+
.get('/', async (c) => {
|
|
12
|
+
try {
|
|
13
|
+
const brandId = c.req.query('brandId');
|
|
14
|
+
const hoursInactive = parseInt(c.req.query('hoursInactive') || '24');
|
|
15
|
+
const prisma = getPrismaClient(c.env.DATABASE_URL);
|
|
16
|
+
const cutoffTime = new Date(Date.now() - hoursInactive * 60 * 60 * 1000);
|
|
17
|
+
const whereClause = {
|
|
18
|
+
customerPhone: { not: null },
|
|
19
|
+
updatedAt: { lt: cutoffTime },
|
|
20
|
+
expiresAt: { gt: new Date() },
|
|
21
|
+
items: { some: {} },
|
|
22
|
+
};
|
|
23
|
+
if (brandId) {
|
|
24
|
+
whereClause.brandId = brandId;
|
|
25
|
+
}
|
|
26
|
+
const carts = await prisma.cart.findMany({
|
|
27
|
+
where: whereClause,
|
|
28
|
+
include: {
|
|
29
|
+
brand: true,
|
|
30
|
+
recoveryDiscountCode: true,
|
|
31
|
+
deliveryZone: true,
|
|
32
|
+
items: {
|
|
33
|
+
include: {
|
|
34
|
+
variant: {
|
|
35
|
+
include: {
|
|
36
|
+
product: true,
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
orderBy: {
|
|
43
|
+
updatedAt: 'asc',
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
// Calculate pricing for each cart
|
|
47
|
+
const cartsWithPricing = await Promise.all(carts.map((cart) => buildCartResponseWithPricing(prisma, cart)));
|
|
48
|
+
return c.json({
|
|
49
|
+
carts: cartsWithPricing,
|
|
50
|
+
total: cartsWithPricing.length,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
console.error('Error fetching abandoned carts:', error);
|
|
55
|
+
return c.json({ error: { code: 'INTERNAL_ERROR', message: error.message } }, 500);
|
|
56
|
+
}
|
|
57
|
+
})
|
|
58
|
+
// Get abandoned cart statistics
|
|
59
|
+
.get('/stats', async (c) => {
|
|
60
|
+
try {
|
|
61
|
+
const brandId = c.req.query('brandId');
|
|
62
|
+
const prisma = getPrismaClient(c.env.DATABASE_URL);
|
|
63
|
+
const now = new Date();
|
|
64
|
+
const last24Hours = new Date(now.getTime() - 24 * 60 * 60 * 1000);
|
|
65
|
+
const last7Days = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
|
|
66
|
+
const whereClause = {
|
|
67
|
+
expiresAt: { gt: now },
|
|
68
|
+
items: { some: {} },
|
|
69
|
+
};
|
|
70
|
+
if (brandId) {
|
|
71
|
+
whereClause.brandId = brandId;
|
|
72
|
+
}
|
|
73
|
+
const totalAbandoned = await prisma.cart.count({
|
|
74
|
+
where: {
|
|
75
|
+
...whereClause,
|
|
76
|
+
customerPhone: { not: null },
|
|
77
|
+
updatedAt: { lt: last24Hours },
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
const abandonedLast7Days = await prisma.cart.count({
|
|
81
|
+
where: {
|
|
82
|
+
...whereClause,
|
|
83
|
+
customerPhone: { not: null },
|
|
84
|
+
updatedAt: { lt: last24Hours, gt: last7Days },
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
const activeCarts = await prisma.cart.count({
|
|
88
|
+
where: {
|
|
89
|
+
...whereClause,
|
|
90
|
+
updatedAt: { gte: last24Hours },
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
const abandonedCarts = await prisma.cart.findMany({
|
|
94
|
+
where: {
|
|
95
|
+
...whereClause,
|
|
96
|
+
customerPhone: { not: null },
|
|
97
|
+
updatedAt: { lt: last24Hours },
|
|
98
|
+
},
|
|
99
|
+
include: CART_INCLUDE_FULL,
|
|
100
|
+
});
|
|
101
|
+
let totalValue = 0;
|
|
102
|
+
for (const cart of abandonedCarts) {
|
|
103
|
+
const pricing = calculateCartCost(toPricingItems(cart));
|
|
104
|
+
totalValue += pricing.subtotal;
|
|
105
|
+
}
|
|
106
|
+
// Calculate recovery metrics
|
|
107
|
+
const recoveredCarts = await prisma.cart.count({
|
|
108
|
+
where: {
|
|
109
|
+
...whereClause,
|
|
110
|
+
convertedToOrderId: { not: null },
|
|
111
|
+
wasRecovered: true,
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
const recoveredCartsWithOrders = await prisma.cart.findMany({
|
|
115
|
+
where: {
|
|
116
|
+
...whereClause,
|
|
117
|
+
convertedToOrderId: { not: null },
|
|
118
|
+
wasRecovered: true,
|
|
119
|
+
},
|
|
120
|
+
include: {
|
|
121
|
+
convertedOrder: true,
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
let revenueRecovered = 0;
|
|
125
|
+
let totalDiscountGiven = 0;
|
|
126
|
+
for (const cart of recoveredCartsWithOrders) {
|
|
127
|
+
if (cart.convertedOrder) {
|
|
128
|
+
revenueRecovered += parseFloat(cart.convertedOrder.totalPrice.toString());
|
|
129
|
+
// Estimate discount given (5%, 10%, or 15% based on recoveryAttempts)
|
|
130
|
+
const discountPercent = cart.recoveryAttempts === 1 ? 5 : cart.recoveryAttempts === 2 ? 10 : cart.recoveryAttempts === 3 ? 15 : 0;
|
|
131
|
+
const subtotal = parseFloat(cart.convertedOrder.totalPrice.toString()) + parseFloat(cart.convertedOrder.deliveryCharge.toString());
|
|
132
|
+
totalDiscountGiven += Math.round((subtotal / (1 - discountPercent / 100)) * (discountPercent / 100));
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return c.json({
|
|
136
|
+
totalAbandoned,
|
|
137
|
+
abandonedLast7Days,
|
|
138
|
+
activeCarts,
|
|
139
|
+
totalValue,
|
|
140
|
+
averageValue: totalAbandoned > 0 ? Math.round(totalValue / totalAbandoned) : 0,
|
|
141
|
+
// Recovery metrics
|
|
142
|
+
totalRecovered: recoveredCarts,
|
|
143
|
+
recoveryRate: totalAbandoned > 0 ? Math.round((recoveredCarts / totalAbandoned) * 100) : 0,
|
|
144
|
+
revenueRecovered: Math.round(revenueRecovered),
|
|
145
|
+
totalDiscountGiven: Math.round(totalDiscountGiven),
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
console.error('Error fetching abandoned cart stats:', error);
|
|
150
|
+
return c.json({ error: { code: 'INTERNAL_ERROR', message: error.message } }, 500);
|
|
151
|
+
}
|
|
152
|
+
})
|
|
153
|
+
// Clean up expired carts
|
|
154
|
+
.post('/cleanup', async (c) => {
|
|
155
|
+
try {
|
|
156
|
+
const prisma = getPrismaClient(c.env.DATABASE_URL);
|
|
157
|
+
const result = await prisma.cart.deleteMany({
|
|
158
|
+
where: {
|
|
159
|
+
expiresAt: {
|
|
160
|
+
lt: new Date(),
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
});
|
|
164
|
+
return c.json({
|
|
165
|
+
deletedCount: result.count,
|
|
166
|
+
message: `Cleaned up ${result.count} expired carts`,
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
catch (error) {
|
|
170
|
+
console.error('Error cleaning up expired carts:', error);
|
|
171
|
+
return c.json({ error: { code: 'INTERNAL_ERROR', message: error.message } }, 500);
|
|
172
|
+
}
|
|
173
|
+
})
|
|
174
|
+
// Get specific cart details
|
|
175
|
+
.get('/:id', async (c) => {
|
|
176
|
+
try {
|
|
177
|
+
const cartId = c.req.param('id');
|
|
178
|
+
const prisma = getPrismaClient(c.env.DATABASE_URL);
|
|
179
|
+
const cart = await prisma.cart.findUnique({
|
|
180
|
+
where: { id: cartId },
|
|
181
|
+
include: CART_INCLUDE_FULL,
|
|
182
|
+
});
|
|
183
|
+
if (!cart) {
|
|
184
|
+
return c.json({ error: { code: 'CART_NOT_FOUND', message: 'Cart not found' } }, 404);
|
|
185
|
+
}
|
|
186
|
+
const formattedCart = await buildCartResponseWithPricing(prisma, cart);
|
|
187
|
+
return c.json(formattedCart);
|
|
188
|
+
}
|
|
189
|
+
catch (error) {
|
|
190
|
+
console.error('Error fetching cart details:', error);
|
|
191
|
+
return c.json({ error: { code: 'INTERNAL_ERROR', message: error.message } }, 500);
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
export default app;
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { AppContext } from '../../types';
|
|
2
|
+
declare const app: import("hono/hono-base").HonoBase<AppContext, {
|
|
3
|
+
"/": {
|
|
4
|
+
$get: {
|
|
5
|
+
input: {};
|
|
6
|
+
output: {
|
|
7
|
+
name: string;
|
|
8
|
+
id: string;
|
|
9
|
+
slug: string;
|
|
10
|
+
logoUrl: string | null;
|
|
11
|
+
siteUrl: string;
|
|
12
|
+
domain: string;
|
|
13
|
+
metaPixelId: string | null;
|
|
14
|
+
createdAt: string;
|
|
15
|
+
updatedAt: string;
|
|
16
|
+
deletedAt: string;
|
|
17
|
+
}[];
|
|
18
|
+
outputFormat: "json";
|
|
19
|
+
status: import("hono/utils/http-status").ContentfulStatusCode;
|
|
20
|
+
} | {
|
|
21
|
+
input: {};
|
|
22
|
+
output: {
|
|
23
|
+
error: {
|
|
24
|
+
code: string;
|
|
25
|
+
message: string;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
outputFormat: "json";
|
|
29
|
+
status: 500;
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
} & {
|
|
33
|
+
"/:id": {
|
|
34
|
+
$get: {
|
|
35
|
+
input: {
|
|
36
|
+
param: {
|
|
37
|
+
id: string;
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
output: {
|
|
41
|
+
error: {
|
|
42
|
+
code: string;
|
|
43
|
+
message: string;
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
outputFormat: "json";
|
|
47
|
+
status: 404;
|
|
48
|
+
} | {
|
|
49
|
+
input: {
|
|
50
|
+
param: {
|
|
51
|
+
id: string;
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
output: {
|
|
55
|
+
name: string;
|
|
56
|
+
id: string;
|
|
57
|
+
slug: string;
|
|
58
|
+
logoUrl: string | null;
|
|
59
|
+
siteUrl: string;
|
|
60
|
+
domain: string;
|
|
61
|
+
metaPixelId: string | null;
|
|
62
|
+
createdAt: string;
|
|
63
|
+
updatedAt: string;
|
|
64
|
+
deletedAt: string;
|
|
65
|
+
};
|
|
66
|
+
outputFormat: "json";
|
|
67
|
+
status: import("hono/utils/http-status").ContentfulStatusCode;
|
|
68
|
+
} | {
|
|
69
|
+
input: {
|
|
70
|
+
param: {
|
|
71
|
+
id: string;
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
output: {
|
|
75
|
+
error: {
|
|
76
|
+
code: string;
|
|
77
|
+
message: string;
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
outputFormat: "json";
|
|
81
|
+
status: 500;
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
} & {
|
|
85
|
+
"/": {
|
|
86
|
+
$post: {
|
|
87
|
+
input: {};
|
|
88
|
+
output: {
|
|
89
|
+
name: string;
|
|
90
|
+
id: string;
|
|
91
|
+
slug: string;
|
|
92
|
+
logoUrl: string | null;
|
|
93
|
+
siteUrl: string;
|
|
94
|
+
domain: string;
|
|
95
|
+
metaPixelId: string | null;
|
|
96
|
+
createdAt: string;
|
|
97
|
+
updatedAt: string;
|
|
98
|
+
deletedAt: string;
|
|
99
|
+
};
|
|
100
|
+
outputFormat: "json";
|
|
101
|
+
status: 201;
|
|
102
|
+
} | {
|
|
103
|
+
input: {};
|
|
104
|
+
output: {
|
|
105
|
+
error: {
|
|
106
|
+
code: string;
|
|
107
|
+
message: string;
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
outputFormat: "json";
|
|
111
|
+
status: 500;
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
} & {
|
|
115
|
+
"/:id": {
|
|
116
|
+
$patch: {
|
|
117
|
+
input: {
|
|
118
|
+
param: {
|
|
119
|
+
id: string;
|
|
120
|
+
};
|
|
121
|
+
};
|
|
122
|
+
output: {
|
|
123
|
+
name: string;
|
|
124
|
+
id: string;
|
|
125
|
+
slug: string;
|
|
126
|
+
logoUrl: string | null;
|
|
127
|
+
siteUrl: string;
|
|
128
|
+
domain: string;
|
|
129
|
+
metaPixelId: string | null;
|
|
130
|
+
createdAt: string;
|
|
131
|
+
updatedAt: string;
|
|
132
|
+
deletedAt: string;
|
|
133
|
+
};
|
|
134
|
+
outputFormat: "json";
|
|
135
|
+
status: import("hono/utils/http-status").ContentfulStatusCode;
|
|
136
|
+
} | {
|
|
137
|
+
input: {
|
|
138
|
+
param: {
|
|
139
|
+
id: string;
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
output: {
|
|
143
|
+
error: {
|
|
144
|
+
code: string;
|
|
145
|
+
message: string;
|
|
146
|
+
};
|
|
147
|
+
};
|
|
148
|
+
outputFormat: "json";
|
|
149
|
+
status: 500;
|
|
150
|
+
};
|
|
151
|
+
};
|
|
152
|
+
} & {
|
|
153
|
+
"/:id": {
|
|
154
|
+
$delete: {
|
|
155
|
+
input: {
|
|
156
|
+
param: {
|
|
157
|
+
id: string;
|
|
158
|
+
};
|
|
159
|
+
};
|
|
160
|
+
output: {};
|
|
161
|
+
outputFormat: string;
|
|
162
|
+
status: import("hono/utils/http-status").StatusCode;
|
|
163
|
+
} | {
|
|
164
|
+
input: {
|
|
165
|
+
param: {
|
|
166
|
+
id: string;
|
|
167
|
+
};
|
|
168
|
+
};
|
|
169
|
+
output: {};
|
|
170
|
+
outputFormat: string;
|
|
171
|
+
status: import("hono/utils/http-status").StatusCode;
|
|
172
|
+
};
|
|
173
|
+
};
|
|
174
|
+
}, "/">;
|
|
175
|
+
export default app;
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { Hono } from 'hono';
|
|
2
|
+
import { zValidator } from '@hono/zod-validator';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { createBrandSchema, updateBrandSchema } from '../../validators/brand';
|
|
5
|
+
import { getPrismaClient } from '../../lib/prisma';
|
|
6
|
+
import { generateSlug } from '../../lib/utils';
|
|
7
|
+
const app = new Hono()
|
|
8
|
+
// List all brands
|
|
9
|
+
.get('/', async (c) => {
|
|
10
|
+
try {
|
|
11
|
+
const prisma = getPrismaClient(c.env.DATABASE_URL);
|
|
12
|
+
const brands = await prisma.brand.findMany({
|
|
13
|
+
where: { deletedAt: null },
|
|
14
|
+
orderBy: { name: 'asc' },
|
|
15
|
+
});
|
|
16
|
+
return c.json(brands);
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
return c.json({ error: { code: 'INTERNAL_ERROR', message: 'Failed to retrieve brands' } }, 500);
|
|
20
|
+
}
|
|
21
|
+
})
|
|
22
|
+
// Get brand by ID
|
|
23
|
+
.get('/:id', zValidator('param', z.object({ id: z.string().uuid() })), async (c) => {
|
|
24
|
+
try {
|
|
25
|
+
const { id } = c.req.valid('param');
|
|
26
|
+
const prisma = getPrismaClient(c.env.DATABASE_URL);
|
|
27
|
+
const brand = await prisma.brand.findFirst({
|
|
28
|
+
where: { id },
|
|
29
|
+
});
|
|
30
|
+
if (!brand) {
|
|
31
|
+
return c.json({
|
|
32
|
+
error: { code: 'BRAND_NOT_FOUND', message: 'Brand not found' },
|
|
33
|
+
}, 404);
|
|
34
|
+
}
|
|
35
|
+
return c.json(brand);
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
return c.json({ error: { code: 'INTERNAL_ERROR', message: 'Failed to retrieve brand' } }, 500);
|
|
39
|
+
}
|
|
40
|
+
})
|
|
41
|
+
// Create brand
|
|
42
|
+
.post('/', zValidator('json', createBrandSchema), async (c) => {
|
|
43
|
+
try {
|
|
44
|
+
const input = c.req.valid('json');
|
|
45
|
+
const prisma = getPrismaClient(c.env.DATABASE_URL);
|
|
46
|
+
// Generate slug from name
|
|
47
|
+
let slug = generateSlug(input.name);
|
|
48
|
+
// Ensure slug is unique by appending number if needed
|
|
49
|
+
let slugExists = await prisma.brand.findUnique({ where: { slug } });
|
|
50
|
+
let counter = 1;
|
|
51
|
+
while (slugExists) {
|
|
52
|
+
slug = `${generateSlug(input.name)}-${counter}`;
|
|
53
|
+
slugExists = await prisma.brand.findUnique({ where: { slug } });
|
|
54
|
+
counter++;
|
|
55
|
+
}
|
|
56
|
+
const brand = await prisma.brand.create({
|
|
57
|
+
data: {
|
|
58
|
+
...input,
|
|
59
|
+
slug,
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
return c.json(brand, 201);
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
return c.json({ error: { code: 'INTERNAL_ERROR', message: 'Failed to create brand' } }, 500);
|
|
66
|
+
}
|
|
67
|
+
})
|
|
68
|
+
// Update brand
|
|
69
|
+
.patch('/:id', zValidator('param', z.object({ id: z.string().uuid() })), zValidator('json', updateBrandSchema), async (c) => {
|
|
70
|
+
try {
|
|
71
|
+
const { id } = c.req.valid('param');
|
|
72
|
+
const input = c.req.valid('json');
|
|
73
|
+
const prisma = getPrismaClient(c.env.DATABASE_URL);
|
|
74
|
+
// If name is being updated, regenerate slug
|
|
75
|
+
let updateData = { ...input };
|
|
76
|
+
if (input.name) {
|
|
77
|
+
let slug = generateSlug(input.name);
|
|
78
|
+
// Ensure slug is unique (excluding current brand)
|
|
79
|
+
let slugExists = await prisma.brand.findFirst({
|
|
80
|
+
where: { slug, id: { not: id } },
|
|
81
|
+
});
|
|
82
|
+
let counter = 1;
|
|
83
|
+
while (slugExists) {
|
|
84
|
+
slug = `${generateSlug(input.name)}-${counter}`;
|
|
85
|
+
slugExists = await prisma.brand.findFirst({
|
|
86
|
+
where: { slug, id: { not: id } },
|
|
87
|
+
});
|
|
88
|
+
counter++;
|
|
89
|
+
}
|
|
90
|
+
updateData.slug = slug;
|
|
91
|
+
}
|
|
92
|
+
const brand = await prisma.brand.update({
|
|
93
|
+
where: { id },
|
|
94
|
+
data: updateData,
|
|
95
|
+
});
|
|
96
|
+
return c.json(brand);
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
return c.json({ error: { code: 'INTERNAL_ERROR', message: 'Failed to update brand' } }, 500);
|
|
100
|
+
}
|
|
101
|
+
})
|
|
102
|
+
// Soft delete brand
|
|
103
|
+
.delete('/:id', zValidator('param', z.object({ id: z.string().uuid() })), async (c) => {
|
|
104
|
+
try {
|
|
105
|
+
const { id } = c.req.valid('param');
|
|
106
|
+
const prisma = getPrismaClient(c.env.DATABASE_URL);
|
|
107
|
+
// Soft delete by setting deletedAt timestamp
|
|
108
|
+
await prisma.brand.update({
|
|
109
|
+
where: { id },
|
|
110
|
+
data: { deletedAt: new Date() },
|
|
111
|
+
});
|
|
112
|
+
return c.status(204);
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
return c.json({ error: { code: 'INTERNAL_ERROR', message: 'Failed to delete brand' } }, 500);
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
export default app;
|