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 q ? or(ilike(aspects.name, q), eq(aspects.id, q)) : undefined
) )
) )
.orderBy(aspects.name)
.offset(page * limit) .offset(page * limit)
.limit(limit); .limit(limit);
@ -132,7 +133,8 @@ const managementAspectRoute = new Hono<HonoEnv>()
}) })
.from(aspects) .from(aspects)
.leftJoin(subAspects, eq(subAspects.aspectId, aspects.id)) .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 // Grouping sub aspects by aspect ID
const groupedResult = result.reduce((acc, curr) => { const groupedResult = result.reduce((acc, curr) => {
@ -287,10 +289,23 @@ const managementAspectRoute = new Hono<HonoEnv>()
if (aspectData.subAspects) { if (aspectData.subAspects) {
const subAspectsArray = JSON.parse(aspectData.subAspects) as string[]; const subAspectsArray = JSON.parse(aspectData.subAspects) as string[];
// Insert new sub aspects into the database without checking for sub aspect duplication // Create a Set to check for duplicates
if (subAspectsArray.length) { 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( await db.insert(subAspects).values(
subAspectsArray.map((subAspect) => ({ filteredSubAspects.map((subAspect) => ({
aspectId, aspectId,
name: subAspect, 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 // 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);
// 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) { if (existingSubAspect) {
// Update if sub aspect already exists // Update if sub aspect already exists
await db await db
@ -402,12 +427,14 @@ const managementAspectRoute = new Hono<HonoEnv>()
await db await db
.insert(subAspects) .insert(subAspects)
.values({ .values({
id: subAspect.id,
aspectId, aspectId,
name: subAspect.name, name: subAspect.name,
createdAt: new Date(), createdAt: new Date(),
}); });
} }
// Add the name to the Set after processing
uniqueSubAspectNames.add(subAspect.name);
} }
return c.json({ return c.json({