diff --git a/apps/backend/src/drizzle/schema/answers.ts b/apps/backend/src/drizzle/schema/answers.ts index 6f8bf5c..4855a9d 100644 --- a/apps/backend/src/drizzle/schema/answers.ts +++ b/apps/backend/src/drizzle/schema/answers.ts @@ -9,11 +9,14 @@ import { } from "drizzle-orm/pg-core"; import { options } from "./options"; import { assessments } from "./assessments"; +import { questions } from "./questions"; export const answers = pgTable("answers", { id: varchar("id", { length: 50 }) .primaryKey() .$defaultFn(() => createId()), + questionId: varchar("questionId", { length: 50 }) + .references(() => questions.id), optionId: varchar("optionId", { length: 50 }) .references(() => options.id), assessmentId: varchar("assessmentId", { length: 50 }) diff --git a/apps/backend/src/routes/assessmentRequest/route.ts b/apps/backend/src/routes/assessmentRequest/route.ts index 5f5b395..977a4f4 100644 --- a/apps/backend/src/routes/assessmentRequest/route.ts +++ b/apps/backend/src/routes/assessmentRequest/route.ts @@ -12,6 +12,11 @@ import checkPermission from "../../middlewares/checkPermission"; import requestValidator from "../../utils/requestValidator"; import { HTTPException } from "hono/http-exception"; import { createId } from "@paralleldrive/cuid2"; +import { questions } from "../../drizzle/schema/questions"; +import { subAspects } from "../../drizzle/schema/subAspects"; +import { aspects } from "../../drizzle/schema/aspects"; +import { answers } from "../../drizzle/schema/answers"; +import { options } from "../../drizzle/schema/options"; const assessmentRequestRoute = new Hono() .use(authInfo) @@ -138,7 +143,7 @@ const assessmentRequestRoute = new Hono() eq(assessments.respondentId, respondentId), eq(assessments.status, "dalam pengerjaan") ) - );console.log(existingAssessment); + ); if (!existingAssessment.length) { const newAssessment = await db @@ -161,7 +166,8 @@ const assessmentRequestRoute = new Hono() } ) - // Update assessment status when the user clicks the start assessment button + /*Update assessment status when the user clicks the start assessment button + and Post all answers for assessment by ID */ .patch( "/:assessmentId", checkPermission("assessmentRequest.update"), @@ -189,7 +195,7 @@ const assessmentRequestRoute = new Hono() message: "User not authenticated", }); } - + // Check if the assessment with the given assessmentId exists and belongs to the user const existingAssessment = await db .select() @@ -202,7 +208,7 @@ const assessmentRequestRoute = new Hono() message: "Assessment not found or unauthorized.", }); } - + // Update assessment status to "dalam pengerjaan" const updatedAssessment = await db .update(assessments) @@ -212,12 +218,61 @@ const assessmentRequestRoute = new Hono() .where(eq(assessments.id, assessmentId)) .returning(); + // Get all questions with options related to the assessment by aspect and sub-aspect + const questionsWithOptions = await db + .select({ + questionId: questions.id, + }) + .from(questions) + .leftJoin(options, eq(questions.id, options.questionId)) + .where(sql`${options.id} IS NOT NULL`) // Filter only questions with options + .leftJoin(subAspects, eq(questions.subAspectId, subAspects.id)) + .leftJoin(aspects, eq(subAspects.aspectId, aspects.id)) + .groupBy(questions.id); + + if (!questionsWithOptions.length) { + throw notFound({ + message: "No questions found for this assessment.", + }); + } + + // Check answers that already have optionId + const existingAnswers = await db + .select({ + questionId: answers.questionId, + }) + .from(answers) + .where( + and(eq(answers.assessmentId, assessmentId), sql`${answers.optionId} IS NOT NULL`) + ); + + const existingAnswerIds = new Set(existingAnswers.map((answer) => answer.questionId)); + + // Create a list of new answers only for questions that have options and do not have existing answers + const answerRecords = questionsWithOptions + .filter((q) => !existingAnswerIds.has(q.questionId)) + .map((q) => ({ + id: createId(), + questionId: q.questionId, + optionId: null, + validationInformation: "", + assessmentId, + })); + + // Insert new answers with null values + if (answerRecords.length > 0) { + await db.insert(answers).values(answerRecords); + } + return c.json({ - message: "Assessment status updated successfully", - data: updatedAssessment, + message: "Assessment status updated started successfully", + data: { + updatedAssessment, + answerRecords,} }); } - ); - + ) + + export default assessmentRequestRoute; \ No newline at end of file