Skip to content

Commit

Permalink
[Backend] Fix some types
Browse files Browse the repository at this point in the history
  • Loading branch information
valtterikantanen committed Jul 17, 2024
1 parent ba35393 commit 95a97f2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
4 changes: 2 additions & 2 deletions services/backend/src/services/faculty/facultyBasics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import {
transferredTo,
} from './facultyTransfers'

const filterDuplicateStudyrights = (studyRights: { studyrightid: string }[]) => {
const filterDuplicateStudyrights = (studyRights: Awaited<ReturnType<typeof startedStudyrights>>) => {
// bachelor+master students have two studyrights (separated by two last digits in studyrightid)
// choose only bachelor one, so we don't count start of masters as starting in faculty
const rightsToCount = {}
const rightsToCount: Record<string, Awaited<ReturnType<typeof startedStudyrights>>[number]> = {}
studyRights.forEach(studyRight => {
const id = studyRight.studyrightid.slice(0, -2)
if (studyRight.studyrightid.slice(-2) === '-1') {
Expand Down
33 changes: 16 additions & 17 deletions services/backend/src/util/index.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
import { Role } from '../types'

type Comparator = (val1: string, val2: string) => number
type FieldComparator = (val1: Record<string, any>, val2: Record<string, any>) => number
type FieldComparator = (val1: Record<string, string>, val2: Record<string, string>) => number

/**
* Returns a sorting function that can be used to sort strings
* so that Finnish alphabetical order is respected.
*
* @param {string} [field] - The field to sort by, defaults to null.
* Returns a sorting function that can be used to sort strings so that Finnish alphabetical order is respected.
* @param field The field to sort by (optional: if not given, the function will sort by the strings themselves)
*/
export const createLocaleComparator = (field: string | null = null): Comparator | FieldComparator => {
export function createLocaleComparator(field: string): FieldComparator
export function createLocaleComparator(): Comparator
export function createLocaleComparator(field?: string): Comparator | FieldComparator {
const comparator: Comparator = (val1, val2) => val1.localeCompare(val2, 'fi', { sensitivity: 'accent' })
if (!field) {
return (val1: string, val2: string) => val1.localeCompare(val2, 'fi', { sensitivity: 'accent' })
}
return (val1: Record<string, any>, val2: Record<string, any>) => {
return val1[field].localeCompare(val2[field], 'fi', { sensitivity: 'accent' })
return comparator
}
return (val1: Record<string, string>, val2: Record<string, string>) => comparator(val1[field], val2[field])
}

export const getFullStudyProgrammeRights = programmeRights => {
return programmeRights.filter(({ limited }) => !limited).map(({ code }) => code)
}

export const hasFullAccessToStudentData = (roles: Role[]): boolean => {
export const hasFullAccessToStudentData = (roles?: Role[]) => {
const rolesWithFullAccess: Role[] = ['admin', 'fullSisuAccess']
return roles?.some(role => rolesWithFullAccess.includes(role))
return roles != null && roles.some(role => rolesWithFullAccess.includes(role))
}

export const splitByEmptySpace = (str: string): string[] => str.split(/\s+/g)
export const splitByEmptySpace = (str: string) => str.split(/\s+/g)

export const validateParamLength = (param: string, minLength: number): boolean => {
return param && param.trim().length >= minLength
export const validateParamLength = (param: any, minLength: number) => {
return typeof param === 'string' && param.trim().length >= minLength
}

export const sortByProgrammeCode = (a: string, b: string): number => {
const getPrefixPriority = (code: string): 1 | 2 | 3 => {
export const sortByProgrammeCode = (a: string, b: string) => {
const getPrefixPriority = (code: string) => {
if (code.startsWith('KH')) return 1
if (code.startsWith('MH')) return 2
return 3
Expand Down

0 comments on commit 95a97f2

Please sign in to comment.