From a6e7c80c539c6e85a5679ffdb604ecb4f4b57578 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20M=C3=BCller?= Date: Fri, 1 Oct 2021 18:04:40 +0200 Subject: [PATCH] API-31: Endpoint to get single rapla event details --- .../DHBWRaPlaChanges.router.ts | 18 ++++++ .../changes/changes.service.ts | 61 +++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/src/models/dhbw-rapla-changes/DHBWRaPlaChanges.router.ts b/src/models/dhbw-rapla-changes/DHBWRaPlaChanges.router.ts index 705d28c..1e424cc 100644 --- a/src/models/dhbw-rapla-changes/DHBWRaPlaChanges.router.ts +++ b/src/models/dhbw-rapla-changes/DHBWRaPlaChanges.router.ts @@ -28,3 +28,21 @@ dhbwRaPlaChangesRouter.get('/', async (req: Request, res: Response) => { }); } }); + +dhbwRaPlaChangesRouter.get('/:id', async (req: Request, res: Response) => { + try { + let id: string = (req.params.id ?? '').toString(); + + let changes = await ChangeService.getEventById('TINF19B4', id); + + res.status(200).send(changes); + } catch (e) { + let errorGuid = Guid.create().toString(); + logger.error('Error handling a request: ' + e.message, {reference: errorGuid}); + res.status(500).send({ + 'status': 'PROCESSING_ERROR', + 'message': 'Internal Server Error. Try again later.', + 'reference': errorGuid + }); + } +}); diff --git a/src/models/dhbw-rapla-changes/changes/changes.service.ts b/src/models/dhbw-rapla-changes/changes/changes.service.ts index 0182bb2..e235934 100644 --- a/src/models/dhbw-rapla-changes/changes/changes.service.ts +++ b/src/models/dhbw-rapla-changes/changes/changes.service.ts @@ -84,3 +84,64 @@ export const getChanges = async (course: string, week: string): Promise } } }; + +export const getEventById = async (course: string, id: string): Promise => { + let conn; + try { + conn = await pool.getConnection(); + + let rows = await conn.query('SELECT c.change_id, c.entry_id, c.change_timestamp, c.isDeleted, c.new_summary, c.new_description, c.new_start, c.new_last_modified, c.new_end, c.new_created, c.new_location, c.new_organizer, c.new_categories, e.uid FROM rapla_changes c LEFT OUTER JOIN rapla_entries e ON c.entry_id = e.entry_id WHERE e.uid = ? ORDER BY c.change_id', id); + + let eventsMap = new Map(); + + for (let row of rows) { + let change: Change = { + change_id: row.change_id, + event_id: row.event_id, + change_timestamp: row.change_timestamp, + is_deleted: row.isDeleted, + new_summary: row.new_summary, + new_description: row.new_description, + new_start: row.new_start, + new_end: row.new_end, + new_last_modified: row.new_last_modified, + new_created: row.new_created, + new_location: row.new_location, + new_organizer: row.new_organizer, + new_categories: row.new_categories, + new_recurring: row.new_recurring + }; + + if (eventsMap.has(row.entry_id)) { + let event = eventsMap.get(row.entry_id); + + // Only adjust these fields if the event is not deleted as otherwise they would be null + if (!row.isDeleted) { + event.latest_event_summary = row.new_summary; + event.latest_start_date = row.new_start; + } + event.changes.push(change); + + eventsMap.set(row.entry_id, event); + } else { + let event: Event = { + event_id: row.event_id, + event_uid: row.uid, + latest_event_summary: row.new_summary, + latest_start_date: row.new_start, + changes: [change] + }; + + eventsMap.set(row.entry_id, event); + } + } + + return Array.from(eventsMap.values())[0]; + } catch (err) { + throw err; + } finally { + if (conn) { + conn.end(); + } + } +};