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 // Schema for creating and updating subAspects
export const subAspectFormSchema = z.object({ export const subAspectFormSchema = z.object({
name: z.string().min(1).max(50), name: z.string().min(1).max(50),
aspectId: z.string().uuid(), aspectId: z.string()
}); });
export const subAspectUpdateSchema = subAspectFormSchema.extend({}); export const subAspectUpdateSchema = subAspectFormSchema.extend({});
@ -89,9 +89,12 @@ const managementAspectRoute = new Hono<HonoEnv>()
createdAt: aspects.createdAt, createdAt: aspects.createdAt,
updatedAt: aspects.updatedAt, updatedAt: aspects.updatedAt,
...(includeTrashed ? { deletedAt: aspects.deletedAt } : {}), ...(includeTrashed ? { deletedAt: aspects.deletedAt } : {}),
subAspectId : subAspects.id,
subAspectName : subAspects.name,
fullCount: totalCountQuery, fullCount: totalCountQuery,
}) })
.from(aspects) .from(aspects)
.leftJoin(subAspects, eq(subAspects.aspectId, aspects.id))
.where( .where(
and( and(
includeTrashed ? undefined : isNull(aspects.deletedAt), includeTrashed ? undefined : isNull(aspects.deletedAt),
@ -261,22 +264,6 @@ const managementAspectRoute = new Hono<HonoEnv>()
}) })
.where(eq(aspects.id, aspectId)); .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({ return c.json({
message: "Aspect updated successfully", message: "Aspect updated successfully",
}); });
@ -394,7 +381,7 @@ const managementAspectRoute = new Hono<HonoEnv>()
eq(subAspects.aspectId, subAspectData.aspectId))); eq(subAspects.aspectId, subAspectData.aspectId)));
if (existingSubAspect.length > 0) { if (existingSubAspect.length > 0) {
throw forbidden({ message: "Nama Sub Aspek sudah tersedia!" }); throw forbidden({ message: "Sub aspect name already exists!" });
} }
const [aspect] = await db const [aspect] = await db
@ -423,28 +410,39 @@ const managementAspectRoute = new Hono<HonoEnv>()
// Update sub aspect // Update sub aspect
.patch( .patch(
"/subAspect/:id", checkPermission("managementAspect.update"), "/subAspect/:id",
checkPermission("managementAspect.update"),
requestValidator("json", subAspectUpdateSchema), requestValidator("json", subAspectUpdateSchema),
async (c) => { async (c) => {
const subAspectId = c.req.param("id"); const subAspectId = c.req.param("id");
const subAspectData = c.req.valid("json"); 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 const existingSubAspect = await db
.select() .select()
.from(subAspects) .from(subAspects)
.where( .where(
eq(subAspects.aspectId, subAspectData.aspectId)); and(
eq(subAspects.name, subAspectData.name),
eq(subAspects.aspectId, subAspectData.aspectId),
)
);
if (existingSubAspect.length > 0) { 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({ throw notFound({
message: "The sub aspect is not found", message: "The sub aspect is not found",
}); });
// Update the sub aspect
await db await db
.update(subAspects) .update(subAspects)
.set({ .set({
@ -454,9 +452,10 @@ const managementAspectRoute = new Hono<HonoEnv>()
.where(eq(subAspects.id, subAspectId)); .where(eq(subAspects.id, subAspectId));
return c.json({ return c.json({
message: "Sub aspect updated successfully", message: "Sub Aspect updated successfully",
}); });
}) }
)
// Delete sub aspect // Delete sub aspect
.delete( .delete(