Skip to content

Commit

Permalink
fix: fix the validation to be more transparent with the format (ISO86…
Browse files Browse the repository at this point in the history
…01) and add date-fns package
  • Loading branch information
SanderArntzen committed Apr 3, 2022
1 parent db37b17 commit 14ccce9
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 25 deletions.
49 changes: 30 additions & 19 deletions backend/controllers/admissionPeriodController.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import { Request, Response } from 'express'
import { UnauthorizedUserError } from 'ntnui-tools/customError'
import isBefore from 'date-fns/isBefore'
import { AdmissionPeriodModel } from '../models/AdmissionPeriod'
import { getUserCommitteeIdsByUserId } from '../utils/userCommittee'
import { RequestWithNtnuiNo } from '../utils/request'
import MAIN_BOARD_ID from '../utils/constants'
import isAdmissionPeriodActive from '../utils/isApplicationPeriodActive'

function isValidDate(value: any) {
const regexDate = /^\d{4}-\d{2}-\d{2}$/
if (!regexDate.test(value)) {
return false
}
return true
function validateAndFormatDateString(value: string): string {
// Typical ISOString, 2022-01-31T00:00:00.000Z, fetching only the 10 first characters
const dateString = new Date(Date.parse(value)).toISOString().substring(0, 10)
return dateString
}

const getAdmissionPeriod = async (req: Request, res: Response) => {
// Only one admission period in the db at any time, so findOne() returns only the one object
const admissionPeriod = await AdmissionPeriodModel.findOne()

if (admissionPeriod) {
return res.status(200).json({ admissionPeriod })
const admissionPeriodResponse = {
start_date: admissionPeriod.start_date_string,
end_date: admissionPeriod.end_date_string,
}
return res.status(200).json({ admissionPeriodResponse })
}

return res
Expand All @@ -31,20 +35,27 @@ const putAdmissionPeriod = async (req: RequestWithNtnuiNo, res: Response) => {
if (!ntnuiNo) throw UnauthorizedUserError
const committeeIds: number[] = await getUserCommitteeIdsByUserId(ntnuiNo)

if (!(isValidDate(req.body.start_date) && isValidDate(req.body.end_date))) {
return res.status(500).json({ message: 'The dates are invalid' })
}
if (committeeIds.includes(MAIN_BOARD_ID)) {
let update

if (req.body.start_date > req.body.end_date) {
return res
.status(500)
.json({ message: "The start date can't be after the end date" })
}
try {
update = {
start_date_string: validateAndFormatDateString(req.body.start_date),
end_date_string: validateAndFormatDateString(req.body.end_date),
}
} catch (error) {
return res.status(400).json({ message: 'The dates are invalid' })
}

if (committeeIds.includes(MAIN_BOARD_ID)) {
const update = {
start_date: req.body.start_date,
end_date: req.body.end_date,
if (
!isBefore(
Date.parse(update.start_date_string),
Date.parse(update.end_date_string)
)
) {
return res
.status(400)
.json({ message: "The start date can't be the same or after the end date" })
}

// Update the existing admission period or create a new one if it doesn't exist
Expand Down
11 changes: 7 additions & 4 deletions backend/models/AdmissionPeriod.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import mongoose from 'mongoose'

// Since only the "date-part" of the date is used, not the "clocktime-part"
// 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: Date
end_date: Date
start_date_string: string
end_date_string: string
}

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

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

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"config": "^3.3.7",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"date-fns": "^2.28.0",
"dotenv": "^16.0.0",
"express": "^4.17.3",
"jsonwebtoken": "^8.5.1",
Expand Down
7 changes: 5 additions & 2 deletions backend/utils/isApplicationPeriodActive.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { CustomError } from 'ntnui-tools/customError'
import add from 'date-fns/add'
import { AdmissionPeriodModel } from '../models/AdmissionPeriod'

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

0 comments on commit 14ccce9

Please sign in to comment.