Interface adjustments
All checks were successful
Jenkins Production Deployment

This commit is contained in:
Patrick Müller 2022-12-25 20:53:09 +01:00
parent ccfa28877c
commit 6cb7f0d59b
Signed by: Paddy
GPG Key ID: 37ABC11275CAABCE
5 changed files with 61 additions and 46 deletions

3
app.ts
View File

@ -29,7 +29,8 @@ app.use(express.json());
// Configure CORS // Configure CORS
let allowedHosts = [ let allowedHosts = [
'https://www.nachklang.art', 'https://www.nachklang.art',
'https://calendar.nachklang.art' 'https://calendar.nachklang.art',
'http://localhost:4200'
]; ];
app.use(cors({ app.use(cors({
origin: function (origin: any, callback: any) { origin: function (origin: any, callback: any) {

View File

@ -1,13 +1,13 @@
export interface Event { export interface Event {
event_id: number; eventId: number;
calendar_id: number; calendarId: number;
uuid: string; uuid: string;
name: string; name: string;
description: string; description: string;
start_datetime: Date; startDateTime: Date;
end_datetime: Date; endDateTime: Date;
created_date: Date; createdDate: Date;
location: string; location: string;
created_by: string; createdBy: string;
url: string; url: string;
} }

View File

@ -38,19 +38,19 @@ eventsRouter.get('/:calendar/json', async (req: Request, res: Response) => {
let password: string = req.query.password as string ?? ''; let password: string = req.query.password as string ?? '';
if (calendarName.length < 1) { if (calendarName.length < 1) {
res.status(401).send({'message': 'Please state the name of the calendar you want events from.'}); res.status(400).send({'message': 'Please state the name of the calendar you want events from.'});
return; return;
} }
if (!Array.from(calendarNames.keys()).includes(calendarName)) { if (!Array.from(calendarNames.keys()).includes(calendarName)) {
res.status(401).send({'message': 'Unknown calendar.'}); res.status(400).send({'message': 'Unknown calendar.'});
return; return;
} }
let calendarId: number = calendarNames.get(calendarName)!.id; let calendarId: number = calendarNames.get(calendarName)!.id;
if (!CredentialService.hasAccess(calendarName, password)) { if (!CredentialService.hasAccess(calendarName, password)) {
res.status(401).send({'message': 'You do not have access to the specified calendar.'}); res.status(403).send({'message': 'You do not have access to the specified calendar.'});
return; return;
} }
@ -72,19 +72,19 @@ eventsRouter.get('/:calendar/ical', async (req: Request, res: Response) => {
let password: string = req.query.password as string ?? ''; let password: string = req.query.password as string ?? '';
if (calendarName.length < 1) { if (calendarName.length < 1) {
res.status(401).send({'message': 'Please state the name of the calendar you want events from.'}); res.status(400).send({'message': 'Please state the name of the calendar you want events from.'});
return; return;
} }
if (!Array.from(calendarNames.keys()).includes(calendarName)) { if (!Array.from(calendarNames.keys()).includes(calendarName)) {
res.status(401).send({'message': 'Unknown calendar.'}); res.status(400).send({'message': 'Unknown calendar.'});
return; return;
} }
let calendarId: number = calendarNames.get(calendarName)!.id; let calendarId: number = calendarNames.get(calendarName)!.id;
if (!CredentialService.hasAccess(calendarName, password)) { if (!CredentialService.hasAccess(calendarName, password)) {
res.status(401).send({'message': 'You do not have access to the specified calendar.'}); res.status(403).send({'message': 'You do not have access to the specified calendar.'});
return; return;
} }
@ -125,21 +125,21 @@ eventsRouter.post('/', async (req: Request, res: Response) => {
req.body.startDateTime === undefined || req.body.startDateTime === undefined ||
req.body.endDateTime === undefined req.body.endDateTime === undefined
) { ) {
res.status(401).send({'message': 'Required parameters missing'}); res.status(400).send({'message': 'Required parameters missing'});
return; return;
} }
let event: Event = { let event: Event = {
event_id: -1, eventId: -1,
calendar_id: req.body.calendarId, calendarId: req.body.calendarId,
uuid: '', uuid: '',
name: req.body.name, name: req.body.name,
description: req.body.description ?? '', description: req.body.description ?? '',
start_datetime: new Date(req.body.startDateTime), startDateTime: new Date(req.body.startDateTime),
end_datetime: new Date(req.body.endDateTime), endDateTime: new Date(req.body.endDateTime),
created_date: new Date(), createdDate: new Date(),
location: req.body.location ?? '', location: req.body.location ?? '',
created_by: req.body.createdBy ?? '', createdBy: req.body.createdBy ?? '',
url: req.body.url ?? '' url: req.body.url ?? ''
}; };
@ -174,28 +174,30 @@ eventsRouter.put('/:eventId', async (req: Request, res: Response) => {
req.body.startDateTime === undefined || req.body.startDateTime === undefined ||
req.body.endDateTime === undefined req.body.endDateTime === undefined
) { ) {
res.status(401).send({'message': 'Required parameters missing'}); res.status(400).send({'message': 'Required parameters missing'});
return; return;
} }
let event: Event = { let event: Event = {
event_id: parseInt(req.params.eventId, 10), eventId: parseInt(req.params.eventId, 10),
calendar_id: req.body.calendarId, calendarId: req.body.calendarId,
uuid: '', uuid: '',
name: req.body.name, name: req.body.name,
description: req.body.description ?? '', description: req.body.description ?? '',
start_datetime: new Date(req.body.startDateTime), startDateTime: new Date(req.body.startDateTime),
end_datetime: new Date(req.body.endDateTime), endDateTime: new Date(req.body.endDateTime),
created_date: new Date(), createdDate: new Date(),
location: req.body.location ?? '', location: req.body.location ?? '',
created_by: req.body.createdBy ?? '', createdBy: req.body.createdBy ?? '',
url: req.body.url ?? '' url: req.body.url ?? ''
}; };
let success = await EventService.updateEvent(event); let successRows = await EventService.updateEvent(event);
if (success) { if (successRows === 1) {
res.status(200).send({'message': 'Event was successfully updated'}); res.status(200).send({'message': 'Event was successfully updated'});
} else if (successRows === 0) {
res.status(200).send({'message': '0 rows were updated'});
} else { } else {
res.status(500).send({'message': 'An error occurred during the update process. Please try again.'}); res.status(500).send({'message': 'An error occurred during the update process. Please try again.'});
} }
@ -223,21 +225,21 @@ eventsRouter.delete('/:eventId', async (req: Request, res: Response) => {
if ( if (
req.params.eventId === undefined req.params.eventId === undefined
) { ) {
res.status(401).send({'message': 'Required parameters missing'}); res.status(400).send({'message': 'Required parameters missing'});
return; return;
} }
let event: Event = { let event: Event = {
event_id: parseInt(req.params.eventId, 10), eventId: parseInt(req.params.eventId, 10),
calendar_id: -1, calendarId: -1,
uuid: '', uuid: '',
name: '', name: '',
description: '', description: '',
start_datetime: new Date(), startDateTime: new Date(),
end_datetime: new Date(), endDateTime: new Date(),
created_date: new Date(), createdDate: new Date(),
location: '', location: '',
created_by: '', createdBy: '',
url: '' url: ''
}; };

View File

@ -18,7 +18,19 @@ export const getAllEvents = async (calendarId: number): Promise<Event[]> => {
const eventsRes = await conn.query(eventsQuery, calendarId); const eventsRes = await conn.query(eventsQuery, calendarId);
for (let row of eventsRes) { for (let row of eventsRes) {
eventRows.push(row); eventRows.push({
eventId: row.event_id,
calendarId: row.calendar_id,
uuid: row.uuid,
name: row.name,
description: row.description,
startDateTime: row.start_datetime,
endDateTime: row.end_datetime,
createdDate: row.created_date,
location: row.location,
createdBy: row.created_by,
url: row.url
});
} }
return eventRows; return eventRows;
@ -39,7 +51,7 @@ export const createEvent = async (event: Event): Promise<number> => {
try { try {
let eventUUID = Guid.create().toString(); let eventUUID = Guid.create().toString();
const eventsQuery = 'INSERT INTO events (calendar_id, uuid, name, description, start_datetime, end_datetime, location, created_by, url) VALUES (?,?,?,?,?,?,?,?,?) RETURNING event_id'; const eventsQuery = 'INSERT INTO events (calendar_id, uuid, name, description, start_datetime, end_datetime, location, created_by, url) VALUES (?,?,?,?,?,?,?,?,?) RETURNING event_id';
const eventsRes = await conn.execute(eventsQuery, [event.calendar_id, eventUUID, event.name, event.description, event.start_datetime, event.end_datetime, event.location, event.created_by, event.url]); const eventsRes = await conn.execute(eventsQuery, [event.calendarId, eventUUID, event.name, event.description, event.startDateTime, event.endDateTime, event.location, event.createdBy, event.url]);
return eventsRes[0].event_id; return eventsRes[0].event_id;
} catch (err) { } catch (err) {
@ -56,13 +68,13 @@ export const createEvent = async (event: Event): Promise<number> => {
* Update the given event in the database * Update the given event in the database
* @param event The event to update * @param event The event to update
*/ */
export const updateEvent = async (event: Event): Promise<boolean> => { export const updateEvent = async (event: Event): Promise<number> => {
let conn = await NachklangCalendarDB.getConnection(); let conn = await NachklangCalendarDB.getConnection();
try { try {
const eventsQuery = 'UPDATE events SET name = ?, description = ?, start_datetime = ?, end_datetime = ?, location = ?, created_by = ?, url = ? WHERE event_id = ?'; const eventsQuery = 'UPDATE events SET name = ?, description = ?, start_datetime = ?, end_datetime = ?, location = ?, created_by = ?, url = ? WHERE event_id = ?';
const eventsRes = await conn.execute(eventsQuery, [event.name, event.description, event.start_datetime, event.end_datetime, event.location, event.created_by, event.url, event.event_id]); const eventsRes = await conn.execute(eventsQuery, [event.name, event.description, event.startDateTime, event.endDateTime, event.location, event.createdBy, event.url, event.eventId]);
return eventsRes.affectedRows === 1; return eventsRes.affectedRows;
} catch (err) { } catch (err) {
await conn.rollback(); await conn.rollback();
throw err; throw err;
@ -81,7 +93,7 @@ export const deleteEvent = async (event: Event): Promise<boolean> => {
let conn = await NachklangCalendarDB.getConnection(); let conn = await NachklangCalendarDB.getConnection();
try { try {
const eventsQuery = 'DELETE FROM events WHERE event_id = ?'; const eventsQuery = 'DELETE FROM events WHERE event_id = ?';
const eventsRes = await conn.execute(eventsQuery, [event.event_id]); const eventsRes = await conn.execute(eventsQuery, [event.eventId]);
return eventsRes.affectedRows === 1; return eventsRes.affectedRows === 1;
} catch (err) { } catch (err) {

View File

@ -85,10 +85,10 @@ const createIcalEvent = (event: Event): iCalEvent => {
return { return {
header: 'BEGIN:VEVENT\n', header: 'BEGIN:VEVENT\n',
uid: event.uuid + '\n', uid: event.uuid + '\n',
created: formatDate(event.created_date) + 'Z\n', created: formatDate(event.createdDate) + 'Z\n',
organizer: event.created_by + '\n', organizer: event.createdBy + '\n',
start: formatDate(event.start_datetime) + '\n', start: formatDate(event.startDateTime) + '\n',
end: formatDate(event.end_datetime) + '\n', end: formatDate(event.endDateTime) + '\n',
summary: event.name + '\n', summary: event.name + '\n',
description: event.description + '\n', description: event.description + '\n',
location: event.location + '\n', location: event.location + '\n',