From 3f88629e9ede11e3d0f2d98aa4a4b74d59453a5c Mon Sep 17 00:00:00 2001 From: Mathis Hofer Date: Mon, 9 Dec 2024 13:08:18 +0100 Subject: [PATCH] Only load legal repr. & apprent. contracts for students #727 --- .../services/student-profile.service.spec.ts | 33 ++++++++++++++++++- .../services/student-profile.service.ts | 8 +++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/app/shared/services/student-profile.service.spec.ts b/src/app/shared/services/student-profile.service.spec.ts index 27104d1b5..d87dc08be 100644 --- a/src/app/shared/services/student-profile.service.spec.ts +++ b/src/app/shared/services/student-profile.service.spec.ts @@ -22,6 +22,7 @@ import { import { ApprenticeshipManager } from "../models/apprenticeship-manager.model"; import { DropDownItem } from "../models/drop-down-item.model"; import { JobTrainer } from "../models/job-trainer.model"; +import { StorageService } from "./storage.service"; describe("StudentProfileService", () => { let httpTestingController: HttpTestingController; @@ -37,9 +38,25 @@ describe("StudentProfileService", () => { let legalRepresentative2: Person; let persons: Person[]; let dropDownItems: DropDownItem[]; + let roles: string; beforeEach(() => { - TestBed.configureTestingModule(buildTestModuleMetadata({})); + roles = "StudentRole"; + + TestBed.configureTestingModule( + buildTestModuleMetadata({ + providers: [ + { + provide: StorageService, + useValue: { + getPayload: () => ({ + roles, + }), + }, + }, + ], + }), + ); httpTestingController = TestBed.inject(HttpTestingController); service = TestBed.inject(StudentProfileService); @@ -229,6 +246,20 @@ describe("StudentProfileService", () => { expectJobTrainerRequest(apprenticeshipContract.JobTrainer); } }); + + it("does not load legal representatives & apprenticeship contracts for non-student", () => { + roles = "TeacherRole"; + service.getMyProfile().subscribe((result: Profile) => { + expect(result).toEqual({ + student: myself, + stayPermitValue: "Permit Value", + legalRepresentativePersons: [], + apprenticeshipCompanies: [], + }); + }); + expectMyPersonRequest(); + expectLoadStayPermitValueRequest(); + }); }); function expectStudentRequest(id: number, response = student): void { diff --git a/src/app/shared/services/student-profile.service.ts b/src/app/shared/services/student-profile.service.ts index a4ea123c2..e95dd76d8 100644 --- a/src/app/shared/services/student-profile.service.ts +++ b/src/app/shared/services/student-profile.service.ts @@ -19,6 +19,7 @@ import { RestErrorInterceptorOptions } from "../interceptors/rest-error.intercep import { notNull } from "../utils/filter"; import { isAdult } from "../utils/persons"; import { DropDownItemsRestService } from "./drop-down-items-rest.service"; +import { StorageService } from "./storage.service"; export interface Profile { student: T; @@ -46,6 +47,7 @@ export class StudentProfileService { private jobTrainersService: JobTrainersRestService, private loadingService: LoadingService, private dropDownItemsService: DropDownItemsRestService, + private storageService: StorageService, ) {} /** @@ -65,6 +67,8 @@ export class StudentProfileService { * Returns the profile of the current user. */ getMyProfile(): Observable> { + const roles = this.storageService.getPayload()?.roles?.split(";") ?? []; + const isStudent = roles.includes("StudentRole"); return this.loadingService.load( this.personsService .getMyself({ @@ -76,8 +80,8 @@ export class StudentProfileService { switchMap((person) => combineLatest([ of(person), - this.loadLegalRepresentatives(person.Id), - this.loadApprenticeshipContracts(person.Id), + isStudent ? this.loadLegalRepresentatives(person.Id) : of([]), + isStudent ? this.loadApprenticeshipContracts(person.Id) : of([]), this.loadStayPermitValue(person.StayPermit), ]), ),