Pull Request branch dev-clone to main #1
|
|
@ -16,6 +16,14 @@ import path from "path";
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { notFound } from "../../errors/DashboardError";
|
import { notFound } from "../../errors/DashboardError";
|
||||||
|
|
||||||
|
export const answerFormSchema = z.object({
|
||||||
|
optionId: z.string().min(1),
|
||||||
|
assessmentId: z.string().min(1),
|
||||||
|
isFlagged: z.boolean().optional().default(false),
|
||||||
|
filename: z.string().optional(),
|
||||||
|
validationInformation: z.string().min(1),
|
||||||
|
});
|
||||||
|
|
||||||
// optionFormSchema: untuk /submitOption
|
// optionFormSchema: untuk /submitOption
|
||||||
export const optionFormSchema = z.object({
|
export const optionFormSchema = z.object({
|
||||||
optionId: z.string().min(1),
|
optionId: z.string().min(1),
|
||||||
|
|
@ -32,6 +40,8 @@ export const validationFormSchema = z.object({
|
||||||
validationInformation: z.string().min(1, "Validation information is required"),
|
validationInformation: z.string().min(1, "Validation information is required"),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const answerUpdateSchema = answerFormSchema.partial();
|
||||||
|
|
||||||
// Helper untuk menyimpan file
|
// Helper untuk menyimpan file
|
||||||
async function saveFile(filePath: string, fileBuffer: Buffer): Promise<void> {
|
async function saveFile(filePath: string, fileBuffer: Buffer): Promise<void> {
|
||||||
await fs.promises.writeFile(filePath, fileBuffer);
|
await fs.promises.writeFile(filePath, fileBuffer);
|
||||||
|
|
@ -198,6 +208,36 @@ const assessmentsRoute = new Hono<HonoEnv>()
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Get data for current Assessment Score from submitted options By Assessment Id
|
||||||
|
.get(
|
||||||
|
"/getCurrentAssessmentScore",
|
||||||
|
checkPermission("assessments.readAssessmentScore"),
|
||||||
|
requestValidator(
|
||||||
|
"query",
|
||||||
|
z.object({
|
||||||
|
assessmentId: z.string(),
|
||||||
|
})
|
||||||
|
),
|
||||||
|
async (c) => {
|
||||||
|
const { assessmentId } = c.req.valid("query");
|
||||||
|
|
||||||
|
// Query to sum the scores of selected options for the current assessment
|
||||||
|
const result = await db
|
||||||
|
.select({
|
||||||
|
totalScore: sql<number>`SUM(${options.score})`,
|
||||||
|
})
|
||||||
|
.from(answers)
|
||||||
|
.leftJoin(options, eq(answers.optionId, options.id))
|
||||||
|
.where(eq(answers.assessmentId, assessmentId))
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
return c.json({
|
||||||
|
assessmentId,
|
||||||
|
totalScore: result[0]?.totalScore ?? 0, // Return 0 if no answers are found
|
||||||
|
});
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
// Get all Questions and Options that relate to Sub Aspects and Aspects
|
// Get all Questions and Options that relate to Sub Aspects and Aspects
|
||||||
.get(
|
.get(
|
||||||
"/getAllQuestions",
|
"/getAllQuestions",
|
||||||
|
|
@ -519,6 +559,35 @@ const assessmentsRoute = new Hono<HonoEnv>()
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Submit option to table answers from use-form in frontend
|
||||||
|
.post(
|
||||||
|
"/submitAnswer",
|
||||||
|
checkPermission("assessments.submitAnswer"),
|
||||||
|
requestValidator("json", answerFormSchema),
|
||||||
|
async (c) => {
|
||||||
|
const answerData = c.req.valid("json");
|
||||||
|
|
||||||
|
const answer = await db
|
||||||
|
.insert(answers)
|
||||||
|
.values({
|
||||||
|
optionId: answerData.optionId,
|
||||||
|
assessmentId: answerData.assessmentId,
|
||||||
|
isFlagged: answerData.isFlagged,
|
||||||
|
filename: answerData.filename,
|
||||||
|
validationInformation: answerData.validationInformation,
|
||||||
|
})
|
||||||
|
.returning();
|
||||||
|
|
||||||
|
return c.json(
|
||||||
|
{
|
||||||
|
message: "Answer created successfully",
|
||||||
|
answer: answer[0],
|
||||||
|
},
|
||||||
|
201
|
||||||
|
);
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
.post(
|
.post(
|
||||||
"/submitOption",
|
"/submitOption",
|
||||||
// checkPermission("assessments.submitOption"),
|
// checkPermission("assessments.submitOption"),
|
||||||
|
|
@ -633,6 +702,133 @@ const assessmentsRoute = new Hono<HonoEnv>()
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Update answer in table answers if answer changes
|
||||||
|
.patch(
|
||||||
|
"/:id/updateAnswer",
|
||||||
|
checkPermission("assessments.updateAnswer"),
|
||||||
|
requestValidator("json", answerUpdateSchema),
|
||||||
|
async (c) => {
|
||||||
|
const answerId = c.req.param("id");
|
||||||
|
const answerData = c.req.valid("json");
|
||||||
|
|
||||||
|
const updatedAnswer = await db
|
||||||
|
.update(answers)
|
||||||
|
.set({
|
||||||
|
optionId: answerData.optionId,
|
||||||
|
})
|
||||||
|
.where(eq(answers.id, answerId))
|
||||||
|
.returning();
|
||||||
|
|
||||||
|
if (!updatedAnswer.length) {
|
||||||
|
throw notFound({
|
||||||
|
message: "Answer not found or update failed"
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.json({
|
||||||
|
message: "Answer updated successfully",
|
||||||
|
answer: updatedAnswer[0],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// Get data for One Sub Aspect average score By Sub Aspect Id and Assessment Id
|
||||||
|
.get(
|
||||||
|
'/average-score/sub-aspects/:subAspectId/assessments/:assessmentId',
|
||||||
|
checkPermission("assessments.readAssessmentScore"),
|
||||||
|
async (c) => {
|
||||||
|
const { subAspectId, assessmentId } = c.req.param();
|
||||||
|
|
||||||
|
const averageScore = await db
|
||||||
|
.select({
|
||||||
|
subAspectName: subAspects.name,
|
||||||
|
average: sql`AVG(options.score)`
|
||||||
|
})
|
||||||
|
.from(answers)
|
||||||
|
.innerJoin(options, eq(answers.optionId, options.id))
|
||||||
|
.innerJoin(questions, eq(options.questionId, questions.id))
|
||||||
|
.innerJoin(subAspects, eq(questions.subAspectId, subAspects.id))
|
||||||
|
.innerJoin(assessments, eq(answers.assessmentId, assessments.id))
|
||||||
|
.where(
|
||||||
|
sql`sub_aspects.id = ${subAspectId} AND assessments.id = ${assessmentId}`
|
||||||
|
)
|
||||||
|
.groupBy(subAspects.id);
|
||||||
|
|
||||||
|
return c.json({
|
||||||
|
subAspectId,
|
||||||
|
subAspectName: averageScore[0].subAspectName,
|
||||||
|
assessmentId,
|
||||||
|
averageScore: averageScore.length > 0 ? averageScore[0].average : 0
|
||||||
|
});
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// Get data for All Sub Aspects average score By Assessment Id
|
||||||
|
.get(
|
||||||
|
'/average-score/sub-aspects/assessments/:assessmentId',
|
||||||
|
checkPermission("assessments.readAssessmentScore"),
|
||||||
|
async (c) => {
|
||||||
|
const { assessmentId } = c.req.param();
|
||||||
|
|
||||||
|
const averageScores = await db
|
||||||
|
.select({
|
||||||
|
aspectId: subAspects.aspectId,
|
||||||
|
subAspectId: subAspects.id,
|
||||||
|
subAspectName: subAspects.name,
|
||||||
|
average: sql`AVG(options.score)`
|
||||||
|
})
|
||||||
|
.from(answers)
|
||||||
|
.innerJoin(options, eq(answers.optionId, options.id))
|
||||||
|
.innerJoin(questions, eq(options.questionId, questions.id))
|
||||||
|
.innerJoin(subAspects, eq(questions.subAspectId, subAspects.id))
|
||||||
|
.innerJoin(assessments, eq(answers.assessmentId, assessments.id))
|
||||||
|
.where(eq(assessments.id, assessmentId))
|
||||||
|
.groupBy(subAspects.id);
|
||||||
|
|
||||||
|
return c.json({
|
||||||
|
assessmentId,
|
||||||
|
subAspects: averageScores.map(score => ({
|
||||||
|
subAspectId: score.subAspectId,
|
||||||
|
subAspectName: score.subAspectName,
|
||||||
|
averageScore: score.average,
|
||||||
|
aspectId: score.aspectId
|
||||||
|
}))
|
||||||
|
});
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// Get data for One Aspect average score By Aspect Id and Assessment Id
|
||||||
|
.get(
|
||||||
|
"/average-score/aspects/:aspectId/assessments/:assessmentId",
|
||||||
|
checkPermission("assessments.readAssessmentScore"),
|
||||||
|
async (c) => {
|
||||||
|
const { aspectId, assessmentId } = c.req.param();
|
||||||
|
|
||||||
|
const averageScore = await db
|
||||||
|
.select({
|
||||||
|
aspectName: aspects.name,
|
||||||
|
average: sql`AVG(options.score)`
|
||||||
|
})
|
||||||
|
.from(answers)
|
||||||
|
.innerJoin(options, eq(answers.optionId, options.id))
|
||||||
|
.innerJoin(questions, eq(options.questionId, questions.id))
|
||||||
|
.innerJoin(subAspects, eq(questions.subAspectId, subAspects.id))
|
||||||
|
.innerJoin(aspects, eq(subAspects.aspectId, aspects.id))
|
||||||
|
.innerJoin(assessments, eq(answers.assessmentId, assessments.id))
|
||||||
|
.where(
|
||||||
|
sql`aspects.id = ${aspectId} AND assessments.id = ${assessmentId}`
|
||||||
|
)
|
||||||
|
.groupBy(aspects.id);
|
||||||
|
|
||||||
|
return c.json({
|
||||||
|
aspectId,
|
||||||
|
aspectName: averageScore[0].aspectName,
|
||||||
|
assessmentId,
|
||||||
|
averageScore: averageScore.length > 0 ? averageScore[0].average : 0
|
||||||
|
});
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
// Get data for Aspects average score and all related Sub Aspects average score By Assessment Id
|
// Get data for Aspects average score and all related Sub Aspects average score By Assessment Id
|
||||||
.get(
|
.get(
|
||||||
'/average-score/aspects/assessments/:assessmentId',
|
'/average-score/aspects/assessments/:assessmentId',
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user