Skip to content

Commit

Permalink
fix: Fix logic errors (#207)
Browse files Browse the repository at this point in the history
Fix all logic errors bugs and edge cases till what is completed
  • Loading branch information
alllenshibu authored Dec 29, 2023
1 parent dba5a1d commit 3e49dca
Show file tree
Hide file tree
Showing 14 changed files with 218 additions and 96 deletions.
36 changes: 30 additions & 6 deletions apps/core/src/controllers/checkin.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,28 @@ const checkinController = (checkinService: CheckinService) => {
return res.status(400).json({ error: 'Checkin time is required' });
}

if (!req.body.user.assets.find((asset: any) => asset.organizationId === organizationId)) {
return res.status(400).json({ error: 'You are not a member of this organization' });
}

if (!req.body.user.assets.find((asset: any) => asset.eventId === eventId)) {
return res.status(400).json({ error: 'No such event' });
}

const newParticipantCheckin = await checkinService().checkinParticipantService(
organizationId,
eventId,
participantId,
checkinTime,
user.id,
);
return res.status(200).json({ newParticipantCheckin });
} catch (err) {
return res.status(500).json({ error: 'Something went wrong' });
return res.status(201).json({
message: 'Participant checked in successfully',
newParticipantCheckin,
});
} catch (err: any) {
console.error(err.message);
return res.status(400).json({ error: err.message });
}
},
getParticipantCheckinDetailsController: async (req: Request, res: Response) => {
Expand All @@ -58,15 +70,27 @@ const checkinController = (checkinService: CheckinService) => {
return res.status(400).json({ error: 'Participant ID is required' });
}

if (!req.body.user.assets.find((asset: any) => asset.organizationId === organizationId)) {
return res.status(400).json({ error: 'You are not a member of this organization' });
}

if (!req.body.user.assets.find((asset: any) => asset.eventId === eventId)) {
return res.status(400).json({ error: 'No such event' });
}

const participantCheckinDetails =
await checkinService().getParticipantCheckinDetailsService(
organizationId,
eventId,
participantId,
);
return res.status(200).json({ participantCheckinDetails });
} catch (err) {
return res.status(500).json({ error: 'Something went wrong' });
return res.status(200).json({
message: 'Participant checkin details retrieved successfully',
participantCheckinDetails,
});
} catch (err: any) {
console.error(err.message);
return res.status(400).json({ error: err.message });
}
},
};
Expand Down
38 changes: 31 additions & 7 deletions apps/core/src/controllers/event.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,47 @@ const eventController = (eventService: EventService) => {
return res.status(400).json({ error: 'Event name is required' });
}

if (!req.body.user.assets.find((asset: any) => asset.organizationId === organizationId)) {
return res.status(400).json({ error: 'You are not a member of this organization' });
}

const newEvent = await eventService().addNewEventService(organizationId, name);

return res.status(201).json({
message: 'Successfully created new event',
event: newEvent,
});
} catch (err) {
console.error(err);
} catch (err: any) {
console.error(err.message);
return res.status(400).json({ error: err.message });
}
},
getAllEventsController: async (req: Request, res: Response) => {
try {
const user = req.body.user;
const organizationId = req?.params?.organizationId;

if (!user || user === '' || user === undefined) {
return res.status(400).json({ error: 'Authentication error' });
}

if (!organizationId || organizationId === '' || organizationId === undefined) {
return res.status(400).json({ error: 'Organization ID is required' });
}

const events = await eventService().getAllEventsService(organizationId);
if (!req.body.user.assets.find((asset: any) => asset.organizationId === organizationId)) {
return res.status(400).json({ error: 'You are not a member of this organization' });
}

const events = await eventService().getAllEventsService(user, organizationId);

return res.status(200).json({
message: 'Successfully retrieved all events',
events: events,
});
} catch (err) {
console.error(err);
} catch (err: any) {
console.error(err.message);
return res.status(400).json({ error: err.message });
}
},
getEventController: async (req: Request, res: Response) => {
Expand All @@ -56,14 +71,23 @@ const eventController = (eventService: EventService) => {
return res.status(400).json({ error: 'Event ID is required' });
}

if (!req.body.user.assets.find((asset: any) => asset.organizationId === organizationId)) {
return res.status(400).json({ error: 'You are not a member of this organization' });
}

if (!req.body.user.assets.find((asset: any) => asset.eventId === eventId)) {
return res.status(400).json({ error: 'No such event' });
}

const event = await eventService().getEventService(organizationId, eventId);

return res.status(200).json({
message: 'Successfully retrieved event',
event: event,
});
} catch (err) {
console.error(err);
} catch (err: any) {
console.error(err.message);
return res.status(400).json({ error: err.message });
}
},
};
Expand Down
44 changes: 37 additions & 7 deletions apps/core/src/controllers/participant.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ const participantController = (participant: ParticipantService) => {
return res.status(400).json({ error: 'Last name is required' });
}

if (!req.body.user.assets.find((asset: any) => asset.organizationId === organizationId)) {
return res.status(400).json({ error: 'You are not a member of this organization' });
}

if (!req.body.user.assets.find((asset: any) => asset.eventId === eventId)) {
return res.status(400).json({ error: 'No such event' });
}

const newParticipant = await participantService().addNewParticipantService(
organizationId,
eventId,
Expand All @@ -37,8 +45,9 @@ const participantController = (participant: ParticipantService) => {
message: 'Successfully added new participant to event',
participant: newParticipant,
});
} catch (err) {
console.log(err);
} catch (err: any) {
console.log(err.message);
return res.status(400).json({ error: err.message });
}
},
getAllParticipantsController: async (req: Request, res: Response) => {
Expand All @@ -53,6 +62,14 @@ const participantController = (participant: ParticipantService) => {
return res.status(400).json({ error: 'Event ID is required' });
}

if (!req.body.user.assets.find((asset: any) => asset.organizationId === organizationId)) {
return res.status(400).json({ error: 'You are not a member of this organization' });
}

if (!req.body.user.assets.find((asset: any) => asset.eventId === eventId)) {
return res.status(400).json({ error: 'No such event' });
}

const participants = await participantService().getAllParticipantsService(
organizationId,
eventId,
Expand All @@ -62,8 +79,9 @@ const participantController = (participant: ParticipantService) => {
message: 'Successfully retrieved all participants',
participants: participants,
});
} catch (err) {
console.log(err);
} catch (err: any) {
console.log(err.message);
return res.status(400).json({ error: err.message });
}
},
getParticipantController: async (req: Request, res: Response) => {
Expand All @@ -82,15 +100,27 @@ const participantController = (participant: ParticipantService) => {
return res.status(400).json({ error: 'Participant ID is required' });
}

if (!req.body.user.assets.find((asset: any) => asset.organizationId === organizationId)) {
return res.status(400).json({ error: 'You are not a member of this organization' });
}

if (!req.body.user.assets.find((asset: any) => asset.eventId === eventId)) {
return res.status(400).json({ error: 'No such event' });
}

const participant = await participantService().getParticipantService(
organizationId,
eventId,
participantId,
);

return res.status(200).json({ participant: participant });
} catch (err) {
console.log(err);
return res.status(200).json({
message: 'Successfully retrieved participant',
participant: participant,
});
} catch (err: any) {
console.log(err.message);
return res.status(400).json({ error: err.message });
}
},
};
Expand Down
21 changes: 17 additions & 4 deletions apps/core/src/middlewares/auth.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,32 @@ const authorize = async (req: Request & typeof User, res: Response, next: NextFu
return res.sendStatus(403);
}

const organizations = (
// Todo: Remove names of events and organizations after testing
const assets = (
await pg.query(
'SELECT * FROM ORGANIZATION JOIN ORGANIZATION_USER ON ORGANIZATION.ID = ORGANIZATION_USER.ORGANIZATION_ID WHERE ORGANIZATION_USER.USER_ID = $1',
` SELECT
organization.id as "organizationId",
organization.name as "organizationName",
role_id as "roleId",
event.id as "eventId",
event.name as "eventName"
FROM organization
JOIN organization_user
ON organization.id = organization_user.organization_id
JOIN event
ON organization.id = event.organization_id
WHERE organization_user.user_id = $1`,
[user.id],
)
)?.rows;

req.body.user = user;
req.body.organizations = organizations.rows;
req.body.user.assets = assets;

next();
});
} else {
res.sendStatus(401);
res.sendStatus(403);
}
};

Expand Down
14 changes: 11 additions & 3 deletions apps/core/src/passport/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ var config = {
secretKey: process.env.SECRET_KEY,
};

const createUser = async (email: String, password: String, firstName: String, lastName: String) => {
function validateEmail(email: string) {
var emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

return emailPattern.test(email);
}

const createUser = async (email: string, password: string, firstName: string, lastName: string) => {
if (!validateEmail(email)) throw new Error('Invalid email');

const salt = await bcrypt.genSalt(10);
const hash = await bcrypt.hash(password, salt);

Expand All @@ -24,12 +32,12 @@ const createUser = async (email: String, password: String, firstName: String, la
return data.rows[0];
};

const matchPassword = async (password: String, hashPassword: String) => {
const matchPassword = async (password: string, hashPassword: string) => {
const match = await bcrypt.compare(password, hashPassword);
return match;
};

const emailExists = async (email: String) => {
const emailExists = async (email: string) => {
const data = await pg.query('SELECT * FROM "user" WHERE email=$1', [email]);

if (data.rowCount == 0) return false;
Expand Down
46 changes: 42 additions & 4 deletions apps/core/src/routes/auth.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,65 @@ router.post(
passport.authenticate('local-signup', { session: false }),
async (req: any, res, next) => {
try {
if (
!req.body?.firstName ||
req.body?.firstName === '' ||
req.body?.firstName === null ||
req.body?.firstName === undefined
)
throw new Error('First name is required');

if (
!req.body?.lastName ||
req.body?.lastName === '' ||
req.body?.lastName === null ||
req.body?.lastName === undefined
)
throw new Error('Last name is required');

if (
!req.user?.email ||
req.user?.email === '' ||
req.user?.email === null ||
req.user?.email === undefined
)
throw new Error('Email is required');

if (
!req.user?.password ||
req.user?.password === '' ||
req.user?.password === null ||
req.user?.password === undefined
)
throw new Error('Password is required');

const user = await createUser(
req.user?.email,
req.user?.password,
req.body.firstName,
req.body.lastName,
req.body?.firstName,
req.body?.lastName,
);
return res.status(201).json({
message: 'Successfully created new user',
});
} catch (err: any) {
console.error(err);
console.error(err.message);
return res.status(400).json({ error: err.message });
}
},
);

router.post(
'/login',

passport.authenticate('local-login', { session: false }),
(req: any, res, next) => {
res.json({ message: 'Succefully logged in', token: req.user.token });
try {
res.json({ message: 'Succefully logged in', token: req.user.token });
} catch (err: any) {
console.error(err);
return res.status(400).json({ error: err.message });
}
},
);
export { router as authrouter };
13 changes: 9 additions & 4 deletions apps/core/src/services/checkin.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ const checkinService: CheckinService = () => {
return newParticipantCheckin;
} catch (err: any) {
await pg.query('ROLLBACK');
console.error(err);
throw new Error('Something went wrong');
console.error(err.message);
throw err;
}
},
getParticipantCheckinDetailsService: async (
Expand Down Expand Up @@ -116,10 +116,15 @@ const checkinService: CheckinService = () => {
)
).rows[0];

if (!participantCheckinDetails)
participantCheckinDetails = {
checkedIn: false,
};

return participantCheckinDetails;
} catch (err: any) {
console.error(err);
throw new Error('Something went wrong');
console.error(err.message);
throw err;
}
},
};
Expand Down
Loading

0 comments on commit 3e49dca

Please sign in to comment.