Perbaikan UI & API Menu Ekonomi Pasar Desa

This commit is contained in:
2025-07-04 11:09:06 +08:00
parent 0fd47e3e94
commit 4f97c01501
11 changed files with 343 additions and 274 deletions

View File

@@ -7,32 +7,61 @@ type FormCreate = {
alamatUsaha: string;
imageId: string;
rating: number;
kategoriId: string[]; // Array of KategoriMakanan IDs
kategoriId: string[]; // Array of KategoriProduk IDs
};
export default async function pasarDesaCreate(context: Context) {
const body = context.body as FormCreate;
// First, create the PasarDesa record
const pasarDesa = await prisma.pasarDesa.create({
data: {
nama: body.nama,
harga: Number(body.harga),
alamatUsaha: body.alamatUsaha,
imageId: body.imageId,
rating: Number(body.rating),
kategori: {
connect: body.kategoriId.map((id) => ({ id })),
},
},
include: {
image: true,
kategori: true,
},
});
if (!body.kategoriId || body.kategoriId.length === 0) {
throw new Error("At least one kategoriId is required");
}
try {
// Start a transaction to ensure data consistency
const result = await prisma.$transaction(async (prisma) => {
// 1. Create PasarDesa with the first kategoriId as the main category
const pasarDesa = await prisma.pasarDesa.create({
data: {
nama: body.nama,
harga: Number(body.harga),
alamatUsaha: body.alamatUsaha,
imageId: body.imageId,
rating: Number(body.rating),
kategoriProdukId: body.kategoriId[0], // Use the first category as the main one
},
});
// 2. Create category relationships in KategoriToPasar for all categories
await prisma.kategoriToPasar.createMany({
data: body.kategoriId.map((kategoriId) => ({
pasarDesaId: pasarDesa.id,
kategoriId: kategoriId, // Note: The field is 'kategoriId' in the schema, not 'kategoriProdukId'
})),
});
// 3. Get the complete data with relationships
return await prisma.pasarDesa.findUnique({
where: { id: pasarDesa.id },
include: {
image: true,
kategoriProduk: true,
KategoriToPasar: {
include: {
kategori: true,
},
},
},
});
});
return {
success: true,
message: "Success create pasar desa",
data: pasarDesa,
data: result,
};
} catch (error) {
console.error("Error creating PasarDesa:", error);
throw new Error("Failed to create PasarDesa: " + (error as Error).message);
}
}