From 964e9f4eb745083662e6ee9afbc16e0400ea3d94 Mon Sep 17 00:00:00 2001 From: percyfikri Date: Thu, 5 Sep 2024 14:06:16 +0700 Subject: [PATCH] Update : API for management-aspect --- .../src/routes/managementAspect/route.ts | 59 +++++++++---------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/apps/backend/src/routes/managementAspect/route.ts b/apps/backend/src/routes/managementAspect/route.ts index da8a140..772aed5 100644 --- a/apps/backend/src/routes/managementAspect/route.ts +++ b/apps/backend/src/routes/managementAspect/route.ts @@ -40,7 +40,7 @@ export const aspectUpdateSchema = aspectFormSchema.extend({ // Schema for creating and updating subAspects export const subAspectFormSchema = z.object({ name: z.string().min(1).max(50), - aspectId: z.string().uuid(), + aspectId: z.string() }); export const subAspectUpdateSchema = subAspectFormSchema.extend({}); @@ -89,9 +89,12 @@ const managementAspectRoute = new Hono() createdAt: aspects.createdAt, updatedAt: aspects.updatedAt, ...(includeTrashed ? { deletedAt: aspects.deletedAt } : {}), + subAspectId : subAspects.id, + subAspectName : subAspects.name, fullCount: totalCountQuery, }) .from(aspects) + .leftJoin(subAspects, eq(subAspects.aspectId, aspects.id)) .where( and( includeTrashed ? undefined : isNull(aspects.deletedAt), @@ -261,22 +264,6 @@ const managementAspectRoute = new Hono() }) .where(eq(aspects.id, aspectId)); - //Update for Sub-Aspects - // if (aspectData.subAspects) { - // const subAspectsArray = JSON.parse(aspectData.subAspects) as string[]; - - // await db.delete(subAspects).where(eq(subAspects.aspectId, aspectId)); - - // if (subAspectsArray.length) { - // await db.insert(subAspects).values( - // subAspectsArray.map((subAspect) => ({ - // aspectId: aspectId, - // name: subAspect, - // })) - // ); - // } - // } - return c.json({ message: "Aspect updated successfully", }); @@ -394,7 +381,7 @@ const managementAspectRoute = new Hono() eq(subAspects.aspectId, subAspectData.aspectId))); if (existingSubAspect.length > 0) { - throw forbidden({ message: "Nama Sub Aspek sudah tersedia!" }); + throw forbidden({ message: "Sub aspect name already exists!" }); } const [aspect] = await db @@ -423,28 +410,39 @@ const managementAspectRoute = new Hono() // Update sub aspect .patch( - "/subAspect/:id", checkPermission("managementAspect.update"), + "/subAspect/:id", + checkPermission("managementAspect.update"), requestValidator("json", subAspectUpdateSchema), async (c) => { const subAspectId = c.req.param("id"); const subAspectData = c.req.valid("json"); - - // Validation to check if the new sub aspect name already exists + + // Validate if the new sub aspect name already exists for the given aspect const existingSubAspect = await db .select() .from(subAspects) .where( - eq(subAspects.aspectId, subAspectData.aspectId)); - + and( + eq(subAspects.name, subAspectData.name), + eq(subAspects.aspectId, subAspectData.aspectId), + ) + ); + if (existingSubAspect.length > 0) { - throw forbidden({ message: "Name Sub Aspect already exists" }); + throw forbidden({ message: "Sub Aspect name already exists" }); } - - if (!existingSubAspect[0]) + + const [currentSubAspect] = await db + .select() + .from(subAspects) + .where(eq(subAspects.id, subAspectId)); + + if (!currentSubAspect) throw notFound({ message: "The sub aspect is not found", }); - + + // Update the sub aspect await db .update(subAspects) .set({ @@ -452,11 +450,12 @@ const managementAspectRoute = new Hono() updatedAt: new Date(), }) .where(eq(subAspects.id, subAspectId)); - + return c.json({ - message: "Sub aspect updated successfully", + message: "Sub Aspect updated successfully", }); - }) + } + ) // Delete sub aspect .delete(