Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete Notification Page Functionality (Backend) #53

Merged
merged 7 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 81 additions & 10 deletions routes/publishedSchedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ publishedScheduleRouter.get('/', async (req, res) => {
PS.start_time,
PS.end_time,
PS.cohort,
PS.notes
PS.notes,
PS.created_on
FROM
published_schedule PS
LEFT JOIN catalog C ON PS.event_id = C.id;
Expand All @@ -31,6 +32,64 @@ publishedScheduleRouter.get('/', async (req, res) => {
}
});

// GET/published-schedule/recently-added - returns the rows that were added in the past week
publishedScheduleRouter.get('/recently-added', async (req, res) => {
try {
const recentAddResult = await db.query(
`
SELECT
PS.id,
C.title,
C.event_type,
C.year,
PS.start_time,
PS.end_time,
PS.confirmed,
PS.confirmed_on,
PS.cohort,
PS.notes,
PS.created_on
FROM published_schedule PS
LEFT JOIN catalog C ON PS.event_id = C.id
WHERE PS.created_on = PS.confirmed_on AND PS.created_on > current_date - 7 AND confirmed = true
ORDER BY created_on DESC;
`,
);
res.status(200).json(keysToCamel(recentAddResult));
} catch (err) {
res.status(400).send(err.message);
}
});

// GET/published-schedule/recently-confirmed - returns the rows that were confirmed in the past week
publishedScheduleRouter.get('/recently-confirmed', async (req, res) => {
try {
const recentConfirm = await db.query(
`
SELECT
PS.id,
C.title,
C.event_type,
C.year,
PS.start_time,
PS.end_time,
PS.confirmed,
PS.confirmed_on,
PS.cohort,
PS.notes,
PS.created_on
FROM published_schedule PS
LEFT JOIN catalog C ON PS.event_id = C.id
WHERE PS.confirmed_on > current_date - 7
ORDER BY created_on DESC;
`,
);
res.status(200).json(keysToCamel(recentConfirm));
} catch (err) {
res.status(400).send(err.message);
}
});

// GET /published-schedule/all-seasons - return all the seasons
publishedScheduleRouter.get('/all-seasons', async (req, res) => {
const getSeason = (date) => {
Expand Down Expand Up @@ -66,10 +125,10 @@ publishedScheduleRouter.get('/all-seasons', async (req, res) => {
try {
const allDatesResult = await db.query(
`
SELECT D.event_date
SELECT D.event_date
FROM
published_schedule AS PS, day AS D
WHERE
WHERE
D.id = PS.day_id
`,
);
Expand Down Expand Up @@ -131,7 +190,8 @@ publishedScheduleRouter.get('/season', async (req, res) => {
PS.confirmed,
PS.confirmed_on,
PS.cohort,
PS.notes
PS.notes,
PS.created_on
FROM published_schedule PS
LEFT JOIN catalog C ON PS.event_id = C.id
LEFT JOIN day D on PS.day_id = D.id
Expand Down Expand Up @@ -235,7 +295,8 @@ publishedScheduleRouter.get('/:id', async (req, res) => {
PS.start_time,
PS.end_time,
PS.cohort,
PS.notes
PS.notes,
PS.created_on
FROM
published_schedule PS
LEFT JOIN catalog C ON PS.event_id = C.id
Expand All @@ -253,6 +314,7 @@ publishedScheduleRouter.get('/:id', async (req, res) => {
// NOTE: there is a requirement that the day already exist,
// 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;
try {
const dayResult = await db.query(
Expand Down Expand Up @@ -281,11 +343,12 @@ publishedScheduleRouter.post('/', async (req, res) => {
start_time,
end_time,
cohort,
notes
notes,
created_on
)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8)
RETURNING id;
($1, $2, $3, $4, $5, $6, $7, $8, $9)
RETURNING id, created_on;
`,
[
eventId,
Expand All @@ -296,6 +359,7 @@ publishedScheduleRouter.post('/', async (req, res) => {
endTime,
calculateYear(eventDate, cohort),
notes,
currDate,
],
);
res.status(201).json({
Expand All @@ -315,7 +379,10 @@ publishedScheduleRouter.post('/', async (req, res) => {
publishedScheduleRouter.put('/:id', async (req, res) => {
try {
const { id } = req.params;
const { eventId, dayId, confirmed, confirmedOn, startTime, endTime, cohort, notes } = req.body;

const { eventId, dayId, confirmed, confirmedOn, startTime, endTime, cohort, notes, createdOn } =
req.body;

// get the current day from the PS table
const psDayIdResult = keysToCamel(
await db.query(`SELECT day_id FROM published_schedule WHERE id = $1;`, id),
Expand All @@ -329,6 +396,7 @@ publishedScheduleRouter.put('/:id', async (req, res) => {
// grab the eventDate so that you can set the years
const { eventDate } = dayResult;
// update the PS

const updatedPublishedSchedule = await db.query(
`
UPDATE published_schedule
Expand All @@ -341,7 +409,9 @@ publishedScheduleRouter.put('/:id', async (req, res) => {
end_time = COALESCE($6, end_time),
cohort = COALESCE($7, cohort),
notes = COALESCE($8, notes)
WHERE id = $9
created_on = COALESCE($9, created_on)

WHERE id = $10

RETURNING *;
`,
Expand All @@ -354,6 +424,7 @@ publishedScheduleRouter.put('/:id', async (req, res) => {
endTime,
cohort ? calculateYear(eventDate, cohort) : cohort,
notes,
createdOn,
id,
],
);
Expand Down
18 changes: 8 additions & 10 deletions routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,15 @@ userRouter.get('/:uid', async (req, res) => {

userRouter.post('/create', async (req, res) => {
try {
const { id, email, type, approved } = req.body;
await db.query(`INSERT INTO users (id, email, "type", approved) VALUES ($1, $2, $3, $4);`, [
id,
email,
type,
approved,
]);
const { id, email, type, approved, approvedOn } = req.body;
await db.query(
`INSERT INTO users (id, email, "type", approved, approved_on) VALUES ($1, $2, $3, $4, $5);`,
[id, email, type, approved, approvedOn],
);
res.status(201).json({
id,
});
} catch (err) {
console.log('err', err);
res.status(500).json({
status: 'Failed',
msg: err.message,
Expand All @@ -59,9 +56,10 @@ userRouter.post('/create', async (req, res) => {
userRouter.put('/approve/:uid', async (req, res) => {
try {
const { uid } = req.params;
const currDate = new Date();
const updatedApproval = await db.query(
`UPDATE users SET approved = TRUE WHERE id = $1 RETURNING *;`,
[uid],
`UPDATE users SET approved = TRUE, approved_on = $1 WHERE id = $2 RETURNING *;`,
[currDate, uid],
);
return res.status(200).send(keysToCamel(updatedApproval));
} catch (err) {
Expand Down
1 change: 1 addition & 0 deletions server/schema/published_schedule.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CREATE TABLE IF NOT EXISTS published_schedule (
end_time time NOT NULL,
cohort varchar[] NOT NULL,
notes varchar(100),
created_on date NOT NULL,
FOREIGN KEY (event_id)
REFERENCES catalog (id),
FOREIGN KEY (day_id)
Expand Down
3 changes: 2 additions & 1 deletion server/schema/users.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ CREATE TABLE users (
id VARCHAR ( 256 ) PRIMARY KEY,
email VARCHAR ( 50 ) NOT NULL,
type account_type NOT NULL,
approved BOOLEAN NOT NULL
approved BOOLEAN NOT NULL,
approved_on DATE NOT NULL,
);
Loading