API-20: Adding invite endpoint (#11)
All checks were successful
Jenkins Production Deployment

Co-authored-by: Patrick Müller <patrick@mueller-patrick.tech>
Reviewed-on: #11
Co-authored-by: Patrick Müller <patrick@plutodev.de>
Co-committed-by: Patrick Müller <patrick@plutodev.de>
This commit is contained in:
Patrick Müller 2021-08-27 18:34:54 +00:00
parent ac10b1b43c
commit a3d137f1c6
2 changed files with 97 additions and 1 deletions

View File

@ -138,3 +138,35 @@ dataRouter.get('/event/:isDevCall', async (req: Request, res: Response) => {
res.status(500).send({'message': 'Internal Server Error. Try again later.'});
}
});
dataRouter.get('/invite/:isDevCall', async (req: Request, res: Response) => {
try {
let userId = (req.query.userId ?? '').toString();
let sessionId = (req.query.sessionId ?? '').toString();
let sessionKey = (req.query.sessionKey ?? '').toString();
let useDev: boolean = (req.params.isDevCall ?? '') === 'dev'; // TBD
if (userId === '' || sessionId === '' || sessionKey === '') {
res.status(400).send({
'status': 'WRONG_PARAMS',
'message': 'Missing or wrong parameters'
});
return;
}
if (!await UserService.checkSession(useDev, userId, sessionId, sessionKey)) {
res.status(403).send({
'status': 'INVALID_SESSION',
'message': 'The user or session could not be found or the session is invalid'
});
return;
}
let data = await DataService.getInvitesData(useDev, userId);
res.status(200).send(data);
} catch (e) {
logger.error('Error handling a request: ' + e.message);
res.status(500).send({'message': 'Internal Server Error. Try again later.'});
}
});

View File

@ -88,6 +88,24 @@ export interface Event {
registrations: Registration[];
}
/**
* Used in the getInvitesData method as a return value
*/
export interface ReceivedInvite {
inviteId: string;
validUntil: Date;
alreadyUsed: boolean;
inviteKey: string;
eventName: string;
eventDescription: string;
takesPlaceDate: Date;
registrationUntilDate: Date;
maxParticipants: number;
eventCreatorId: string;
creatorFirstName: string;
creatorLastName: string;
}
/**
* Returns all data about the given user
* @param useDev If the dev or prod database should be used
@ -268,7 +286,7 @@ export const getEventData = async (useDev: boolean, userId: string): Promise<Eve
invitedPersonName: row.invited_person_name
});
}
let eventsList: Event[] = [];
for (let event of eventsMap.values()) {
eventsList.push(event);
@ -283,3 +301,49 @@ export const getEventData = async (useDev: boolean, userId: string): Promise<Eve
}
}
};
/**
* Returns all events the user is invited to
* @param useDev If the dev or prod database should be used
* @param userId The userId of the user to fetch the friends for
* @return ReceivedInvite[] A list of invites
*/
export const getInvitesData = async (useDev: boolean, userId: string): Promise<ReceivedInvite[]> => {
let conn;
try {
if (useDev) {
conn = await dev_pool.getConnection();
} else {
conn = await prod_pool.getConnection();
}
let rows = await conn.query('SELECT i.invite_id, i.valid_until, i.already_used, i.invite_key, e.name as event_name, e.description as event_description, e.takes_place_date, e.registration_until_date, e.max_participants, e.creator_id, u.first_name, u.last_name FROM invitations i LEFT OUTER JOIN events e ON e.event_id = i.event_id LEFT OUTER JOIN users u ON u.user_id = e.creator_id WHERE i.user_id = ?', userId);
let invites: ReceivedInvite[] = [];
for (let row of rows) {
invites.push({
inviteId: row.invite_id,
validUntil: row.valid_until,
alreadyUsed: row.already_used,
inviteKey: row.invite_key,
eventName: row.event_name,
eventDescription: row.event_description,
takesPlaceDate: row.takes_place_date,
registrationUntilDate: row.registration_until_date,
maxParticipants: row.max_participants,
eventCreatorId: row.creator_id,
creatorFirstName: row.first_name,
creatorLastName: row.last_name
});
}
return invites;
} catch (err) {
throw err;
} finally {
if (conn) {
conn.end();
}
}
};