From a47c0761d22cf9173511113ce0574ec45df14ca9 Mon Sep 17 00:00:00 2001 From: Stuart Long Chay Boon Date: Thu, 13 Oct 2022 00:23:03 +0800 Subject: [PATCH] [offers][chore] add user to replies and return error if profile not found --- .../migration.sql | 12 +++++++ apps/portal/prisma/schema.prisma | 5 ++- .../src/pages/offers/test/createProfile.tsx | 31 +++++++++---------- .../src/pages/offers/test/listOffers.tsx | 2 +- .../router/offers/offers-profile-router.ts | 15 +++++++-- 5 files changed, 44 insertions(+), 21 deletions(-) create mode 100644 apps/portal/prisma/migrations/20221012161857_add_user_to_replies/migration.sql diff --git a/apps/portal/prisma/migrations/20221012161857_add_user_to_replies/migration.sql b/apps/portal/prisma/migrations/20221012161857_add_user_to_replies/migration.sql new file mode 100644 index 00000000..bc51e8e1 --- /dev/null +++ b/apps/portal/prisma/migrations/20221012161857_add_user_to_replies/migration.sql @@ -0,0 +1,12 @@ +/* + Warnings: + + - You are about to drop the column `creator` on the `OffersReply` table. All the data in the column will be lost. + +*/ +-- AlterTable +ALTER TABLE "OffersReply" DROP COLUMN "creator", +ADD COLUMN "userId" TEXT; + +-- AddForeignKey +ALTER TABLE "OffersReply" ADD CONSTRAINT "OffersReply_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE; diff --git a/apps/portal/prisma/schema.prisma b/apps/portal/prisma/schema.prisma index 1fba7c6a..27a1d7c5 100644 --- a/apps/portal/prisma/schema.prisma +++ b/apps/portal/prisma/schema.prisma @@ -59,6 +59,7 @@ model User { questionsAnswerComments QuestionsAnswerComment[] questionsAnswerCommentVotes QuestionsAnswerCommentVote[] OffersProfile OffersProfile[] + offersDiscussion OffersReply[] } enum Vote { @@ -280,7 +281,6 @@ model OffersEducation { model OffersReply { id String @id @default(cuid()) - creator String createdAt DateTime @default(now()) message String @@ -290,6 +290,9 @@ model OffersReply { profile OffersProfile @relation(fields: [profileId], references: [id], onDelete: Cascade) profileId String + + user User? @relation(fields: [userId], references: [id]) + userId String? } model OffersOffer { diff --git a/apps/portal/src/pages/offers/test/createProfile.tsx b/apps/portal/src/pages/offers/test/createProfile.tsx index 07a20f7f..9c21d52c 100644 --- a/apps/portal/src/pages/offers/test/createProfile.tsx +++ b/apps/portal/src/pages/offers/test/createProfile.tsx @@ -14,10 +14,11 @@ function Test() { // ]); const [createdData, setCreatedData] = useState(''); + const [error, setError] = useState(""); const createMutation = trpc.useMutation(['offers.profile.create'], { - onError(error: any) { - alert(error); + onError(err: any) { + alert(err); }, onSuccess(data) { setCreatedData(JSON.stringify(data)); @@ -38,7 +39,7 @@ function Test() { ], experiences: [ { - companyId: 'cl93patjt0004txew88wkcqpu', + companyId: 'cl95u79f000007im531ysjg79', durationInMonths: 24, jobType: 'FULLTIME', level: 'Junior', @@ -66,7 +67,7 @@ function Test() { offers: [ { // Comments: '', - companyId: 'cl93patjt0004txew88wkcqpu', + companyId: 'cl95u79f000007im531ysjg79', job: { base: { currency: 'SGD', @@ -95,7 +96,7 @@ function Test() { }, { comments: undefined, - companyId: 'cl93patjt0004txew88wkcqpu', + companyId: 'cl95u79f000007im531ysjg79', job: { base: { currency: 'SGD', @@ -126,30 +127,29 @@ function Test() { }); }; - const profileId = 'cl93vnvs1007aw35m9ppn7dsy'; // Remember to change this filed after testing deleting + const profileId = 'cl95u9ju500eo7ipd54kurv8d'; // Remember to change this filed after testing deleting const data = trpc.useQuery([ `offers.profile.listOne`, { profileId, - token: 'e5e1951c9c301ab59bf4cad641e2e7b56c3db58aa852cee755b6ff8dd316cda6', + token: 'dccc05133962013ba8b4ee396e51baee1b53a66051732b48fd015129ae1d593e', }, - ]); + ], { + onError(err) { + setError(err.shape?.message || "") + } + }); const deleteMutation = trpc.useMutation(['offers.profile.delete']); const handleDelete = (id: string) => { deleteMutation.mutate({ profileId: id, - token: 'e5e1951c9c301ab59bf4cad641e2e7b56c3db58aa852cee755b6ff8dd316cda6', + token: 'dccc05133962013ba8b4ee396e51baee1b53a66051732b48fd015129ae1d593e', }); }; return ( - // <>
{createdData}
{JSON.stringify(data.data)}
- - {/* */} +
{JSON.stringify(error)}
); } diff --git a/apps/portal/src/pages/offers/test/listOffers.tsx b/apps/portal/src/pages/offers/test/listOffers.tsx index d7e5e8c4..7baaac90 100644 --- a/apps/portal/src/pages/offers/test/listOffers.tsx +++ b/apps/portal/src/pages/offers/test/listOffers.tsx @@ -6,7 +6,7 @@ function Test() { const data = trpc.useQuery([ 'offers.list', { - companyId: 'cl93patjt0004txew88wkcqpu', + companyId: 'cl95u79f000007im531ysjg79', limit: 20, location: 'Singapore, Singapore', offset: 0, diff --git a/apps/portal/src/server/router/offers/offers-profile-router.ts b/apps/portal/src/server/router/offers/offers-profile-router.ts index 2017a639..1876010a 100644 --- a/apps/portal/src/server/router/offers/offers-profile-router.ts +++ b/apps/portal/src/server/router/offers/offers-profile-router.ts @@ -1,6 +1,7 @@ import crypto, { randomUUID } from 'crypto'; import { z } from 'zod'; import { Prisma } from '@prisma/client'; +import * as trpc from '@trpc/server'; import { createRouter } from '../context'; @@ -128,9 +129,14 @@ export const offersProfileRouter = createRouter() }, }); - return result - ? exclude(computeIsEditable(result, input.token), 'editToken') - : result; + if (result) { + return exclude(computeIsEditable(result, input.token), 'editToken') + } + + throw new trpc.TRPCError({ + code: 'NOT_FOUND', + message: 'Profile does not exist', + }); }, }) .mutation('create', { @@ -421,3 +427,6 @@ export const offersProfileRouter = createRouter() // TODO: Throw 401 }, }); + // .mutation('update', { + + // });