From c7ccc50085062648cfe591f2fc46c168d14f6de1 Mon Sep 17 00:00:00 2001 From: Allen Shibu <93600615+alllenshibu@users.noreply.github.com> Date: Thu, 29 Feb 2024 17:23:51 +0530 Subject: [PATCH] fix: Unique email --- apps/core-admin/src/controllers/participants.ts | 5 +++++ .../[eventId]/participants/new/upload-csv/index.jsx | 8 ++++---- .../prisma/migrations/20240229114739_/migration.sql | 12 ++++++++++++ packages/database/prisma/schema.prisma | 4 +++- 4 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 packages/database/prisma/migrations/20240229114739_/migration.sql diff --git a/apps/core-admin/src/controllers/participants.ts b/apps/core-admin/src/controllers/participants.ts index f04b0f5f..54ffd2c7 100644 --- a/apps/core-admin/src/controllers/participants.ts +++ b/apps/core-admin/src/controllers/participants.ts @@ -164,6 +164,11 @@ export const addNewParticipant = async (req: Request, res: Response) => { return res.status(200).json({ newParticipant }); } } catch (err: any) { + if (err.code === 'P2002') { + return res + .status(400) + .json({ error: 'An email present in CSV data already exists in the event' }); + } console.error(err); return res.status(500).json({ error: 'Something went wrong' }); } diff --git a/apps/web-admin/src/pages/organizations/[orgId]/events/[eventId]/participants/new/upload-csv/index.jsx b/apps/web-admin/src/pages/organizations/[orgId]/events/[eventId]/participants/new/upload-csv/index.jsx index e1a56c75..ab1b06bb 100644 --- a/apps/web-admin/src/pages/organizations/[orgId]/events/[eventId]/participants/new/upload-csv/index.jsx +++ b/apps/web-admin/src/pages/organizations/[orgId]/events/[eventId]/participants/new/upload-csv/index.jsx @@ -46,7 +46,7 @@ export default function NewParticipantByCSVUpload() { duration: 10000, }); setCSVData(null); - e.target.value = ''; + //e.target.value = ''; return; } @@ -77,7 +77,7 @@ export default function NewParticipantByCSVUpload() { duration: 10000, }); setCSVData(null); - e.target.value = ''; + //e.target.value = ''; } if ( @@ -120,7 +120,7 @@ export default function NewParticipantByCSVUpload() { duration: 10000, }); setCSVData(null); - e.target.value = ''; + //e.target.value = ''; setColumns([ { field: 'firstName', headerName: 'First Name' }, { field: 'lastName', headerName: 'Last Name' }, @@ -148,7 +148,7 @@ export default function NewParticipantByCSVUpload() { duration: 10000, }); setCSVData(null); - e.target.value = ''; + // e.target.value = ''; setColumns([ { field: 'firstName', headerName: 'First Name' }, { field: 'lastName', headerName: 'Last Name' }, diff --git a/packages/database/prisma/migrations/20240229114739_/migration.sql b/packages/database/prisma/migrations/20240229114739_/migration.sql new file mode 100644 index 00000000..c8a3453d --- /dev/null +++ b/packages/database/prisma/migrations/20240229114739_/migration.sql @@ -0,0 +1,12 @@ +/* + Warnings: + + - A unique constraint covering the columns `[email,eventId]` on the table `Participant` will be added. If there are existing duplicate values, this will fail. + - Made the column `email` on table `Participant` required. This step will fail if there are existing NULL values in that column. + +*/ +-- AlterTable +ALTER TABLE "Participant" ALTER COLUMN "email" SET NOT NULL; + +-- CreateIndex +CREATE UNIQUE INDEX "Participant_email_eventId_key" ON "Participant"("email", "eventId"); diff --git a/packages/database/prisma/schema.prisma b/packages/database/prisma/schema.prisma index 8fd4f037..e6ef827d 100644 --- a/packages/database/prisma/schema.prisma +++ b/packages/database/prisma/schema.prisma @@ -71,7 +71,7 @@ model Participant { updatedAt DateTime @updatedAt firstName String lastName String? - email String? + email String phone String? checkInKey String? eventId String @db.Uuid @@ -82,6 +82,8 @@ model Participant { participantAttributes ParticipantAttributes[] participantExtras ParticipantExtras[] participantExtrasCheckIn ParticipantExtrasCheckIn[] + + @@unique([email, eventId]) } model ParticipantCheckIn {