Update : API for management-aspect

This commit is contained in:
percyfikri 2024-09-05 14:06:16 +07:00
parent 282b4dfae3
commit 964e9f4eb7

View File

@ -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<HonoEnv>()
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<HonoEnv>()
})
.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<HonoEnv>()
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<HonoEnv>()
// 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({
@ -454,9 +452,10 @@ const managementAspectRoute = new Hono<HonoEnv>()
.where(eq(subAspects.id, subAspectId));
return c.json({
message: "Sub aspect updated successfully",
message: "Sub Aspect updated successfully",
});
})
}
)
// Delete sub aspect
.delete(