update: revision for removal of unnecessary endpoints and queries in assessments

This commit is contained in:
falendikategar 2024-10-18 14:54:04 +07:00
parent a3883b7280
commit 8a25d44f91
2 changed files with 0 additions and 243 deletions

View File

@ -16,14 +16,6 @@ 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),
@ -40,8 +32,6 @@ 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);
@ -208,36 +198,6 @@ 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",
@ -559,35 +519,6 @@ 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"),
@ -702,133 +633,6 @@ 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',

View File

@ -12,20 +12,6 @@ type SubmitOptionResponse = {
}; };
}; };
// Query untuk mendapatkan skor assessment saat ini
export const getCurrentAssessmentScoreQueryOptions = (assessmentId: string) =>
queryOptions({
queryKey: ["assessment", { assessmentId }],
queryFn: () =>
fetchRPC(
client.assessments.getCurrentAssessmentScore.$get({
query: {
assessmentId,
},
})
),
});
export const fetchAspects = async () => { export const fetchAspects = async () => {
return await fetchRPC( return await fetchRPC(
client.assessments.aspect.$get({ client.assessments.aspect.$get({
@ -81,26 +67,6 @@ export const toggleFlagAnswerMutationOptions = (questionId: string) => ({
mutationFn: () => toggleFlagAnswer(questionId), mutationFn: () => toggleFlagAnswer(questionId),
}); });
// Di file queries (sesuaikan tipe yang diperlukan untuk submitAnswer)
export const submitAnswer = async (
form: {
optionId: string;
assessmentId: string;
validationInformation: string;
}
) => {
return await fetchRPC(
client.assessments.submitAnswer.$post({
json: form,
})
);
};
// Opsional: Jika Anda ingin menggunakan react-query untuk submitAnswer
export const submitAnswerMutationOptions = () => ({
mutationFn: submitAnswer,
});
// Query untuk mendapatkan rata-rata skor berdasarkan aspectId dan assessmentId // Query untuk mendapatkan rata-rata skor berdasarkan aspectId dan assessmentId
export const getAverageScoreQueryOptions = (assessmentId: string) => export const getAverageScoreQueryOptions = (assessmentId: string) =>
queryOptions({ queryOptions({
@ -115,19 +81,6 @@ export const getAverageScoreQueryOptions = (assessmentId: string) =>
), ),
}); });
export const getAverageScoreSubAspectQueryOptions = (assessmentId: string) =>
queryOptions({
queryKey: ["averageScoreSubAspects", { assessmentId }],
queryFn: () =>
fetchRPC(
client.assessments["average-score"]["sub-aspects"].assessments[":assessmentId"].$get({
param: {
assessmentId,
},
})
),
});
export const submitOption = async (form: { export const submitOption = async (form: {
optionId: string; optionId: string;
assessmentId: string; assessmentId: string;