revision: backend for aspect management
This commit is contained in:
parent
b94bea1237
commit
75d7e93d4c
|
|
@ -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) => {
|
||||
|
|
@ -182,7 +184,7 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
|||
},
|
||||
});
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
// Get aspect by id
|
||||
.get(
|
||||
|
|
@ -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,
|
||||
}))
|
||||
|
|
@ -305,7 +320,7 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
|||
201
|
||||
);
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
// Update aspect
|
||||
.patch(
|
||||
|
|
@ -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({
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user