From ee85a8e38f70ccfa5f5ccae680813460b741c44f Mon Sep 17 00:00:00 2001 From: Jeff Sieu Date: Wed, 19 Oct 2022 18:02:52 +0800 Subject: [PATCH] [questions][feat] update frontend to use companyId --- .../questions/ContributeQuestionForm.tsx | 5 +- apps/portal/src/pages/questions/index.tsx | 4 +- .../router/questions-question-router.ts | 110 ++++++++++-------- 3 files changed, 63 insertions(+), 56 deletions(-) diff --git a/apps/portal/src/components/questions/ContributeQuestionForm.tsx b/apps/portal/src/components/questions/ContributeQuestionForm.tsx index 5e230d73..a0e4d0b6 100644 --- a/apps/portal/src/components/questions/ContributeQuestionForm.tsx +++ b/apps/portal/src/components/questions/ContributeQuestionForm.tsx @@ -84,9 +84,8 @@ export default function ContributeQuestionForm({ name="company" render={({ field }) => ( { - // TODO: To change from using company name to company id (i.e., value) - field.onChange(label); + onSelect={({ id }) => { + field.onChange(id); }} /> )} diff --git a/apps/portal/src/pages/questions/index.tsx b/apps/portal/src/pages/questions/index.tsx index 93ac9725..69caac9f 100644 --- a/apps/portal/src/pages/questions/index.tsx +++ b/apps/portal/src/pages/questions/index.tsx @@ -65,7 +65,7 @@ export default function QuestionsHomePage() { [ 'questions.questions.getQuestionsByFilter', { - companies: selectedCompanies, + companyNames: selectedCompanies, endDate: today, locations: selectedLocations, questionTypes: selectedQuestionTypes, @@ -252,7 +252,7 @@ export default function QuestionsHomePage() { { createQuestion({ - company: data.company, + companyId: data.company, content: data.questionContent, location: data.location, questionType: data.questionType, diff --git a/apps/portal/src/server/router/questions-question-router.ts b/apps/portal/src/server/router/questions-question-router.ts index 1315a451..75fb7d17 100644 --- a/apps/portal/src/server/router/questions-question-router.ts +++ b/apps/portal/src/server/router/questions-question-router.ts @@ -9,7 +9,7 @@ import type { Question } from '~/types/questions'; export const questionsQuestionRouter = createProtectedRouter() .query('getQuestionsByFilter', { input: z.object({ - companyIds: z.string().array(), + companyNames: z.string().array(), endDate: z.date(), locations: z.string().array(), questionTypes: z.nativeEnum(QuestionsQuestionType).array(), @@ -51,68 +51,69 @@ export const questionsQuestionRouter = createProtectedRouter() }, } : {}), - encounters : { + encounters: { some: { - ...(input.companies.length > 0 + ...(input.companyNames.length > 0 ? { - company : { - in : input.companies - } - } + company: { + name: { + in: input.companyNames, + }, + }, + } : {}), ...(input.locations.length > 0 ? { location: { - in: input.locations + in: input.locations, }, - } + } : {}), ...(input.roles.length > 0 ? { - role : { - in: input.roles - } - } + role: { + in: input.roles, + }, + } : {}), - } - } + }, + }, }, }); - return questionsData - .map((data) => { - const votes: number = data.votes.reduce( - (previousValue: number, currentValue) => { - let result: number = previousValue; + return questionsData.map((data) => { + const votes: number = data.votes.reduce( + (previousValue: number, currentValue) => { + let result: number = previousValue; - switch (currentValue.vote) { - case Vote.UPVOTE: - result += 1; - break; - case Vote.DOWNVOTE: - result -= 1; - break; - } - return result; - }, - 0, - ); + switch (currentValue.vote) { + case Vote.UPVOTE: + result += 1; + break; + case Vote.DOWNVOTE: + result -= 1; + break; + } + return result; + }, + 0, + ); - const question: Question = { - company: data.encounters[0].company!.name ?? 'Unknown company', - content: data.content, - id: data.id, - location: data.encounters[0].location ?? 'Unknown location', - numAnswers: data._count.answers, - numComments: data._count.comments, - numVotes: votes, - role: data.encounters[0].role ?? 'Unknown role', - seenAt: data.encounters[0].seenAt, - type: data.questionType, - updatedAt: data.updatedAt, - user: data.user?.name ?? '', - }; - return question; - }); + const question: Question = { + company: data.encounters[0].company!.name ?? 'Unknown company', + content: data.content, + id: data.id, + location: data.encounters[0].location ?? 'Unknown location', + numAnswers: data._count.answers, + numComments: data._count.comments, + numVotes: votes, + role: data.encounters[0].role ?? 'Unknown role', + seenAt: data.encounters[0].seenAt, + type: data.questionType, + updatedAt: data.updatedAt, + user: data.user?.name ?? '', + }; + return question; + }); }, }) .query('getQuestionById', { @@ -205,11 +206,19 @@ export const questionsQuestionRouter = createProtectedRouter() encounters: { create: [ { - company: input.companyId, + company: { + connect: { + id: input.companyId, + }, + }, location: input.location, role: input.role, seenAt: input.seenAt, - userId, + user: { + connect: { + id: userId, + }, + }, }, ], }, @@ -245,7 +254,6 @@ export const questionsQuestionRouter = createProtectedRouter() const { content, questionType } = input; return await ctx.prisma.questionsQuestion.update({ - data: { content, questionType,