Update : Post on AssessmentRequest and typo on respondents schema

This commit is contained in:
percyfikri 2024-09-27 16:27:20 +07:00
parent f7ea4ed632
commit 4453cc49d7
2 changed files with 11 additions and 12 deletions

View File

@ -15,7 +15,7 @@ export const respondents = pgTable("respondents", {
phoneNumber: varchar("phoneNumber", { length: 13 }).notNull(),
createdAt: timestamp("createdAt", { mode: "date" }).defaultNow(),
updatedAt: timestamp("updatedAt", { mode: "date" }).defaultNow(),
deletedAt: timestamp("deletetAt", { mode: "date" }),
deletedAt: timestamp("deletedAt", { mode: "date" }),
});
export const respondentsRelations = relations(respondents, ({ one }) => ({

View File

@ -97,7 +97,6 @@ const assessmentRequestRoute = new Hono<HonoEnv>()
});
}
)
// Post assessment request by user ID
.post(
@ -106,7 +105,7 @@ const assessmentRequestRoute = new Hono<HonoEnv>()
requestValidator(
"json",
z.object({
respondentId: z.string().min(1),
respondentId: z.string().min(1), // Memastikan respondentId minimal ada
})
),
async (c) => {
@ -114,36 +113,36 @@ const assessmentRequestRoute = new Hono<HonoEnv>()
const currentUser = c.get("currentUser");
const userId = currentUser?.id; // Mengambil userId dari currentUser yang disimpan di context
// Make sure the userId exists
// Memastikan user sudah terautentikasi
if (!userId) {
throw new HTTPException(400, { message: "User not authenticated" });
return c.text("User not authenticated", 401);
}
// Validate if respondent exists
// Validasi apakah respondent dengan respondentId tersebut ada
const respondent = await db
.select()
.from(respondents)
.where(eq(respondents.id, respondentId));
.where(and(eq(respondents.id, respondentId), eq(respondents.userId, userId)));
if (!respondent.length) {
throw new HTTPException(404, { message: "Respondent not found." });
throw new HTTPException(404, { message: "Respondent not found or unauthorized." });
}
// Create the assessment request
// Membuat permohonan asesmen baru
const newAssessment = await db
.insert(assessments)
.values({
id: createId(),
respondentId,
status: "menunggu konfirmasi",
status: "menunggu konfirmasi", // Status awal permohonan
validatedBy: null,
validatedAt: null,
createdAt: new Date(),
})
.returning();
return c.json({ message: "Successfully submitted the assessment request" }, 201);
return c.json({ message: "Successfully submitted the assessment request", data: newAssessment }, 201);
}
);
export default assessmentRequestRoute;