From a73ed8e307515c1d6cdcc24cdc94a9c1801361f0 Mon Sep 17 00:00:00 2001 From: Adrian Polak Date: Tue, 20 Dec 2022 09:56:30 +0100 Subject: [PATCH] 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 --- apps/api/.env-example | 4 +- .../api/modules/questions/questions.routes.ts | 44 +++++++++++-------- .../modules/questions/questions.schemas.ts | 26 ++++++----- 3 files changed, 43 insertions(+), 31 deletions(-) diff --git a/apps/api/.env-example b/apps/api/.env-example index a1601448..5199fc16 100644 --- a/apps/api/.env-example +++ b/apps/api/.env-example @@ -1,5 +1,5 @@ -ENV=dev -NODE_ENV=dev +ENV=development +NODE_ENV=development PORT=3002 COOKIE_DOMAIN=devfaq.localhost diff --git a/apps/api/modules/questions/questions.routes.ts b/apps/api/modules/questions/questions.routes.ts index fe4fcbe6..d1ff4f62 100644 --- a/apps/api/modules/questions/questions.routes.ts +++ b/apps/api/modules/questions/questions.routes.ts @@ -81,26 +81,34 @@ const questionsPlugin: FastifyPluginAsync = async (fastify) => { async handler(request, reply) { const { question, level, category } = request.body; - const newQuestion = await fastify.db.question.create({ - data: { - question, - levelId: level, - categoryId: category, - statusId: "pending", - }, - }); + try { + const newQuestion = await fastify.db.question.create({ + data: { + question, + levelId: level, + categoryId: category, + statusId: "pending", + }, + }); - const data = { - id: newQuestion.id, - question: newQuestion.question, - _categoryId: newQuestion.categoryId, - _levelId: newQuestion.levelId, - _statusId: newQuestion.statusId, - acceptedAt: newQuestion.acceptedAt?.toISOString(), - votesCount: 0, - }; + const data = { + id: newQuestion.id, + question: newQuestion.question, + _categoryId: newQuestion.categoryId, + _levelId: newQuestion.levelId, + _statusId: newQuestion.statusId, + acceptedAt: newQuestion.acceptedAt?.toISOString(), + votesCount: 0, + }; - return { data }; + return { data }; + } catch (err) { + if (isPrismaError(err) && err.code === "P2002") { + throw fastify.httpErrors.conflict(`Question with content: ${question} already exists!`); + } + + throw err; + } }, }); diff --git a/apps/api/modules/questions/questions.schemas.ts b/apps/api/modules/questions/questions.schemas.ts index d0d4e97c..16775046 100644 --- a/apps/api/modules/questions/questions.schemas.ts +++ b/apps/api/modules/questions/questions.schemas.ts @@ -15,17 +15,21 @@ const generateGetQuestionsQuerySchema = < levels: Levels; statuses: Statuses; }) => - Type.Object({ - category: Type.Optional(Type.Union(args.categories.map((val) => Type.Literal(val)))), - status: Type.Optional(Type.Union(args.statuses.map((val) => Type.Literal(val)))), - level: Type.Optional(Type.String({ pattern: `^([${args.levels.join("|")}],?)+$` })), - limit: Type.Optional(Type.Integer()), - offset: Type.Optional(Type.Integer()), - orderBy: Type.Optional( - Type.Union([Type.Literal("acceptedAt"), Type.Literal("level"), Type.Literal("votesCount")]), - ), - order: Type.Optional(Type.Union([Type.Literal("asc"), Type.Literal("desc")])), - }); + Type.Partial( + Type.Object({ + category: Type.Union(args.categories.map((val) => Type.Literal(val))), + status: Type.Union(args.statuses.map((val) => Type.Literal(val))), + level: Type.String({ pattern: `^([${args.levels.join("|")}],?)+$` }), + limit: Type.Integer(), + offset: Type.Integer(), + orderBy: Type.Union([ + Type.Literal("acceptedAt"), + Type.Literal("level"), + Type.Literal("votesCount"), + ]), + order: Type.Union([Type.Literal("asc"), Type.Literal("desc")]), + }), + ); export type GetQuestionsQuery = Static>; export type GetQuestionsOrderBy = GetQuestionsQuery["orderBy"]; export type GetQuestionsOrder = GetQuestionsQuery["order"];