Pull Request branch dev-clone to main #1
|
|
@ -57,381 +57,438 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
||||||
* - withMetadata: boolean
|
* - withMetadata: boolean
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Get all aspects
|
// Get all aspects
|
||||||
.get(
|
.get(
|
||||||
"/",
|
"/",
|
||||||
checkPermission("managementAspect.readAll"),
|
checkPermission("managementAspect.readAll"),
|
||||||
requestValidator(
|
requestValidator(
|
||||||
"query",
|
"query",
|
||||||
z.object({
|
z.object({
|
||||||
includeTrashed: z
|
includeTrashed: z
|
||||||
.string()
|
.string()
|
||||||
.optional()
|
.optional()
|
||||||
.transform((v) => v?.toLowerCase() === "true"),
|
.transform((v) => v?.toLowerCase() === "true"),
|
||||||
withMetadata: z
|
withMetadata: z
|
||||||
.string()
|
.string()
|
||||||
.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(""),
|
||||||
|
})
|
||||||
|
),
|
||||||
|
async (c) => {
|
||||||
|
const { includeTrashed, page, limit, q } = c.req.valid("query");
|
||||||
|
|
||||||
|
const totalCountQuery = includeTrashed
|
||||||
|
? sql<number>`(SELECT count(DISTINCT ${aspects.id}) FROM ${aspects})`
|
||||||
|
: sql<number>`(SELECT count(DISTINCT ${aspects.id}) FROM ${aspects} WHERE ${aspects.deletedAt} IS NULL)`;
|
||||||
|
|
||||||
|
const aspectIdsQuery = await db
|
||||||
|
.select({
|
||||||
|
id: aspects.id,
|
||||||
})
|
})
|
||||||
),
|
.from(aspects)
|
||||||
async (c) => {
|
.where(
|
||||||
const { includeTrashed, page, limit, q } = c.req.valid("query");
|
and(
|
||||||
|
includeTrashed ? undefined : isNull(aspects.deletedAt),
|
||||||
const totalCountQuery = includeTrashed
|
q ? or(ilike(aspects.name, q), eq(aspects.id, q)) : undefined
|
||||||
? sql<number>`(SELECT count(*) FROM ${aspects})`
|
|
||||||
: sql<number>`(SELECT count(*) FROM ${aspects} WHERE ${aspects.deletedAt} IS NULL)`;
|
|
||||||
|
|
||||||
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,
|
|
||||||
fullCount: totalCountQuery,
|
|
||||||
})
|
|
||||||
.from(aspects)
|
|
||||||
.leftJoin(subAspects, eq(subAspects.aspectId, aspects.id))
|
|
||||||
.where(
|
|
||||||
and(
|
|
||||||
includeTrashed ? undefined : isNull(aspects.deletedAt),
|
|
||||||
q ? or(ilike(aspects.name, q), eq(aspects.id, q)) : undefined
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
.offset(page * limit)
|
)
|
||||||
.limit(limit);
|
.offset(page * limit)
|
||||||
|
.limit(limit);
|
||||||
// Grouping sub-aspects by aspect ID
|
|
||||||
const groupedResult = result.reduce((acc, curr) => {
|
const aspectIds = aspectIdsQuery.map(a => a.id);
|
||||||
const aspectId = curr.id;
|
|
||||||
|
if (aspectIds.length === 0) {
|
||||||
if (!acc[aspectId]) {
|
|
||||||
acc[aspectId] = {
|
|
||||||
id: curr.id,
|
|
||||||
name: curr.name,
|
|
||||||
createdAt: curr.createdAt ? new Date(curr.createdAt).toISOString() : null,
|
|
||||||
updatedAt: curr.updatedAt ? new Date(curr.updatedAt).toISOString() : null,
|
|
||||||
subAspects: curr.subAspectName ? [{ id: curr.subAspectId!, name: curr.subAspectName }] : [],
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
if (curr.subAspectName) {
|
|
||||||
acc[aspectId].subAspects.push({ id: curr.subAspectId!, name: curr.subAspectName });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return acc;
|
|
||||||
}, {} as Record<string, {
|
|
||||||
id: string;
|
|
||||||
name: string;
|
|
||||||
createdAt: string | null;
|
|
||||||
updatedAt: string | null;
|
|
||||||
subAspects: { id: string; name: string }[];
|
|
||||||
}>);
|
|
||||||
|
|
||||||
const groupedArray = Object.values(groupedResult);
|
|
||||||
|
|
||||||
return c.json({
|
return c.json({
|
||||||
data: groupedArray,
|
data: [],
|
||||||
_metadata: {
|
_metadata: {
|
||||||
currentPage: page,
|
currentPage: page,
|
||||||
totalPages: Math.ceil((Number(result[0]?.fullCount) ?? 0) / limit),
|
totalPages: 0,
|
||||||
totalItems: Number(result[0]?.fullCount) ?? 0,
|
totalItems: 0,
|
||||||
perPage: limit,
|
perPage: limit,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
// Main query to get aspects, sub-aspects, and number of questions
|
||||||
// Get aspect by id
|
const result = await db
|
||||||
.get(
|
.select({
|
||||||
"/:id",
|
id: aspects.id,
|
||||||
checkPermission("managementAspect.readAll"),
|
name: aspects.name,
|
||||||
requestValidator(
|
createdAt: aspects.createdAt,
|
||||||
"query",
|
updatedAt: aspects.updatedAt,
|
||||||
z.object({
|
...(includeTrashed ? { deletedAt: aspects.deletedAt } : {}),
|
||||||
includeTrashed: z.string().default("false"),
|
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)
|
||||||
async (c) => {
|
.leftJoin(subAspects, eq(subAspects.aspectId, aspects.id))
|
||||||
const aspectId = c.req.param("id");
|
.where(inArray(aspects.id, aspectIds));
|
||||||
|
|
||||||
if (!aspectId)
|
// Grouping sub aspects by aspect ID
|
||||||
throw notFound({
|
const groupedResult = result.reduce((acc, curr) => {
|
||||||
message: "Missing id",
|
const aspectId = curr.id;
|
||||||
});
|
|
||||||
|
if (!acc[aspectId]) {
|
||||||
const includeTrashed = c.req.query("includeTrashed")?.toLowerCase() === "true";
|
acc[aspectId] = {
|
||||||
|
id: curr.id,
|
||||||
const queryResult = await db
|
name: curr.name,
|
||||||
.select({
|
createdAt: curr.createdAt ? new Date(curr.createdAt).toISOString() : null,
|
||||||
id: aspects.id,
|
updatedAt: curr.updatedAt ? new Date(curr.updatedAt).toISOString() : null,
|
||||||
name: aspects.name,
|
subAspects: curr.subAspectName
|
||||||
createdAt: aspects.createdAt,
|
? [{ id: curr.subAspectId!, name: curr.subAspectName, questionCount: curr.questionCount }]
|
||||||
updatedAt: aspects.updatedAt,
|
: [],
|
||||||
...(includeTrashed ? { deletedAt: aspects.deletedAt } : {}),
|
};
|
||||||
subAspect: {
|
|
||||||
name: subAspects.name,
|
|
||||||
id: subAspects.id,
|
|
||||||
questionCount: sql`COUNT(${questions.id})`.as("questionCount"),
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.from(aspects)
|
|
||||||
.leftJoin(subAspects, eq(aspects.id, subAspects.aspectId))
|
|
||||||
.leftJoin(questions, eq(subAspects.id, questions.subAspectId))
|
|
||||||
.where(and(eq(aspects.id, aspectId), !includeTrashed ? isNull(aspects.deletedAt) : undefined))
|
|
||||||
.groupBy(aspects.id, aspects.name, aspects.createdAt, aspects.updatedAt, subAspects.id, subAspects.name);
|
|
||||||
|
|
||||||
if (!queryResult.length)
|
|
||||||
throw forbidden({
|
|
||||||
message: "The aspect does not exist",
|
|
||||||
});
|
|
||||||
|
|
||||||
const subAspectsList = queryResult.reduce((prev, curr) => {
|
|
||||||
if (!curr.subAspect) return prev;
|
|
||||||
prev.set(curr.subAspect.id, {
|
|
||||||
name: curr.subAspect.name,
|
|
||||||
questionCount: Number(curr.subAspect.questionCount),
|
|
||||||
});
|
|
||||||
return prev;
|
|
||||||
}, new Map<string, { name: string; questionCount: number }>());
|
|
||||||
|
|
||||||
const aspectData = {
|
|
||||||
...queryResult[0],
|
|
||||||
subAspect: undefined,
|
|
||||||
subAspects: Array.from(subAspectsList, ([id, { name, questionCount }]) => ({ id, name, questionCount })),
|
|
||||||
};
|
|
||||||
|
|
||||||
return c.json(aspectData);
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
// Create aspect
|
|
||||||
.post("/",
|
|
||||||
checkPermission("managementAspect.create"),
|
|
||||||
requestValidator("json", aspectFormSchema),
|
|
||||||
async (c) => {
|
|
||||||
const aspectData = c.req.valid("json");
|
|
||||||
|
|
||||||
// Cek jika nama aspek sudah ada
|
|
||||||
const existingAspect = await db
|
|
||||||
.select()
|
|
||||||
.from(aspects)
|
|
||||||
.where(eq(aspects.name, aspectData.name));
|
|
||||||
|
|
||||||
let aspectId;
|
|
||||||
if (existingAspect.length > 0) {
|
|
||||||
// Jika aspek sudah ada, ambil ID-nya
|
|
||||||
aspectId = existingAspect[0].id;
|
|
||||||
} else {
|
} else {
|
||||||
// Jika tidak ada, buat aspek baru
|
if (curr.subAspectName) {
|
||||||
const aspect = await db
|
const exists = acc[aspectId].subAspects.some(sub => sub.id === curr.subAspectId);
|
||||||
.insert(aspects)
|
if (!exists) {
|
||||||
.values({
|
acc[aspectId].subAspects.push({
|
||||||
name: aspectData.name,
|
id: curr.subAspectId!,
|
||||||
})
|
name: curr.subAspectName,
|
||||||
.returning();
|
questionCount: curr.questionCount,
|
||||||
aspectId = aspect[0].id;
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Jika ada data sub-aspek, parse dan masukkan ke database
|
|
||||||
if (aspectData.subAspects) {
|
|
||||||
const subAspectsArray = JSON.parse(aspectData.subAspects) as string[];
|
|
||||||
|
|
||||||
// Masukkan sub-aspek baru ke database tanpa cek duplikasi sub-aspek
|
|
||||||
if (subAspectsArray.length) {
|
|
||||||
await db.insert(subAspects).values(
|
|
||||||
subAspectsArray.map((subAspect) => ({
|
|
||||||
aspectId,
|
|
||||||
name: subAspect,
|
|
||||||
}))
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.json(
|
return acc;
|
||||||
{
|
}, {} as Record<string, {
|
||||||
message: "Aspect and sub-aspects created successfully",
|
id: string;
|
||||||
|
name: string;
|
||||||
|
createdAt: string | null;
|
||||||
|
updatedAt: string | null;
|
||||||
|
subAspects: { id: string; name: string; questionCount: number }[];
|
||||||
|
}>);
|
||||||
|
|
||||||
|
const groupedArray = Object.values(groupedResult);
|
||||||
|
|
||||||
|
return c.json({
|
||||||
|
data: groupedArray,
|
||||||
|
_metadata: {
|
||||||
|
currentPage: page,
|
||||||
|
totalPages: Math.ceil((Number(result[0]?.fullCount) ?? 0) / limit),
|
||||||
|
totalItems: Number(result[0]?.fullCount) ?? 0,
|
||||||
|
perPage: limit,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// Get aspect by id
|
||||||
|
.get(
|
||||||
|
"/:id",
|
||||||
|
checkPermission("managementAspect.readAll"),
|
||||||
|
requestValidator(
|
||||||
|
"query",
|
||||||
|
z.object({
|
||||||
|
includeTrashed: z.string().default("false"),
|
||||||
|
})
|
||||||
|
),
|
||||||
|
async (c) => {
|
||||||
|
const aspectId = c.req.param("id");
|
||||||
|
|
||||||
|
if (!aspectId)
|
||||||
|
throw notFound({
|
||||||
|
message: "Missing id",
|
||||||
|
});
|
||||||
|
|
||||||
|
const includeTrashed = c.req.query("includeTrashed")?.toLowerCase() === "true";
|
||||||
|
|
||||||
|
const queryResult = await db
|
||||||
|
.select({
|
||||||
|
id: aspects.id,
|
||||||
|
name: aspects.name,
|
||||||
|
createdAt: aspects.createdAt,
|
||||||
|
updatedAt: aspects.updatedAt,
|
||||||
|
...(includeTrashed ? { deletedAt: aspects.deletedAt } : {}),
|
||||||
|
subAspect: {
|
||||||
|
name: subAspects.name,
|
||||||
|
id: subAspects.id,
|
||||||
|
questionCount: sql`COUNT(${questions.id})`.as("questionCount"),
|
||||||
},
|
},
|
||||||
201
|
})
|
||||||
|
.from(aspects)
|
||||||
|
.leftJoin(subAspects, eq(aspects.id, subAspects.aspectId))
|
||||||
|
.leftJoin(questions, eq(subAspects.id, questions.subAspectId))
|
||||||
|
.where(and(eq(aspects.id, aspectId), !includeTrashed ? isNull(aspects.deletedAt) : undefined))
|
||||||
|
.groupBy(aspects.id, aspects.name, aspects.createdAt, aspects.updatedAt, subAspects.id, subAspects.name);
|
||||||
|
|
||||||
|
if (!queryResult.length)
|
||||||
|
throw notFound({
|
||||||
|
message: "The aspect does not exist",
|
||||||
|
});
|
||||||
|
|
||||||
|
const subAspectsList = queryResult.reduce((prev, curr) => {
|
||||||
|
if (!curr.subAspect) return prev;
|
||||||
|
prev.set(curr.subAspect.id, {
|
||||||
|
name: curr.subAspect.name,
|
||||||
|
questionCount: Number(curr.subAspect.questionCount),
|
||||||
|
});
|
||||||
|
return prev;
|
||||||
|
}, new Map<string, { name: string; questionCount: number }>());
|
||||||
|
|
||||||
|
const aspectData = {
|
||||||
|
...queryResult[0],
|
||||||
|
subAspect: undefined,
|
||||||
|
subAspects: Array.from(subAspectsList, ([id, { name, questionCount }]) => ({ id, name, questionCount })),
|
||||||
|
};
|
||||||
|
|
||||||
|
return c.json(aspectData);
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// Create aspect
|
||||||
|
.post("/",
|
||||||
|
checkPermission("managementAspect.create"),
|
||||||
|
requestValidator("json", aspectFormSchema),
|
||||||
|
async (c) => {
|
||||||
|
const aspectData = c.req.valid("json");
|
||||||
|
|
||||||
|
// Check if aspect name already exists and is not deleted
|
||||||
|
const existingAspect = await db
|
||||||
|
.select()
|
||||||
|
.from(aspects)
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(aspects.name, aspectData.name),
|
||||||
|
isNull(aspects.deletedAt)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (existingAspect.length > 0) {
|
||||||
|
// Return an error if the aspect name already exists
|
||||||
|
return c.json(
|
||||||
|
{ message: "Aspect name already exists" },
|
||||||
|
400 // Bad Request
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
|
||||||
// Update aspect
|
// If it doesn't exist, create a new aspect
|
||||||
.patch(
|
const aspect = await db
|
||||||
"/:id",
|
.insert(aspects)
|
||||||
checkPermission("managementAspect.update"),
|
.values({
|
||||||
requestValidator("json", aspectUpdateSchema),
|
name: aspectData.name,
|
||||||
async (c) => {
|
})
|
||||||
const aspectId = c.req.param("id");
|
.returning();
|
||||||
const aspectData = c.req.valid("json");
|
|
||||||
|
const aspectId = aspect[0].id;
|
||||||
// Cek jika nama aspek baru sudah ada
|
|
||||||
const existingAspect = await db
|
// If there is sub aspect data, parse and insert into the database.
|
||||||
.select()
|
if (aspectData.subAspects) {
|
||||||
.from(aspects)
|
const subAspectsArray = JSON.parse(aspectData.subAspects) as string[];
|
||||||
|
|
||||||
|
// Insert new sub aspects into the database without checking for sub aspect duplication
|
||||||
|
if (subAspectsArray.length) {
|
||||||
|
await db.insert(subAspects).values(
|
||||||
|
subAspectsArray.map((subAspect) => ({
|
||||||
|
aspectId,
|
||||||
|
name: subAspect,
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.json(
|
||||||
|
{
|
||||||
|
message: "Aspect and sub aspects created successfully",
|
||||||
|
},
|
||||||
|
201
|
||||||
|
);
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// Update aspect
|
||||||
|
.patch(
|
||||||
|
"/:id",
|
||||||
|
checkPermission("managementAspect.update"),
|
||||||
|
requestValidator("json", aspectUpdateSchema),
|
||||||
|
async (c) => {
|
||||||
|
const aspectId = c.req.param("id");
|
||||||
|
const aspectData = c.req.valid("json");
|
||||||
|
|
||||||
|
// Check if new aspect name already exists
|
||||||
|
const existingAspect = await db
|
||||||
|
.select()
|
||||||
|
.from(aspects)
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(aspects.name, aspectData.name),
|
||||||
|
isNull(aspects.deletedAt),
|
||||||
|
sql`${aspects.id} <> ${aspectId}`
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (existingAspect.length > 0) {
|
||||||
|
throw notFound({
|
||||||
|
message: "Aspect name already exists",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the aspect in question exists
|
||||||
|
const aspect = await db
|
||||||
|
.select()
|
||||||
|
.from(aspects)
|
||||||
|
.where(and(eq(aspects.id, aspectId), isNull(aspects.deletedAt)));
|
||||||
|
|
||||||
|
if (!aspect[0]) throw notFound();
|
||||||
|
|
||||||
|
// Update aspect name
|
||||||
|
await db
|
||||||
|
.update(aspects)
|
||||||
|
.set({
|
||||||
|
name: aspectData.name,
|
||||||
|
updatedAt: new Date(),
|
||||||
|
})
|
||||||
|
.where(eq(aspects.id, aspectId));
|
||||||
|
|
||||||
|
// Get new sub aspect data from request
|
||||||
|
const newSubAspects = aspectData.subAspects || [];
|
||||||
|
|
||||||
|
// Take the existing sub aspects
|
||||||
|
const currentSubAspects = await db
|
||||||
|
.select({ id: subAspects.id, name: subAspects.name })
|
||||||
|
.from(subAspects)
|
||||||
|
.where(eq(subAspects.aspectId, aspectId));
|
||||||
|
|
||||||
|
const currentSubAspectMap = new Map(currentSubAspects.map(sub => [sub.id, sub.name]));
|
||||||
|
|
||||||
|
// Sub aspects to be removed
|
||||||
|
const subAspectsToDelete = currentSubAspects
|
||||||
|
.filter(sub => !newSubAspects.some(newSub => newSub.id === sub.id))
|
||||||
|
.map(sub => sub.id);
|
||||||
|
|
||||||
|
// Delete sub aspects that do not exist in the new data
|
||||||
|
if (subAspectsToDelete.length) {
|
||||||
|
await db
|
||||||
|
.delete(subAspects)
|
||||||
.where(
|
.where(
|
||||||
and(
|
and(
|
||||||
eq(aspects.name, aspectData.name),
|
eq(subAspects.aspectId, aspectId),
|
||||||
isNull(aspects.deletedAt),
|
inArray(subAspects.id, subAspectsToDelete)
|
||||||
sql`${aspects.id} <> ${aspectId}`
|
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
}
|
||||||
if (existingAspect.length > 0) {
|
|
||||||
throw forbidden({
|
// Update or add new sub aspects
|
||||||
message: "Aspect name already exists",
|
for (const subAspect of newSubAspects) {
|
||||||
});
|
const existingSubAspect = currentSubAspectMap.has(subAspect.id);
|
||||||
}
|
|
||||||
|
if (existingSubAspect) {
|
||||||
// Cek jika aspek yang dimaksud ada
|
// Update if sub aspect already exists
|
||||||
const aspect = await db
|
|
||||||
.select()
|
|
||||||
.from(aspects)
|
|
||||||
.where(and(eq(aspects.id, aspectId), isNull(aspects.deletedAt)));
|
|
||||||
|
|
||||||
if (!aspect[0]) throw notFound();
|
|
||||||
|
|
||||||
// Update nama aspek
|
|
||||||
await db
|
|
||||||
.update(aspects)
|
|
||||||
.set({
|
|
||||||
name: aspectData.name,
|
|
||||||
updatedAt: new Date(),
|
|
||||||
})
|
|
||||||
.where(eq(aspects.id, aspectId));
|
|
||||||
|
|
||||||
// Ambil data sub-aspek baru dari request
|
|
||||||
const newSubAspects = aspectData.subAspects || [];
|
|
||||||
|
|
||||||
// Ambil sub-aspek yang ada saat ini
|
|
||||||
const currentSubAspects = await db
|
|
||||||
.select({ id: subAspects.id, name: subAspects.name })
|
|
||||||
.from(subAspects)
|
|
||||||
.where(eq(subAspects.aspectId, aspectId));
|
|
||||||
|
|
||||||
const currentSubAspectMap = new Map(currentSubAspects.map(sub => [sub.id, sub.name]));
|
|
||||||
|
|
||||||
// Sub-aspek yang harus dihapus
|
|
||||||
const subAspectsToDelete = currentSubAspects
|
|
||||||
.filter(sub => !newSubAspects.some(newSub => newSub.id === sub.id))
|
|
||||||
.map(sub => sub.id);
|
|
||||||
|
|
||||||
// Hapus sub-aspek yang tidak ada di data baru
|
|
||||||
if (subAspectsToDelete.length) {
|
|
||||||
await db
|
await db
|
||||||
.delete(subAspects)
|
.update(subAspects)
|
||||||
|
.set({
|
||||||
|
name: subAspect.name,
|
||||||
|
updatedAt: new Date(),
|
||||||
|
})
|
||||||
.where(
|
.where(
|
||||||
and(
|
and(
|
||||||
eq(subAspects.aspectId, aspectId),
|
eq(subAspects.id, subAspect.id),
|
||||||
inArray(subAspects.id, subAspectsToDelete)
|
eq(subAspects.aspectId, aspectId)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
|
// Add if new sub aspect
|
||||||
|
await db
|
||||||
|
.insert(subAspects)
|
||||||
|
.values({
|
||||||
|
id: subAspect.id,
|
||||||
|
aspectId,
|
||||||
|
name: subAspect.name,
|
||||||
|
createdAt: new Date(),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update atau tambahkan sub-aspek baru
|
|
||||||
for (const subAspect of newSubAspects) {
|
|
||||||
const existingSubAspect = currentSubAspectMap.has(subAspect.id);
|
|
||||||
|
|
||||||
if (existingSubAspect) {
|
|
||||||
// Update jika sub-aspek sudah ada
|
|
||||||
await db
|
|
||||||
.update(subAspects)
|
|
||||||
.set({
|
|
||||||
name: subAspect.name,
|
|
||||||
updatedAt: new Date(),
|
|
||||||
})
|
|
||||||
.where(
|
|
||||||
and(
|
|
||||||
eq(subAspects.id, subAspect.id),
|
|
||||||
eq(subAspects.aspectId, aspectId)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
// Tambah jika sub-aspek baru
|
|
||||||
await db
|
|
||||||
.insert(subAspects)
|
|
||||||
.values({
|
|
||||||
id: subAspect.id, // Pastikan `id` diberikan
|
|
||||||
aspectId,
|
|
||||||
name: subAspect.name,
|
|
||||||
createdAt: new Date(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.json({
|
|
||||||
message: "Aspect and sub-aspects updated successfully",
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
|
||||||
// Delete aspect
|
return c.json({
|
||||||
.delete(
|
message: "Aspect and sub aspects updated successfully",
|
||||||
"/:id",
|
});
|
||||||
checkPermission("managementAspect.delete"),
|
}
|
||||||
requestValidator(
|
)
|
||||||
"form",
|
|
||||||
z.object({
|
// Delete aspect
|
||||||
// skipTrash: z.string().default("false"),
|
.delete(
|
||||||
|
"/:id",
|
||||||
|
checkPermission("managementAspect.delete"),
|
||||||
|
async (c) => {
|
||||||
|
const aspectId = c.req.param("id");
|
||||||
|
|
||||||
|
// Check if aspect exists before deleting
|
||||||
|
const aspect = await db
|
||||||
|
.select()
|
||||||
|
.from(aspects)
|
||||||
|
.where(and(eq(aspects.id, aspectId), isNull(aspects.deletedAt)));
|
||||||
|
|
||||||
|
if (!aspect[0])
|
||||||
|
throw notFound({
|
||||||
|
message: "The aspect is not found",
|
||||||
|
});
|
||||||
|
|
||||||
|
// Update deletedAt column on aspect (soft delete)
|
||||||
|
await db
|
||||||
|
.update(aspects)
|
||||||
|
.set({
|
||||||
|
deletedAt: new Date(),
|
||||||
})
|
})
|
||||||
),
|
.where(eq(aspects.id, aspectId));
|
||||||
async (c) => {
|
|
||||||
const aspectId = c.req.param("id");
|
|
||||||
|
|
||||||
// const skipTrash = c.req.valid("form").skipTrash.toLowerCase() === "true";
|
// Soft delete related sub aspects (update deletedAt on the sub-aspect)
|
||||||
|
await db
|
||||||
|
.update(subAspects)
|
||||||
|
.set({
|
||||||
|
deletedAt: new Date(),
|
||||||
|
})
|
||||||
|
.where(eq(subAspects.aspectId, aspectId));
|
||||||
|
|
||||||
const aspect = await db
|
return c.json({
|
||||||
.select()
|
message: "Aspect and sub aspects soft deleted successfully",
|
||||||
.from(aspects)
|
});
|
||||||
.where(and(eq(aspects.id, aspectId), isNull(aspects.deletedAt)));
|
}
|
||||||
|
)
|
||||||
|
|
||||||
if (!aspect[0])
|
// Undo delete
|
||||||
throw notFound({
|
.patch(
|
||||||
message: "The aspect is not found",
|
"/restore/:id",
|
||||||
});
|
checkPermission("managementAspect.restore"),
|
||||||
|
async (c) => {
|
||||||
|
const aspectId = c.req.param("id");
|
||||||
|
|
||||||
await db
|
// Check if the desired aspects are present
|
||||||
.update(aspects)
|
const aspect = (await db.select().from(aspects).where(eq(aspects.id, aspectId)))[0];
|
||||||
.set({
|
|
||||||
deletedAt: new Date(),
|
if (!aspect) {
|
||||||
})
|
throw notFound({
|
||||||
.where(eq(aspects.id, aspectId));
|
message: "The aspect is not found",
|
||||||
|
|
||||||
return c.json({
|
|
||||||
message: "Aspect deleted successfully",
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
|
||||||
// Undo delete
|
// Make sure the aspect has been deleted (there is deletedAt)
|
||||||
.patch(
|
if (!aspect.deletedAt) {
|
||||||
"/restore/:id",
|
throw notFound({
|
||||||
checkPermission("managementAspect.restore"),
|
message: "The aspect is not deleted",
|
||||||
async (c) => {
|
|
||||||
const aspectId = c.req.param("id");
|
|
||||||
|
|
||||||
const aspect = (await db.select().from(aspects).where(eq(aspects.id, aspectId)))[0];
|
|
||||||
|
|
||||||
if (!aspect) throw notFound();
|
|
||||||
|
|
||||||
if (!aspect.deletedAt) {
|
|
||||||
throw forbidden({
|
|
||||||
message: "The aspect is not deleted",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
await db.update(aspects).set({ deletedAt: null }).where(eq(aspects.id, aspectId));
|
|
||||||
|
|
||||||
return c.json({
|
|
||||||
message: "Aspect restored successfully",
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
// Restore aspects (remove deletedAt mark)
|
||||||
|
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({
|
||||||
|
message: "Aspect and sub aspects restored successfully",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
export default managementAspectRoute;
|
export default managementAspectRoute;
|
||||||
Loading…
Reference in New Issue
Block a user