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
2 changed files with 66 additions and 8 deletions
Showing only changes of commit fe827f4fb3 - Show all commits

View File

@ -9,11 +9,14 @@ import {
} from "drizzle-orm/pg-core"; } from "drizzle-orm/pg-core";
import { options } from "./options"; import { options } from "./options";
import { assessments } from "./assessments"; import { assessments } from "./assessments";
import { questions } from "./questions";
export const answers = pgTable("answers", { export const answers = pgTable("answers", {
id: varchar("id", { length: 50 }) id: varchar("id", { length: 50 })
.primaryKey() .primaryKey()
.$defaultFn(() => createId()), .$defaultFn(() => createId()),
questionId: varchar("questionId", { length: 50 })
.references(() => questions.id),
optionId: varchar("optionId", { length: 50 }) optionId: varchar("optionId", { length: 50 })
.references(() => options.id), .references(() => options.id),
assessmentId: varchar("assessmentId", { length: 50 }) assessmentId: varchar("assessmentId", { length: 50 })

View File

@ -12,6 +12,11 @@ import checkPermission from "../../middlewares/checkPermission";
import requestValidator from "../../utils/requestValidator"; import requestValidator from "../../utils/requestValidator";
import { HTTPException } from "hono/http-exception"; import { HTTPException } from "hono/http-exception";
import { createId } from "@paralleldrive/cuid2"; 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>() const assessmentRequestRoute = new Hono<HonoEnv>()
.use(authInfo) .use(authInfo)
@ -138,7 +143,7 @@ const assessmentRequestRoute = new Hono<HonoEnv>()
eq(assessments.respondentId, respondentId), eq(assessments.respondentId, respondentId),
eq(assessments.status, "dalam pengerjaan") eq(assessments.status, "dalam pengerjaan")
) )
);console.log(existingAssessment); );
if (!existingAssessment.length) { if (!existingAssessment.length) {
const newAssessment = await db 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( .patch(
"/:assessmentId", "/:assessmentId",
checkPermission("assessmentRequest.update"), checkPermission("assessmentRequest.update"),
@ -212,12 +218,61 @@ const assessmentRequestRoute = new Hono<HonoEnv>()
.where(eq(assessments.id, assessmentId)) .where(eq(assessments.id, assessmentId))
.returning(); .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({ return c.json({
message: "Assessment status updated successfully", message: "Assessment status updated started successfully",
data: updatedAssessment, data: {
updatedAssessment,
answerRecords,}
}); });
} }
); )
export default assessmentRequestRoute; export default assessmentRequestRoute;