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