Skip to content

Commit

Permalink
Day planner flow (#63)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
michellelin1 and ThatMegamind authored Mar 29, 2024
1 parent fc9ecfa commit 7e184a1
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 61 deletions.
37 changes: 20 additions & 17 deletions common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
};
Expand All @@ -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);
});
Expand All @@ -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]
Expand Down
66 changes: 39 additions & 27 deletions routes/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
9 changes: 9 additions & 0 deletions routes/day.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
46 changes: 30 additions & 16 deletions routes/publishedSchedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -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,
Expand All @@ -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 *;`,
Expand Down Expand Up @@ -342,7 +356,7 @@ publishedScheduleRouter.post('/', async (req, res) => {
eventId,
dayId,
confirmed,
confirmedOn,
new Date(),
startTime,
endTime,
calculateYear(eventDate, cohort),
Expand Down
2 changes: 1 addition & 1 deletion server/schema/catalog.sql
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down

0 comments on commit 7e184a1

Please sign in to comment.