Skip to content

Commit

Permalink
[Backend] Type semesters.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
valtterikantanen committed Jul 16, 2024
1 parent 6bd7eed commit c64ebfd
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 50 deletions.
3 changes: 2 additions & 1 deletion services/backend/src/models/semester.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Column, CreatedAt, DataType, HasMany, Model, PrimaryKey, Table, UpdatedAt } from 'sequelize-typescript'

import { SemesterEnrollment } from './semesterEnrollment'
import { Name } from '../types/name'

@Table({
underscored: true,
Expand All @@ -19,7 +20,7 @@ export class Semester extends Model {
semestercode: number

@Column(DataType.JSONB)
name: object
name: Name

@Column(DataType.INTEGER)
startYear: number
Expand Down
49 changes: 0 additions & 49 deletions services/backend/src/services/semesters.js

This file was deleted.

57 changes: 57 additions & 0 deletions services/backend/src/services/semesters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import moment from 'moment'
import { Op } from 'sequelize'

import { Semester } from '../models'
import { Name } from '../types/name'

type SemestersAndYears = {
years: {
[yearcode: string]: { yearcode: number; yearname: string; startdate: moment.Moment; enddate: moment.Moment }
}
semesters: {
[semestercode: string]: { semestercode: number; name: Name; yearcode: number; startdate: Date; enddate: Date }
}
}

export const getCurrentSemester = async () => {
const today = new Date()
const currentSemester = await Semester.findOne({
where: {
startdate: {
[Op.lte]: today,
},
enddate: {
[Op.gte]: today,
},
},
})
return currentSemester
}

export const getSemestersAndYears = async () => {
const semesters = await Semester.findAll()
const result = semesters.reduce(
(acc, semester) => {
const { semestercode, name, yearcode, yearname, startdate, enddate } = semester
acc.semesters[semestercode] = { semestercode, name, yearcode, startdate, enddate }
if (!acc.years[yearcode]) {
acc.years[yearcode] = {
yearcode,
yearname,
startdate: moment(startdate),
enddate: moment(enddate),
}
} else {
acc.years[yearcode] = {
yearcode,
yearname,
startdate: moment.min(acc.years[yearcode].startdate, moment(startdate)),
enddate: moment.max(acc.years[yearcode].enddate, moment(enddate)),
}
}
return acc
},
{ years: {}, semesters: {} } as SemestersAndYears
)
return result
}

0 comments on commit c64ebfd

Please sign in to comment.