From 55beb386e84ac8aed0e4fc83935b38bf2cb7fcad Mon Sep 17 00:00:00 2001 From: Stuart Long Chay Boon Date: Thu, 17 Nov 2022 17:52:34 +0800 Subject: [PATCH] [offers][feat] add admin table in db --- .../migration.sql | 13 +++++++++++++ apps/portal/prisma/schema.prisma | 7 +++++++ .../server/router/offers/offers-admin-router.ts | 15 +++++++++++++-- 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 apps/portal/prisma/migrations/20221117094655_add_offersadmin_table/migration.sql diff --git a/apps/portal/prisma/migrations/20221117094655_add_offersadmin_table/migration.sql b/apps/portal/prisma/migrations/20221117094655_add_offersadmin_table/migration.sql new file mode 100644 index 00000000..3d3bd7bd --- /dev/null +++ b/apps/portal/prisma/migrations/20221117094655_add_offersadmin_table/migration.sql @@ -0,0 +1,13 @@ +-- CreateTable +CREATE TABLE "OffersAdmin" ( + "id" TEXT NOT NULL, + "userId" TEXT NOT NULL, + + CONSTRAINT "OffersAdmin_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "OffersAdmin_userId_key" ON "OffersAdmin"("userId"); + +-- AddForeignKey +ALTER TABLE "OffersAdmin" ADD CONSTRAINT "OffersAdmin_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/apps/portal/prisma/schema.prisma b/apps/portal/prisma/schema.prisma index 31f846d9..c73aab8a 100644 --- a/apps/portal/prisma/schema.prisma +++ b/apps/portal/prisma/schema.prisma @@ -62,6 +62,7 @@ model User { OffersProfile OffersProfile[] offersDiscussion OffersReply[] questionsLists QuestionsList[] + OffersAdmin OffersAdmin? } enum Vote { @@ -241,6 +242,12 @@ model OffersProfile { analysis OffersAnalysis? } +model OffersAdmin { + id String @id @default(cuid()) + userId String @unique + user User @relation(fields: [userId], references: [id], onDelete: Cascade) +} + model OffersBackground { id String @id @default(cuid()) diff --git a/apps/portal/src/server/router/offers/offers-admin-router.ts b/apps/portal/src/server/router/offers/offers-admin-router.ts index 4403b959..cd923d07 100644 --- a/apps/portal/src/server/router/offers/offers-admin-router.ts +++ b/apps/portal/src/server/router/offers/offers-admin-router.ts @@ -10,7 +10,7 @@ import { Currency } from '~/utils/offers/currency/CurrencyEnum'; import { convertWithDate } from '~/utils/offers/currency/currencyExchange'; import { createValidationRegex } from '~/utils/offers/zodRegex'; -import { createRouter } from '../context'; +import { createProtectedRouter } from '../context'; const getOrder = (prefix: string) => { return prefix === '+' ? 'asc' : 'desc'; @@ -43,7 +43,7 @@ const getYoeRange = (yoeCategory: number | null | undefined) => { : null; // Internship }; -export const offerAdminRouter = createRouter().query('list', { +export const offerAdminRouter = createProtectedRouter().query('list', { input: z.object({ companyId: z.string().nullish(), countryId: z.string().nullish(), @@ -463,4 +463,15 @@ export const offerAdminRouter = createRouter().query('list', { !yoeRange ? JobType.INTERN : JobType.FULLTIME, ); }, +}).query('isAdmin', { + async resolve({ ctx }) { + const userId = ctx.session.user.id; + const result = await ctx.prisma.offersAdmin.findFirst({ + where: { + userId + } + }) + + return result ? true : false + } });