Update: added questionId column and post all options with empty string
This commit is contained in:
parent
ad775c5778
commit
fe827f4fb3
|
|
@ -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 })
|
||||
|
|
|
|||
|
|
@ -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<HonoEnv>()
|
||||
.use(authInfo)
|
||||
|
|
@ -138,7 +143,7 @@ const assessmentRequestRoute = new Hono<HonoEnv>()
|
|||
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<HonoEnv>()
|
|||
}
|
||||
)
|
||||
|
||||
// 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"),
|
||||
|
|
@ -212,12 +218,61 @@ const assessmentRequestRoute = new Hono<HonoEnv>()
|
|||
.where(eq(assessments.id, assessmentId))
|
||||
.returning();
|
||||
|
||||
return c.json({
|
||||
message: "Assessment status updated successfully",
|
||||
data: updatedAssessment,
|
||||
// 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 started successfully",
|
||||
data: {
|
||||
updatedAssessment,
|
||||
answerRecords,}
|
||||
});
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
|
||||
export default assessmentRequestRoute;
|
||||
Loading…
Reference in New Issue
Block a user