Skip to content

Commit

Permalink
fix: A lot of urgent stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
alllenshibu committed Feb 21, 2024
1 parent 188ce5d commit 62be976
Show file tree
Hide file tree
Showing 16 changed files with 420 additions and 254 deletions.
50 changes: 49 additions & 1 deletion apps/core-admin/src/controllers/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const getAttributeById = async (req: Request, res: Response) => {
try {
const { orgId, eventId, attributeId } = req?.params;

const attribute = await prisma.attributes.findMany({
const attribute = await prisma.attributes.findFirst({
where: {
organizationId: orgId,
eventId,
Expand All @@ -54,6 +54,29 @@ export const getAttributeById = async (req: Request, res: Response) => {
}
};

export const editAttribute = async (req: Request, res: Response) => {
try {
const { orgId, eventId, attributeId } = req?.params;
const { name } = req?.body;

if (!name) return res.status(400).json({ error: 'Name is required' });

const updatedAttribute = await prisma.attributes.update({
where: {
id: attributeId,
},
data: {
name,
},
});

return res.status(200).json({ updatedAttribute });
} catch (err: any) {
console.error(err);
return res.status(500).json({ error: 'Something went wrong' });
}
};

export const addNewAttribute = async (req: Request, res: Response) => {
try {
const { orgId, eventId } = req?.params;
Expand All @@ -73,3 +96,28 @@ export const addNewAttribute = async (req: Request, res: Response) => {
return res.status(500).json({ error: 'Something went wrong' });
}
};

export const getAttributeParticipants = async (req: Request, res: Response) => {
try {
const { orgId, eventId, attributeId } = req?.params;

const participants = await prisma.participant.findMany({
where: {
eventId,
organizationId: orgId,
},
include: {
participantAttributes: {
where: {
attributeId,
},
},
},
});

return res.status(200).json({ participants });
} catch (err: any) {
console.error(err);
return res.status(500).json({ error: 'Something went wrong' });
}
};
113 changes: 57 additions & 56 deletions apps/core-admin/src/controllers/participants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export const addNewParticipant = async (req: Request, res: Response) => {
if (isBulk === 'true') {
const { participants } = req?.body;

if (!participants || participants.length === 0) {
return res.status(400).json({ error: 'No participants provided' });
}

const newParticipants = await prisma.participant.createMany({
data: participants.map((p: any) => {
return {
Expand Down Expand Up @@ -45,71 +49,36 @@ export const addNewParticipant = async (req: Request, res: Response) => {
}
};

export const addNewParticipantInBulk = async (req: Request, res: Response) => {
export const editParticipant = async (req: Request, res: Response) => {
try {
const { orgId, eventId } = req?.params;
const { participants } = req?.body;
const { orgId, eventId, participantId } = req?.params;
const { firstName, lastName } = req?.body;

const newParticipants = await prisma.participant.createMany({
data: participants.map((participant: any) => {
return {
firstName: participant.firstName,
lastName: participant.lastName,
organizationId: orgId,
eventId,
};
}),
if (!firstName || !lastName) {
return res.status(400).json({ error: 'First name and last name are required' });
}

const updatedParticipant = await prisma.participant.update({
where: {
id: participantId,
},
data: {
firstName,
lastName,
},
});
return res.status(200).json(newParticipants);

if (!updatedParticipant) {
return res.status(500).json({ error: 'Something went wrong' });
}

return res.status(200).json({ updatedParticipant });
} catch (err: any) {
console.error(err);
return res.status(500).json({ error: 'Something went wrong' });
}
};

// export const addParticipantsInBulk = async (req: Request, res: Response) => {
// try {
// const { orgId, eventId } = req?.params;
// const { participants } = req?.body;

// const newParticipants = [];
// const failedParticipants = [];

// for (const participant of participants) {
// try {
// const newParticipant = await prisma.participant.create({
// data: {
// firstName: participant.firstName,
// lastName: participant.lastName,
// email: participant.email,
// phone: participant.phoneNumber,
// organizationId: orgId,
// eventId,
// },
// });
// newParticipants.push(newParticipant);
// } catch (error) {
// 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(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 {
const { orgId, eventId } = req?.params;
Expand Down Expand Up @@ -298,6 +267,34 @@ export const setParticipantAttribute = async (req: Request, res: Response) => {
}
};

export const updateParticipantAttribute = async (req: Request, res: Response) => {
try {
const { orgId, eventId, participantId, attributeId } = req?.params;
const { value } = req?.body;

const participantAttribute = await prisma.participantAttributes.update({
where: {
participantId_attributeId: {
participantId,
attributeId,
},
},
data: {
value,
},
});

if (!participantAttribute) {
return res.status(500).json({ error: 'Something went wrong' });
}

return res.status(200).json({ participantAttribute });
} catch (err: any) {
console.error(err);
return res.status(500).json({ error: 'Something went wrong' });
}
};

export const checkInParticipant = async (req: Request, res: Response) => {
try {
const userId = req?.auth?.payload?.sub;
Expand All @@ -306,6 +303,10 @@ export const checkInParticipant = async (req: Request, res: Response) => {

const { checkedInAt } = req?.body;

if (!checkedInAt) {
return res.status(400).json({ error: 'checkedInAt is required' });
}

const participantAlreadyCheckedIn = await prisma.participantCheckIn.findFirst({
where: {
participantId,
Expand Down
22 changes: 19 additions & 3 deletions apps/core-admin/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@ import {
getParticipantAttributes,
setParticipantAttribute,
checkOutParticipant,
addNewParticipantInBulk,
editParticipant,
updateParticipantAttribute,
} from './controllers/participants';
import { addNewAttribute, getAllAttributes, getAttributeById } from './controllers/attributes';
import {
addNewAttribute,
editAttribute,
getAllAttributes,
getAttributeById,
getAttributeParticipants,
} from './controllers/attributes';
import { fetchAccountDetails, updateAccountDetails } from './controllers/users';

const router: Router = express.Router();
Expand Down Expand Up @@ -45,7 +52,7 @@ router.post('/organizations/:orgId/events', createNewEvent);

router.get('/organizations/:orgId/events/:eventId/participants', getAllParticipants);
router.post('/organizations/:orgId/events/:eventId/participants', addNewParticipant);
router.post('/organizations/:orgId/events/:eventId/bulkParticipants', addNewParticipantInBulk);
router.put('/organizations/:orgId/events/:eventId/participants/:participantId', editParticipant);

router.get(
'/organizations/:orgId/events/:eventId/participants/check-in',
Expand All @@ -69,9 +76,18 @@ router.post(
'/organizations/:orgId/events/:eventId/participants/:participantId/attributes',
setParticipantAttribute,
);
router.put(
'/organizations/:orgId/events/:eventId/participants/:participantId/attributes/:attributeId',
updateParticipantAttribute,
);

router.get('/organizations/:orgId/events/:eventId/attributes', getAllAttributes);
router.get('/organizations/:orgId/events/:eventId/attributes/:attributeId', getAttributeById);
router.get(
'/organizations/:orgId/events/:eventId/attributes/:attributeId/participants',
getAttributeParticipants,
);
router.put('/organizations/:orgId/events/:eventId/attributes/:attributeId', editAttribute);
router.post('/organizations/:orgId/events/:eventId/attributes', addNewAttribute);

export default router;
4 changes: 0 additions & 4 deletions apps/web-admin/src/layouts/DashboardLayout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ export default function DashboardLayout({ children }) {
const [isMobile] = useMediaQuery('(max-width: 768px)');
const [isSidebarOpen, setSidebarOpen] = useState(isMobile);

const toggleSidebar = () => {
setSidebarOpen(!isSidebarOpen);
};

return (
<Box
sx={{
Expand Down
Loading

0 comments on commit 62be976

Please sign in to comment.