Pull Request branch dev-clone to main #1
|
|
@ -1,4 +1,4 @@
|
|||
import { and, eq, ilike, isNull, inArray, or, sql } from "drizzle-orm";
|
||||
import { and, eq, ilike, isNull, inArray, or, sql, is } from "drizzle-orm";
|
||||
import { Hono } from "hono";
|
||||
import { z } from "zod";
|
||||
import db from "../../drizzle";
|
||||
|
|
@ -55,6 +55,13 @@ export const newValidationFormSchema = z.object({
|
|||
newValidationInformation: z.string().min(1, "Validation information is required"),
|
||||
});
|
||||
|
||||
// validationFormSchema: untuk /submitValidation
|
||||
export const flagFormSchema = z.object({
|
||||
assessmentId: z.string().min(1),
|
||||
questionId: z.string().min(1),
|
||||
isFlagged: z.boolean().optional().default(false),
|
||||
});
|
||||
|
||||
export const answerUpdateSchema = answerFormSchema.partial();
|
||||
|
||||
// Helper untuk menyimpan file
|
||||
|
|
@ -434,52 +441,84 @@ const assessmentsRoute = new Hono<HonoEnv>()
|
|||
}
|
||||
)
|
||||
|
||||
// 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(
|
||||
"/:questionId/toggleFlag",
|
||||
checkPermission("assessments.toggleFlag"),
|
||||
"/toggleFlag",
|
||||
checkPermission("assessments.submitOption"),
|
||||
requestValidator("json", flagFormSchema),
|
||||
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
|
||||
const flagData = c.req.valid("json");
|
||||
|
||||
// Update jawaban yang ada berdasarkan assessmentId dan questionId
|
||||
const answer = await db
|
||||
.update(answers)
|
||||
.set({
|
||||
isFlagged: newIsFlaggedValue,
|
||||
isFlagged: flagData.isFlagged, // Ubah ke pilihan baru
|
||||
})
|
||||
.where(eq(answers.id, currentAnswer[0].answerId))
|
||||
.where(
|
||||
and(
|
||||
eq(answers.assessmentId, flagData.assessmentId),
|
||||
eq(answers.questionId, flagData.questionId)
|
||||
)
|
||||
)
|
||||
.returning();
|
||||
|
||||
if (!updatedAnswer.length) {
|
||||
throw notFound({
|
||||
message: "Failed to update answer",
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
return c.json(
|
||||
{
|
||||
message: "Answer flag toggled successfully",
|
||||
answer: updatedAnswer[0],
|
||||
message: "Flag changed successfully",
|
||||
answer: answer[0],
|
||||
},
|
||||
200
|
||||
);
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user