Skip to content

Commit

Permalink
Merged dev into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Aokijiop committed May 20, 2024
2 parents f90c34b + b36064a commit 5e75cff
Show file tree
Hide file tree
Showing 11 changed files with 921 additions and 91 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/CD.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: EC2 Deploy

on:
push:
branches: dev

jobs:
Deploy:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Deploy in EC2
env:
PRIVATE_KEY: ${{ secrets.AWS_EC2_PRIVATE_KEY }}
HOSTNAME : ${{ secrets.AWS_EC2_HOSTNAME }}
USER: ${{ secrets.AWS_EC2_USER }}

run: |
echo "$PRIVATE_KEY" > private_key.pem && chmod 600 private_key.pem
ssh -o StrictHostKeyChecking=no -i private_key.pem ${USER}@${HOSTNAME} '
#Now we have got the access of EC2 and we will start the deploy .
cd ~/aiss-backend &&
git checkout dev &&
git stash &&
git pull origin dev &&
yarn &&
pm2 restart app.js
'
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const email = require('./routes/nodeMailer');
const app = express();

const catalogRouter = require('./routes/catalog');
const dayRouter = require('./routes/day');

const PORT = process.env.PORT || 3001;

Expand All @@ -33,6 +34,7 @@ app.use('/users', users);
app.use('/catalog', catalogRouter);
app.use('/nodeMailer', email);
app.use('/auth', authRouter);
app.use('/day', dayRouter);

app.listen(PORT, () => {
console.log(`Server listening on ${PORT}`);
Expand Down
84 changes: 79 additions & 5 deletions common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,44 @@ const isISODate = (str) => {
}
};

// dependency for catalog.js
const isInteger = (value) => {
return value && /^\d+$/.test(value);
};

// dependency for publishedSchedule.js
const calculateYear = (eventDate, gradeLevel) => {
const currentDay = new Date(eventDate);
if (gradeLevel && gradeLevel.length === 1) {
// console.log('current day', currentDay.getFullYear() + (currentDay.getMonth() >= 7 ? 2 : 1));
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[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[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 [];
};

const isObject = (o) => {
return o === Object(o) && !isArray(o) && typeof o !== 'function' && !isISODate(o);
};
Expand All @@ -34,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 @@ -45,11 +84,46 @@ 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;
};

module.exports = { keysToCamel };
const getSeasonFromMonthAndYear = (month, year) => {
// spring
// march-may -> winter [year]
if (month >= 0 && month <= 4) {
return `Spring ${year}`;
}
// summer
// june-august -> summer [year]
if (month >= 5 && month <= 7) {
return `Summer ${year}`;
}
// fall
// september-november -> fall [year]
return `Fall ${year}`;
};

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,
};
156 changes: 117 additions & 39 deletions routes/catalog.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,75 @@
const express = require('express');

const { db } = require('../server/db');

const catalogRouter = express.Router();
const { keysToCamel } = require('../common/utils');
const { keysToCamel, isInteger } = require('../common/utils');

// -- GET - Returns all data from the catalog table
catalogRouter.get('/', async (req, res) => {
try {
const allInfo = await db.query(`SELECT * from catalog;`);
res.status(200).json(keysToCamel(allInfo));
const { title, eventType, subject, season, year } = req.query;

let { limit, page } = req.query;
limit = isInteger(limit) ? parseInt(limit, 10) : 10;
page = isInteger(page) ? parseInt(page, 10) : 1;

const offset = (page - 1) * limit;

let query = 'FROM catalog WHERE 1=1 AND hidden = false';
// removed space at beginning here

const params = [];

if (title) {
query += ' AND (title ILIKE $1';
query += ' OR host ILIKE $1';
query += ' OR description ILIKE $1) ';
params.push(`%${title}%`);
} else {
params.push('');
}

if (subject) {
const array = subject.split(',');
query += ' AND subject && $2::subject[]';
params.push(array);
} else {
params.push('');
}

if (eventType) {
const array = eventType.split(',');
query += ' AND event_type && $3::event[]';
params.push(array);
} else {
params.push('');
}

if (season) {
const array = season.split(',');
query += ' AND season && $4::season[]';
params.push(array);
} else {
params.push('');
}

if (year) {
const array = year.split(',');
query += ' AND year && $5::year[]';
params.push(array);
} else {
params.push('');
}

const eventCount = await db.query(`SELECT COUNT(*) ${query};`, params);

query += ' ORDER BY title ASC LIMIT $6 OFFSET $7;';
params.push(limit);
params.push(offset);

const reqInfo = await db.query(`SELECT * ${query}`, params);

res.status(200).json(keysToCamel({ events: reqInfo, count: eventCount }));
} catch (err) {
res.status(500).send(err.message);
}
Expand All @@ -19,22 +79,22 @@ 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;`, [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);
}
});

// -- POST - Adds a new row to the catalog table
catalogRouter.post('/', async (req, res) => {
const { host, title, eventType, subject, description, year, season, location } = req.body;
const { host, title, eventType, subject, description, year, season } = req.body;
try {
const returnedData = await db.query(
`INSERT INTO catalog (id, host, title, event_type, subject, description, year, season, location)
VALUES (nextval('catalog_id_seq'), $1, $2, $3, $4, $5, $6, $7, $8)
`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 id;`,
[host, title, eventType, subject, description, year, season, location],
[host, title, eventType, subject, description, year, season],
);
res.status(201).json({ id: returnedData[0].id, status: 'Success' });
} catch (err) {
Expand All @@ -50,34 +110,46 @@ catalogRouter.post('/', async (req, res) => {
catalogRouter.put('/:id', async (req, res) => {
try {
const { id } = req.params;
const { host, title, eventType, subject, description, year, location, season } = req.body;

const updatedCatalog = await db.query(
`UPDATE catalog SET
${host ? 'host = $(host), ' : ''}
${title ? 'title = $(title),' : ''}
${eventType ? 'event_type = $(eventType), ' : ''}
${subject ? 'subject = $(subject), ' : ''}
${description ? 'description = $(description), ' : ''}
${year ? 'year = $(year), ' : ''}
${location ? 'location = $(location), ' : ''}
${season ? 'season = $(season), ' : ''}
id = '${id}'
WHERE id = '${id}'
const { host, title, eventType, subject, description, year, season } = req.body;

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,
location,
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 All @@ -87,8 +159,14 @@ catalogRouter.put('/:id', async (req, res) => {
catalogRouter.delete('/:id', async (req, res) => {
try {
const { id } = req.params;
const delUser = await db.query(`DELETE FROM catalog WHERE id = $1 RETURNING *;`, [id]);
res.status(200).send(keysToCamel(delUser));
const inUse = await db.query(`SELECT * FROM published_schedule WHERE event_id = $1;`, [id]);
let hidden;
if (inUse && inUse.length) {
hidden = await db.query(`UPDATE catalog SET hidden = true WHERE id = $1 RETURNING *;`, [id]);
} else {
hidden = await db.query(`DELETE FROM catalog WHERE id = $1 RETURNING *;`, [id]);
}
res.status(200).send(keysToCamel(hidden));
} catch (err) {
res.status(500).send(err.message);
}
Expand Down
Loading

0 comments on commit 5e75cff

Please sign in to comment.