Skip to content

Commit

Permalink
refactor: change attribute names in model and replace date-fns-packag…
Browse files Browse the repository at this point in the history
…e with dayjs-package in backend
  • Loading branch information
SanderArntzen committed Apr 4, 2022
1 parent 14ccce9 commit b49655e
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 37 deletions.
17 changes: 6 additions & 11 deletions backend/controllers/admissionPeriodController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Request, Response } from 'express'
import { UnauthorizedUserError } from 'ntnui-tools/customError'
import isBefore from 'date-fns/isBefore'
import dayjs from 'dayjs'
import { AdmissionPeriodModel } from '../models/AdmissionPeriod'
import { getUserCommitteeIdsByUserId } from '../utils/userCommittee'
import { RequestWithNtnuiNo } from '../utils/request'
Expand All @@ -19,8 +19,8 @@ const getAdmissionPeriod = async (req: Request, res: Response) => {

if (admissionPeriod) {
const admissionPeriodResponse = {
start_date: admissionPeriod.start_date_string,
end_date: admissionPeriod.end_date_string,
start_date: admissionPeriod.start_date,
end_date: admissionPeriod.end_date,
}
return res.status(200).json({ admissionPeriodResponse })
}
Expand All @@ -40,19 +40,14 @@ const putAdmissionPeriod = async (req: RequestWithNtnuiNo, res: Response) => {

try {
update = {
start_date_string: validateAndFormatDateString(req.body.start_date),
end_date_string: validateAndFormatDateString(req.body.end_date),
start_date: validateAndFormatDateString(req.body.start_date),
end_date: validateAndFormatDateString(req.body.end_date),
}
} catch (error) {
return res.status(400).json({ message: 'The dates are invalid' })
}

if (
!isBefore(
Date.parse(update.start_date_string),
Date.parse(update.end_date_string)
)
) {
if (!dayjs(update.start_date).isBefore(dayjs(update.end_date))) {
return res
.status(400)
.json({ message: "The start date can't be the same or after the end date" })
Expand Down
8 changes: 4 additions & 4 deletions backend/models/AdmissionPeriod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import mongoose from 'mongoose'
// it's easier to handle it as a string, and not a Date, since mongoose
// will then add a clocktime-part aswell
interface IAdmissionPeriod {
start_date_string: string
end_date_string: string
start_date: string
end_date: string
}

const AdmissionPeriodModel = mongoose.model<IAdmissionPeriod>(
'AdmissionPeriod',
new mongoose.Schema<IAdmissionPeriod>(
{
start_date_string: { type: String, required: true },
end_date_string: { type: String, required: true },
start_date: { type: String, required: true },
end_date: { type: String, required: true },
},
{ collection: 'admissionperiod' }
)
Expand Down
25 changes: 9 additions & 16 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"config": "^3.3.7",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"date-fns": "^2.28.0",
"dayjs": "^1.11.0",
"dotenv": "^16.0.0",
"express": "^4.17.3",
"jsonwebtoken": "^8.5.1",
Expand Down
2 changes: 1 addition & 1 deletion backend/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const MAIN_BOARD_ID = 9
const MAIN_BOARD_ID = 1

export default MAIN_BOARD_ID
11 changes: 7 additions & 4 deletions backend/utils/isApplicationPeriodActive.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { CustomError } from 'ntnui-tools/customError'
import add from 'date-fns/add'
import dayjs from 'dayjs'
import { AdmissionPeriodModel } from '../models/AdmissionPeriod'

const isAdmissionPeriodActive = async () => {
const admissionPeriod = await AdmissionPeriodModel.findOne()
if (admissionPeriod) {
if (
new Date(admissionPeriod.start_date_string).getTime() <= Date.now() &&
(
(dayjs(admissionPeriod.start_date).isBefore(dayjs(Date.now()))) ||
(dayjs(admissionPeriod.start_date).isSame(dayjs(Date.now())))
)
&&
// Creating a date of end date + 1 day
add(new Date(admissionPeriod.end_date_string), { days: 1 }).getTime() >
Date.now()
dayjs(admissionPeriod.end_date).add(1, 'day').isAfter(dayjs(Date.now()))
) {
return true
}
Expand Down

0 comments on commit b49655e

Please sign in to comment.