import { Injectable } from '@nestjs/common' import { Competition, CompetitionCategories, CompetitionCompetitor } from '../../models/ranking' import { PatientRepository } from '../../repositories' import { CompetitionRepository } from '../../repositories/ranking' import { RankingNotificationService } from '../notification' import { AcceptInviteCompetitionService, CreateCompetitionService, FinishCompetitionService, SortCompetitorsByScoreService, UpdatePatientCompetitionsService, } from './services' @Injectable() export class CompetitionService { constructor( private readonly acceptInviteCompetitionService: AcceptInviteCompetitionService, private readonly createCompetitionService: CreateCompetitionService, private readonly finishCompetitionService: FinishCompetitionService, private readonly sortCompetitorsByScoreService: SortCompetitorsByScoreService, private readonly updatePatientCompetitionsService: UpdatePatientCompetitionsService, private readonly competitionRepository: CompetitionRepository, private readonly rankingNotificationService: RankingNotificationService, private readonly patientRepository: PatientRepository, ) {} async create(data: { title: string category: CompetitionCategories willEndAt: Date patientId: string }): Promise { return this.createCompetitionService.create(data) } async acceptInvite(data: { competitionId: string; patientId: string }): Promise { return this.acceptInviteCompetitionService.accept(data) } async finish(willEndAt: Date): Promise { return this.finishCompetitionService.finish(willEndAt) } async notifyAcceptInvite(competitor: CompetitionCompetitor): Promise { const competition = await this.competitionRepository.get(competitor.competition_id) if (competitor.patient_id === competition.patient_id) return const patient = await this.patientRepository.get(competition.patient_id) return this.rankingNotificationService.send(patient, 'ranking_invite_accepted_push', competition) } async generateNewScoreForAllCompetitions({ patientId, anthropometryId, }: { patientId: string anthropometryId: string }): Promise { await this.updatePatientCompetitionsService.update({ patientId, anthropometryId }) } async generatePositionsAndSort(patientId: string): Promise { const competitions = await this.competitionRepository.getAllUnfinished(patientId) for (const competition of competitions) await this.sortCompetitorsByScoreService.sort(competition) } }