Skip to content

Commit

Permalink
fixed season retrieval for stat modal
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatMegamind committed May 1, 2024
1 parent 1bd80e6 commit 4554450
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
18 changes: 17 additions & 1 deletion common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,20 @@ const getSeasonFromMonthAndYear = (month, year) => {
return `Fall ${year}`;
};

module.exports = { keysToCamel, isInteger, calculateYear, getSeasonFromMonthAndYear };
const getMonthRangeFromSeason = (season) => {
if (season === 'spring') {
return [0, 4];
}
if (season === 'summer') {
return [5, 7];
}
return [8, 12];
};

module.exports = {
keysToCamel,
isInteger,
calculateYear,
getSeasonFromMonthAndYear,
getMonthRangeFromSeason,
};
15 changes: 11 additions & 4 deletions routes/publishedSchedule.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const express = require('express');
const { db } = require('../server/db');
const { keysToCamel, calculateYear, getSeasonFromMonthAndYear } = require('../common/utils');
const {
keysToCamel,
calculateYear,
getSeasonFromMonthAndYear,
getMonthRangeFromSeason,
} = require('../common/utils');

const publishedScheduleRouter = express.Router();

Expand Down Expand Up @@ -221,6 +226,7 @@ publishedScheduleRouter.get('/season', async (req, res) => {
publishedScheduleRouter.get('/stats', async (req, res) => {
try {
const { season, year } = req.query;
const [monthStart, monthEnd] = getMonthRangeFromSeason(season);

const statResult = await db.query(
`
Expand Down Expand Up @@ -249,16 +255,17 @@ publishedScheduleRouter.get('/stats', async (req, res) => {
FROM catalog c
JOIN published_schedule ps ON c.id = ps.event_id
JOIN day d ON PS.day_id = d.id
WHERE $1 = ANY(c.season)
AND EXTRACT(YEAR FROM d.event_date) = $2
WHERE EXTRACT(MONTH FROM d.event_date) >= $1
AND EXTRACT(MONTH FROM d.event_date) <= $2
AND EXTRACT(YEAR FROM d.event_date) = $3
) c ON ap.event_type = ANY(c.event_type) AND ap.subject = ANY(c.subject)
GROUP BY ROLLUP (ap.event_type), ROLLUP (ap.subject)
ORDER BY CASE WHEN ap.event_type IS NULL THEN 1 ELSE 0 END,
CASE WHEN ap.subject IS NULL THEN 1 ELSE 0 END,
ap.event_type NULLS FIRST,
ap.subject NULLS FIRST;
`,
[season, year],
[monthStart, monthEnd, year],
);

res.status(200).json(keysToCamel(statResult));
Expand Down

0 comments on commit 4554450

Please sign in to comment.