Compare commits
2 Commits
b8a68c2480
...
76e6bbdbbf
Author | SHA1 | Date | |
---|---|---|---|
76e6bbdbbf | |||
5e84eaea70 |
|
@ -9,7 +9,8 @@ export interface Event {
|
||||||
createdDate: Date;
|
createdDate: Date;
|
||||||
location: string;
|
location: string;
|
||||||
createdBy?: string;
|
createdBy?: string;
|
||||||
createdById: number,
|
createdById: number;
|
||||||
url: string;
|
url: string;
|
||||||
wholeDay: boolean;
|
wholeDay: boolean;
|
||||||
|
status?: string;
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,13 +53,22 @@ eventsRouter.get('/:calendar/json', async (req: Request, res: Response) => {
|
||||||
|
|
||||||
let calendarId: number = calendarNames.get(calendarName)!.id;
|
let calendarId: number = calendarNames.get(calendarName)!.id;
|
||||||
|
|
||||||
|
let user = await UserService.checkSession(sessionId, sessionKey, ip);
|
||||||
|
|
||||||
|
if(user === null || !user.isActive) {
|
||||||
if (! await CredentialService.hasAccess(calendarName, sessionId, sessionKey, password, ip)) {
|
if (! await CredentialService.hasAccess(calendarName, sessionId, sessionKey, password, ip)) {
|
||||||
res.status(403).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;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Get events
|
let events: Event[];
|
||||||
let events = await EventService.getAllEvents(calendarId);
|
|
||||||
|
if(user.isActive) {
|
||||||
|
events = await EventService.getAllEventsAdmin(calendarId);
|
||||||
|
} else {
|
||||||
|
events = await EventService.getAllEvents(calendarId);
|
||||||
|
}
|
||||||
|
|
||||||
// Send the events back
|
// Send the events back
|
||||||
res.status(200).send(events);
|
res.status(200).send(events);
|
||||||
|
@ -152,7 +161,8 @@ eventsRouter.post('/', async (req: Request, res: Response) => {
|
||||||
location: req.body.location ?? '',
|
location: req.body.location ?? '',
|
||||||
createdById: user.userId ?? -1,
|
createdById: user.userId ?? -1,
|
||||||
url: req.body.url ?? '',
|
url: req.body.url ?? '',
|
||||||
wholeDay: req.body.wholeDay ?? false
|
wholeDay: req.body.wholeDay ?? false,
|
||||||
|
status: req.body.status ?? 'PUBLIC'
|
||||||
};
|
};
|
||||||
|
|
||||||
let eventId = await EventService.createEvent(event);
|
let eventId = await EventService.createEvent(event);
|
||||||
|
@ -210,7 +220,8 @@ eventsRouter.put('/:eventId', async (req: Request, res: Response) => {
|
||||||
createdBy: req.body.createdBy ?? '',
|
createdBy: req.body.createdBy ?? '',
|
||||||
createdById: user.userId ?? -1,
|
createdById: user.userId ?? -1,
|
||||||
url: req.body.url ?? '',
|
url: req.body.url ?? '',
|
||||||
wholeDay: req.body.wholeDay ?? false
|
wholeDay: req.body.wholeDay ?? false,
|
||||||
|
status: req.body.status ?? 'PUBLIC'
|
||||||
};
|
};
|
||||||
|
|
||||||
let successRows = await EventService.updateEvent(event);
|
let successRows = await EventService.updateEvent(event);
|
||||||
|
@ -265,9 +276,10 @@ eventsRouter.delete('/:eventId', async (req: Request, res: Response) => {
|
||||||
createdDate: new Date(),
|
createdDate: new Date(),
|
||||||
location: '',
|
location: '',
|
||||||
createdBy: '',
|
createdBy: '',
|
||||||
createdById: -1,
|
createdById: user.userId ?? -1,
|
||||||
url: '',
|
url: '',
|
||||||
wholeDay: false
|
wholeDay: false,
|
||||||
|
status: 'DELETED'
|
||||||
};
|
};
|
||||||
|
|
||||||
let success = await EventService.deleteEvent(event);
|
let success = await EventService.deleteEvent(event);
|
||||||
|
|
|
@ -14,7 +14,19 @@ export const getAllEvents = async (calendarId: number): Promise<Event[]> => {
|
||||||
let conn = await NachklangCalendarDB.getConnection();
|
let conn = await NachklangCalendarDB.getConnection();
|
||||||
let eventRows: Event[] = [];
|
let eventRows: Event[] = [];
|
||||||
try {
|
try {
|
||||||
const eventsQuery = 'SELECT e.*, u.full_name FROM events e LEFT OUTER JOIN users u ON e.created_by_id = u.user_id WHERE calendar_id = ? AND status=\'PUBLIC\'';
|
const eventsQuery = `
|
||||||
|
SELECT e.calendar_id, u.full_name, v.* FROM events e
|
||||||
|
INNER JOIN (
|
||||||
|
SELECT event_id, MAX(event_version_id) AS latest_version
|
||||||
|
FROM event_versions
|
||||||
|
GROUP BY event_id
|
||||||
|
) latest_versions
|
||||||
|
ON e.event_id = latest_versions.event_id
|
||||||
|
INNER JOIN event_versions v
|
||||||
|
ON v.event_id = latest_versions.event_id AND v.event_version_id = latest_versions.latest_version
|
||||||
|
LEFT OUTER JOIN users u ON u.user_id = e.created_by_id
|
||||||
|
WHERE e.calendar_id = ? AND v.status = 'PUBLIC'
|
||||||
|
ORDER BY e.event_id`;
|
||||||
const eventsRes = await conn.query(eventsQuery, calendarId);
|
const eventsRes = await conn.query(eventsQuery, calendarId);
|
||||||
|
|
||||||
for (let row of eventsRes) {
|
for (let row of eventsRes) {
|
||||||
|
@ -44,6 +56,53 @@ export const getAllEvents = async (calendarId: number): Promise<Event[]> => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const getAllEventsAdmin = async (calendarId: number): Promise<Event[]> => {
|
||||||
|
let conn = await NachklangCalendarDB.getConnection();
|
||||||
|
let eventRows: Event[] = [];
|
||||||
|
try {
|
||||||
|
const eventsQuery = `
|
||||||
|
SELECT e.calendar_id, u.full_name, v.* FROM events e
|
||||||
|
INNER JOIN (
|
||||||
|
SELECT event_id, MAX(event_version_id) AS latest_version
|
||||||
|
FROM event_versions
|
||||||
|
GROUP BY event_id
|
||||||
|
) latest_versions
|
||||||
|
ON e.event_id = latest_versions.event_id
|
||||||
|
INNER JOIN event_versions v
|
||||||
|
ON v.event_id = latest_versions.event_id AND v.event_version_id = latest_versions.latest_version
|
||||||
|
LEFT OUTER JOIN users u ON u.user_id = e.created_by_id
|
||||||
|
WHERE e.calendar_id = ?
|
||||||
|
ORDER BY e.event_id`;
|
||||||
|
const eventsRes = await conn.query(eventsQuery, calendarId);
|
||||||
|
|
||||||
|
for (let row of eventsRes) {
|
||||||
|
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.full_name,
|
||||||
|
createdById: row.created_by_id,
|
||||||
|
url: row.url,
|
||||||
|
wholeDay: row.whole_day,
|
||||||
|
status: row.status
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return eventRows;
|
||||||
|
} catch (err) {
|
||||||
|
throw err;
|
||||||
|
} finally {
|
||||||
|
// Return connection
|
||||||
|
await conn.end();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create the given event in the database
|
* Create the given event in the database
|
||||||
* @param event The event to create
|
* @param event The event to create
|
||||||
|
@ -52,8 +111,13 @@ export const createEvent = async (event: Event): Promise<number> => {
|
||||||
let conn = await NachklangCalendarDB.getConnection();
|
let conn = await NachklangCalendarDB.getConnection();
|
||||||
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_id, url, whole_day, status) VALUES (?,?,?,?,?,?,?,?,?,?, \'PUBLIC\') RETURNING event_id';
|
const eventsQuery = 'INSERT INTO events (calendar_id, uuid, created_by_id) VALUES (?,?,?) RETURNING event_id';
|
||||||
const eventsRes = await conn.execute(eventsQuery, [event.calendarId, eventUUID, event.name, event.description, event.startDateTime, event.endDateTime, event.location, event.createdById, event.url, event.wholeDay]);
|
const eventsRes = await conn.execute(eventsQuery, [event.calendarId, eventUUID, event.createdById]);
|
||||||
|
|
||||||
|
const versionQuery = 'INSERT INTO event_versions (event_id, name, description, start_datetime, end_datetime, whole_day, location, url, status, version_created_by_id) VALUES (?,?,?,?,?,?,?,?,?,?);'
|
||||||
|
const versionRes = await conn.execute(versionQuery, [eventsRes[0].event_id, event.name, event.description, event.startDateTime, event.endDateTime, event.wholeDay, event.location, event.url, event.status, event.createdById]);
|
||||||
|
|
||||||
|
await conn.commit();
|
||||||
|
|
||||||
return eventsRes[0].event_id;
|
return eventsRes[0].event_id;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
@ -73,10 +137,12 @@ export const createEvent = async (event: Event): Promise<number> => {
|
||||||
export const updateEvent = async (event: Event): Promise<number> => {
|
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_id = ?, url = ?, whole_day = ? WHERE event_id = ?';
|
const versionQuery = 'INSERT INTO event_versions (event_id, name, description, start_datetime, end_datetime, whole_day, location, url, status, version_created_by_id) VALUES (?,?,?,?,?,?,?,?,?,?);'
|
||||||
const eventsRes = await conn.execute(eventsQuery, [event.name, event.description, event.startDateTime, event.endDateTime, event.location, event.createdById, event.url, event.wholeDay, event.eventId]);
|
const versionRes = await conn.execute(versionQuery, [event.eventId, event.name, event.description, event.startDateTime, event.endDateTime, event.wholeDay, event.location, event.url, event.status, event.createdById]);
|
||||||
|
|
||||||
return eventsRes.affectedRows;
|
await conn.commit();
|
||||||
|
|
||||||
|
return versionRes.affectedRows;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
await conn.rollback();
|
await conn.rollback();
|
||||||
throw err;
|
throw err;
|
||||||
|
@ -94,10 +160,12 @@ export const updateEvent = async (event: Event): Promise<number> => {
|
||||||
export const deleteEvent = async (event: Event): Promise<boolean> => {
|
export const deleteEvent = async (event: Event): Promise<boolean> => {
|
||||||
let conn = await NachklangCalendarDB.getConnection();
|
let conn = await NachklangCalendarDB.getConnection();
|
||||||
try {
|
try {
|
||||||
const eventsQuery = 'UPDATE events SET status=\'DELETED\' WHERE event_id = ?';
|
const versionQuery = 'INSERT INTO event_versions (event_id, status, version_created_by_id) VALUES (?,?,?);'
|
||||||
const eventsRes = await conn.execute(eventsQuery, [event.eventId]);
|
const versionRes = await conn.execute(versionQuery, [event.eventId, 'DELETED', event.createdById]);
|
||||||
|
|
||||||
return eventsRes.affectedRows === 1;
|
await conn.commit();
|
||||||
|
|
||||||
|
return versionRes.affectedRows === 1;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
await conn.rollback();
|
await conn.rollback();
|
||||||
throw err;
|
throw err;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user