fix: revise questions management

This commit is contained in:
Sukma Gladys 2024-08-14 10:22:01 +07:00
parent aba7199df7
commit f9d7e0398d

View File

@ -11,6 +11,7 @@ import authInfo from "../../middlewares/authInfo";
import checkPermission from "../../middlewares/checkPermission";
import { aspects } from "../../drizzle/schema/aspects";
import { subAspects } from "../../drizzle/schema/subAspects";
import { notFound } from "../../errors/DashboardError";
export const questionFormSchema = z.object({
subAspectId: z.string().min(1).max(255),
@ -19,9 +20,9 @@ export const questionFormSchema = z.object({
});
export const questionUpdateSchema = questionFormSchema.extend({
question: z.string().min(1).max(255).optional().or(z.literal("")),
subAspectiD: z.string().min(1).max(255).optional().or(z.literal("")),
needFile: z.boolean().default(false).optional().or(z.boolean()),
question: z.string().min(1).max(255).or(z.literal("")),
subAspectId: z.string().min(1).max(255).or(z.literal("")),
needFile: z.boolean().default(false).or(z.boolean()),
});
const questionsRoute = new Hono<HonoEnv>()
@ -30,7 +31,7 @@ const questionsRoute = new Hono<HonoEnv>()
* Get All Questions (With Metadata)
*
* Query params:
* - includeTrashed: boolean (default: false)\
* - includeTrashed: boolean (default: false)
* - withMetadata: boolean
*/
.get(
@ -79,10 +80,6 @@ const questionsRoute = new Hono<HonoEnv>()
includeTrashed ? undefined : isNull(questions.deletedAt),
q
? or(
ilike(subAspects.aspectId, q),
ilike(questions.subAspectId, q),
ilike(questions.question, q),
ilike(questions.needFile, q),
ilike(questions.createdAt, q),
ilike(questions.updatedAt, q),
ilike(questions.deletedAt, q),
@ -107,7 +104,13 @@ const questionsRoute = new Hono<HonoEnv>()
});
}
)
// get user by id
/**
* Get Question by ID
*
* Query params:
* - id: string
* - includeTrashed: boolean (default: false)
*/
.get(
"/:id",
checkPermission("questions.readAll"),
@ -149,14 +152,19 @@ const questionsRoute = new Hono<HonoEnv>()
message: "The question does not exists",
});
const userData = {
const questionData = {
...queryResult[0],
};
return c.json(userData);
return c.json(questionData);
}
)
// create question
/**
* Create Question
*
* JSON:
* - questionFormSchema: object
*/
.post(
"/",
checkPermission("questions.create"),
@ -182,8 +190,12 @@ const questionsRoute = new Hono<HonoEnv>()
);
}
)
//update question
/**
* Update Question
*
* JSON:
* - questionUpdateSchema: object
*/
.patch(
"/:id",
checkPermission("questions.update"),
@ -197,7 +209,7 @@ const questionsRoute = new Hono<HonoEnv>()
.from(questions)
.where(and(eq(questions.id, questionId), isNull(questions.deletedAt)));
if (!question[0]) return c.notFound();
if (!question[0]) throw notFound();
await db
.update(questions)
@ -212,8 +224,13 @@ const questionsRoute = new Hono<HonoEnv>()
});
}
)
//delete user
/**
* Delete Question
*
* Query params:
* - id: string
* - skipTrash: string (default: false)
*/
.delete(
"/:id",
checkPermission("questions.delete"),
@ -259,8 +276,12 @@ const questionsRoute = new Hono<HonoEnv>()
});
}
)
// undo delete
/**
* Restore Question
*
* Query params:
* - id: string
*/
.patch("/restore/:id",
checkPermission("questions.restore"),
async (c) => {