Pull Request branch dev-clone to main #1

Merged
gitea merged 429 commits from dev-clone into main 2024-12-23 09:31:34 +00:00
3 changed files with 0 additions and 336 deletions
Showing only changes of commit 21eef05b39 - Show all commits

View File

@ -230,36 +230,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(
"/getAllQuestions",
@ -414,85 +384,6 @@ const assessmentsRoute = new Hono<HonoEnv>()
}
)
.get(
"/getAllAnswers/:assessmentId", // Use :assessmentId in the URL path
checkPermission("assessments.readAnswers"),
async (c) => {
const assessmentId = c.req.param("assessmentId"); // Retrieve assessmentId from the URL
// Query to retrieve answers for the specific assessmentId, including the associated questionId
const result = await db
.select({
id: answers.id,
assessmentId: answers.assessmentId,
questionId: options.questionId, // Get the questionId from the options table
optionId: answers.optionId,
isFlagged: answers.isFlagged,
filename: answers.filename,
validationInformation: answers.validationInformation,
})
.from(answers)
.leftJoin(options, eq(answers.optionId, options.id)) // Join with the options table
.where(eq(answers.assessmentId, assessmentId)); // Filter by assessmentId
return c.json({
data: result,
});
}
)
// Toggles the isFlagged field between true and false
// .patch(
// "/:questionId/toggleFlag",
// checkPermission("assessments.toggleFlag"),
// async (c) => {
// const questionId = c.req.param("questionId");
// // Join answers and options to retrieve answer based on questionId
// const currentAnswer = await db
// .select({
// isFlagged: answers.isFlagged,
// answerId: answers.id,
// })
// .from(answers)
// .innerJoin(options, eq(answers.optionId, options.id))
// .where(eq(options.questionId, questionId))
// .limit(1);
// if (!currentAnswer.length) {
// throw notFound({
// message: "Answer not found",
// });
// }
// // Toggle the isFlagged value
// const newIsFlaggedValue = !currentAnswer[0].isFlagged;
// // Update the answer with the toggled value
// const updatedAnswer = await db
// .update(answers)
// .set({
// isFlagged: newIsFlaggedValue,
// })
// .where(eq(answers.id, currentAnswer[0].answerId))
// .returning();
// if (!updatedAnswer.length) {
// throw notFound({
// message: "Failed to update answer",
// });
// }
// return c.json(
// {
// message: "Answer flag toggled successfully",
// answer: updatedAnswer[0],
// },
// 200
// );
// }
// )
// Toggles the isFlagged field between true and false
.patch(
"/toggleFlag",
@ -525,39 +416,6 @@ const assessmentsRoute = new Hono<HonoEnv>()
}
)
// Get data answers from table answers by optionId and assessmentId
.post(
"/checkDataAnswer",
checkPermission("assessments.checkAnswer"),
async (c) => {
const { optionId, assessmentId } = await c.req.json();
const result = await db
.select()
.from(answers)
.where(
and(eq(answers.optionId, optionId), eq(answers.assessmentId, assessmentId))
)
.execute();
const existingAnswer = result[0];
let response;
if (existingAnswer) {
response = {
exists: true,
answerId: existingAnswer.id
};
} else {
response = {
exists: false
};
}
return c.json(response);
}
)
// Upload filename to the table answers and save the file on the local storage
.post(
"/uploadFile",
@ -618,93 +476,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(
// "/submitOption",
// checkPermission("assessments.submitOption"),
// requestValidator("json", optionFormSchema),
// async (c) => {
// const optionData = c.req.valid("json");
// // Cek apakah jawaban sudah ada berdasarkan assessmentId dan questionId
// const existingAnswer = await db
// .select()
// .from(answers)
// .leftJoin(options, eq(answers.optionId, options.id))
// .leftJoin(questions, eq(options.questionId, questions.id))
// .where(
// sql`answers."assessmentId" = ${optionData.assessmentId}
// AND questions.id = ${optionData.questionId}`
// )
// .limit(1);
// let answer;
// if (existingAnswer.length > 0) {
// // Update jika jawaban sudah ada
// answer = await db
// .update(answers)
// .set({
// optionId: optionData.optionId, // Ubah ke pilihan baru
// })
// .where(
// sql`answers."assessmentId" = ${optionData.assessmentId}
// AND answers."optionId" IN (
// SELECT id FROM options WHERE "questionId" = ${optionData.questionId}
// )` // Mendapatkan optionId berdasarkan questionId
// )
// .returning();
// } else {
// // Insert jika belum ada jawaban
// answer = await db
// .insert(answers)
// .values({
// optionId: optionData.optionId,
// assessmentId: optionData.assessmentId,
// isFlagged: optionData.isFlagged ?? false,
// filename: optionData.filename ?? null,
// validationInformation: "", // Placeholder untuk not-null constraint
// })
// .returning();
// }
// return c.json(
// {
// message: "Option submitted successfully",
// answer: answer[0],
// },
// 201
// );
// }
// )
.post(
"/submitOption",
checkPermission("assessments.submitOption"),
@ -823,67 +594,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',
@ -918,38 +628,6 @@ const assessmentsRoute = new Hono<HonoEnv>()
}
)
// 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(
'/average-score/aspects/assessments/:assessmentId',

View File

@ -60,19 +60,6 @@ export const getAnswersQueryOptions = (
});
};
export const getAllAnswer = (assessmentId: string) =>
queryOptions({
queryKey: ["Ans", { assessmentId }],
queryFn: () =>
fetchRPC(
client.assessments.getAllAnswers[":assessmentId"].$get({
param: {
assessmentId,
},
})
),
});
// Query untuk toggle flag jawaban berdasarkan questionId
export const toggleFlagAnswer = async (form: {
assessmentId: string;

View File

@ -24,7 +24,6 @@ import {
import { useQuery, useMutation } from "@tanstack/react-query";
import {
getAnswersQueryOptions,
getAllAnswer,
submitAssessmentMutationOptions,
uploadFileMutationOptions,
submitValidationQuery,