Merge pull request #19 from digitalsolutiongroup/feat/management-aspect
Update API for management aspect
This commit is contained in:
commit
16a93322fa
|
|
@ -1,6 +1,6 @@
|
||||||
import { and, eq, ilike, isNull, or, sql } from "drizzle-orm";
|
import { and, eq, ilike, isNull, or, sql, inArray } from "drizzle-orm";
|
||||||
import { Hono } from "hono";
|
import { Hono } from "hono";
|
||||||
|
import { questions } from "../../drizzle/schema/questions";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import db from "../../drizzle";
|
import db from "../../drizzle";
|
||||||
import { aspects } from "../../drizzle/schema/aspects";
|
import { aspects } from "../../drizzle/schema/aspects";
|
||||||
|
|
@ -33,14 +33,16 @@ export const aspectFormSchema = z.object({
|
||||||
.optional(),
|
.optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const aspectUpdateSchema = aspectFormSchema.extend({
|
|
||||||
subAspects: z.string().optional().or(z.literal("")),
|
|
||||||
});
|
|
||||||
|
|
||||||
// Schema for creating and updating subAspects
|
// Schema for creating and updating subAspects
|
||||||
export const subAspectFormSchema = z.object({
|
export const subAspectFormSchema = z.object({
|
||||||
|
id: z.string(),
|
||||||
name: z.string().min(1).max(50),
|
name: z.string().min(1).max(50),
|
||||||
aspectId: z.string().uuid(),
|
aspectId: z.string().optional(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const aspectUpdateSchema = z.object({
|
||||||
|
name: z.string(),
|
||||||
|
subAspects: z.array(subAspectFormSchema),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const subAspectUpdateSchema = subAspectFormSchema.extend({});
|
export const subAspectUpdateSchema = subAspectFormSchema.extend({});
|
||||||
|
|
@ -79,17 +81,12 @@ 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 } : {}),
|
|
||||||
fullCount: totalCountQuery,
|
|
||||||
})
|
})
|
||||||
.from(aspects)
|
.from(aspects)
|
||||||
.where(
|
.where(
|
||||||
|
|
@ -101,8 +98,82 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
||||||
.offset(page * limit)
|
.offset(page * limit)
|
||||||
.limit(limit);
|
.limit(limit);
|
||||||
|
|
||||||
|
const aspectIds = aspectIdsQuery.map(a => a.id);
|
||||||
|
|
||||||
|
if (aspectIds.length === 0) {
|
||||||
return c.json({
|
return c.json({
|
||||||
data: result.map((d) => ({ ...d, fullCount: undefined })),
|
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 aspectId = curr.id;
|
||||||
|
|
||||||
|
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, questionCount: curr.questionCount }]
|
||||||
|
: [],
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
if (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,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return acc;
|
||||||
|
}, {} as Record<string, {
|
||||||
|
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: {
|
_metadata: {
|
||||||
currentPage: page,
|
currentPage: page,
|
||||||
totalPages: Math.ceil((Number(result[0]?.fullCount) ?? 0) / limit),
|
totalPages: Math.ceil((Number(result[0]?.fullCount) ?? 0) / limit),
|
||||||
|
|
@ -143,27 +214,33 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
||||||
subAspect: {
|
subAspect: {
|
||||||
name: subAspects.name,
|
name: subAspects.name,
|
||||||
id: subAspects.id,
|
id: subAspects.id,
|
||||||
|
questionCount: sql`COUNT(${questions.id})`.as("questionCount"),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
.from(aspects)
|
.from(aspects)
|
||||||
.leftJoin(subAspects, eq(aspects.id, subAspects.aspectId))
|
.leftJoin(subAspects, eq(aspects.id, subAspects.aspectId))
|
||||||
.where(and(eq(aspects.id, aspectId), !includeTrashed ? isNull(aspects.deletedAt) : undefined));
|
.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)
|
if (!queryResult.length)
|
||||||
throw forbidden({
|
throw notFound({
|
||||||
message: "The aspect does not exist",
|
message: "The aspect does not exist",
|
||||||
});
|
});
|
||||||
|
|
||||||
const subAspectsList = queryResult.reduce((prev, curr) => {
|
const subAspectsList = queryResult.reduce((prev, curr) => {
|
||||||
if (!curr.subAspect) return prev;
|
if (!curr.subAspect) return prev;
|
||||||
prev.set(curr.subAspect.id, curr.subAspect.name);
|
prev.set(curr.subAspect.id, {
|
||||||
|
name: curr.subAspect.name,
|
||||||
|
questionCount: Number(curr.subAspect.questionCount),
|
||||||
|
});
|
||||||
return prev;
|
return prev;
|
||||||
}, new Map<string, string>()); // Map<id, name>
|
}, new Map<string, { name: string; questionCount: number }>());
|
||||||
|
|
||||||
const aspectData = {
|
const aspectData = {
|
||||||
...queryResult[0],
|
...queryResult[0],
|
||||||
subAspect: undefined,
|
subAspect: undefined,
|
||||||
subAspects: Array.from(subAspectsList, ([id, name]) => ({ id, name })),
|
subAspects: Array.from(subAspectsList, ([id, { name, questionCount }]) => ({ id, name, questionCount })),
|
||||||
};
|
};
|
||||||
|
|
||||||
return c.json(aspectData);
|
return c.json(aspectData);
|
||||||
|
|
@ -177,18 +254,26 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
||||||
async (c) => {
|
async (c) => {
|
||||||
const aspectData = c.req.valid("json");
|
const aspectData = c.req.valid("json");
|
||||||
|
|
||||||
// Validation to check if the aspect name already exists
|
// 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)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
if (existingAspect.length > 0) {
|
if (existingAspect.length > 0) {
|
||||||
throw forbidden({
|
// Return an error if the aspect name already exists
|
||||||
message: "Aspect name already exists",
|
return c.json(
|
||||||
});
|
{ message: "Aspect name already exists" },
|
||||||
|
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({
|
||||||
|
|
@ -196,14 +281,17 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
||||||
})
|
})
|
||||||
.returning();
|
.returning();
|
||||||
|
|
||||||
// if sub-aspects are available, parse them into a string array
|
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[];
|
||||||
// if there are sub-aspects, insert them into the database
|
|
||||||
|
// 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) => ({
|
||||||
aspectId: aspect[0].id,
|
aspectId,
|
||||||
name: subAspect,
|
name: subAspect,
|
||||||
}))
|
}))
|
||||||
);
|
);
|
||||||
|
|
@ -212,7 +300,7 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
||||||
|
|
||||||
return c.json(
|
return c.json(
|
||||||
{
|
{
|
||||||
message: "Aspect created successfully",
|
message: "Aspect and sub aspects created successfully",
|
||||||
},
|
},
|
||||||
201
|
201
|
||||||
);
|
);
|
||||||
|
|
@ -228,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");
|
||||||
|
|
||||||
// Validation to check if the new aspect name already exists
|
// Check if new aspect name already exists
|
||||||
const existingAspect = await db
|
const existingAspect = await db
|
||||||
.select()
|
.select()
|
||||||
.from(aspects)
|
.from(aspects)
|
||||||
|
|
@ -241,11 +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",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if the aspect in question exists
|
||||||
const aspect = await db
|
const aspect = await db
|
||||||
.select()
|
.select()
|
||||||
.from(aspects)
|
.from(aspects)
|
||||||
|
|
@ -253,32 +342,76 @@ const managementAspectRoute = new Hono<HonoEnv>()
|
||||||
|
|
||||||
if (!aspect[0]) throw notFound();
|
if (!aspect[0]) throw notFound();
|
||||||
|
|
||||||
|
// Update aspect name
|
||||||
await db
|
await db
|
||||||
.update(aspects)
|
.update(aspects)
|
||||||
.set({
|
.set({
|
||||||
...aspectData,
|
name: aspectData.name,
|
||||||
updatedAt: new Date(),
|
updatedAt: new Date(),
|
||||||
})
|
})
|
||||||
.where(eq(aspects.id, aspectId));
|
.where(eq(aspects.id, aspectId));
|
||||||
|
|
||||||
//Update for Sub-Aspects
|
// Get new sub aspect data from request
|
||||||
// if (aspectData.subAspects) {
|
const newSubAspects = aspectData.subAspects || [];
|
||||||
// const subAspectsArray = JSON.parse(aspectData.subAspects) as string[];
|
|
||||||
|
|
||||||
// await db.delete(subAspects).where(eq(subAspects.aspectId, aspectId));
|
// Take the existing sub aspects
|
||||||
|
const currentSubAspects = await db
|
||||||
|
.select({ id: subAspects.id, name: subAspects.name })
|
||||||
|
.from(subAspects)
|
||||||
|
.where(eq(subAspects.aspectId, aspectId));
|
||||||
|
|
||||||
// if (subAspectsArray.length) {
|
const currentSubAspectMap = new Map(currentSubAspects.map(sub => [sub.id, sub.name]));
|
||||||
// await db.insert(subAspects).values(
|
|
||||||
// subAspectsArray.map((subAspect) => ({
|
// Sub aspects to be removed
|
||||||
// aspectId: aspectId,
|
const subAspectsToDelete = currentSubAspects
|
||||||
// name: subAspect,
|
.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(
|
||||||
|
and(
|
||||||
|
eq(subAspects.aspectId, aspectId),
|
||||||
|
inArray(subAspects.id, subAspectsToDelete)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update or add new sub aspects
|
||||||
|
for (const subAspect of newSubAspects) {
|
||||||
|
const existingSubAspect = currentSubAspectMap.has(subAspect.id);
|
||||||
|
|
||||||
|
if (existingSubAspect) {
|
||||||
|
// Update if sub aspect already exists
|
||||||
|
await db
|
||||||
|
.update(subAspects)
|
||||||
|
.set({
|
||||||
|
name: subAspect.name,
|
||||||
|
updatedAt: new Date(),
|
||||||
|
})
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(subAspects.id, subAspect.id),
|
||||||
|
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(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return c.json({
|
return c.json({
|
||||||
message: "Aspect updated successfully",
|
message: "Aspect and sub aspects updated successfully",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
@ -287,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)
|
||||||
|
|
@ -308,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({
|
||||||
|
|
@ -315,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",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
@ -328,165 +463,32 @@ 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",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// Get sub aspects by aspect ID
|
|
||||||
.get(
|
|
||||||
"/subAspects/:aspectId",
|
|
||||||
checkPermission("managementAspect.readAll"),
|
|
||||||
async (c) => {
|
|
||||||
const aspectId = c.req.param("aspectId");
|
|
||||||
|
|
||||||
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",
|
|
||||||
});
|
|
||||||
|
|
||||||
const subAspectsData = await db
|
|
||||||
.select()
|
|
||||||
.from(subAspects)
|
|
||||||
.where(eq(subAspects.aspectId, aspectId));
|
|
||||||
|
|
||||||
return c.json({
|
|
||||||
subAspects: subAspectsData,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
// Create sub aspect
|
|
||||||
.post(
|
|
||||||
"/subAspect",
|
|
||||||
checkPermission("managementAspect.create"),
|
|
||||||
requestValidator("json", subAspectFormSchema),
|
|
||||||
async (c) => {
|
|
||||||
const subAspectData = c.req.valid("json");
|
|
||||||
|
|
||||||
// Validation to check if the sub aspect name already exists
|
|
||||||
const existingSubAspect = await db
|
|
||||||
.select()
|
|
||||||
.from(subAspects)
|
|
||||||
.where(
|
|
||||||
and(
|
|
||||||
eq(subAspects.name, subAspectData.name),
|
|
||||||
eq(subAspects.aspectId, subAspectData.aspectId)));
|
|
||||||
|
|
||||||
if (existingSubAspect.length > 0) {
|
|
||||||
throw forbidden({ message: "Nama Sub Aspek sudah tersedia!" });
|
|
||||||
}
|
|
||||||
|
|
||||||
const [aspect] = await db
|
|
||||||
.select()
|
|
||||||
.from(aspects)
|
|
||||||
.where(
|
|
||||||
and(
|
|
||||||
eq(aspects.id, subAspectData.aspectId),
|
|
||||||
isNull(aspects.deletedAt)));
|
|
||||||
|
|
||||||
if (!aspect)
|
|
||||||
throw forbidden({
|
|
||||||
message: "The aspect is not found",
|
|
||||||
});
|
|
||||||
|
|
||||||
await db.insert(subAspects).values(subAspectData);
|
|
||||||
|
|
||||||
return c.json(
|
|
||||||
{
|
|
||||||
message: "Sub aspect created successfully",
|
|
||||||
},
|
|
||||||
201
|
|
||||||
);
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
// Update sub aspect
|
|
||||||
.patch(
|
|
||||||
"/subAspect/:id", checkPermission("managementAspect.update"),
|
|
||||||
requestValidator("json", subAspectUpdateSchema),
|
|
||||||
async (c) => {
|
|
||||||
const subAspectId = c.req.param("id");
|
|
||||||
const subAspectData = c.req.valid("json");
|
|
||||||
|
|
||||||
// Validation to check if the new sub aspect name already exists
|
|
||||||
const existingSubAspect = await db
|
|
||||||
.select()
|
|
||||||
.from(subAspects)
|
|
||||||
.where(
|
|
||||||
eq(subAspects.aspectId, subAspectData.aspectId));
|
|
||||||
|
|
||||||
if (existingSubAspect.length > 0) {
|
|
||||||
throw forbidden({ message: "Name Sub Aspect already exists" });
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!existingSubAspect[0])
|
|
||||||
throw notFound({
|
|
||||||
message: "The sub aspect is not found",
|
|
||||||
});
|
|
||||||
|
|
||||||
await db
|
|
||||||
.update(subAspects)
|
|
||||||
.set({
|
|
||||||
...subAspectData,
|
|
||||||
updatedAt: new Date(),
|
|
||||||
})
|
|
||||||
.where(eq(subAspects.id, subAspectId));
|
|
||||||
|
|
||||||
return c.json({
|
|
||||||
message: "Sub aspect updated successfully",
|
|
||||||
});
|
|
||||||
})
|
|
||||||
|
|
||||||
// Delete sub aspect
|
|
||||||
.delete(
|
|
||||||
"/subAspect/:id",
|
|
||||||
checkPermission("managementAspect.delete"),
|
|
||||||
async (c) => {
|
|
||||||
const subAspectId = c.req.param("id");
|
|
||||||
|
|
||||||
const subAspect = await db
|
|
||||||
.select()
|
|
||||||
.from(subAspects)
|
|
||||||
.where(eq(subAspects.id, subAspectId));
|
|
||||||
|
|
||||||
if (!subAspect[0])
|
|
||||||
throw notFound({
|
|
||||||
message: "The sub aspect is not found",
|
|
||||||
});
|
|
||||||
|
|
||||||
await db
|
|
||||||
.update(subAspects)
|
|
||||||
.set({
|
|
||||||
deletedAt: new Date(),
|
|
||||||
})
|
|
||||||
.where(eq(subAspects.id, subAspectId));
|
|
||||||
// await db.delete(subAspects).where(eq(subAspects.id, subAspectId));
|
|
||||||
|
|
||||||
return c.json({
|
|
||||||
message: "Sub aspect deleted successfully",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
export default managementAspectRoute;
|
export default managementAspectRoute;
|
||||||
Loading…
Reference in New Issue
Block a user