Skip to content

Commit a73ed8e

Browse files
authored
fix(api): fix Internal Server Error when question already exists in database (#443)
* feat(api): add filtering questions by content * fix(app): fix Internal Server Error when question already exists in database * refactor(api): remove filtering questions by content
1 parent 6dfe163 commit a73ed8e

File tree

3 files changed

+43
-31
lines changed

3 files changed

+43
-31
lines changed

apps/api/.env-example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
ENV=dev
2-
NODE_ENV=dev
1+
ENV=development
2+
NODE_ENV=development
33
PORT=3002
44

55
COOKIE_DOMAIN=devfaq.localhost

apps/api/modules/questions/questions.routes.ts

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -81,26 +81,34 @@ const questionsPlugin: FastifyPluginAsync = async (fastify) => {
8181
async handler(request, reply) {
8282
const { question, level, category } = request.body;
8383

84-
const newQuestion = await fastify.db.question.create({
85-
data: {
86-
question,
87-
levelId: level,
88-
categoryId: category,
89-
statusId: "pending",
90-
},
91-
});
84+
try {
85+
const newQuestion = await fastify.db.question.create({
86+
data: {
87+
question,
88+
levelId: level,
89+
categoryId: category,
90+
statusId: "pending",
91+
},
92+
});
9293

93-
const data = {
94-
id: newQuestion.id,
95-
question: newQuestion.question,
96-
_categoryId: newQuestion.categoryId,
97-
_levelId: newQuestion.levelId,
98-
_statusId: newQuestion.statusId,
99-
acceptedAt: newQuestion.acceptedAt?.toISOString(),
100-
votesCount: 0,
101-
};
94+
const data = {
95+
id: newQuestion.id,
96+
question: newQuestion.question,
97+
_categoryId: newQuestion.categoryId,
98+
_levelId: newQuestion.levelId,
99+
_statusId: newQuestion.statusId,
100+
acceptedAt: newQuestion.acceptedAt?.toISOString(),
101+
votesCount: 0,
102+
};
102103

103-
return { data };
104+
return { data };
105+
} catch (err) {
106+
if (isPrismaError(err) && err.code === "P2002") {
107+
throw fastify.httpErrors.conflict(`Question with content: ${question} already exists!`);
108+
}
109+
110+
throw err;
111+
}
104112
},
105113
});
106114

apps/api/modules/questions/questions.schemas.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,21 @@ const generateGetQuestionsQuerySchema = <
1515
levels: Levels;
1616
statuses: Statuses;
1717
}) =>
18-
Type.Object({
19-
category: Type.Optional(Type.Union(args.categories.map((val) => Type.Literal(val)))),
20-
status: Type.Optional(Type.Union(args.statuses.map((val) => Type.Literal(val)))),
21-
level: Type.Optional(Type.String({ pattern: `^([${args.levels.join("|")}],?)+$` })),
22-
limit: Type.Optional(Type.Integer()),
23-
offset: Type.Optional(Type.Integer()),
24-
orderBy: Type.Optional(
25-
Type.Union([Type.Literal("acceptedAt"), Type.Literal("level"), Type.Literal("votesCount")]),
26-
),
27-
order: Type.Optional(Type.Union([Type.Literal("asc"), Type.Literal("desc")])),
28-
});
18+
Type.Partial(
19+
Type.Object({
20+
category: Type.Union(args.categories.map((val) => Type.Literal(val))),
21+
status: Type.Union(args.statuses.map((val) => Type.Literal(val))),
22+
level: Type.String({ pattern: `^([${args.levels.join("|")}],?)+$` }),
23+
limit: Type.Integer(),
24+
offset: Type.Integer(),
25+
orderBy: Type.Union([
26+
Type.Literal("acceptedAt"),
27+
Type.Literal("level"),
28+
Type.Literal("votesCount"),
29+
]),
30+
order: Type.Union([Type.Literal("asc"), Type.Literal("desc")]),
31+
}),
32+
);
2933
export type GetQuestionsQuery = Static<ReturnType<typeof generateGetQuestionsQuerySchema>>;
3034
export type GetQuestionsOrderBy = GetQuestionsQuery["orderBy"];
3135
export type GetQuestionsOrder = GetQuestionsQuery["order"];

0 commit comments

Comments
 (0)