From 7e184a1364fd6a5d0524237aeb9c9a7eab7c8778 Mon Sep 17 00:00:00 2001 From: michellelin1 <66575725+michellelin1@users.noreply.github.com> Date: Fri, 29 Mar 2024 10:09:43 -0700 Subject: [PATCH] Day planner flow (#63) * fixed calculateYear + auto confirmedOn * updated route to use dayId instead of date * retrieve description from /season query * retrieve host from /dayid route * fixed keysToCamel * sorted events in day * retrieve more event info from PS * retrieving more data in route * fixed season cut off * change catalog put route * modified day post to check if date already exists --------- Co-authored-by: ThatMegamind --- common/utils.js | 37 +++++++++++---------- routes/catalog.js | 66 ++++++++++++++++++++++--------------- routes/day.js | 9 +++++ routes/publishedSchedule.js | 46 +++++++++++++++++--------- server/schema/catalog.sql | 2 +- 5 files changed, 99 insertions(+), 61 deletions(-) diff --git a/common/utils.js b/common/utils.js index ba67f5e..2ba4fef 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 []; }; @@ -67,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); }); @@ -78,24 +84,21 @@ 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; }; 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/catalog.js b/routes/catalog.js index 1b53de5..3552435 100644 --- a/routes/catalog.js +++ b/routes/catalog.js @@ -77,10 +77,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); } @@ -112,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/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 ( diff --git a/routes/publishedSchedule.js b/routes/publishedSchedule.js index 2e85ecb..30aa1af 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( @@ -171,6 +164,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 +194,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, @@ -223,9 +218,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 @@ -244,6 +240,7 @@ publishedScheduleRouter.get('/date', 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, @@ -255,12 +252,23 @@ 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], ); + + 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); @@ -276,8 +284,14 @@ 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, PS.start_time, @@ -303,7 +317,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 +356,7 @@ publishedScheduleRouter.post('/', async (req, res) => { eventId, dayId, confirmed, - confirmedOn, + new Date(), startTime, endTime, calculateYear(eventDate, cohort), diff --git a/server/schema/catalog.sql b/server/schema/catalog.sql index 10a0185..31b97a4 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 (