Pull Request branch dev-clone to main #1
|
|
@ -73,7 +73,7 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
||||||
.optional()
|
.optional()
|
||||||
.transform((v) => v?.toLowerCase() === "true"),
|
.transform((v) => v?.toLowerCase() === "true"),
|
||||||
page: z.coerce.number().int().min(0).default(0),
|
page: z.coerce.number().int().min(0).default(0),
|
||||||
limit: z.coerce.number().int().min(1).max(1000).default(1000),
|
limit: z.coerce.number().int().min(1).max(1000).default(10),
|
||||||
q: z.string().default(""),
|
q: z.string().default(""),
|
||||||
})
|
})
|
||||||
),
|
),
|
||||||
|
|
@ -81,22 +81,14 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
||||||
const { includeTrashed, page, limit, q } = c.req.valid("query");
|
const { includeTrashed, page, limit, q } = c.req.valid("query");
|
||||||
|
|
||||||
const totalCountQuery = includeTrashed
|
const totalCountQuery = includeTrashed
|
||||||
? sql<number>`(SELECT count(*) FROM ${aspects})`
|
? sql<number>`(SELECT count(DISTINCT ${aspects.id}) FROM ${aspects})`
|
||||||
: sql<number>`(SELECT count(*) FROM ${aspects} WHERE ${aspects.deletedAt} IS NULL)`;
|
: sql<number>`(SELECT count(DISTINCT ${aspects.id}) FROM ${aspects} WHERE ${aspects.deletedAt} IS NULL)`;
|
||||||
|
|
||||||
const result = await db
|
const aspectIdsQuery = await db
|
||||||
.select({
|
.select({
|
||||||
id: aspects.id,
|
id: aspects.id,
|
||||||
name: aspects.name,
|
|
||||||
createdAt: aspects.createdAt,
|
|
||||||
updatedAt: aspects.updatedAt,
|
|
||||||
...(includeTrashed ? { deletedAt: aspects.deletedAt } : {}),
|
|
||||||
subAspectId: subAspects.id,
|
|
||||||
subAspectName: subAspects.name,
|
|
||||||
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),
|
||||||
|
|
@ -106,7 +98,43 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
||||||
.offset(page * limit)
|
.offset(page * limit)
|
||||||
.limit(limit);
|
.limit(limit);
|
||||||
|
|
||||||
// Grouping sub-aspects by aspect ID
|
const aspectIds = aspectIdsQuery.map(a => a.id);
|
||||||
|
|
||||||
|
if (aspectIds.length === 0) {
|
||||||
|
return c.json({
|
||||||
|
data: [],
|
||||||
|
_metadata: {
|
||||||
|
currentPage: page,
|
||||||
|
totalPages: 0,
|
||||||
|
totalItems: 0,
|
||||||
|
perPage: limit,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Main query to get aspects, sub-aspects, and number of questions
|
||||||
|
const result = await db
|
||||||
|
.select({
|
||||||
|
id: aspects.id,
|
||||||
|
name: aspects.name,
|
||||||
|
createdAt: aspects.createdAt,
|
||||||
|
updatedAt: aspects.updatedAt,
|
||||||
|
...(includeTrashed ? { deletedAt: aspects.deletedAt } : {}),
|
||||||
|
subAspectId: subAspects.id,
|
||||||
|
subAspectName: subAspects.name,
|
||||||
|
// Increase the number of questions related to sub aspects
|
||||||
|
questionCount: sql<number>`(
|
||||||
|
SELECT count(*)
|
||||||
|
FROM ${questions}
|
||||||
|
WHERE ${questions.subAspectId} = ${subAspects.id}
|
||||||
|
)`.as('questionCount'),
|
||||||
|
fullCount: totalCountQuery,
|
||||||
|
})
|
||||||
|
.from(aspects)
|
||||||
|
.leftJoin(subAspects, eq(subAspects.aspectId, aspects.id))
|
||||||
|
.where(inArray(aspects.id, aspectIds));
|
||||||
|
|
||||||
|
// Grouping sub aspects by aspect ID
|
||||||
const groupedResult = result.reduce((acc, curr) => {
|
const groupedResult = result.reduce((acc, curr) => {
|
||||||
const aspectId = curr.id;
|
const aspectId = curr.id;
|
||||||
|
|
||||||
|
|
@ -116,11 +144,20 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
||||||
name: curr.name,
|
name: curr.name,
|
||||||
createdAt: curr.createdAt ? new Date(curr.createdAt).toISOString() : null,
|
createdAt: curr.createdAt ? new Date(curr.createdAt).toISOString() : null,
|
||||||
updatedAt: curr.updatedAt ? new Date(curr.updatedAt).toISOString() : null,
|
updatedAt: curr.updatedAt ? new Date(curr.updatedAt).toISOString() : null,
|
||||||
subAspects: curr.subAspectName ? [{ id: curr.subAspectId!, name: curr.subAspectName }] : [],
|
subAspects: curr.subAspectName
|
||||||
|
? [{ id: curr.subAspectId!, name: curr.subAspectName, questionCount: curr.questionCount }]
|
||||||
|
: [],
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
if (curr.subAspectName) {
|
if (curr.subAspectName) {
|
||||||
acc[aspectId].subAspects.push({ id: curr.subAspectId!, name: curr.subAspectName });
|
const exists = acc[aspectId].subAspects.some(sub => sub.id === curr.subAspectId);
|
||||||
|
if (!exists) {
|
||||||
|
acc[aspectId].subAspects.push({
|
||||||
|
id: curr.subAspectId!,
|
||||||
|
name: curr.subAspectName,
|
||||||
|
questionCount: curr.questionCount,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -130,7 +167,7 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
||||||
name: string;
|
name: string;
|
||||||
createdAt: string | null;
|
createdAt: string | null;
|
||||||
updatedAt: string | null;
|
updatedAt: string | null;
|
||||||
subAspects: { id: string; name: string }[];
|
subAspects: { id: string; name: string; questionCount: number }[];
|
||||||
}>);
|
}>);
|
||||||
|
|
||||||
const groupedArray = Object.values(groupedResult);
|
const groupedArray = Object.values(groupedResult);
|
||||||
|
|
@ -187,7 +224,7 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
||||||
.groupBy(aspects.id, aspects.name, aspects.createdAt, aspects.updatedAt, subAspects.id, subAspects.name);
|
.groupBy(aspects.id, aspects.name, aspects.createdAt, aspects.updatedAt, subAspects.id, subAspects.name);
|
||||||
|
|
||||||
if (!queryResult.length)
|
if (!queryResult.length)
|
||||||
throw forbidden({
|
throw notFound({
|
||||||
message: "The aspect does not exist",
|
message: "The aspect does not exist",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -217,32 +254,40 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
||||||
async (c) => {
|
async (c) => {
|
||||||
const aspectData = c.req.valid("json");
|
const aspectData = c.req.valid("json");
|
||||||
|
|
||||||
// Cek jika nama aspek sudah ada
|
// Check if aspect name already exists and is not deleted
|
||||||
const existingAspect = await db
|
const existingAspect = await db
|
||||||
.select()
|
.select()
|
||||||
.from(aspects)
|
.from(aspects)
|
||||||
.where(eq(aspects.name, aspectData.name));
|
.where(
|
||||||
|
and(
|
||||||
|
eq(aspects.name, aspectData.name),
|
||||||
|
isNull(aspects.deletedAt)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
let aspectId;
|
|
||||||
if (existingAspect.length > 0) {
|
if (existingAspect.length > 0) {
|
||||||
// Jika aspek sudah ada, ambil ID-nya
|
// Return an error if the aspect name already exists
|
||||||
aspectId = existingAspect[0].id;
|
return c.json(
|
||||||
} else {
|
{ message: "Aspect name already exists" },
|
||||||
// Jika tidak ada, buat aspek baru
|
400 // Bad Request
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If it doesn't exist, create a new aspect
|
||||||
const aspect = await db
|
const aspect = await db
|
||||||
.insert(aspects)
|
.insert(aspects)
|
||||||
.values({
|
.values({
|
||||||
name: aspectData.name,
|
name: aspectData.name,
|
||||||
})
|
})
|
||||||
.returning();
|
.returning();
|
||||||
aspectId = aspect[0].id;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Jika ada data sub-aspek, parse dan masukkan ke database
|
const aspectId = aspect[0].id;
|
||||||
|
|
||||||
|
// If there is sub aspect data, parse and insert into the database.
|
||||||
if (aspectData.subAspects) {
|
if (aspectData.subAspects) {
|
||||||
const subAspectsArray = JSON.parse(aspectData.subAspects) as string[];
|
const subAspectsArray = JSON.parse(aspectData.subAspects) as string[];
|
||||||
|
|
||||||
// Masukkan sub-aspek baru ke database tanpa cek duplikasi sub-aspek
|
// Insert new sub aspects into the database without checking for sub aspect duplication
|
||||||
if (subAspectsArray.length) {
|
if (subAspectsArray.length) {
|
||||||
await db.insert(subAspects).values(
|
await db.insert(subAspects).values(
|
||||||
subAspectsArray.map((subAspect) => ({
|
subAspectsArray.map((subAspect) => ({
|
||||||
|
|
@ -255,7 +300,7 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
||||||
|
|
||||||
return c.json(
|
return c.json(
|
||||||
{
|
{
|
||||||
message: "Aspect and sub-aspects created successfully",
|
message: "Aspect and sub aspects created successfully",
|
||||||
},
|
},
|
||||||
201
|
201
|
||||||
);
|
);
|
||||||
|
|
@ -271,7 +316,7 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
||||||
const aspectId = c.req.param("id");
|
const aspectId = c.req.param("id");
|
||||||
const aspectData = c.req.valid("json");
|
const aspectData = c.req.valid("json");
|
||||||
|
|
||||||
// Cek jika nama aspek baru sudah ada
|
// Check if new aspect name already exists
|
||||||
const existingAspect = await db
|
const existingAspect = await db
|
||||||
.select()
|
.select()
|
||||||
.from(aspects)
|
.from(aspects)
|
||||||
|
|
@ -284,12 +329,12 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
||||||
);
|
);
|
||||||
|
|
||||||
if (existingAspect.length > 0) {
|
if (existingAspect.length > 0) {
|
||||||
throw forbidden({
|
throw notFound({
|
||||||
message: "Aspect name already exists",
|
message: "Aspect name already exists",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cek jika aspek yang dimaksud ada
|
// Check if the aspect in question exists
|
||||||
const aspect = await db
|
const aspect = await db
|
||||||
.select()
|
.select()
|
||||||
.from(aspects)
|
.from(aspects)
|
||||||
|
|
@ -297,7 +342,7 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
||||||
|
|
||||||
if (!aspect[0]) throw notFound();
|
if (!aspect[0]) throw notFound();
|
||||||
|
|
||||||
// Update nama aspek
|
// Update aspect name
|
||||||
await db
|
await db
|
||||||
.update(aspects)
|
.update(aspects)
|
||||||
.set({
|
.set({
|
||||||
|
|
@ -306,10 +351,10 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
||||||
})
|
})
|
||||||
.where(eq(aspects.id, aspectId));
|
.where(eq(aspects.id, aspectId));
|
||||||
|
|
||||||
// Ambil data sub-aspek baru dari request
|
// Get new sub aspect data from request
|
||||||
const newSubAspects = aspectData.subAspects || [];
|
const newSubAspects = aspectData.subAspects || [];
|
||||||
|
|
||||||
// Ambil sub-aspek yang ada saat ini
|
// Take the existing sub aspects
|
||||||
const currentSubAspects = await db
|
const currentSubAspects = await db
|
||||||
.select({ id: subAspects.id, name: subAspects.name })
|
.select({ id: subAspects.id, name: subAspects.name })
|
||||||
.from(subAspects)
|
.from(subAspects)
|
||||||
|
|
@ -317,12 +362,12 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
||||||
|
|
||||||
const currentSubAspectMap = new Map(currentSubAspects.map(sub => [sub.id, sub.name]));
|
const currentSubAspectMap = new Map(currentSubAspects.map(sub => [sub.id, sub.name]));
|
||||||
|
|
||||||
// Sub-aspek yang harus dihapus
|
// Sub aspects to be removed
|
||||||
const subAspectsToDelete = currentSubAspects
|
const subAspectsToDelete = currentSubAspects
|
||||||
.filter(sub => !newSubAspects.some(newSub => newSub.id === sub.id))
|
.filter(sub => !newSubAspects.some(newSub => newSub.id === sub.id))
|
||||||
.map(sub => sub.id);
|
.map(sub => sub.id);
|
||||||
|
|
||||||
// Hapus sub-aspek yang tidak ada di data baru
|
// Delete sub aspects that do not exist in the new data
|
||||||
if (subAspectsToDelete.length) {
|
if (subAspectsToDelete.length) {
|
||||||
await db
|
await db
|
||||||
.delete(subAspects)
|
.delete(subAspects)
|
||||||
|
|
@ -334,12 +379,12 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update atau tambahkan sub-aspek baru
|
// Update or add new sub aspects
|
||||||
for (const subAspect of newSubAspects) {
|
for (const subAspect of newSubAspects) {
|
||||||
const existingSubAspect = currentSubAspectMap.has(subAspect.id);
|
const existingSubAspect = currentSubAspectMap.has(subAspect.id);
|
||||||
|
|
||||||
if (existingSubAspect) {
|
if (existingSubAspect) {
|
||||||
// Update jika sub-aspek sudah ada
|
// Update if sub aspect already exists
|
||||||
await db
|
await db
|
||||||
.update(subAspects)
|
.update(subAspects)
|
||||||
.set({
|
.set({
|
||||||
|
|
@ -353,11 +398,11 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
// Tambah jika sub-aspek baru
|
// Add if new sub aspect
|
||||||
await db
|
await db
|
||||||
.insert(subAspects)
|
.insert(subAspects)
|
||||||
.values({
|
.values({
|
||||||
id: subAspect.id, // Pastikan `id` diberikan
|
id: subAspect.id,
|
||||||
aspectId,
|
aspectId,
|
||||||
name: subAspect.name,
|
name: subAspect.name,
|
||||||
createdAt: new Date(),
|
createdAt: new Date(),
|
||||||
|
|
@ -366,7 +411,7 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.json({
|
return c.json({
|
||||||
message: "Aspect and sub-aspects updated successfully",
|
message: "Aspect and sub aspects updated successfully",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
@ -375,17 +420,10 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
||||||
.delete(
|
.delete(
|
||||||
"/:id",
|
"/:id",
|
||||||
checkPermission("managementAspect.delete"),
|
checkPermission("managementAspect.delete"),
|
||||||
requestValidator(
|
|
||||||
"form",
|
|
||||||
z.object({
|
|
||||||
// skipTrash: z.string().default("false"),
|
|
||||||
})
|
|
||||||
),
|
|
||||||
async (c) => {
|
async (c) => {
|
||||||
const aspectId = c.req.param("id");
|
const aspectId = c.req.param("id");
|
||||||
|
|
||||||
// const skipTrash = c.req.valid("form").skipTrash.toLowerCase() === "true";
|
// Check if aspect exists before deleting
|
||||||
|
|
||||||
const aspect = await db
|
const aspect = await db
|
||||||
.select()
|
.select()
|
||||||
.from(aspects)
|
.from(aspects)
|
||||||
|
|
@ -396,6 +434,7 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
||||||
message: "The aspect is not found",
|
message: "The aspect is not found",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Update deletedAt column on aspect (soft delete)
|
||||||
await db
|
await db
|
||||||
.update(aspects)
|
.update(aspects)
|
||||||
.set({
|
.set({
|
||||||
|
|
@ -403,8 +442,16 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
||||||
})
|
})
|
||||||
.where(eq(aspects.id, aspectId));
|
.where(eq(aspects.id, aspectId));
|
||||||
|
|
||||||
|
// Soft delete related sub aspects (update deletedAt on the sub-aspect)
|
||||||
|
await db
|
||||||
|
.update(subAspects)
|
||||||
|
.set({
|
||||||
|
deletedAt: new Date(),
|
||||||
|
})
|
||||||
|
.where(eq(subAspects.aspectId, aspectId));
|
||||||
|
|
||||||
return c.json({
|
return c.json({
|
||||||
message: "Aspect deleted successfully",
|
message: "Aspect and sub aspects soft deleted successfully",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
@ -416,20 +463,30 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
||||||
async (c) => {
|
async (c) => {
|
||||||
const aspectId = c.req.param("id");
|
const aspectId = c.req.param("id");
|
||||||
|
|
||||||
|
// Check if the desired aspects are present
|
||||||
const aspect = (await db.select().from(aspects).where(eq(aspects.id, aspectId)))[0];
|
const aspect = (await db.select().from(aspects).where(eq(aspects.id, aspectId)))[0];
|
||||||
|
|
||||||
if (!aspect) throw notFound();
|
if (!aspect) {
|
||||||
|
throw notFound({
|
||||||
|
message: "The aspect is not found",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make sure the aspect has been deleted (there is deletedAt)
|
||||||
if (!aspect.deletedAt) {
|
if (!aspect.deletedAt) {
|
||||||
throw forbidden({
|
throw notFound({
|
||||||
message: "The aspect is not deleted",
|
message: "The aspect is not deleted",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Restore aspects (remove deletedAt mark)
|
||||||
await db.update(aspects).set({ deletedAt: null }).where(eq(aspects.id, aspectId));
|
await db.update(aspects).set({ deletedAt: null }).where(eq(aspects.id, aspectId));
|
||||||
|
|
||||||
|
// Restore all related sub aspects that have also been deleted (if any)
|
||||||
|
await db.update(subAspects).set({ deletedAt: null }).where(eq(subAspects.aspectId, aspectId));
|
||||||
|
|
||||||
return c.json({
|
return c.json({
|
||||||
message: "Aspect restored successfully",
|
message: "Aspect and sub aspects restored successfully",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user