Update : Post on AssessmentRequest and typo on respondents schema
This commit is contained in:
parent
f7ea4ed632
commit
4453cc49d7
|
|
@ -15,7 +15,7 @@ export const respondents = pgTable("respondents", {
|
||||||
phoneNumber: varchar("phoneNumber", { length: 13 }).notNull(),
|
phoneNumber: varchar("phoneNumber", { length: 13 }).notNull(),
|
||||||
createdAt: timestamp("createdAt", { mode: "date" }).defaultNow(),
|
createdAt: timestamp("createdAt", { mode: "date" }).defaultNow(),
|
||||||
updatedAt: timestamp("updatedAt", { 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 }) => ({
|
export const respondentsRelations = relations(respondents, ({ one }) => ({
|
||||||
|
|
|
||||||
|
|
@ -98,7 +98,6 @@ const assessmentRequestRoute = new Hono<HonoEnv>()
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
// Post assessment request by user ID
|
// Post assessment request by user ID
|
||||||
.post(
|
.post(
|
||||||
"/",
|
"/",
|
||||||
|
|
@ -106,7 +105,7 @@ const assessmentRequestRoute = new Hono<HonoEnv>()
|
||||||
requestValidator(
|
requestValidator(
|
||||||
"json",
|
"json",
|
||||||
z.object({
|
z.object({
|
||||||
respondentId: z.string().min(1),
|
respondentId: z.string().min(1), // Memastikan respondentId minimal ada
|
||||||
})
|
})
|
||||||
),
|
),
|
||||||
async (c) => {
|
async (c) => {
|
||||||
|
|
@ -114,35 +113,35 @@ const assessmentRequestRoute = new Hono<HonoEnv>()
|
||||||
const currentUser = c.get("currentUser");
|
const currentUser = c.get("currentUser");
|
||||||
const userId = currentUser?.id; // Mengambil userId dari currentUser yang disimpan di context
|
const userId = currentUser?.id; // Mengambil userId dari currentUser yang disimpan di context
|
||||||
|
|
||||||
// Make sure the userId exists
|
// Memastikan user sudah terautentikasi
|
||||||
if (!userId) {
|
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
|
const respondent = await db
|
||||||
.select()
|
.select()
|
||||||
.from(respondents)
|
.from(respondents)
|
||||||
.where(eq(respondents.id, respondentId));
|
.where(and(eq(respondents.id, respondentId), eq(respondents.userId, userId)));
|
||||||
|
|
||||||
if (!respondent.length) {
|
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
|
const newAssessment = await db
|
||||||
.insert(assessments)
|
.insert(assessments)
|
||||||
.values({
|
.values({
|
||||||
id: createId(),
|
id: createId(),
|
||||||
respondentId,
|
respondentId,
|
||||||
status: "menunggu konfirmasi",
|
status: "menunggu konfirmasi", // Status awal permohonan
|
||||||
validatedBy: null,
|
validatedBy: null,
|
||||||
validatedAt: null,
|
validatedAt: null,
|
||||||
createdAt: new Date(),
|
createdAt: new Date(),
|
||||||
})
|
})
|
||||||
.returning();
|
.returning();
|
||||||
|
|
||||||
return c.json({ message: "Successfully submitted the assessment request" }, 201);
|
return c.json({ message: "Successfully submitted the assessment request", data: newAssessment }, 201);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user