From c618aca446c9579f2c1cdca891354241e1be4163 Mon Sep 17 00:00:00 2001 From: michellelin1 Date: Sun, 24 Mar 2024 16:14:33 -0700 Subject: [PATCH 01/11] fixed calculateYear + auto confirmedOn --- common/utils.js | 15 ++++++++++----- routes/publishedSchedule.js | 4 ++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/common/utils.js b/common/utils.js index ba67f5e..88984a9 100644 --- a/common/utils.js +++ b/common/utils.js @@ -27,28 +27,33 @@ const isInteger = (value) => { // dependency for publishedSchedule.js const calculateYear = (eventDate, gradeLevel) => { - if (gradeLevel && gradeLevel.length) { - const currentDay = new Date(eventDate); + const currentDay = new Date(eventDate); + if (gradeLevel && gradeLevel.length === 1) { // console.log('current day', currentDay.getFullYear() + (currentDay.getMonth() >= 7 ? 2 : 1)); - if (gradeLevel.toLowerCase() === 'junior') { + if (gradeLevel[0].toLowerCase() === 'junior') { // if the current month is august or later // then junior will be current year + 2 // otherwise junior will be current year + 1 // months are zero indexed return [(currentDay.getFullYear() + (currentDay.getMonth() >= 7 ? 2 : 1)).toString(10)]; } - if (gradeLevel.toLowerCase() === 'senior') { + if (gradeLevel[0].toLowerCase() === 'senior') { // if the current month is august or later // then senior will be current year + 1 // otherwise senior will be current year return [(currentDay.getFullYear() + (currentDay.getMonth() >= 7 ? 1 : 0)).toString(10)]; } - if (gradeLevel.toLowerCase() === 'both') { + if (gradeLevel[0].toLowerCase() === 'both') { return [ (currentDay.getFullYear() + (currentDay.getMonth() >= 7 ? 1 : 0)).toString(10), (currentDay.getFullYear() + (currentDay.getMonth() >= 7 ? 2 : 1)).toString(10), ]; } + } else if (gradeLevel && gradeLevel.length > 1) { + return [ + (currentDay.getFullYear() + (currentDay.getMonth() >= 7 ? 1 : 0)).toString(10), + (currentDay.getFullYear() + (currentDay.getMonth() >= 7 ? 2 : 1)).toString(10), + ]; } return []; }; diff --git a/routes/publishedSchedule.js b/routes/publishedSchedule.js index 2e85ecb..b5f17fa 100644 --- a/routes/publishedSchedule.js +++ b/routes/publishedSchedule.js @@ -303,7 +303,7 @@ publishedScheduleRouter.get('/:id', async (req, res) => { // as that is how we are able to calculate the cohort from the event date publishedScheduleRouter.post('/', async (req, res) => { const currDate = new Date(); - const { eventId, dayId, confirmed, confirmedOn, startTime, endTime, cohort, notes } = req.body; + const { eventId, dayId, confirmed, startTime, endTime, cohort, notes } = req.body; try { const dayResult = await db.query( `UPDATE day SET day_count = day_count + 1 WHERE id = $1 RETURNING *;`, @@ -342,7 +342,7 @@ publishedScheduleRouter.post('/', async (req, res) => { eventId, dayId, confirmed, - confirmedOn, + new Date(), startTime, endTime, calculateYear(eventDate, cohort), From 50cd72e2a431b485e53a3553320d5b56669f4549 Mon Sep 17 00:00:00 2001 From: ThatMegamind Date: Mon, 25 Mar 2024 10:14:30 -0700 Subject: [PATCH 02/11] updated route to use dayId instead of date --- routes/publishedSchedule.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/routes/publishedSchedule.js b/routes/publishedSchedule.js index b5f17fa..b90455c 100644 --- a/routes/publishedSchedule.js +++ b/routes/publishedSchedule.js @@ -223,9 +223,10 @@ publishedScheduleRouter.get('/season', async (req, res) => { }); // GET /published-schedule/date - returns all events occurring on a specific date -publishedScheduleRouter.get('/date', async (req, res) => { +publishedScheduleRouter.get('/dayId', async (req, res) => { try { - const { date } = req.query; + const { dayId } = req.query; + const seasonResult = await db.query( ` SELECT @@ -255,12 +256,13 @@ publishedScheduleRouter.get('/date', async (req, res) => { FROM day D LEFT JOIN published_schedule PS ON PS.day_id = D.id LEFT JOIN catalog C ON PS.event_id = C.id - WHERE D.event_date = $1::date + WHERE D.id = $1 GROUP BY d.event_date, d.id ORDER BY d.event_date; `, - [date], + [dayId], ); + // console.log(seasonResult[0]); res.status(200).json(keysToCamel(seasonResult)[0]); } catch (err) { res.status(500).send(err.message); From a70e69f48be616b5d4e63b4f0cf476985863b1c6 Mon Sep 17 00:00:00 2001 From: ThatMegamind Date: Mon, 25 Mar 2024 14:07:58 -0700 Subject: [PATCH 03/11] retrieve description from /season query --- routes/publishedSchedule.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/routes/publishedSchedule.js b/routes/publishedSchedule.js index b90455c..d198196 100644 --- a/routes/publishedSchedule.js +++ b/routes/publishedSchedule.js @@ -171,6 +171,7 @@ publishedScheduleRouter.get('/season', async (req, res) => { C.title, C.event_type, C.year, + C.description, PS.start_time, PS.end_time, PS.confirmed, @@ -200,6 +201,7 @@ publishedScheduleRouter.get('/season', async (req, res) => { 'id', seasonPS.id, 'event_id', seasonPS.event_id, 'title', seasonPS.title, + 'description', seasonPS.description, 'event_type', seasonPS.event_type, 'year', seasonPS.year, 'start_time', seasonPS.start_time, From fc44fd2ea6dad3f4585d1d3bdd96bc110427046c Mon Sep 17 00:00:00 2001 From: ThatMegamind Date: Mon, 25 Mar 2024 15:24:02 -0700 Subject: [PATCH 04/11] retrieve host from /dayid route --- routes/publishedSchedule.js | 1 + 1 file changed, 1 insertion(+) diff --git a/routes/publishedSchedule.js b/routes/publishedSchedule.js index d198196..1e04505 100644 --- a/routes/publishedSchedule.js +++ b/routes/publishedSchedule.js @@ -247,6 +247,7 @@ publishedScheduleRouter.get('/dayId', async (req, res) => { 'title', C.title, 'event_type', C.event_type, 'year', C.year, + 'host', C.host, 'start_time', PS.start_time, 'end_time', PS.end_time, 'confirmed', PS.confirmed, From a4eb45a4e5c11ff3a6b7f3e2c5f4f259ae975d15 Mon Sep 17 00:00:00 2001 From: michellelin1 Date: Tue, 26 Mar 2024 00:00:08 -0700 Subject: [PATCH 05/11] fixed keysToCamel --- common/utils.js | 12 ++++++++---- routes/catalog.js | 6 ++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/common/utils.js b/common/utils.js index 88984a9..7eec2d9 100644 --- a/common/utils.js +++ b/common/utils.js @@ -72,7 +72,8 @@ const keysToCamel = (data) => { }); return newData; } - if (isArray(data)) { + if (isArray(data) && data.length) { + // console.log(data) return data.map((i) => { return keysToCamel(i); }); @@ -83,9 +84,12 @@ const keysToCamel = (data) => { data[0] === '{' && data[data.length - 1] === '}' ) { - let parsedList = data.replaceAll('"', ''); - parsedList = parsedList.slice(1, parsedList.length - 1).split(','); - return parsedList; + if (data.length > 2) { + let parsedList = data.replaceAll('"', ''); + parsedList = parsedList.slice(1, parsedList.length - 1).split(','); + return parsedList; + } + return []; } return data; }; diff --git a/routes/catalog.js b/routes/catalog.js index e286012..8802a68 100644 --- a/routes/catalog.js +++ b/routes/catalog.js @@ -85,10 +85,8 @@ catalogRouter.get('/', async (req, res) => { catalogRouter.get('/:id', async (req, res) => { try { const { id } = req.params; - const allUsers = await db.query(`SELECT * FROM catalog WHERE id = $1 AND hidden = false;`, [ - id, - ]); - res.status(200).json(keysToCamel(allUsers)); + const response = await db.query(`SELECT * FROM catalog WHERE id = $1;`, [id]); + res.status(200).json(keysToCamel(response)); } catch (err) { res.status(500).send(err.message); } From 49f6ad890b89ed636ddb23debf24f6fcb8d73b9d Mon Sep 17 00:00:00 2001 From: michellelin1 Date: Tue, 26 Mar 2024 18:33:38 -0700 Subject: [PATCH 06/11] sorted events in day --- routes/publishedSchedule.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/routes/publishedSchedule.js b/routes/publishedSchedule.js index 1e04505..7ed975a 100644 --- a/routes/publishedSchedule.js +++ b/routes/publishedSchedule.js @@ -265,7 +265,17 @@ publishedScheduleRouter.get('/dayId', async (req, res) => { `, [dayId], ); - // console.log(seasonResult[0]); + + seasonResult[0].data.sort((a, b) => { + if (a.start_time < b.start_time) { + return -1; + } + if (a.start_time > b.start_time) { + return 1; + } + return 0; + }); + res.status(200).json(keysToCamel(seasonResult)[0]); } catch (err) { res.status(500).send(err.message); From ca8e25d3945e41ff7fbcf9da84d5525aafd7c6ee Mon Sep 17 00:00:00 2001 From: michellelin1 Date: Wed, 27 Mar 2024 01:27:44 -0700 Subject: [PATCH 07/11] retrieve more event info from PS --- routes/publishedSchedule.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/routes/publishedSchedule.js b/routes/publishedSchedule.js index 7ed975a..3e9d40d 100644 --- a/routes/publishedSchedule.js +++ b/routes/publishedSchedule.js @@ -293,6 +293,10 @@ publishedScheduleRouter.get('/:id', async (req, res) => { PS.day_id, C.host, C.title, + C.year, + C.season, + C.event_type, + C.subject, PS.confirmed, PS.confirmed_on, PS.start_time, From 9204d0b7ee559f0b6cc820e7a4a2fa7e73aeb857 Mon Sep 17 00:00:00 2001 From: ThatMegamind Date: Wed, 27 Mar 2024 15:27:46 -0700 Subject: [PATCH 08/11] retrieving more data in route --- routes/publishedSchedule.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/routes/publishedSchedule.js b/routes/publishedSchedule.js index 1e04505..56e1fd3 100644 --- a/routes/publishedSchedule.js +++ b/routes/publishedSchedule.js @@ -283,6 +283,10 @@ publishedScheduleRouter.get('/:id', async (req, res) => { PS.day_id, C.host, C.title, + C.event_type, + C.season, + C.subject, + C.description, PS.confirmed, PS.confirmed_on, PS.start_time, From da2e354675092871cc6a8e928f0178b45a305d06 Mon Sep 17 00:00:00 2001 From: michellelin1 Date: Wed, 27 Mar 2024 21:46:03 -0700 Subject: [PATCH 09/11] fixed season cut off --- common/utils.js | 10 ++-------- routes/publishedSchedule.js | 13 +++---------- server/schema/catalog.sql | 2 +- 3 files changed, 6 insertions(+), 19 deletions(-) diff --git a/common/utils.js b/common/utils.js index 7eec2d9..2ba4fef 100644 --- a/common/utils.js +++ b/common/utils.js @@ -95,16 +95,10 @@ const keysToCamel = (data) => { }; const getSeasonFromMonthAndYear = (month, year) => { - if (month === 11) { - return `Winter ${year + 1}`; - } - if (month === 0 || month === 1) { - return `Winter ${year}`; - } // spring // march-may -> winter [year] - if (month >= 2 && month <= 4) { - return `Winter ${year}`; + if (month >= 0 && month <= 4) { + return `Spring ${year}`; } // summer // june-august -> summer [year] diff --git a/routes/publishedSchedule.js b/routes/publishedSchedule.js index 91117ed..1e090ce 100644 --- a/routes/publishedSchedule.js +++ b/routes/publishedSchedule.js @@ -136,22 +136,15 @@ publishedScheduleRouter.get('/season', async (req, res) => { const { season, year } = req.query; // getting the intervals for each season - if (season.toLowerCase() === 'winter') { - startTime = `${year - 1}-12-01`; - if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) { - endTime = `${year}-02-29`; - } else { - endTime = `${year}-02-28`; - } - } else if (season.toLowerCase() === 'spring') { - startTime = `${year}-03-01`; + if (season.toLowerCase() === 'spring') { + startTime = `${year}-01-01`; endTime = `${year}-05-31`; } else if (season.toLowerCase() === 'summer') { startTime = `${year}-06-01`; endTime = `${year}-08-31`; } else { startTime = `${year}-09-01`; - endTime = `${year}-11-30`; + endTime = `${year}-12-31`; } const seasonResult = await db.query( diff --git a/server/schema/catalog.sql b/server/schema/catalog.sql index e32df3e..a2a19be 100644 --- a/server/schema/catalog.sql +++ b/server/schema/catalog.sql @@ -1,7 +1,7 @@ CREATE TYPE event AS ENUM ('guest speaker', 'study-trip', 'workshop', 'other'); CREATE TYPE subject AS ENUM ('life skills', 'science', 'technology', 'engineering', 'math', 'college readiness'); CREATE TYPE year AS ENUM ('junior', 'senior', 'both'); -CREATE TYPE season AS ENUM ('spring', 'summer', 'fall', 'winter'); +CREATE TYPE season AS ENUM ('spring', 'summer', 'fall'); DROP TABLE IF EXISTS catalog; CREATE TABLE catalog ( From 037f98100914df9a76d9765ecd94916a5ab0236b Mon Sep 17 00:00:00 2001 From: michellelin1 Date: Thu, 28 Mar 2024 17:28:02 -0700 Subject: [PATCH 10/11] change catalog put route --- routes/catalog.js | 60 +++++++++++++++++++++++-------------- routes/publishedSchedule.js | 2 ++ 2 files changed, 39 insertions(+), 23 deletions(-) diff --git a/routes/catalog.js b/routes/catalog.js index 4a97d75..3552435 100644 --- a/routes/catalog.js +++ b/routes/catalog.js @@ -110,30 +110,44 @@ catalogRouter.put('/:id', async (req, res) => { const { id } = req.params; const { host, title, eventType, subject, description, year, season } = req.body; - const updatedCatalog = await db.query( - `UPDATE catalog SET - ${host ? 'host = $(host), ' : ''} - ${title ? 'title = $(title),' : ''} - ${eventType ? 'event_type = $(eventType)::event[], ' : ''} - ${subject ? 'subject = $(subject)::subject[], ' : ''} - ${description ? 'description = $(description), ' : ''} - ${year ? 'year = $(year)::year[], ' : ''} - ${season ? 'season = $(season)::season[], ' : ''} - id = '${id}' - WHERE id = '${id}' + const { count } = ( + await db.query(`SELECT COUNT(*) FROM published_schedule WHERE event_id = $1;`, [id]) + )[0]; + + if (count === 1) { + const updatedCatalog = await db.query( + `UPDATE catalog SET + ${host ? 'host = $(host), ' : ''} + ${title ? 'title = $(title),' : ''} + ${eventType ? 'event_type = $(eventType)::event[], ' : ''} + ${subject ? 'subject = $(subject)::subject[], ' : ''} + ${description ? 'description = $(description), ' : ''} + ${year ? 'year = $(year)::year[], ' : ''} + ${season ? 'season = $(season)::season[], ' : ''} + id = '${id}' + WHERE id = '${id}' + RETURNING *;`, + { + host, + title, + eventType, + subject, + description, + year, + id, + season, + }, + ); + res.status(200).send(keysToCamel(updatedCatalog)); + } else { + const newCatalogEvent = await db.query( + `INSERT INTO catalog (id, host, title, event_type, subject, description, year, season, hidden) + VALUES (nextval('catalog_id_seq'), $1, $2, $3::event[], $4::subject[], $5, $6::year[], $7::season[], false) RETURNING *;`, - { - host, - title, - eventType, - subject, - description, - year, - id, - season, - }, - ); - res.status(200).send(keysToCamel(updatedCatalog)); + [host, title, eventType, subject, description, year, season], + ); + res.status(200).send(keysToCamel(newCatalogEvent)); + } } catch (err) { res.status(500).send(err.message); } diff --git a/routes/publishedSchedule.js b/routes/publishedSchedule.js index 1e090ce..30aa1af 100644 --- a/routes/publishedSchedule.js +++ b/routes/publishedSchedule.js @@ -284,11 +284,13 @@ publishedScheduleRouter.get('/:id', async (req, res) => { SELECT PS.id, PS.day_id, + PS.event_id, C.host, C.title, C.event_type, C.season, C.subject, + C.year, C.description, PS.confirmed, PS.confirmed_on, From 793ed3a434225409fa3fce7ea324c57e7b599d4d Mon Sep 17 00:00:00 2001 From: michellelin1 Date: Fri, 29 Mar 2024 09:46:30 -0700 Subject: [PATCH 11/11] modified day post to check if date already exists --- routes/day.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/routes/day.js b/routes/day.js index 388f8c1..faf7762 100644 --- a/routes/day.js +++ b/routes/day.js @@ -40,6 +40,15 @@ dayRouter.get('/:id', async (req, res) => { dayRouter.post('/', async (req, res) => { try { const { eventDate, location, notes } = req.body; + const inUse = await db.query(`SELECT * FROM day WHERE event_date = $1;`, [eventDate]); + if (inUse.length) { + res.status(201).json({ + status: 'Failed', + message: 'Day already exists', + }); + return; + } + const newDay = await db.query( ` INSERT INTO day (