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 { Hono } from "hono";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import db from "../../drizzle";
|
import db from "../../drizzle";
|
||||||
|
|
@ -55,6 +55,13 @@ export const newValidationFormSchema = z.object({
|
||||||
newValidationInformation: z.string().min(1, "Validation information is required"),
|
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();
|
export const answerUpdateSchema = answerFormSchema.partial();
|
||||||
|
|
||||||
// Helper untuk menyimpan file
|
// 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
|
// Toggles the isFlagged field between true and false
|
||||||
.patch(
|
.patch(
|
||||||
"/:questionId/toggleFlag",
|
"/toggleFlag",
|
||||||
checkPermission("assessments.toggleFlag"),
|
checkPermission("assessments.submitOption"),
|
||||||
|
requestValidator("json", flagFormSchema),
|
||||||
async (c) => {
|
async (c) => {
|
||||||
const questionId = c.req.param("questionId");
|
const flagData = c.req.valid("json");
|
||||||
|
|
||||||
// Join answers and options to retrieve answer based on questionId
|
// Update jawaban yang ada berdasarkan assessmentId dan questionId
|
||||||
const currentAnswer = await db
|
const answer = 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)
|
.update(answers)
|
||||||
.set({
|
.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();
|
.returning();
|
||||||
|
|
||||||
if (!updatedAnswer.length) {
|
|
||||||
throw notFound({
|
|
||||||
message: "Failed to update answer",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.json(
|
return c.json(
|
||||||
{
|
{
|
||||||
message: "Answer flag toggled successfully",
|
message: "Flag changed successfully",
|
||||||
answer: updatedAnswer[0],
|
answer: answer[0],
|
||||||
},
|
},
|
||||||
200
|
200
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user