From 93ec39bedfe704cf6c26ba72078b108c7498248a Mon Sep 17 00:00:00 2001 From: Christian Gawron Date: Thu, 17 Oct 2024 15:15:15 +0200 Subject: [PATCH] feat: new rest api - use regular REST routes --- backend/package.json | 1 + backend/src/controller/event_controller.ts | 12 +++--- backend/src/controller/user_controller.ts | 2 +- client/cypress/fixtures/calendarList.json | 6 --- client/cypress/fixtures/user.json | 5 +-- client/src/helpers/services/event_services.ts | 10 ++--- client/src/helpers/services/user_services.ts | 2 +- yarn.lock | 38 +++++++++++++++++++ 8 files changed, 53 insertions(+), 23 deletions(-) diff --git a/backend/package.json b/backend/package.json index 7f23078d..66d29778 100644 --- a/backend/package.json +++ b/backend/package.json @@ -51,6 +51,7 @@ "nodemailer": "^6.4.14", "remark": "15.0.1", "remark-html": "16.0.1", + "tsdav": "^2.1.2", "validator": "^13.12.0" }, "devDependencies": { diff --git a/backend/src/controller/event_controller.ts b/backend/src/controller/event_controller.ts index 9b2b0ee4..8ab755e6 100644 --- a/backend/src/controller/event_controller.ts +++ b/backend/src/controller/event_controller.ts @@ -71,7 +71,7 @@ export const getAvailableTimes = (req: Request, res: Response): void => { }); function calculateBlocked(events, event, timeMin, timeMax) { - const days = {}; + const eventsPerDay = {}; const blocked = new IntervalSet([{ start: timeMin, end: timeMin }, { start: timeMax, end: timeMax }]); events.forEach(evt => { console.log('event: %o', evt); @@ -79,14 +79,14 @@ export const getAvailableTimes = (req: Request, res: Response): void => { return; } const day = startOfDay(new Date(evt.start.dateTime)).toISOString(); - if (day in days) { - days[day] += 1; + if (day in eventsPerDay) { + eventsPerDay[day] += 1; } else { - days[day] = 1; + eventsPerDay[day] = 1; } }); - for (const day in days) { - if (days[day] >= event.maxPerDay) { + for (const day in eventsPerDay) { + if (eventsPerDay[day] >= event.maxPerDay) { blocked.addRange({ start: new Date(day), end: addDays(new Date(day), 1) }); } } diff --git a/backend/src/controller/user_controller.ts b/backend/src/controller/user_controller.ts index 69af69be..f8903daa 100644 --- a/backend/src/controller/user_controller.ts +++ b/backend/src/controller/user_controller.ts @@ -78,7 +78,7 @@ export const putUser = (req: Request, res: Response): void => { * @param {response} res */ export const getUserByUrl = (req: Request, res: Response): void => { - const user_url = req.query.url; + const user_url = req.params.url || req.query.url; UserModel.findOne({ user_url: user_url }) .select("_id email name picture_url user_url welcome") .exec() diff --git a/client/cypress/fixtures/calendarList.json b/client/cypress/fixtures/calendarList.json index 5eeea483..112cea11 100644 --- a/client/cypress/fixtures/calendarList.json +++ b/client/cypress/fixtures/calendarList.json @@ -9,12 +9,6 @@ "comment": "gzip" } ], - "headers": { - "x-goog-api-client": "gdcl/7.0.1 gl-node/18.20.4", - "Accept-Encoding": "gzip", - "User-Agent": "google-api-nodejs-client/7.0.1 (gzip)", - "Authorization": "Bearer ya29.a0AcM612xf6HxDLrtzihWDcobYv77tDABBNZNwcu5XLwk5BzBXHZu-Ye4S0YxrCSt83JWC8FccCnXK_1iTBWGoIH3UKdto0T-gYMFl0fp5_JzAd5kLMuiAARQSe0a0-y7Jyt8CPMmPVTxxwaI5oFoVATPVTt6puxwXQsc7g-e9TwaCgYKAcwSARMSFQHGX2Mis5a6O2i8pMPSovpLdBf3Pw0177" - }, "params": {}, "retry": true, "responseType": "unknown" diff --git a/client/cypress/fixtures/user.json b/client/cypress/fixtures/user.json index 4a557b24..5ebfcd01 100644 --- a/client/cypress/fixtures/user.json +++ b/client/cypress/fixtures/user.json @@ -13,8 +13,5 @@ ], "updatedAt": "2024-10-05T15:09:00.077Z", "user_url": "christian-gawron", - "push_calendar": "t6hldgptmgr36ctkm1720bvcfg@group.calendar.google.com", - "google_tokens": { - "access_token": "ya29.a0AcM612xTO2HcmaSxOzyMlf3hje-CL8WGmBrfUtM_mrOTWOfj6gbFgFtr6jSRI4GGmwK9fCS4paZroX9Wq3JPt-Q0oC0Hbx2WUSpufG2gU98Y2ZkK0TcVNhi72vFkr60vU7ROpi55csAHmgQM8onpGfy51x8iGdZDdH1KSBg1aCgYKAZcSARMSFQHGX2Mi0IhuHlo5kYzESZHe0G5l-w0175" - } + "push_calendar": "t6hldgptmgr36ctkm1720bvcfg@group.calendar.google.com" } diff --git a/client/src/helpers/services/event_services.ts b/client/src/helpers/services/event_services.ts index 12ddc88e..c7dfd357 100644 --- a/client/src/helpers/services/event_services.ts +++ b/client/src/helpers/services/event_services.ts @@ -6,7 +6,7 @@ export async function saveUserEvent( userid: string ) { const response = await axios.post( - `${import.meta.env.REACT_APP_API_URL}/events/addEvent`, + `${import.meta.env.REACT_APP_API_URL}/events/event`, { ...event, user: userid }, { withCredentials: true, @@ -17,7 +17,7 @@ export async function saveUserEvent( export async function deleteEvent(id: string) { const response = await axios.delete( - `${import.meta.env.REACT_APP_API_URL}/events/deleteEvent/${id}`, + `${import.meta.env.REACT_APP_API_URL}/events/event/${id}`, { withCredentials: true, } @@ -28,7 +28,7 @@ export async function deleteEvent(id: string) { export async function getEventByID(id: string) { const response = await axios.get( - `${import.meta.env.REACT_APP_API_URL}/events/getEvent/${id}`, + `${import.meta.env.REACT_APP_API_URL}/events/event/${id}`, { withCredentials: true, } @@ -43,7 +43,7 @@ export async function updateEvent( ) { console.log('available: %o', event.available); const response = await axios.put( - `${import.meta.env.REACT_APP_API_URL}/events/updateEvent/${id}`, + `${import.meta.env.REACT_APP_API_URL}/events/event/${id}`, { data: event }, @@ -81,7 +81,7 @@ export function getAvailableTimes(timeMin: Date, timeMax: Date, url: string, use export async function getUsersEvents() { const response = await axios.get( - `${import.meta.env.REACT_APP_API_URL}/events/getEvents`, + `${import.meta.env.REACT_APP_API_URL}/events/event`, { withCredentials: true, } diff --git a/client/src/helpers/services/user_services.ts b/client/src/helpers/services/user_services.ts index 5af49efe..08644874 100644 --- a/client/src/helpers/services/user_services.ts +++ b/client/src/helpers/services/user_services.ts @@ -23,7 +23,7 @@ export async function updateUser(user: any) { export async function getUserByUrl(url: string) { return axios.get( - `${import.meta.env.REACT_APP_API_URL}/users/findUserByUrl`, + `${import.meta.env.REACT_APP_API_URL}/users/user/${url}`, { params: { url: url }, } diff --git a/yarn.lock b/yarn.lock index 1283e8e0..6d299a62 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6935,6 +6935,7 @@ __metadata: supertest: "npm:^7.0.0" ts-jest: "npm:^29.2.5" ts-node: "npm:^10.9.1" + tsdav: "npm:^2.1.2" typescript: "npm:^5.6.2" validator: "npm:^13.12.0" vitest: "npm:^2.1.3" @@ -6962,6 +6963,13 @@ __metadata: languageName: node linkType: hard +"base-64@npm:1.0.0": + version: 1.0.0 + resolution: "base-64@npm:1.0.0" + checksum: 10/d10b64a1fc9b2c5a5f39f1ce1e6c9d1c5b249222bbfa3a0604c592d90623caf74419983feadd8a170f27dc0c3389704f72faafa3e645aeb56bfc030c93ff074a + languageName: node + linkType: hard + "base64-js@npm:^1.3.0, base64-js@npm:^1.3.1": version: 1.5.1 resolution: "base64-js@npm:1.5.1" @@ -19515,6 +19523,13 @@ __metadata: languageName: node linkType: hard +"sax@npm:^1.2.4": + version: 1.4.1 + resolution: "sax@npm:1.4.1" + checksum: 10/b1c784b545019187b53a0c28edb4f6314951c971e2963a69739c6ce222bfbc767e54d320e689352daba79b7d5e06d22b5d7113b99336219d6e93718e2f99d335 + languageName: node + linkType: hard + "scheduler@npm:^0.23.2": version: 0.23.2 resolution: "scheduler@npm:0.23.2" @@ -21178,6 +21193,18 @@ __metadata: languageName: node linkType: hard +"tsdav@npm:^2.1.2": + version: 2.1.2 + resolution: "tsdav@npm:2.1.2" + dependencies: + base-64: "npm:1.0.0" + cross-fetch: "npm:4.0.0" + debug: "npm:4.3.7" + xml-js: "npm:1.6.11" + checksum: 10/133b4a0ae3fea51034a2aa63d01dae1dec317102e4d12b87944e7a8e5a8ea6fe6cc3722949d6a4eff9fe147df4960975e8ee57772a5b6d4bd9530abe99089e96 + languageName: node + linkType: hard + "tslib@npm:^1.8.1, tslib@npm:^1.9.0, tslib@npm:^1.9.3": version: 1.14.1 resolution: "tslib@npm:1.14.1" @@ -22577,6 +22604,17 @@ __metadata: languageName: node linkType: hard +"xml-js@npm:1.6.11": + version: 1.6.11 + resolution: "xml-js@npm:1.6.11" + dependencies: + sax: "npm:^1.2.4" + bin: + xml-js: ./bin/cli.js + checksum: 10/55ce342a47bf14a138a3fcea0c9e325b81484cfc1a8aac78df13b4d6ca01f20e32820572bc3e927cd9b61b9da9cdee4657cb2f304e460343d8d85d6a3659d749 + languageName: node + linkType: hard + "xmlcreate@npm:^2.0.4": version: 2.0.4 resolution: "xmlcreate@npm:2.0.4"