From 717546828dacbcdf9b8bdbbffce5dd905cae008d Mon Sep 17 00:00:00 2001 From: Claudia Asti Date: Wed, 28 Oct 2020 16:00:14 +0100 Subject: [PATCH] Refactor student service to use new endpoints --- ...file-apprenticeship-company.component.html | 63 +++++------ .../services/student-profile.service.ts | 106 +++++++++++------- 2 files changed, 98 insertions(+), 71 deletions(-) diff --git a/src/app/shared/components/student-profile-apprenticeship-company/student-profile-apprenticeship-company.component.html b/src/app/shared/components/student-profile-apprenticeship-company/student-profile-apprenticeship-company.component.html index f0a256157..a92492124 100644 --- a/src/app/shared/components/student-profile-apprenticeship-company/student-profile-apprenticeship-company.component.html +++ b/src/app/shared/components/student-profile-apprenticeship-company/student-profile-apprenticeship-company.component.html @@ -4,36 +4,35 @@ /> - {{ company.apprenticeshipManagerPerson.LastName }} - {{ company.apprenticeshipManagerPerson.FirstName }}
+ {{ company.apprenticeshipManager.Lastname }} + {{ company.apprenticeshipManager.Firstname }}
- {{ company.apprenticeshipManagerPerson.AddressLine1 }}
{{ company.apprenticeshipManager.AddressLine1 }}
- {{ company.apprenticeshipManagerPerson.AddressLine2 }}
{{ company.apprenticeshipManager.AddressLine2 }}
{{ company.apprenticeshipManagerPerson.Zip }} - {{ company.apprenticeshipManagerPerson.Location }}
{{ company.apprenticeshipManager.PostalCode }} + {{ company.apprenticeshipManager.Location }}
- - {{ company.apprenticeshipManagerPerson.PhoneBusiness }}
+ {{ company.apprenticeshipManager.PhoneBusiness }}
- - {{ company.apprenticeshipManagerPerson | erzPersonEmail }} + {{ + company.apprenticeshipManager | erzPersonEmail + }}
@@ -43,22 +42,22 @@ company.apprenticeshipContract.ContractDateTo | date: 'dd.MM.yyyy' }}
-
+
{{ 'shared.profile.job-trainer' | translate }}:
- {{ company.jobTrainerPerson.LastName }} - {{ company.jobTrainerPerson.FirstName }}
- - {{ company.jobTrainerPerson.PhoneBusiness }}
+ + {{ company.jobTrainer.PhoneBusiness }}
- - {{ company.jobTrainerPerson.PhoneMobile }}
+ {{ company.jobTrainer.PhoneMobile }}
- - {{ - company.jobTrainerPerson | erzPersonEmail + + {{ + company.jobTrainer | erzPersonEmail }}
diff --git a/src/app/shared/services/student-profile.service.ts b/src/app/shared/services/student-profile.service.ts index 8897db2a6..30e8112c0 100644 --- a/src/app/shared/services/student-profile.service.ts +++ b/src/app/shared/services/student-profile.service.ts @@ -7,9 +7,13 @@ import { ApprenticeshipContract } from 'src/app/shared/models/apprenticeship-con import { LegalRepresentative } from 'src/app/shared/models/legal-representative.model'; import { Person } from 'src/app/shared/models/person.model'; import { Student } from 'src/app/shared/models/student.model'; +import { ApprenticeshipManager } from 'src/app/shared/models/apprenticeship-manager.model'; +import { JobTrainer } from 'src/app/shared/models/job-trainer.model'; import { LoadingService } from 'src/app/shared/services/loading-service'; import { PersonsRestService } from 'src/app/shared/services/persons-rest.service'; import { StudentsRestService } from 'src/app/shared/services/students-rest.service'; +import { ApprenticeshipManagersRestService } from 'src/app/shared/services/apprenticeship-managers-rest.service'; +import { JobTrainersRestService } from 'src/app/shared/services/job-trainers-rest.service'; import { DropDownItemsRestService } from './drop-down-items-rest.service'; import { spread } from 'src/app/shared/utils/function'; import { catch404 } from 'src/app/shared/utils/observable'; @@ -25,8 +29,8 @@ export interface Profile { export interface ApprenticeshipCompany { apprenticeshipContract: ApprenticeshipContract; - jobTrainerPerson: Option; - apprenticeshipManagerPerson: Option; + jobTrainer: Option; + apprenticeshipManager: Option; } @Injectable({ @@ -38,6 +42,8 @@ export class StudentProfileService { constructor( private studentService: StudentsRestService, private personsService: PersonsRestService, + private apprenticeshipManagersService: ApprenticeshipManagersRestService, + private jobTrainersService: JobTrainersRestService, private loadingService: LoadingService, private dropDownItemsService: DropDownItemsRestService ) {} @@ -117,68 +123,89 @@ export class StudentProfileService { if (!student) { return of(null); } - return this.loadPersons(legalRepresentatives, apprenticeshipContracts).pipe( - map((persons) => + + legalRepresentatives = this.getRelevantLegalRepresentatives( + student, + legalRepresentatives + ); + + return combineLatest([ + this.loadLegalRepresentativPersons(legalRepresentatives), + this.loadJobTrainers(apprenticeshipContracts), + this.loadApprenticeshipManagers(apprenticeshipContracts), + ]).pipe( + map(([persons, trainers, managers]) => this.createProfile( student, stayPermitValue, legalRepresentatives, + persons, apprenticeshipContracts, - persons + managers, + trainers ) ) ); } - private loadPersons( - legalRepresentatives: ReadonlyArray, - apprenticeshipContracts: ReadonlyArray + private loadLegalRepresentativPersons( + legalRepresentatives: ReadonlyArray ): Observable> { - if ([...legalRepresentatives, ...apprenticeshipContracts].length === 0) { + if (legalRepresentatives.length === 0) { return of([]); } + return this.personsService.getListForIds( - this.getPersonIds(legalRepresentatives, apprenticeshipContracts) + legalRepresentatives.map((r) => r.RepresentativeId) ); } - private getPersonIds( - legalRepresentatives: ReadonlyArray, - apprenticeshipContracts: ReadonlyArray - ): ReadonlyArray { - return [ - ...legalRepresentatives.map( - (legalRepresentative) => legalRepresentative.RepresentativeId - ), - ...apprenticeshipContracts - .map((contract) => contract.JobTrainer) - .filter((id): id is number => typeof id === 'number'), - ...apprenticeshipContracts.map( - (contract) => contract.ApprenticeshipManagerId - ), - ]; + private loadJobTrainers( + contracts: ReadonlyArray + ): Observable> { + const ids = contracts + .map((contract) => contract.JobTrainer) + .filter((id): id is number => typeof id === 'number'); + + return combineLatest(ids.map((id) => this.jobTrainersService.get(id))); + } + + private loadApprenticeshipManagers( + contracts: ReadonlyArray + ): Observable> { + const ids = contracts.map((contract) => contract.ApprenticeshipManagerId); + + return combineLatest( + ids.map((id) => this.apprenticeshipManagersService.get(id)) + ); } private createProfile( student: T, stayPermitValue: Option, legalRepresentatives: ReadonlyArray, + legalRepresentativePersons: ReadonlyArray, apprenticeshipContracts: ReadonlyArray, - persons: ReadonlyArray + apprenticeshipManagers: ReadonlyArray, + jobTrainers: ReadonlyArray ): Profile { const profile: Profile = { student, stayPermitValue: stayPermitValue || undefined, - legalRepresentativePersons: this.getRelevantLegalRepresentatives( - student, - legalRepresentatives - ) + legalRepresentativePersons: legalRepresentatives .map((legalRepresentative) => - this.findPerson(legalRepresentative.RepresentativeId, persons) + this.findPerson( + legalRepresentative.RepresentativeId, + legalRepresentativePersons + ) ) .filter(notNull), apprenticeshipCompanies: apprenticeshipContracts.map((contract) => - this.createApprenticeshipCompany(contract, persons) + this.createApprenticeshipCompany( + contract, + apprenticeshipManagers, + jobTrainers + ) ), }; return profile; @@ -196,23 +223,24 @@ export class StudentProfileService { private createApprenticeshipCompany( contract: ApprenticeshipContract, - persons: ReadonlyArray + managers: ReadonlyArray, + trainers: ReadonlyArray ): ApprenticeshipCompany { const apprenticeshipCompany: ApprenticeshipCompany = { apprenticeshipContract: contract, - jobTrainerPerson: this.findPerson(contract.JobTrainer, persons), - apprenticeshipManagerPerson: this.findPerson( + jobTrainer: this.findPerson(contract.JobTrainer, trainers), + apprenticeshipManager: this.findPerson( contract.ApprenticeshipManagerId, - persons + managers ), }; return apprenticeshipCompany; } - private findPerson( + private findPerson( id: Maybe, - persons: ReadonlyArray - ): Option { + persons: ReadonlyArray + ): Option { return id ? persons.find((p) => p.Id === id) || null : null; } }