diff --git a/AdminWebsite/AdminWebsite/ClientApp/src/app/booking/judicial-office-holders/add-judicial-office-holders/add-judicial-office-holders.component.spec.ts b/AdminWebsite/AdminWebsite/ClientApp/src/app/booking/judicial-office-holders/add-judicial-office-holders/add-judicial-office-holders.component.spec.ts index f8ea6b4df..9857dce17 100644 --- a/AdminWebsite/AdminWebsite/ClientApp/src/app/booking/judicial-office-holders/add-judicial-office-holders/add-judicial-office-holders.component.spec.ts +++ b/AdminWebsite/AdminWebsite/ClientApp/src/app/booking/judicial-office-holders/add-judicial-office-holders/add-judicial-office-holders.component.spec.ts @@ -14,6 +14,7 @@ import { SearchForJudicialMemberComponent } from '../search-for-judicial-member/ import { JudicialService } from '../../services/judicial.service'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { PageUrls } from 'src/app/shared/page-url.constants'; +import { JudicialMemberDto } from '../models/add-judicial-member.model'; describe('AddJudicialOfficeHoldersComponent', () => { let component: AddJudicialOfficeHoldersComponent; @@ -111,4 +112,58 @@ describe('AddJudicialOfficeHoldersComponent', () => { expect(routerSpy.navigate).toHaveBeenCalledWith([PageUrls.AddParticipants]); }); }); + + describe('prepoplateFormForEdit', () => { + it('should set participantToEdit and editingJudge to true when participant role is Judge', () => { + // Arrange + const participantEmail = 'test@example.com'; + const judicialMember = new JudicialMemberDto('Test', 'User', 'Test User', participantEmail, '1234567890', '1234'); + judicialMember.roleCode = 'Judge'; + judicialMember.displayName = 'Test User display name'; + component.hearing.judiciaryParticipants = [judicialMember]; + + // Act + component.prepoplateFormForEdit(participantEmail); + + // Assert + expect(component.participantToEdit).toEqual(judicialMember); + expect(component.editingJudge).toBeTrue(); + expect(component.editingPanelMember).toBeFalse(); + expect(component.showAddPanelMember).toBeFalse(); + }); + + it('should set participantToEdit, editingPanelMember and showAddPanelMember to true when participant role is not Judge', () => { + // Arrange + const participantEmail = 'test@example.com'; + const panelMemberParticipant = new JudicialMemberDto('Test', 'User', 'Test User', participantEmail, '1234567890', '1234'); + panelMemberParticipant.roleCode = 'PanelMember'; + component.hearing.judiciaryParticipants = [panelMemberParticipant]; + + // Act + component.prepoplateFormForEdit(participantEmail); + + // Assert + expect(component.participantToEdit).toEqual(panelMemberParticipant); + expect(component.editingJudge).toBeFalse(); + expect(component.editingPanelMember).toBeTrue(); + expect(component.showAddPanelMember).toBeTrue(); + }); + + it('should log warning when participant is not found', () => { + // Arrange + const participantEmail = 'test@example.com'; + component.hearing.judiciaryParticipants = []; + + // Act + component.prepoplateFormForEdit(participantEmail); + + // Assert + expect(loggerSpy.warn.calls.mostRecent().args[0].includes('Unable to find participant to edit.')).toBeTrue(); + expect(loggerSpy.warn.calls.mostRecent().args[1]).toBe(participantEmail); + expect(component.participantToEdit).toBeNull(); + expect(component.editingJudge).toBeFalse(); + expect(component.editingPanelMember).toBeFalse(); + expect(component.showAddPanelMember).toBeFalse(); + }); + }); }); diff --git a/AdminWebsite/AdminWebsite/ClientApp/src/app/booking/judicial-office-holders/add-judicial-office-holders/add-judicial-office-holders.component.ts b/AdminWebsite/AdminWebsite/ClientApp/src/app/booking/judicial-office-holders/add-judicial-office-holders/add-judicial-office-holders.component.ts index afc821a6b..1df31d276 100644 --- a/AdminWebsite/AdminWebsite/ClientApp/src/app/booking/judicial-office-holders/add-judicial-office-holders/add-judicial-office-holders.component.ts +++ b/AdminWebsite/AdminWebsite/ClientApp/src/app/booking/judicial-office-holders/add-judicial-office-holders/add-judicial-office-holders.component.ts @@ -23,8 +23,8 @@ export class AddJudicialOfficeHoldersComponent implements OnInit, OnDestroy { showAddPanelMember = false; addPanelMemberText = this.noPanelMemberText; - editingJudge: boolean; - editingPanelMember: boolean; + editingJudge = false; + editingPanelMember = false; destroyed$ = new Subject(); @@ -51,7 +51,7 @@ export class AddJudicialOfficeHoldersComponent implements OnInit, OnDestroy { this.removeJudiciaryParticipant(participantEmail); }); this.participantsListComponent.$selectedForEdit.pipe(takeUntil(this.destroyed$)).subscribe(participant => { - this.editParticipant(participant); + this.prepoplateFormForEdit(participant); }); } @@ -60,11 +60,11 @@ export class AddJudicialOfficeHoldersComponent implements OnInit, OnDestroy { if (!emailToEdit) { return; } - this.editParticipant(emailToEdit); + this.prepoplateFormForEdit(emailToEdit); this.bookingService.removeParticipantEmail(); } - editParticipant(participantEmail: string) { + prepoplateFormForEdit(participantEmail: string) { const participantIndex = this.hearing.judiciaryParticipants.findIndex(x => x.email === participantEmail); if (participantIndex < 0) { this.logger.warn(`${this.loggerPrefix} Unable to find participant to edit.`, participantEmail); diff --git a/AdminWebsite/AdminWebsite/ClientApp/src/app/booking/participant/item/participant-item.component.spec.ts b/AdminWebsite/AdminWebsite/ClientApp/src/app/booking/participant/item/participant-item.component.spec.ts index 94bc689ee..9e426cd7a 100644 --- a/AdminWebsite/AdminWebsite/ClientApp/src/app/booking/participant/item/participant-item.component.spec.ts +++ b/AdminWebsite/AdminWebsite/ClientApp/src/app/booking/participant/item/participant-item.component.spec.ts @@ -7,6 +7,8 @@ import { Logger } from 'src/app/services/logger'; import { ParticipantItemComponent } from './participant-item.component'; import { VideoHearingsService } from 'src/app/services/video-hearings.service'; import { Constants } from 'src/app/common/constants'; +import { ParticipantModel } from 'src/app/common/model/participant.model'; +import { PageUrls } from 'src/app/shared/page-url.constants'; const router = { navigate: jasmine.createSpy('navigate'), @@ -70,11 +72,42 @@ describe('ParticipantItemComponent', () => { it('should edit participant details', () => { component.isSummaryPage = true; component.participant = { representee: 'rep', is_judge: false, is_exist_person: false, isJudiciaryMember: false }; - component.editParticipant({ email: 'email@hmcts.net', is_exist_person: false, is_judge: false, isJudiciaryMember: false }); + const participant: ParticipantModel = { + email: 'email@hmcts.net', + is_exist_person: false, + is_judge: false, + isJudiciaryMember: false + }; + component.editParticipant(participant); + fixture.detectChanges(); + expect(bookingServiceSpy.setEditMode).toHaveBeenCalled(); + expect(bookingServiceSpy.setParticipantEmail).toHaveBeenCalledWith(participant.email); + expect(router.navigate).toHaveBeenCalledWith([PageUrls.AddParticipants]); + }); + + it('should edit judicial office holder details', () => { + component.isSummaryPage = true; + component.participant = { representee: 'rep', is_judge: true, is_exist_person: false, isJudiciaryMember: true }; + const participant: ParticipantModel = { email: 'email@hmcts.net', is_exist_person: false, is_judge: true, isJudiciaryMember: true }; + component.editParticipant(participant); fixture.detectChanges(); expect(bookingServiceSpy.setEditMode).toHaveBeenCalled(); - expect(bookingServiceSpy.setEditMode).toHaveBeenCalledWith(); - expect(router.navigate).toHaveBeenCalled(); + expect(bookingServiceSpy.setParticipantEmail).toHaveBeenCalledWith(participant.email); + expect(router.navigate).toHaveBeenCalledWith([PageUrls.AddJudicialOfficeHolders]); + }); + + it('should emit edit event for non-summary page', () => { + component.isSummaryPage = false; + const participant: ParticipantModel = { + email: 'email@hmcts.net', + is_exist_person: false, + is_judge: false, + isJudiciaryMember: false + }; + spyOn(component.edit, 'emit'); + component.editParticipant(participant); + fixture.detectChanges(); + expect(component.edit.emit).toHaveBeenCalledWith(participant); }); it('should return true if participant has a representative', () => { diff --git a/AdminWebsite/AdminWebsite/ClientApp/src/app/bookings-list/booking-participant-list/booking-participant-list.component.spec.ts b/AdminWebsite/AdminWebsite/ClientApp/src/app/bookings-list/booking-participant-list/booking-participant-list.component.spec.ts index 2e57fde58..30121b487 100644 --- a/AdminWebsite/AdminWebsite/ClientApp/src/app/bookings-list/booking-participant-list/booking-participant-list.component.spec.ts +++ b/AdminWebsite/AdminWebsite/ClientApp/src/app/bookings-list/booking-participant-list/booking-participant-list.component.spec.ts @@ -7,6 +7,7 @@ import { BookingParticipantListComponent } from './booking-participant-list.comp import { HearingRoles } from '../../common/model/hearing-roles.model'; import { LinkedParticipant } from '../../services/clients/api-client'; import { ParticipantDetailsComponent } from '../participant-details/participant-details.component'; +import { JudiciaryParticipantDetailsModel } from 'src/app/common/model/judiciary-participant-details.model'; describe('BookingParticipantListComponent', () => { let component: BookingParticipantListComponent; @@ -209,4 +210,48 @@ describe('BookingParticipantListComponent', () => { } done(); }); + + it('should sort judiciary participants and members', () => { + const jp1 = new JudiciaryParticipantDetailsModel( + 'Mrs', + 'Alan', + 'Brake', + 'Judge', + 'email.p1@hmcts.net', + 'email1@hmcts.net', + 'Judge', + 'Judge', + 'Alan Brake' + ); + const jp2 = new JudiciaryParticipantDetailsModel( + 'Mr', + 'John', + 'Doe', + 'Winger', + 'email.p2@hmcts.net', + 'email2@hmcts.net', + 'Winger', + 'PanelMember', + 'John Doe' + ); + const jp3 = new JudiciaryParticipantDetailsModel( + 'Ms', + 'Jane', + 'Doe', + 'Panel Member', + 'email.p3@hmcts.net', + 'email3@hmcts.net', + 'Panel Member', + 'PanelMember', + 'Jane Doe' + ); + + const judiciaryParticipants = [jp1, jp2, jp3]; + component.judiciaryParticipants = judiciaryParticipants; + + expect(component.sortedJudiciaryMembers.length).toEqual(3); + expect(component.sortedJudiciaryMembers[0]).toEqual(jp1); + expect(component.sortedJudiciaryMembers[1]).toEqual(jp3); + expect(component.sortedJudiciaryMembers[2]).toEqual(jp2); + }); }); diff --git a/AdminWebsite/AdminWebsite/ClientApp/src/app/bookings-list/booking-participant-list/booking-participant-list.component.ts b/AdminWebsite/AdminWebsite/ClientApp/src/app/bookings-list/booking-participant-list/booking-participant-list.component.ts index 49b720bf3..ea49de937 100644 --- a/AdminWebsite/AdminWebsite/ClientApp/src/app/bookings-list/booking-participant-list/booking-participant-list.component.ts +++ b/AdminWebsite/AdminWebsite/ClientApp/src/app/bookings-list/booking-participant-list/booking-participant-list.component.ts @@ -100,7 +100,7 @@ export class BookingParticipantListComponent { } else if (!a.roleCode.includes('Judge') && b.roleCode.includes('Judge')) { return 1; } else { - return 0; + return a.firstName.localeCompare(b.firstName); } }); this.sortedJudiciaryMembers = sortedJohList; diff --git a/AdminWebsite/AdminWebsite/ClientApp/src/app/services/video-hearings.service.spec.ts b/AdminWebsite/AdminWebsite/ClientApp/src/app/services/video-hearings.service.spec.ts index 96545ae1f..483c48bf7 100644 --- a/AdminWebsite/AdminWebsite/ClientApp/src/app/services/video-hearings.service.spec.ts +++ b/AdminWebsite/AdminWebsite/ClientApp/src/app/services/video-hearings.service.spec.ts @@ -707,4 +707,43 @@ describe('Video hearing service', () => { expect(service['modelHearing'].judiciaryParticipants[0]).toEqual(judicialMember2); }); }); + + fdescribe('removeJudiciaryParticipant', () => { + it('should remove judiciary participant from modelHearing', () => { + // Arrange + const participantEmail = 'test@example.com'; + const judicialMember = new JudicialMemberDto('Test', 'User', 'Test User', participantEmail, '1234567890', '1234'); + service['modelHearing'].judiciaryParticipants = [judicialMember]; + + // Act + service.removeJudiciaryParticipant(participantEmail); + + // Assert + expect(service['modelHearing'].judiciaryParticipants).not.toContain(judicialMember); + }); + + it('should not remove judiciary participant if email does not match', () => { + // Arrange + const participantEmail = 'test@example.com'; + const judicialMember = new JudicialMemberDto('Test', 'User', 'Test User', participantEmail, '1234567890', '1234'); + service['modelHearing'].judiciaryParticipants = [judicialMember]; + + // Act + service.removeJudiciaryParticipant('other@example.com'); + + // Assert + expect(service['modelHearing'].judiciaryParticipants).toContain(judicialMember); + }); + + it('should not remove judiciary participant if modelHearing is undefined', () => { + // Arrange + service['modelHearing'] = undefined; + + // Act + service.removeJudiciaryParticipant('test@example.com'); + + // Assert + expect(service['modelHearing']).toBeUndefined(); + }); + }); }); diff --git a/AdminWebsite/AdminWebsite/ClientApp/src/app/services/video-hearings.service.ts b/AdminWebsite/AdminWebsite/ClientApp/src/app/services/video-hearings.service.ts index 13f8052d4..401d42dd7 100644 --- a/AdminWebsite/AdminWebsite/ClientApp/src/app/services/video-hearings.service.ts +++ b/AdminWebsite/AdminWebsite/ClientApp/src/app/services/video-hearings.service.ts @@ -571,7 +571,8 @@ export class VideoHearingsService { } removeJudiciaryParticipant(participantEmail: string) { - const index = this.modelHearing.judiciaryParticipants.findIndex(judicialMember => judicialMember.email === participantEmail); + const index = + this.modelHearing?.judiciaryParticipants?.findIndex(judicialMember => judicialMember.email === participantEmail) ?? -1; if (index !== -1) { this.modelHearing.judiciaryParticipants.splice(index, 1); }