Skip to content

Commit

Permalink
feat: implement getSemestersSummary method
Browse files Browse the repository at this point in the history
  • Loading branch information
evermake committed Mar 24, 2024
1 parent 964f4b6 commit 8a09e53
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 15 deletions.
47 changes: 47 additions & 0 deletions backend/src/domain/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { User } from './schemas/user'
import type { SemesterSummary } from './types'
import type { Logger } from '~/utils/logging'
import type { Database } from '~/services/database'
import type { SportClient } from '~/services/sport'
Expand Down Expand Up @@ -150,4 +151,50 @@ export class Domain {
trainingId: trainingId,
})
}

public async getSemestersSummary({
telegramId,
}: {
telegramId: number
}): Promise<SemesterSummary[]> {
const user = await this.db.user.findFirstOrThrow({
where: {
telegramId: telegramId,
sportId: { not: null },
},
select: { sportId: true },
})

const [semesters, sportHours, allFitnessTests] = await Promise.all([
this.sport.getAllSemesters(),
this.sport.getSportHoursInfo({ studentId: user.sportId! }),
this.sport.getAllFitnessTestResults({ studentId: user.sportId! }),
])

const allSportHours = [
sportHours.ongoing_semester,
...sportHours.last_semesters_hours,
]

return semesters.map(({ id, name }) => {
const semesterSportHours = allSportHours.find(h => h.id_sem === id)
const semesterFitnessTest = allFitnessTests.find(t => t.semester === name)

return {
title: name,
hoursTotal: semesterSportHours
? (semesterSportHours.hours_not_self + semesterSportHours.hours_self_not_debt)
: 0,
fitnessTest: semesterFitnessTest
? {
passed: semesterFitnessTest.grade,
pointsTotal: semesterFitnessTest.total_score,
}
: {
passed: false,
pointsTotal: 0,
},
}
})
}
}
8 changes: 8 additions & 0 deletions backend/src/domain/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export type SemesterSummary = {
title: string
hoursTotal: number
fitnessTest: {
passed: boolean
pointsTotal: number
}
}
16 changes: 2 additions & 14 deletions backend/src/services/sport/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios from 'axios'
import { z } from 'zod'
import type { AxiosInstance } from 'axios'
import type { TrainingDetailed, TrainingInfo } from './types'
import { CalendarTraining, Training } from './schemas'
import { CalendarTraining, FitnessTestResult, Training } from './schemas'
import type { Logger } from '~/utils/logging'

/**
Expand Down Expand Up @@ -224,19 +224,7 @@ export class SportClient {
return this.request({
method: 'GET',
path: '/fitnesstest/result',
responseSchema: z.array(z.object({
semester: z.string(),
retake: z.boolean(),
grade: z.number(),
total_score: z.number(),
details: z.array(z.object({
exercise: z.string(),
unit: z.string(),
value: z.union([z.string(), z.number()]),
score: z.number(),
max_score: z.number(),
})),
})),
responseSchema: z.array(FitnessTestResult),
})
}

Expand Down
2 changes: 1 addition & 1 deletion backend/src/services/sport/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const Group = z.object({

export const FitnessTestExerciseResult = z.object({
exercise: z.string(),
unit: z.string(),
unit: z.string().nullable(),
value: z.union([z.number(), z.string()]),
score: z.number(),
max_score: z.number(),
Expand Down

0 comments on commit 8a09e53

Please sign in to comment.