diff --git a/src/models/partyplaner/data/Data.router.ts b/src/models/partyplaner/data/Data.router.ts index c228a8e..0ec41bf 100644 --- a/src/models/partyplaner/data/Data.router.ts +++ b/src/models/partyplaner/data/Data.router.ts @@ -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.'}); + } +}); diff --git a/src/models/partyplaner/data/data.service.ts b/src/models/partyplaner/data/data.service.ts index 07ce5f0..3c1962c 100644 --- a/src/models/partyplaner/data/data.service.ts +++ b/src/models/partyplaner/data/data.service.ts @@ -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 => { + 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(); + } + } +};