Skip to content

Commit

Permalink
feat: Add support for participant attributes (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
alllenshibu authored Feb 3, 2024
1 parent 84d4bfd commit fa5861a
Show file tree
Hide file tree
Showing 12 changed files with 844 additions and 35 deletions.
75 changes: 75 additions & 0 deletions apps/core-admin/src/controllers/attributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Request, Response } from 'express';

import prisma from '../utils/database';

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

const attributes = await prisma.attributes.findMany({
where: {
organizationId: orgId,
eventId: eventId,
},
});

if (!attributes) {
return res.status(404).json({ error: 'No attributes found' });
}

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

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

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

if (!attribute) {
return res.status(404).json({ error: 'No attributes found' });
}

return res.status(200).json({ attribute });
} 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;
const { name } = req?.body;

const newAttribute = await prisma.attributes.create({
data: {
name,
organizationId: orgId,
eventId: eventId,
},
});

return res.status(200).json({ newAttribute });
} catch (err: any) {
console.error(err);
return res.status(500).json({ error: 'Something went wrong' });
}
};
49 changes: 49 additions & 0 deletions apps/core-admin/src/controllers/participants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export const getParticipantById = async (req: Request, res: Response) => {
checkedInByUser: true,
},
},
participantAttributes: true,
},
});

Expand All @@ -106,6 +107,54 @@ export const getParticipantById = async (req: Request, res: Response) => {
}
};

export const getParticipantAttributes = async (req: Request, res: Response) => {
try {
const { orgId, eventId, participantId } = req?.params;
const participantAttributes = await prisma.participantAttributes.findMany({
where: {
participantId,
},
include: {
participant: true,
attribute: true,
},
});

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

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

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

const participantAttribute = await prisma.participantAttributes.create({
data: {
participantId,
attributeId,
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 Down
16 changes: 16 additions & 0 deletions apps/core-admin/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import {
getParticipantById,
checkInParticipant,
getAllParticipantsCheckInDetails,
getParticipantAttributes,
setParticipantAttribute,
} from './controllers/participants';
import { addNewAttribute, getAllAttributes, getAttributeById } from './controllers/attributes';

const router: Router = express.Router();

Expand Down Expand Up @@ -38,4 +41,17 @@ router.post(
checkInParticipant,
);

router.get(
'/organizations/:orgId/events/:eventId/participants/:participantId/attributes',
getParticipantAttributes,
);
router.post(
'/organizations/:orgId/events/:eventId/participants/:participantId/attributes',
setParticipantAttribute,
);

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

export default router;
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { useRouter } from 'next/router';

import {
Box,
Flex,
Table,
TableCaption,
Tbody,
Td,
Tfoot,
Th,
Thead,
Tr,
TableContainer,
Text,
} from '@chakra-ui/react';

import { useFetch } from '@/hooks/useFetch';

import DashboardLayout from '@/layouts/DashboardLayout';
import { useEffect, useState } from 'react';

export default function Events() {
const router = useRouter();

const { orgId, eventId, attributeId } = router.query;

const { loading, get } = useFetch();

const [attribute, setAttribute] = useState({});

useEffect(() => {
const fetchAttribute = async () => {
const { data, status } = await get(
`/core/organizations/${orgId}/events/${eventId}/attributes/${attributeId}`,
);
setAttribute(data.attribute || []);
console.log(data);
};
fetchAttribute();
}, [orgId, eventId, attributeId]);

return (
<DashboardLayout>
<Flex
direction="column"
height="100%"
width="100%"
alignItems="center"
justifyContent="center"
gap={8}
>
<Box width="100%" p={8}>
<Text fontSize="4xl" fontWeight="bold">
Attribute Details
</Text>
</Box>
<Box width="100%" height="100%">
{JSON.stringify(attribute)}
</Box>
</Flex>
</DashboardLayout>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { useRouter } from 'next/router';

import {
Box,
Flex,
Table,
TableCaption,
Tbody,
Td,
Tfoot,
Th,
Thead,
Tr,
TableContainer,
Text,
Button,
} from '@chakra-ui/react';

import { useFetch } from '@/hooks/useFetch';

import DashboardLayout from '@/layouts/DashboardLayout';
import { useEffect, useState } from 'react';

export default function Events() {
const router = useRouter();

const { orgId, eventId } = router.query;

const { loading, get } = useFetch();

const [attributes, setAttributes] = useState([]);
const handleClick = () => {
router.push(`/organizations/${orgId}/events/${eventId}/attributes/new`);
};

useEffect(() => {
const fetchAttributes = async () => {
const { data, status } = await get(
`/core/organizations/${orgId}/events/${eventId}/attributes`,
);
setAttributes(data.attributes || []);
console.log(data);
};
fetchAttributes();
}, [orgId, eventId]);

return (
<DashboardLayout>
<Flex
direction="column"
height="100%"
width="100%"
alignItems="center"
justifyContent="center"
gap={8}
>
<Box width="100%" p={8} display="flex" justifyContent="space-between">
<Text fontSize="4xl" fontWeight="bold">
Attributes
</Text>
<Button
padding="4"
minWidth="-moz-initial"
bgColor="rgb(128, 90, 213)"
color="white"
_hover={{ bgColor: 'rgb(100, 70, 183)' }}
onClick={handleClick}
>
Add Attribute
</Button>
</Box>
<Box width="100%" height="100%">
<TableContainer width="100%" height="100%">
<Table variant="simple">
<TableCaption>Attributes</TableCaption>
<Thead>
<Tr>
<Th>ID</Th>
<Th>Name</Th>
</Tr>
</Thead>
<Tbody>
{attributes.map((attribute) => (
<Tr
key={attribute?.id}
onClick={() => {
router.push(
`/organizations/${orgId}/events/${eventId}/attributes/${attribute?.id}`,
);
}}
cursor="pointer"
>
<Td>{attribute?.id}</Td>
<Td>{attribute?.name}</Td>
</Tr>
))}
</Tbody>
<Tfoot>
<Tr>
<Th>{attributes.length} attributes</Th>
</Tr>
</Tfoot>
</Table>
</TableContainer>
</Box>
</Flex>
</DashboardLayout>
);
}
Loading

0 comments on commit fa5861a

Please sign in to comment.