From 390c5a0353391d4fa01c76c4a2ffb993b5990ba5 Mon Sep 17 00:00:00 2001
From: Allen Shibu <93600615+alllenshibu@users.noreply.github.com>
Date: Wed, 7 Feb 2024 19:13:28 +0530
Subject: [PATCH] fix: Fix issues with csv upload participants insertion (#284)
---
.../src/controllers/participants.ts | 59 +++++++++++++------
.../events/[eventId]/participants/index.jsx | 42 ++++++-------
.../participants/new/upload-csv/index.jsx | 38 ++++++++++--
3 files changed, 97 insertions(+), 42 deletions(-)
diff --git a/apps/core-admin/src/controllers/participants.ts b/apps/core-admin/src/controllers/participants.ts
index 9ef2d87a..a4d84439 100644
--- a/apps/core-admin/src/controllers/participants.ts
+++ b/apps/core-admin/src/controllers/participants.ts
@@ -5,22 +5,40 @@ import prisma from '../utils/database';
export const addNewParticipant = async (req: Request, res: Response) => {
try {
const { orgId, eventId } = req?.params;
- const { firstName, lastName } = req?.body;
+ const { isBulk } = req?.query;
+
+ if (isBulk === 'true') {
+ const { participants } = req?.body;
+
+ const newParticipants = await prisma.participant.createMany({
+ data: participants.map((p: any) => {
+ return {
+ firstName: p.firstName,
+ lastName: p.lastName,
+ organizationId: orgId,
+ eventId,
+ };
+ }),
+ });
+ return res.status(200).json({ newParticipants });
+ } else {
+ const { firstName, lastName } = req?.body;
+
+ const newParticipant = await prisma.participant.create({
+ data: {
+ firstName,
+ lastName,
+ organizationId: orgId,
+ eventId,
+ },
+ });
- const newParticipant = await prisma.participant.create({
- data: {
- firstName,
- lastName,
- organizationId: orgId,
- eventId,
- },
- });
+ if (!newParticipant) {
+ return res.status(500).json({ error: 'Something went wrong' });
+ }
- if (!newParticipant) {
- return res.status(500).json({ error: 'Something went wrong' });
+ return res.status(200).json({ newParticipant });
}
-
- return res.status(200).json({ newParticipant });
} catch (err: any) {
console.error(err);
return res.status(500).json({ error: 'Something went wrong' });
@@ -48,7 +66,8 @@ export const addNewParticipantInBulk = async (req: Request, res: Response) => {
return res.status(500).json({ error: 'Something went wrong' });
}
};
-// export const addNewParticipantInBulk = async (req: Request, res: Response) => {
+
+// export const addParticipantsInBulk = async (req: Request, res: Response) => {
// try {
// const { orgId, eventId } = req?.params;
// const { participants } = req?.body;
@@ -70,20 +89,26 @@ export const addNewParticipantInBulk = async (req: Request, res: Response) => {
// });
// newParticipants.push(newParticipant);
// } catch (error) {
-// console.error(`Failed to add participant: ${participant.firstName} ${participant.lastName}`);
+// console.error(
+// `Failed to add participant: ${participant.firstName} ${participant.lastName}`,
+// );
// failedParticipants.push(participant);
// }
// }
// if (failedParticipants.length > 0) {
-// return res.status(201).json({ message: 'Some participants were not added', success: newParticipants, failed: failedParticipants });
+// return res.status(201).json({
+// message: 'Some participants were not added',
+// success: newParticipants,
+// failed: failedParticipants,
+// });
// }
// return res.status(200).json({ newParticipants });
// } catch (err: any) {
// console.error(err);
// return res.status(500).json({ error: 'Something went wrong' });
// }
-// }
+// };
export const getAllParticipants = async (req: Request, res: Response) => {
try {
diff --git a/apps/web-admin/src/pages/organizations/[orgId]/events/[eventId]/participants/index.jsx b/apps/web-admin/src/pages/organizations/[orgId]/events/[eventId]/participants/index.jsx
index 0ede97d8..d2d17b10 100644
--- a/apps/web-admin/src/pages/organizations/[orgId]/events/[eventId]/participants/index.jsx
+++ b/apps/web-admin/src/pages/organizations/[orgId]/events/[eventId]/participants/index.jsx
@@ -62,26 +62,28 @@ export default function Events() {
Participants
-
-
+
+
+
+
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 0b085739..b695f063 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
@@ -2,7 +2,10 @@ import { useState } from 'react';
import Papa from 'papaparse';
import { DataGrid, GridToolbar } from '@mui/x-data-grid';
-import { Flex } from '@chakra-ui/react';
+import { useFetch } from '@/hooks/useFetch';
+
+import { useRouter } from 'next/router';
+import { Flex, Button } from '@chakra-ui/react';
import DashboardLayout from '@/layouts/DashboardLayout';
import { ThemeProvider, createTheme } from '@mui/material';
@@ -10,6 +13,11 @@ import { ThemeProvider, createTheme } from '@mui/material';
const MuiTheme = createTheme({});
export default function NewOrganization() {
+ const { loading, post } = useFetch();
+ const router = useRouter();
+
+ const { orgId, eventId } = router.query;
+
const [csvData, setCSVData] = useState(null);
const handleFileUpload = (event) => {
@@ -19,9 +27,12 @@ export default function NewOrganization() {
header: true,
dynamicTyping: true,
complete: (result) => {
- const dataWithId = result.data.map((row, index) => ({ ...row, id: index + 1 }));
+ const filteredData = result.data.filter((row) => {
+ return Object.values(row).every((value) => value !== null && value !== undefined);
+ });
+
+ const dataWithId = filteredData.map((row, index) => ({ ...row, id: index + 1 }));
setCSVData(dataWithId);
- console.log('CSV Data as JSON:', dataWithId);
},
error: (error) => {
console.error('Error parsing CSV:', error);
@@ -29,9 +40,25 @@ export default function NewOrganization() {
});
};
+ const handleSubmit = async (e) => {
+ e.preventDefault();
+ const { data, status } = await post(
+ `/core/organizations/${orgId}/events/${eventId}/participants?isBulk=true`,
+ {},
+ {
+ participants: csvData,
+ },
+ );
+ if (status === 200) {
+ router.push(`/organizations/${orgId}/events/${eventId}/participants`);
+ } else {
+ alert(data.error);
+ }
+ };
+
const columns = [
- { field: 'FirstName', headerName: 'First Name', width: 150 },
- { field: 'LastName', headerName: 'Last Name', width: 150 },
+ { field: 'firstName', headerName: 'First Name', width: 150 },
+ { field: 'lastName', headerName: 'Last Name', width: 150 },
];
return (
@@ -67,6 +94,7 @@ export default function NewOrganization() {
)}
+
);