x
This commit is contained in:
1 parent
4536db6741
commit
c23a4723ed
2 files changed
+48
-36
No files matched your search
@@ -117,40 +117,52 @@ export const categoriesRoute = new Elysia({ prefix: "/categories" })
|
||||
const id = parseInt(params.id);
|
||||
if (isNaN(id)) return status(400, { message: "Invalid category ID" });
|
||||
|
||||
const payload = body as any;
|
||||
const results: any[] = [];
|
||||
try {
|
||||
const categoryExists = await prisma.category.findUnique({ where: { id } });
|
||||
if (!categoryExists) return status(404, { message: "Category not found" });
|
||||
|
||||
for (const lang of LANGS) {
|
||||
const name = typeof payload.name === 'string'
|
||||
? (lang === 'th' ? payload.name : null)
|
||||
: payload.name?.[lang];
|
||||
const payload = body as any;
|
||||
const results: any[] = [];
|
||||
|
||||
if (!name) continue;
|
||||
for (const lang of LANGS) {
|
||||
const name = typeof payload.name === 'string'
|
||||
? (lang === 'th' ? payload.name : null)
|
||||
: payload.name?.[lang];
|
||||
|
||||
const existing = await prisma.categoryTranslation.findUnique({
|
||||
where: { categoryId_langCode: { categoryId: id, langCode: lang } }
|
||||
if (!name) continue;
|
||||
|
||||
const existing = await prisma.categoryTranslation.findUnique({
|
||||
where: { categoryId_langCode: { categoryId: id, langCode: lang } }
|
||||
});
|
||||
|
||||
if (existing) {
|
||||
const updated = await prisma.categoryTranslation.update({
|
||||
where: { id: existing.id },
|
||||
data: { name },
|
||||
});
|
||||
results.push(updated);
|
||||
} else {
|
||||
const created = await prisma.categoryTranslation.create({
|
||||
data: { categoryId: id, langCode: lang, name },
|
||||
});
|
||||
results.push(created);
|
||||
}
|
||||
}
|
||||
|
||||
if (results.length === 0) {
|
||||
return status(400, { message: "No valid translations provided" });
|
||||
}
|
||||
|
||||
const updated = await prisma.category.findUnique({
|
||||
where: { id },
|
||||
include: { translations: true, _count: { select: { products: true } } },
|
||||
});
|
||||
|
||||
if (existing) {
|
||||
const updated = await prisma.categoryTranslation.update({
|
||||
where: { id: existing.id },
|
||||
data: { name },
|
||||
});
|
||||
results.push(updated);
|
||||
} else {
|
||||
const created = await prisma.categoryTranslation.create({
|
||||
data: { categoryId: id, langCode: lang, name },
|
||||
});
|
||||
results.push(created);
|
||||
}
|
||||
return { success: true, translations: results, category: updated };
|
||||
} catch (err: any) {
|
||||
console.error("Update category translations error:", err);
|
||||
return status(500, { message: err?.message || "Failed to update translations" });
|
||||
}
|
||||
|
||||
const updated = await prisma.category.findUnique({
|
||||
where: { id },
|
||||
include: { translations: true, _count: { select: { products: true } } },
|
||||
});
|
||||
|
||||
return { success: true, translations: results, category: updated };
|
||||
})
|
||||
.post("/", async ({ body, status }) => {
|
||||
try {
|
||||
|
||||
@@ -200,7 +200,7 @@ export const pagesRoute = new Elysia({ prefix: "/pages" })
|
||||
category: {
|
||||
include: { translations: true },
|
||||
},
|
||||
scent: true,
|
||||
brand: true,
|
||||
translations: true,
|
||||
variants: {
|
||||
include: {
|
||||
@@ -219,9 +219,9 @@ export const pagesRoute = new Elysia({ prefix: "/pages" })
|
||||
include: productInclude,
|
||||
take: limit
|
||||
});
|
||||
} else if (payload.type === 'scent' && payload.scentId) {
|
||||
} else if ((payload.type === 'scent' || payload.type === 'brand') && (payload.scentId || payload.brandId)) {
|
||||
products = await prisma.product.findMany({
|
||||
where: { scentId: Number(payload.scentId) },
|
||||
where: { brandId: Number(payload.brandId || payload.scentId) },
|
||||
include: productInclude,
|
||||
take: limit
|
||||
});
|
||||
@@ -247,7 +247,7 @@ export const pagesRoute = new Elysia({ prefix: "/pages" })
|
||||
return { ...p, name: nameObj, description: descObj, details: detailsObj };
|
||||
} else {
|
||||
const trans = p.translations?.find((t: any) => t.langCode === lang) || p.translations?.find((t: any) => t.langCode === 'th') || p.translations?.[0];
|
||||
return { ...p, name: trans?.name || p.scent?.name || '', description: trans?.description, details: trans?.details };
|
||||
return { ...p, name: trans?.name || p.brand?.name || '', description: trans?.description, details: trans?.details };
|
||||
}
|
||||
});
|
||||
} else if (section.type === 'PRODUCT_CAROUSEL' && !Array.isArray(populatedPayload.products)) {
|
||||
@@ -258,7 +258,7 @@ export const pagesRoute = new Elysia({ prefix: "/pages" })
|
||||
category: {
|
||||
include: { translations: true },
|
||||
},
|
||||
scent: true,
|
||||
brand: true,
|
||||
translations: true,
|
||||
variants: {
|
||||
include: {
|
||||
@@ -277,9 +277,9 @@ export const pagesRoute = new Elysia({ prefix: "/pages" })
|
||||
include: productInclude,
|
||||
take: limit
|
||||
});
|
||||
} else if (payload.type === 'scent' && payload.scentId) {
|
||||
} else if ((payload.type === 'scent' || payload.type === 'brand') && (payload.scentId || payload.brandId)) {
|
||||
products = await prisma.product.findMany({
|
||||
where: { scentId: Number(payload.scentId) },
|
||||
where: { brandId: Number(payload.brandId || payload.scentId) },
|
||||
include: productInclude,
|
||||
take: limit
|
||||
});
|
||||
@@ -310,7 +310,7 @@ export const pagesRoute = new Elysia({ prefix: "/pages" })
|
||||
return { ...p, name: nameObj, description: descObj, details: detailsObj };
|
||||
}
|
||||
const trans = p.translations?.find((t: any) => t.langCode === lang) || p.translations?.find((t: any) => t.langCode === 'th') || p.translations?.[0];
|
||||
return { ...p, name: trans?.name || p.scent?.name || '', description: trans?.description, details: trans?.details };
|
||||
return { ...p, name: trans?.name || p.brand?.name || '', description: trans?.description, details: trans?.details };
|
||||
});
|
||||
} else if (section.type === 'CATEGORIES') {
|
||||
let categories: any[] = [];
|
||||
|
||||
Reference in new issue
Block a user