x
This commit is contained in:
1 parent
c2269ea090
commit
14d3282a81
8 files changed
+226
-39
No files matched your search
@@ -122,7 +122,10 @@ export const productsRoute = new Elysia({ prefix: "/products" })
|
||||
const limit = query.limit ? parseInt(query.limit) : 12;
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
const includeDeleted = query.includeDeleted === 'true';
|
||||
|
||||
const where = {
|
||||
...(!includeDeleted && { isDeleted: false }),
|
||||
...(categoryId && { categoryId }),
|
||||
...(brandId && { brandId }),
|
||||
...(search && {
|
||||
@@ -255,7 +258,8 @@ export const productsRoute = new Elysia({ prefix: "/products" })
|
||||
},
|
||||
});
|
||||
|
||||
if (!product) return status(404, { message: "Product not found" });
|
||||
const includeDeleted = query.includeDeleted === 'true';
|
||||
if (!product || (!includeDeleted && product.isDeleted)) return status(404, { message: "Product not found" });
|
||||
|
||||
if (lang === 'all') {
|
||||
const nameObj: any = {};
|
||||
@@ -1011,4 +1015,73 @@ export const productsRoute = new Elysia({ prefix: "/products" })
|
||||
tags: ["products"],
|
||||
},
|
||||
}
|
||||
)
|
||||
.delete(
|
||||
"/:id",
|
||||
async ({ params, status }) => {
|
||||
const id = parseInt(params.id);
|
||||
if (isNaN(id)) return status(400, { success: false, message: "Invalid product ID" });
|
||||
|
||||
const existing = await prisma.product.findUnique({
|
||||
where: { id },
|
||||
select: { id: true, isDeleted: true },
|
||||
});
|
||||
|
||||
if (!existing) {
|
||||
return status(404, { success: false, message: "Product not found" });
|
||||
}
|
||||
|
||||
await prisma.product.update({
|
||||
where: { id },
|
||||
data: {
|
||||
isDeleted: true,
|
||||
deletedAt: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
return { success: true, message: "Product deleted successfully" };
|
||||
},
|
||||
{
|
||||
detail: {
|
||||
summary: "Soft Delete Product",
|
||||
tags: ["products"],
|
||||
},
|
||||
}
|
||||
)
|
||||
.post(
|
||||
"/bulk-delete",
|
||||
async ({ body, status }) => {
|
||||
const { ids } = body as { ids?: number[] };
|
||||
if (!Array.isArray(ids) || ids.length === 0) {
|
||||
return status(400, { success: false, message: "Invalid or empty IDs array" });
|
||||
}
|
||||
|
||||
const validIds = ids.map(id => Number(id)).filter(id => !isNaN(id));
|
||||
if (validIds.length === 0) {
|
||||
return status(400, { success: false, message: "No valid IDs provided" });
|
||||
}
|
||||
|
||||
const result = await prisma.product.updateMany({
|
||||
where: { id: { in: validIds } },
|
||||
data: {
|
||||
isDeleted: true,
|
||||
deletedAt: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
count: result.count,
|
||||
message: `Successfully deleted ${result.count} products`,
|
||||
};
|
||||
},
|
||||
{
|
||||
body: t.Object({
|
||||
ids: t.Array(t.Numeric()),
|
||||
}),
|
||||
detail: {
|
||||
summary: "Bulk Soft Delete Products",
|
||||
tags: ["products"],
|
||||
},
|
||||
}
|
||||
);
|
||||
Reference in new issue
Block a user