revision: backend for aspect management

This commit is contained in:
abiyasa05 2024-10-07 09:01:05 +07:00
parent b94bea1237
commit 75d7e93d4c

View File

@ -95,6 +95,7 @@ const managementAspectRoute = new Hono<HonoEnv>()
q ? or(ilike(aspects.name, q), eq(aspects.id, q)) : undefined
)
)
.orderBy(aspects.name)
.offset(page * limit)
.limit(limit);
@ -132,7 +133,8 @@ const managementAspectRoute = new Hono<HonoEnv>()
})
.from(aspects)
.leftJoin(subAspects, eq(subAspects.aspectId, aspects.id))
.where(inArray(aspects.id, aspectIds));
.where(inArray(aspects.id, aspectIds))
.orderBy(aspects.name);
// Grouping sub aspects by aspect ID
const groupedResult = result.reduce((acc, curr) => {
@ -287,10 +289,23 @@ const managementAspectRoute = new Hono<HonoEnv>()
if (aspectData.subAspects) {
const subAspectsArray = JSON.parse(aspectData.subAspects) as string[];
// Insert new sub aspects into the database without checking for sub aspect duplication
if (subAspectsArray.length) {
// Create a Set to check for duplicates
const uniqueSubAspects = new Set<string>();
// Filter out duplicates
const filteredSubAspects = subAspectsArray.filter((subAspect) => {
if (uniqueSubAspects.has(subAspect)) {
return false; // Skip duplicates
}
uniqueSubAspects.add(subAspect);
return true; // Keep unique sub-aspects
});
// Check if there are any unique sub aspects to insert
if (filteredSubAspects.length) {
// Insert new sub aspects into the database
await db.insert(subAspects).values(
subAspectsArray.map((subAspect) => ({
filteredSubAspects.map((subAspect) => ({
aspectId,
name: subAspect,
}))
@ -379,10 +394,20 @@ const managementAspectRoute = new Hono<HonoEnv>()
);
}
// Create a Set to check for duplicate sub-aspects
const uniqueSubAspectNames = new Set(currentSubAspects.map(sub => sub.name));
// Update or add new sub aspects
for (const subAspect of newSubAspects) {
const existingSubAspect = currentSubAspectMap.has(subAspect.id);
// Check for duplicate sub-aspect names
if (uniqueSubAspectNames.has(subAspect.name) && !existingSubAspect) {
throw notFound({
message: `Sub aspect name "${subAspect.name}" already exists for this aspect.`,
});
}
if (existingSubAspect) {
// Update if sub aspect already exists
await db
@ -402,12 +427,14 @@ const managementAspectRoute = new Hono<HonoEnv>()
await db
.insert(subAspects)
.values({
id: subAspect.id,
aspectId,
name: subAspect.name,
createdAt: new Date(),
});
}
// Add the name to the Set after processing
uniqueSubAspectNames.add(subAspect.name);
}
return c.json({