Skip to content

Commit

Permalink
Fix:Runtime error & Add:single participant (#546)
Browse files Browse the repository at this point in the history
* added markdown editor with save feature

* basic Emailer integration done

* feat: added user registration page (registration-admin) (#507)

* trial push

* added button

* modal added

* added modal form fields

* removed unnecessary code

* added chakra-ui

* form field display (error not fixed)

* feat: Added participand routes and hooks for registration-admin

* Did something

* proper post reuest set

* feat: added route for participant form submission in registration-admin

* registration completed

* Registration complete page added

* added cookie to check registration

* added endOfLine to prettier

* removed eol from prettier

---------

Co-authored-by: aaron-6960 <[email protected]>
Co-authored-by: aaron-6960 <[email protected]>
Co-authored-by: Subramani E <[email protected]>

* Build issues fixed

* Fix:Runtime error solved

---------

Co-authored-by: Subramani E <[email protected]>
Co-authored-by: Midhun Unni <[email protected]>
Co-authored-by: aaron-6960 <[email protected]>
Co-authored-by: aaron-6960 <[email protected]>
Co-authored-by: Subramani E <[email protected]>
  • Loading branch information
6 people authored Nov 17, 2024
1 parent e56a6ef commit d3ef109
Show file tree
Hide file tree
Showing 39 changed files with 2,628 additions and 16,432 deletions.
Empty file.
102 changes: 100 additions & 2 deletions apps/core-admin/src/controllers/mail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import FormData from 'form-data';
const MAILER_URL = process.env.MAILER_URL;
const MAILER_DATABASE_URL = process.env.MAILER_DATABASE_URL;
const AUTHORIZATION_TOKEN = process.env.AUTHORIZATION_TOKEN;
console.log(AUTHORIZATION_TOKEN);
// console.log(AUTHORIZATION_TOKEN);
export const sendMailWithQR = async (req: Request, res: Response) => {
try {
const { subject, html, projectId } = req.body;
Expand Down Expand Up @@ -48,6 +48,10 @@ export const sendMailWithQR = async (req: Request, res: Response) => {
let emailText: string = html;
emailText = emailText.replace('{{payload}}', recipient.payload);
emailText = emailText.replace('{{name}}', recipient.name);
emailText = emailText.replace('{{payload}}', recipient.payload);
emailText = emailText.replace(/\n/g, '');
emailText = emailText.replace(/"/g, "'");

form.append('html', emailText);
form.append('subject', subject);
form.append('text', subject);
Expand Down Expand Up @@ -151,6 +155,38 @@ export const getMailStatus = async (req: Request, res: Response) => {
}
};

export const updateMailProject = async (req: Request, res: Response) => {
try {
const { projectId, html_template } = req.body;
const { orgId } = req?.params;
if (!projectId || !orgId) {
return res.status(400).send({ message: 'Missing required fields!' });
}
console.log(projectId);

// console.log(html_template);
const response = await prisma.Projects.update({
where: { id: projectId },
data: { html_template: html_template },
});
console.log(response);

if (response) {
return res.status(200).json({
message: 'html_template updated',
data: response,
});
} else {
return res.status(400).send({
message: 'Error updating html_template',
});
}
} catch (e) {
console.log(e);
return res.status(400).send({ message: e });
}
};

export const newMailProject = async (req: Request, res: Response) => {
try {
const { name, desc } = req.body;
Expand Down Expand Up @@ -216,7 +252,7 @@ export const newMailProject = async (req: Request, res: Response) => {
export const getMailProjects = async (req: Request, res: Response) => {
try {
const { orgId } = req?.params;
// console.log(orgId)
console.log(orgId);
if (!orgId) {
return res.status(400).send({ message: 'Missing required fields' });
}
Expand Down Expand Up @@ -282,6 +318,68 @@ export const addNewRecipient = async (req: Request, res: Response) => {
return res.status(400).send({ message: e.message || 'Something went wrong' });
}
};
type mytype = {
// projectId: string | undefined;
name: string | undefined;
email: string | undefined;
payload: string | undefined;
};
export const addNewRecipients = async (req: Request, res: Response) => {
try {
const { data, projectId } = req.body;
console.log(data.length);
const arrayOfElements = data as mytype[];
let nonProcessed = [];
let processed = [];
if (!arrayOfElements || !projectId) {
console.log(arrayOfElements, projectId);
return res.status(400).send({ message: 'Missing required fields' });
} else if (Array.isArray(arrayOfElements)) {
for (const element of arrayOfElements) {
if (!element.email || !element.name || !element.payload) {
nonProcessed.push(element);
} else {
const recipientExists = await prisma.Recipients.findFirst({
where: {
projectId: projectId,
email: element.email,
},
});

if (recipientExists) {
processed.push(element);
} else {
// Insert new Recipients if they don't exist
const response = await prisma.Recipients.create({
data: {
name: element.name,
email: element.email,
payload: element.payload,
projectId: projectId,
},
});
if (response) {
processed.push(element);
} else {
nonProcessed.push(element);
}
// console.log('Insert:', response);
// return res.status(200).json({ message: 'User successfully Added' });
}
}
}
return res.status(200).json({
success: processed.length,
failure: nonProcessed.length,
});
} else {
return res.status(400).send({ message: 'Element not an array' });
}
} catch (e: any) {
console.log(e);
return res.status(400).send({ message: e.message || 'Something went wrong' });
}
};
const generateOTP = () => {
// Generates a 6-digit random number
return Math.floor(100000 + Math.random() * 900000);
Expand Down
107 changes: 107 additions & 0 deletions apps/core-admin/src/controllers/registration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { Request, Response } from 'express';

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

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

const event = await prisma.event.findFirst({
where: {
id: eventId,
},
});
if (!event) {
return res.status(404).json({ error: 'Event not found' });
}
if (event.organizationId != orgId) {
return res.status(404).json({ error: "Organisation and event don't match" });
}
return res.status(200).json({ msg: 'Event corresponds to the given org and both exists' });
} catch (err: any) {
console.error(err);
return res.status(500).json({ error: 'Something went wrong' });
}
};

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

const defaultKeys = ['firstName', 'lastName', 'email', 'phone'];
const defaultData: { [key: string]: string } = {};
const attrData: { attributeId: string; value: string }[] = [];

for (const key in data) {
if (defaultKeys.includes(key)) {
defaultData[key] = data[key];
} else {
attrData.push({
attributeId: key,
value: data[key],
});
}
}

const newRegistrant = await prisma.registrant.create({
data: {
firstName: defaultData['firstName'],
lastName: defaultData['lastName'] || null,
email: defaultData['email'],
phone: defaultData['phone'] || null,
eventId: eventId,
organizationId: orgId,
registrantAttributes: {
create: attrData,
},
},
});

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

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

let ExtraAttributes = await prisma.attributes.findMany({
where: {
organizationId: orgId,
eventId: eventId,
},
include: {
participantAttributes: true,
},
});

const defaultAttributes = [
{ name: 'First Name', id: 'firstName' },
{ name: 'Last Name', id: 'lastName' },
{ name: 'Email', id: 'email' },
{ name: 'Phone Number', id: 'phone' },
];

ExtraAttributes = ExtraAttributes.map((attribute: any) => {
return {
name: attribute.name,
id: attribute.id,
};
});

const attributes = defaultAttributes.concat(ExtraAttributes);

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' });
}
};
6 changes: 5 additions & 1 deletion apps/core-admin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,15 @@ app.get('/health', (req: Request, res: Response) => {
});

app.param('eventId', validateUUID);
app.use(jwtCheck);

import router from './routes';
import clientRouter from './unprotectedRoutes';
import { decodeUserInfo } from './middlewares/auth0';

app.use('/registration', clientRouter);

app.use(jwtCheck);

app.use('/core', decodeUserInfo, router);

app.listen(port, () => {
Expand Down
4 changes: 4 additions & 0 deletions apps/core-admin/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ import { addNewExtra, checkInExtra, getAllExtras, getExtraById } from './control
import { validateUUID } from './middlewares/validateParams';
import {
addNewRecipient,
addNewRecipients,
getMailProjects,
getMailStatus,
newMailProject,
sendMailWithQR,
sendOTP,
updateMailProject,
verifyOTP,
} from './controllers/mail';

Expand Down Expand Up @@ -120,9 +122,11 @@ router.post('/organizations/:orgId/events/:eventId/extras', addNewExtra);

//mailer routes
router.post('/organizations/:orgId/newEmailProject', newMailProject);
router.post('/organizations/:orgId/updateEmailProject', updateMailProject);
router.get('/organizations/:orgId/getEmailProjects', getMailProjects);
router.get('/organizations/:orgId/getMailStatus', getMailStatus);
router.post('/organizations/:orgId/addNewRecipient', addNewRecipient);
router.post('/organizations/:orgId/addNewRecipients', addNewRecipients);
router.post('/organizations/:orgId/events/:eventId/mailQR', sendMailWithQR);

// OTP routes
Expand Down
23 changes: 23 additions & 0 deletions apps/core-admin/src/unprotectedRoutes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import express, { Router } from 'express';
import {
orgAndEventVerification,
getFormAttributes,
addFormResponse,
} from './controllers/registration';

const router: Router = express.Router();

router.get('/', (req: any, res: any) => {
try {
return res.send('Hello World!');
} catch (err) {
console.error(err);
return res.status(500);
}
});

router.get('/:orgId/event/:eventId/verify', orgAndEventVerification);
router.get('/:orgId/event/:eventId/attributes', getFormAttributes);
router.post('/:orgId/event/:eventId/submit', addFormResponse);

export default router;
1 change: 0 additions & 1 deletion apps/core-mailer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ app.post('/mail', authorize, async (req: Request, res: Response) => {
console.error('Form parsing error:', err);
return res.status(500).send({ message: 'Error parsing form data' });
}

// Process fields and files as needed
const { name, to, subject, text, html } = fields;
console.log(name, to, subject, text, html);
Expand Down
Loading

0 comments on commit d3ef109

Please sign in to comment.