x
This commit is contained in:
1 parent
46968782b6
commit
08045eabff
18 files changed
+943
-104
No files matched your search
@@ -0,0 +1,2 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE "ProductVariant" ADD COLUMN "colorCode" TEXT;
|
||||
@@ -140,6 +140,7 @@ model ProductVariant {
|
||||
option2 String @default("") // Maps to Product.options[1]
|
||||
option3 String @default("") // Maps to Product.options[2]
|
||||
imageUrl String?
|
||||
colorCode String?
|
||||
|
||||
productId Int
|
||||
product Product @relation(fields: [productId], references: [id])
|
||||
|
||||
@@ -558,25 +558,31 @@ async function resolveProductsForSection(payload, lang) {
|
||||
},
|
||||
};
|
||||
if (payload.type === "featured") {
|
||||
const whereClause = { isDeleted: false };
|
||||
if (payload.categoryId && Number(payload.categoryId) > 0) {
|
||||
whereClause.categoryId = Number(payload.categoryId);
|
||||
}
|
||||
products = await prisma.product.findMany({
|
||||
where: { categoryId: Number(payload.categoryId) || 1 },
|
||||
where: whereClause,
|
||||
include: productInclude,
|
||||
take: limit,
|
||||
orderBy: { createdAt: "desc" },
|
||||
});
|
||||
}
|
||||
else if ((payload.type === "scent" || payload.type === "brand") &&
|
||||
(payload.scentId || payload.brandId)) {
|
||||
products = await prisma.product.findMany({
|
||||
where: { brandId: Number(payload.brandId || payload.scentId) },
|
||||
where: { brandId: Number(payload.brandId || payload.scentId), isDeleted: false },
|
||||
include: productInclude,
|
||||
take: limit,
|
||||
orderBy: { createdAt: "desc" },
|
||||
});
|
||||
}
|
||||
else if (payload.type === "custom" && Array.isArray(payload.productIds)) {
|
||||
const ids = payload.productIds.map(Number);
|
||||
const ids = payload.productIds.map(Number).filter(id => !isNaN(id) && id > 0);
|
||||
if (ids.length > 0) {
|
||||
products = await prisma.product.findMany({
|
||||
where: { id: { in: ids } },
|
||||
where: { id: { in: ids }, isDeleted: false },
|
||||
include: productInclude,
|
||||
});
|
||||
products.sort((a, b) => ids.indexOf(a.id) - ids.indexOf(b.id));
|
||||
@@ -584,8 +590,10 @@ async function resolveProductsForSection(payload, lang) {
|
||||
}
|
||||
else {
|
||||
products = await prisma.product.findMany({
|
||||
where: { isDeleted: false },
|
||||
include: productInclude,
|
||||
take: limit,
|
||||
orderBy: { createdAt: "desc" },
|
||||
});
|
||||
}
|
||||
return products.map((p) => {
|
||||
@@ -619,8 +627,7 @@ export async function hydrateSectionPayload(section, lang) {
|
||||
return section;
|
||||
const populatedPayload = { ...payload };
|
||||
if (section.type === "PRODUCTS" ||
|
||||
(section.type === "PRODUCT_CAROUSEL" &&
|
||||
!Array.isArray(populatedPayload.products))) {
|
||||
section.type === "PRODUCT_CAROUSEL") {
|
||||
populatedPayload.products = await resolveProductsForSection(payload, lang);
|
||||
}
|
||||
else if (section.type === "CATEGORIES") {
|
||||
|
||||
@@ -1473,10 +1473,15 @@ async function resolveProductsForSection(payload: any, lang: string) {
|
||||
};
|
||||
|
||||
if (payload.type === "featured") {
|
||||
const whereClause: any = { isDeleted: false };
|
||||
if (payload.categoryId && Number(payload.categoryId) > 0) {
|
||||
whereClause.categoryId = Number(payload.categoryId);
|
||||
}
|
||||
products = await prisma.product.findMany({
|
||||
where: { categoryId: Number(payload.categoryId) || 1, isDeleted: false },
|
||||
where: whereClause,
|
||||
include: productInclude,
|
||||
take: limit,
|
||||
orderBy: { createdAt: "desc" as const },
|
||||
});
|
||||
} else if (
|
||||
(payload.type === "scent" || payload.type === "brand") &&
|
||||
@@ -1486,9 +1491,10 @@ async function resolveProductsForSection(payload: any, lang: string) {
|
||||
where: { brandId: Number(payload.brandId || payload.scentId), isDeleted: false },
|
||||
include: productInclude,
|
||||
take: limit,
|
||||
orderBy: { createdAt: "desc" as const },
|
||||
});
|
||||
} else if (payload.type === "custom" && Array.isArray(payload.productIds)) {
|
||||
const ids = payload.productIds.map(Number);
|
||||
const ids = payload.productIds.map(Number).filter((id: number) => !isNaN(id) && id > 0);
|
||||
if (ids.length > 0) {
|
||||
products = await prisma.product.findMany({
|
||||
where: { id: { in: ids }, isDeleted: false },
|
||||
@@ -1501,6 +1507,7 @@ async function resolveProductsForSection(payload: any, lang: string) {
|
||||
where: { isDeleted: false },
|
||||
include: productInclude,
|
||||
take: limit,
|
||||
orderBy: { createdAt: "desc" as const },
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1538,8 +1545,7 @@ export async function hydrateSectionPayload(section: any, lang: string) {
|
||||
|
||||
if (
|
||||
section.type === "PRODUCTS" ||
|
||||
(section.type === "PRODUCT_CAROUSEL" &&
|
||||
!Array.isArray(populatedPayload.products))
|
||||
section.type === "PRODUCT_CAROUSEL"
|
||||
) {
|
||||
populatedPayload.products = await resolveProductsForSection(payload, lang);
|
||||
} else if (section.type === "CATEGORIES") {
|
||||
|
||||
@@ -151,6 +151,7 @@ async function createSingleProductTx(tx: any, payload: any, validateVariants: an
|
||||
option3: v.option3 || "",
|
||||
price: v.price != null ? Number(v.price) : 0,
|
||||
imageUrl: v.imageUrl || null,
|
||||
colorCode: v.colorCode || null,
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -715,6 +716,7 @@ export const productsRoute = new Elysia({ prefix: "/products" })
|
||||
option3: v.option3 || "",
|
||||
price: v.price || 0,
|
||||
imageUrl: v.imageUrl !== undefined ? (v.imageUrl || null) : undefined,
|
||||
colorCode: v.colorCode !== undefined ? (v.colorCode || null) : undefined,
|
||||
bomItems: v.bomItems ? {
|
||||
deleteMany: {},
|
||||
create: v.bomItems.map((b: any) => ({
|
||||
@@ -737,6 +739,7 @@ export const productsRoute = new Elysia({ prefix: "/products" })
|
||||
option3: v.option3 || "",
|
||||
price: v.price || 0,
|
||||
imageUrl: v.imageUrl || null,
|
||||
colorCode: v.colorCode || null,
|
||||
bomItems: v.bomItems && v.bomItems.length > 0 ? {
|
||||
create: v.bomItems.map((b: any) => ({
|
||||
rawMaterialId: b.rawMaterialId,
|
||||
|
||||
Reference in new issue
Block a user