x
This commit is contained in:
1 parent
80e715da76
commit
57022d46af
7 files changed
+892
-5
No files matched your search
@@ -34,6 +34,106 @@ export const categoriesRoute = new Elysia({ prefix: "/categories" })
|
||||
name: resolveCatName(cat, lang),
|
||||
}));
|
||||
})
|
||||
.post("/bulk-import", async ({ body, status }) => {
|
||||
try {
|
||||
const raw = body as any;
|
||||
const list = Array.isArray(raw) ? raw : (Array.isArray(raw.categories) ? raw.categories : []);
|
||||
|
||||
if (list.length === 0) {
|
||||
return status(400, { message: "No categories provided in array" });
|
||||
}
|
||||
|
||||
const results = [];
|
||||
let createdCount = 0;
|
||||
let updatedCount = 0;
|
||||
|
||||
for (const item of list) {
|
||||
const baseName = item.name || item.translations?.th || item.translations?.en || 'Unnamed Category';
|
||||
const imageUrl = item.imageUrl || null;
|
||||
const translations = item.translations || {};
|
||||
|
||||
let category: any = null;
|
||||
|
||||
if (item.id && typeof item.id === 'number') {
|
||||
category = await prisma.category.findUnique({
|
||||
where: { id: item.id, deletedAt: null },
|
||||
});
|
||||
}
|
||||
|
||||
if (!category && baseName) {
|
||||
category = await prisma.category.findFirst({
|
||||
where: { name: baseName, deletedAt: null },
|
||||
});
|
||||
}
|
||||
|
||||
if (category) {
|
||||
// Update existing
|
||||
const updateData: any = {};
|
||||
if (imageUrl !== undefined) updateData.imageUrl = imageUrl;
|
||||
if (item.name) updateData.name = item.name;
|
||||
|
||||
if (Object.keys(updateData).length > 0) {
|
||||
category = await prisma.category.update({
|
||||
where: { id: category.id },
|
||||
data: updateData,
|
||||
});
|
||||
}
|
||||
updatedCount++;
|
||||
} else {
|
||||
// Create new
|
||||
category = await prisma.category.create({
|
||||
data: {
|
||||
name: baseName,
|
||||
imageUrl,
|
||||
},
|
||||
});
|
||||
createdCount++;
|
||||
}
|
||||
|
||||
// Upsert translations
|
||||
for (const lang of LANGS) {
|
||||
let transName = typeof translations[lang] === 'string'
|
||||
? translations[lang]
|
||||
: (translations[lang]?.name || (lang === 'th' ? baseName : null));
|
||||
|
||||
if (transName && typeof transName === 'string' && transName.trim() !== '') {
|
||||
await prisma.categoryTranslation.upsert({
|
||||
where: {
|
||||
categoryId_langCode: {
|
||||
categoryId: category.id,
|
||||
langCode: lang,
|
||||
},
|
||||
},
|
||||
update: { name: transName.trim() },
|
||||
create: {
|
||||
categoryId: category.id,
|
||||
langCode: lang,
|
||||
name: transName.trim(),
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const fullCategory = await prisma.category.findUnique({
|
||||
where: { id: category.id },
|
||||
include: { translations: true, _count: { select: { products: { where: { isDeleted: false } } } } },
|
||||
});
|
||||
|
||||
results.push(fullCategory);
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
count: results.length,
|
||||
createdCount,
|
||||
updatedCount,
|
||||
categories: results,
|
||||
};
|
||||
} catch (err: any) {
|
||||
console.error("Bulk import categories error:", err);
|
||||
return status(500, { message: err?.message || "Failed to bulk import categories" });
|
||||
}
|
||||
})
|
||||
.get("/:id", async ({ params, query, status }) => {
|
||||
const id = parseInt(params.id);
|
||||
const lang = (query as any).lang || 'all';
|
||||
|
||||
Reference in new issue
Block a user