diff --git a/apps/portal/prisma/migrations/20221010055218_update_question_encounter_to_use_company_id/migration.sql b/apps/portal/prisma/migrations/20221010055218_update_question_encounter_to_use_company_id/migration.sql new file mode 100644 index 00000000..c79e0b9c --- /dev/null +++ b/apps/portal/prisma/migrations/20221010055218_update_question_encounter_to_use_company_id/migration.sql @@ -0,0 +1,13 @@ +/* + Warnings: + + - You are about to drop the column `company` on the `QuestionsQuestionEncounter` table. All the data in the column will be lost. + - Added the required column `companyId` to the `QuestionsQuestionEncounter` table without a default value. This is not possible if the table is not empty. + +*/ +-- AlterTable +ALTER TABLE "QuestionsQuestionEncounter" DROP COLUMN "company", +ADD COLUMN "companyId" TEXT NOT NULL; + +-- AddForeignKey +ALTER TABLE "QuestionsQuestionEncounter" ADD CONSTRAINT "QuestionsQuestionEncounter_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "Company"("id") ON DELETE SET NULL ON UPDATE CASCADE; diff --git a/apps/portal/prisma/schema.prisma b/apps/portal/prisma/schema.prisma index 4085e88b..45a730a3 100644 --- a/apps/portal/prisma/schema.prisma +++ b/apps/portal/prisma/schema.prisma @@ -91,15 +91,17 @@ enum TodoStatus { } model Company { - id String @id @default(cuid()) - name String @db.Text - slug String @unique - description String? @db.Text - logoUrl String? - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt - OffersExperience OffersExperience[] - OffersOffer OffersOffer[] + id String @id @default(cuid()) + name String @db.Text + slug String @unique + description String? @db.Text + logoUrl String? + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt + + questionsQuestionEncounter QuestionsQuestionEncounter[] + OffersExperience OffersExperience[] + OffersOffer OffersOffer[] } // Start of Resumes project models. @@ -403,19 +405,20 @@ model QuestionsQuestion { } model QuestionsQuestionEncounter { - id String @id @default(cuid()) - questionId String - userId String? - // TODO: sync with models - company String @db.Text - location String @db.Text - role String @db.Text - seenAt DateTime - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt - - user User? @relation(fields: [userId], references: [id], onDelete: SetNull) - question QuestionsQuestion @relation(fields: [questionId], references: [id], onDelete: Cascade) + id String @id @default(cuid()) + questionId String + userId String? + // TODO: sync with models (location, role) + companyId String + location String @db.Text + role String @db.Text + seenAt DateTime + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt + + company Company? @relation(fields: [companyId], references: [id], onDelete: SetNull) + user User? @relation(fields: [userId], references: [id], onDelete: SetNull) + question QuestionsQuestion @relation(fields: [questionId], references: [id], onDelete: Cascade) } model QuestionsQuestionVote { 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 7fbf59ed..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({ - companies: 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, - 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', { @@ -171,7 +172,7 @@ export const questionsQuestionRouter = createProtectedRouter() ); const question: Question = { - company: questionData.encounters[0].company, + company: questionData.encounters[0].company!.name ?? 'Unknown company', content: questionData.content, id: questionData.id, location: questionData.encounters[0].location ?? 'Unknown location', @@ -189,7 +190,7 @@ export const questionsQuestionRouter = createProtectedRouter() }) .mutation('create', { input: z.object({ - company: z.string(), + companyId: z.string(), content: z.string(), location: z.string(), questionType: z.nativeEnum(QuestionsQuestionType), @@ -199,17 +200,25 @@ export const questionsQuestionRouter = createProtectedRouter() async resolve({ ctx, input }) { const userId = ctx.session?.user?.id; - const question = await ctx.prisma.questionsQuestion.create({ + return await ctx.prisma.questionsQuestion.create({ data: { content: input.content, encounters: { create: [ { - company: input.company, + company: { + connect: { + id: input.companyId, + }, + }, location: input.location, role: input.role, seenAt: input.seenAt, - userId, + user: { + connect: { + id: userId, + }, + }, }, ], }, @@ -217,20 +226,6 @@ export const questionsQuestionRouter = createProtectedRouter() userId, }, }); - - // Create question encounter - await ctx.prisma.questionsQuestionEncounter.create({ - data: { - company: input.company, - location: input.location, - questionId: question.id, - role: input.role, - seenAt: input.seenAt, - userId, - }, - }); - - return question; }, }) .mutation('update', { @@ -259,7 +254,6 @@ export const questionsQuestionRouter = createProtectedRouter() const { content, questionType } = input; return await ctx.prisma.questionsQuestion.update({ - data: { content, questionType, diff --git a/apps/portal/src/types/questions.d.ts b/apps/portal/src/types/questions.d.ts index 41286249..9ee7d6f0 100644 --- a/apps/portal/src/types/questions.d.ts +++ b/apps/portal/src/types/questions.d.ts @@ -1,3 +1,5 @@ +import type { QuestionsQuestionType } from '@prisma/client'; + export type Question = { // TODO: company, location, role maps company: string; @@ -9,7 +11,7 @@ export type Question = { numVotes: number; role: string; seenAt: Date; - type: stringl; + type: QuestionsQuestionType; updatedAt: Date; user: string; };