From c64ebfdf46ab53c092c0d0e14d631af316c7c090 Mon Sep 17 00:00:00 2001 From: Valtteri Kantanen Date: Tue, 16 Jul 2024 12:36:09 +0300 Subject: [PATCH] [Backend] Type `semesters.ts` --- services/backend/src/models/semester.ts | 3 +- services/backend/src/services/semesters.js | 49 ------------------- services/backend/src/services/semesters.ts | 57 ++++++++++++++++++++++ 3 files changed, 59 insertions(+), 50 deletions(-) delete mode 100644 services/backend/src/services/semesters.js create mode 100644 services/backend/src/services/semesters.ts diff --git a/services/backend/src/models/semester.ts b/services/backend/src/models/semester.ts index bd7d2df225..e9d22a179d 100644 --- a/services/backend/src/models/semester.ts +++ b/services/backend/src/models/semester.ts @@ -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, @@ -19,7 +20,7 @@ export class Semester extends Model { semestercode: number @Column(DataType.JSONB) - name: object + name: Name @Column(DataType.INTEGER) startYear: number diff --git a/services/backend/src/services/semesters.js b/services/backend/src/services/semesters.js deleted file mode 100644 index dd2e8b7250..0000000000 --- a/services/backend/src/services/semesters.js +++ /dev/null @@ -1,49 +0,0 @@ -const moment = require('moment') -const { Op } = require('sequelize') - -const { Semester } = require('../models') - -const getCurrentSemester = async () => { - const today = new Date() - const currentSemester = await Semester.findOne({ - where: { - startdate: { - [Op.lte]: today, - }, - enddate: { - [Op.gte]: today, - }, - }, - }) - return currentSemester -} - -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, enddate } - else { - acc.years[yearcode] = { - yearcode, - yearname, - startdate: moment.min(moment(acc.years[yearcode].startdate), moment(startdate)), - enddate: moment.max(moment(acc.years[yearcode].enddate), moment(enddate)), - } - } - return acc - }, - { - years: {}, - semesters: {}, - } - ) - return result -} - -module.exports = { - getSemestersAndYears, - getCurrentSemester, -} diff --git a/services/backend/src/services/semesters.ts b/services/backend/src/services/semesters.ts new file mode 100644 index 0000000000..3c01ef9633 --- /dev/null +++ b/services/backend/src/services/semesters.ts @@ -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 +}