Skip to content
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*

.vscode
37 changes: 26 additions & 11 deletions backend/controllers/admissionPeriodController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { UnauthorizedUserError } from 'ntnui-tools/customError'
import dayjs from 'dayjs'
import { AdmissionPeriodModel } from '../models/AdmissionPeriod'
import { RequestWithNtnuiNo } from '../utils/request'
import { MAIN_BOARD_ID } from '../utils/constants'
import { MAIN_BOARD_ID, STUDENTLEKENE_ID } from '../utils/constants'
import getAdmissionPeriodStatus from '../utils/getAdmissionPeriodStatus'
import { UserModel } from '../models/User'

Expand All @@ -19,27 +19,35 @@ function validateAndFormatDateString(value: string): string {
}

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()
const isSL = req.query.sl === 'true'

// Only one admission period of each type in the db at any time, so findOne() returns only the one object
const admissionPeriod = await AdmissionPeriodModel.findOne({
sl: isSL,
})

if (!admissionPeriod) {
return res
.status(404)
.json({ message: 'There is no admission period in the db' })
}
return res
.status(200)
.json({ admissionPeriod, admissionStatus: await getAdmissionPeriodStatus() })
return res.status(200).json({
admissionPeriod,
admissionStatus: await getAdmissionPeriodStatus(isSL),
})
}

const putAdmissionPeriod = async (req: RequestWithNtnuiNo, res: Response) => {
const { ntnuiNo } = req
if (!ntnuiNo) throw UnauthorizedUserError
const isSL = req.body.sl
const user = await UserModel.findById(ntnuiNo)
if (!user) throw UnauthorizedUserError
if (
user.committees.some(
(roleInCommittee) => roleInCommittee.committee === MAIN_BOARD_ID
(roleInCommittee) =>
roleInCommittee.committee === MAIN_BOARD_ID ||
(roleInCommittee.committee === STUDENTLEKENE_ID && isSL)
)
) {
let update
Expand All @@ -49,6 +57,7 @@ const putAdmissionPeriod = async (req: RequestWithNtnuiNo, res: Response) => {
start_date: validateAndFormatDateString(req.body.start_date),
end_date: validateAndFormatDateString(req.body.end_date),
set_by: `${user.first_name} ${user.last_name}`,
sl: isSL,
}
} catch (error) {
return res.status(400).json({ message: 'The dates are invalid' })
Expand All @@ -61,10 +70,16 @@ const putAdmissionPeriod = async (req: RequestWithNtnuiNo, res: Response) => {
}

// Update the existing admission period or create a new one if it doesn't exist
return AdmissionPeriodModel.findOneAndUpdate({}, update, {
new: true,
upsert: true,
})
return AdmissionPeriodModel.findOneAndUpdate(
{
sl: isSL,
},
update,
{
new: true,
upsert: true,
}
)
.then((updatedAdmission) =>
res.status(200).json({ admissionPeriod: updatedAdmission })
)
Expand Down
30 changes: 26 additions & 4 deletions backend/controllers/applicationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import { AdmissionPeriodStatus, SortTypes, StatusTypes } from '../utils/enums'
import { IStatus, StatusModel } from '../models/Status'
import { AdmissionPeriodModel } from '../models/AdmissionPeriod'
import { getSortTypeValue } from '../utils/applicationQueryMiddleware'
import { ELECTION_COMMITTEE_ID, MAIN_BOARD_ID } from '../utils/constants'
import {
ELECTION_COMMITTEE_ID,
MAIN_BOARD_ID,
STUDENTLEKENE_ID,
} from '../utils/constants'
import getAdmissionPeriodStatus from '../utils/getAdmissionPeriodStatus'

async function getUserCommitteeIdsByUserId(userId: number | string) {
Expand Down Expand Up @@ -53,12 +57,14 @@ const getApplicationById = async (
.json({ message: 'The user is not member of any committee' })
}

const isSL = req.query.sl === 'true'

// Retrieve application and committees the application is sent to
const application = await ApplicationModel.findById(req.params.application_id)
.populate<IPopulatedApplicationCommittees>('committees', 'name slug')
.populate<IPopulatedApplicationCommittees>('committees', 'name slug sl')
.populate({
path: 'statuses',
populate: { path: 'committee', model: 'Committee', select: 'name slug' },
populate: { path: 'committee', model: 'Committee', select: 'name slug sl' },
select: '-__v',
})
.then((applicationRes) => applicationRes)
Expand Down Expand Up @@ -94,6 +100,9 @@ const getApplicationById = async (
// Check if user is member of any committee that application is sent to
let isAuthorized = false
for (let id = 0; id < applicationCommittees.length; id += 1) {
if (isSL && applicationCommittees[id].sl) {
isAuthorized = true
}
const appCommitteeId = applicationCommittees[id]._id
if (userCommitteeIds.includes(appCommitteeId)) {
isAuthorized = true
Expand Down Expand Up @@ -146,6 +155,7 @@ const getApplications = async (
const status: string = req.query.status as string
const sortparam: SortTypes = req.query.sort as SortTypes
const sortValue = getSortTypeValue(sortparam) // Parse sort value
const isSL: boolean = (req.query.sl as string) === 'true'

// Aggregation
const aggregationPipeline = []
Expand All @@ -162,6 +172,13 @@ const getApplications = async (
},
}
aggregationPipeline.push(userAuthorizedCommittees)
} else if (isSL && userCommitteeIds.includes(STUDENTLEKENE_ID)) {
const userAuthorizedCommittees = {
$match: {
sl: true,
},
}
aggregationPipeline.push(userAuthorizedCommittees)
} else {
// Normal committees only see applications to their own committee
const userAuthorizedCommittees = {
Expand Down Expand Up @@ -309,6 +326,7 @@ const getApplications = async (
committee: 1,
value: 1,
},
sl: 1,
},
pagination: {
$mergeObjects: [
Expand Down Expand Up @@ -370,7 +388,11 @@ const postApplication = async (
next: NextFunction
) => {
try {
if (!((await getAdmissionPeriodStatus()) === AdmissionPeriodStatus.open)) {
if (
!(
(await getAdmissionPeriodStatus(req.body.sl)) === AdmissionPeriodStatus.open
)
) {
throw new CustomError('Admission period is not active', 403)
}
// Check that all applied committees accepts admissions
Expand Down
11 changes: 8 additions & 3 deletions backend/controllers/committeeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import {
ELECTION_COMMITTEE_ID,
MAIN_BOARD_ID,
LAW_COMMITTEE_ID,
STUDENTLEKENE_ID,
} from '../utils/constants'
import { RequestWithNtnuiNo } from '../utils/request'
import { getUserRoleInCommitteeByUserId } from '../utils/userCommittee'

const getCommittees = (_req: Request, res: Response) => {
CommitteeModel.find()
const getCommittees = (req: Request, res: Response) => {
const isSL = req.query.sl === 'true'
CommitteeModel.find({ sl: isSL })
.then((committees) => res.json(committees))
.catch((err) => res.status(404).json({ message: err.message }))
}
Expand All @@ -29,6 +31,7 @@ async function acceptAdmissions(
if (!committee) {
return res.status(404).json({ message: 'Committee not found' })
}

// Control access
const rolesInCommittees = await getUserRoleInCommitteeByUserId(ntnuiNo)
const isAuthorized = rolesInCommittees.some(
Expand All @@ -43,7 +46,9 @@ async function acceptAdmissions(
committee._id === LAW_COMMITTEE_ID)) ||
// If user is board member of the committee and is not in the main board
(userCommittee.committee === committee._id &&
committee._id !== MAIN_BOARD_ID)
committee._id !== MAIN_BOARD_ID) ||
// If user is in Studentlekene board and it is a studentlekene committee
(committee.sl === true && userCommittee.committee === STUDENTLEKENE_ID)
)
if (isAuthorized) {
// Toggle accepts_admissions for a committee
Expand Down
17 changes: 15 additions & 2 deletions backend/controllers/statusController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { CustomError, UnauthorizedUserError } from 'ntnui-tools/customError'
import { RequestWithNtnuiNo } from '../utils/request'
import { IUser, UserModel } from '../models/User'
import { StatusModel } from '../models/Status'
import { ELECTION_COMMITTEE_ID, MAIN_BOARD_ID } from '../utils/constants'
import {
ELECTION_COMMITTEE_ID,
MAIN_BOARD_ID,
STUDENTLEKENE_ID,
} from '../utils/constants'

const putStatus = async (
req: RequestWithNtnuiNo,
Expand All @@ -30,6 +34,13 @@ const putStatus = async (
)
})
if (!status) throw new CustomError('Could not find status', 404)

const isSL = req.query.sl === 'true'

const isUserInStudentlekeneBoard = user.committees
.map((committee) => committee.committee)
.includes(STUDENTLEKENE_ID)

// Check that user is in committee that status is set for
const isUserInCommittee = user.committees.find(
(committee) => committee.committee === status.committee
Expand All @@ -40,9 +51,11 @@ const putStatus = async (
.includes(ELECTION_COMMITTEE_ID)
// Check if status is for main board
const isStatusForMainBoard = status.committee === MAIN_BOARD_ID

if (
isUserInCommittee ||
(isStatusForMainBoard && isUserInElectionCommittee)
(isStatusForMainBoard && isUserInElectionCommittee) ||
(isSL && isUserInStudentlekeneBoard)
) {
status.value = req.body.value
status.set_by = `${user.first_name} ${user.last_name}`
Expand Down
6 changes: 3 additions & 3 deletions backend/fixtures/development/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ ARG DATABASE_URI
ARG USERNAME
ARG PASSWORD

ENV DATABASE_URI ${DATABASE_URI}
ENV USERNAME ${USERNAME}
ENV PASSWORD ${PASSWORD}
ENV DATABASE_URI=${DATABASE_URI}
ENV USERNAME=${USERNAME}
ENV PASSWORD=${PASSWORD}

COPY admissionperiod.json /admissionperiod.json
COPY applications.json /applications.json
Expand Down
8 changes: 7 additions & 1 deletion backend/fixtures/development/admissionperiod.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
[
{
"start_date": "2025-08-12",
"end_date": "2030-09-03"
"end_date": "2030-09-03",
"sl": false
},
{
"start_date": "2026-08-01",
"end_date": "2030-09-03",
"sl": true
}
]
12 changes: 8 additions & 4 deletions backend/fixtures/development/applications.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
],
"submitted_date": {
"$date": "2022-04-08T10:19:30.473Z"
}
},
"sl": false
},
{
"_id": {
Expand All @@ -38,7 +39,8 @@
],
"submitted_date": {
"$date": "2022-04-08T10:25:16.632Z"
}
},
"sl": false
},
{
"_id": {
Expand All @@ -57,7 +59,8 @@
],
"submitted_date": {
"$date": "2022-04-08T10:25:55.633Z"
}
},
"sl": false
},
{
"_id": {
Expand All @@ -79,6 +82,7 @@
],
"submitted_date": {
"$date": "2022-04-08T10:26:07.451Z"
}
},
"sl": false
}
]
Loading
Loading