Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cpp.ui.core",
"version": "19.0.5",
"version": "19.0.4-PRE-RELEASE-3RD-LINE-V4.1",
"repository": {
"type": "git",
"url": "https://github.com/hmcts/cpp-ui-core"
Expand Down
12 changes: 12 additions & 0 deletions projects/reference-data/src/actions/reference-data.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ export const loadOrganisationUnitsError = createAction(
props<{ error: HttpErrorResponse }>()
);

export const loadAllOrganisationUnits = createAction('LOAD_ALL_COURT_CENTRES');

export const loadAllOrganisationUnitsSuccess = createAction(
'LOAD_ALL_COURT_CENTRES_SUCCESS',
props<{ allOrganisationUnits: OrganisationUnit[] }>()
);

export const loadAllOrganisationUnitsError = createAction(
'LOAD_ALL_COURT_CENTRES_ERROR',
props<{ error: HttpErrorResponse }>()
);

export const loadFixedLists = createAction('LOAD_FIXED_LISTS');

export const loadFixedListsSuccess = createAction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { of, throwError } from 'rxjs';
import { ReferenceDataActions } from '../../actions/index';
import { referenceDataReducer, ReferenceDataState } from '../../reducers/index';
import { ReferenceDataService } from '../../services/reference-data.service';
import { createCourtCentreUnitsGuard } from '../court-centre-units.gard';
import { getAllCourtCentreUnitsGuard } from '../get-all-court-centre-units.guard';

describe('createCourtCentreUnitsGuard', () => {
describe('getAllCourtCentreUnitsGuard', () => {
let store: Store<ReferenceDataState>;

let fetchOrganisationUnits: jest.Mock;
Expand Down Expand Up @@ -62,45 +62,30 @@ describe('createCourtCentreUnitsGuard', () => {
expect.assertions(1);
const snapshot = createSnapshot();

store.dispatch(ReferenceDataActions.loadOrganisationUnitsSuccess({ organisationUnits: [] }));
store.dispatch(
ReferenceDataActions.loadAllOrganisationUnitsSuccess({ allOrganisationUnits: [] })
);

TestBed.runInInjectionContext(() =>
createCourtCentreUnitsGuard()(snapshot, {} as RouterStateSnapshot)
getAllCourtCentreUnitsGuard(snapshot, {} as RouterStateSnapshot)
).subscribe((didResolve) => {
expect(didResolve).toBe(true);
});
});

it('should resolve to true after fetching with includeExpired=false when not found in the store', () => {
it('should resolve to true after fetching the organisation units when not found in the store', () => {
expect.assertions(3);
const snapshot = createSnapshot();

fetchOrganisationUnits.mockReturnValue(of([]));

TestBed.runInInjectionContext(() =>
createCourtCentreUnitsGuard()(snapshot, {} as RouterStateSnapshot)
).subscribe((didResolve) => {
expect(didResolve).toBe(true);
expect(fetchOrganisationUnits).toHaveBeenCalledWith(false);
expect(store.dispatch).toHaveBeenCalledWith(
ReferenceDataActions.loadOrganisationUnitsSuccess({ organisationUnits: [] })
);
});
});

it('should resolve to true after fetching with includeExpired=true when not found in the store', () => {
expect.assertions(3);
const snapshot = createSnapshot();

fetchOrganisationUnits.mockReturnValue(of([]));

TestBed.runInInjectionContext(() =>
createCourtCentreUnitsGuard(true)(snapshot, {} as RouterStateSnapshot)
getAllCourtCentreUnitsGuard(snapshot, {} as RouterStateSnapshot)
).subscribe((didResolve) => {
expect(didResolve).toBe(true);
expect(fetchOrganisationUnits).toHaveBeenCalledWith(true);
expect(store.dispatch).toHaveBeenCalledWith(
ReferenceDataActions.loadOrganisationUnitsSuccess({ organisationUnits: [] })
ReferenceDataActions.loadAllOrganisationUnitsSuccess({ allOrganisationUnits: [] })
);
});
});
Expand All @@ -113,7 +98,7 @@ describe('createCourtCentreUnitsGuard', () => {
fetchOrganisationUnits.mockReturnValue(throwError(error));

TestBed.runInInjectionContext(() =>
createCourtCentreUnitsGuard()(snapshot, {} as RouterStateSnapshot)
getAllCourtCentreUnitsGuard(snapshot, {} as RouterStateSnapshot)
).subscribe((didResolve) => {
expect(didResolve).toBe(false);
expect(navigateByUrl).toHaveBeenCalledWith('/error-page');
Expand Down
52 changes: 0 additions & 52 deletions projects/reference-data/src/guards/court-centre-units.gard.ts

This file was deleted.

2 changes: 1 addition & 1 deletion projects/reference-data/src/guards/court-centres.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getOrganisationUnits, ReferenceDataState } from '../reducers/index';
import { ReferenceDataService } from '../services/reference-data.service';

/**
* @deprecated Use {@link createCourtCentreUnitsGuard} from `court-centre-units.gard` instead.
* @deprecated Use {@link getAllCourtCentreUnitsGuard} from `get-all-court-centre-units.guard` instead.
*/
@Injectable()
export class OrganisationUnitsGuard {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { inject } from '@angular/core';
import { ActivatedRouteSnapshot, Router, RouterStateSnapshot } from '@angular/router';
import { Store } from '@ngrx/store';
import { Observable, of } from 'rxjs';
import { catchError, map, switchMap, take, tap } from 'rxjs/operators';
import { ReferenceDataActions } from '../actions/index';
import { getAllOrganisationUnits, ReferenceDataState } from '../reducers/index';
import { ReferenceDataService } from '../services/reference-data.service';

export function getAllCourtCentreUnitsGuard(
route: ActivatedRouteSnapshot,
_state: RouterStateSnapshot
): Observable<boolean> {
const referenceDataService = inject(ReferenceDataService);
const store = inject(Store<ReferenceDataState>);
const router = inject(Router);
const { referenceDataErrorRedirectTo } = route.data as {
referenceDataErrorRedirectTo?: string;
};

return hasOrganisationUnitsInStore(store).pipe(
switchMap((inStore) =>
inStore ? of(true) : hasOrganisationUnitsInApi(store, referenceDataService)
),
tap({
error: () => {
if (referenceDataErrorRedirectTo) {
router.navigateByUrl(referenceDataErrorRedirectTo);
}
}
}),
catchError(() => of(false))
);
}

const hasOrganisationUnitsInStore = (store: Store<ReferenceDataState>): Observable<boolean> =>
store.pipe(
map(getAllOrganisationUnits),
map((allOrganisationUnits) => !!allOrganisationUnits),
take(1)
);

const hasOrganisationUnitsInApi = (
store: Store<ReferenceDataState>,
service: ReferenceDataService
) =>
service.fetchOrganisationUnits(true).pipe(
tap((allOrganisationUnits) =>
store.dispatch(ReferenceDataActions.loadAllOrganisationUnitsSuccess({ allOrganisationUnits }))
),
map(() => true)
);
2 changes: 1 addition & 1 deletion projects/reference-data/src/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export * from './guards/cps-business-units.guard';
export * from './guards/cps-case-status.guard';
export * from './guards/fixed-lists.guard';
export * from './guards/court-centres.guard';
export * from './guards/court-centre-units.gard';
export * from './guards/get-all-court-centre-units.guard';
export * from './guards/hearing-type.guard';
export * from './guards/local-justice-areas.guard';
export * from './guards/plea-types.guard';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,34 @@ exports[`referenceDataReducer Local Justice Areas ReferenceDataActions.loadLocal
}
`;

exports[`referenceDataReducer all court centres HearingResultsActions.loadAllOrganisationUnitsError should remove the all court centres key 1`] = `{}`;

exports[`referenceDataReducer all court centres HearingResultsActions.loadAllOrganisationUnitsSuccess should add the all court centres to the state 1`] = `
{
"allOrganisationUnits": [
{
"id": "*",
},
],
}
`;

exports[`referenceDataReducer all court centres ReferenceDataActions.loadAllOrganisationUnits should install the all court centres key 1`] = `
{
"allOrganisationUnits": null,
}
`;

exports[`referenceDataReducer all court centres ReferenceDataActions.loadAllOrganisationUnits should not overwrite any existing all court centres 1`] = `
{
"allOrganisationUnits": [
{
"id": "*",
},
],
}
`;

exports[`referenceDataReducer application types HearingResultsActions.loadApplicationTypesError should remove the application types key 1`] = `{}`;

exports[`referenceDataReducer application types HearingResultsActions.loadApplicationTypesSuccess should add the application types to the state 1`] = `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,49 @@ describe('referenceDataReducer', () => {
});
});

describe('all court centres', () => {
describe('ReferenceDataActions.loadAllOrganisationUnits', () => {
it('should install the all court centres key', () => {
const action = ReferenceDataActions.loadAllOrganisationUnits();
const result = fromReferenceData.referenceDataReducer(undefined, action);

expect(result).toMatchSnapshot();
});

it('should not overwrite any existing all court centres', () => {
const action = ReferenceDataActions.loadAllOrganisationUnits();
const result = fromReferenceData.referenceDataReducer(
{ allOrganisationUnits: [{ id: '*' }] as OrganisationUnit[] },
action
);
expect(result).toMatchSnapshot();
});
});

describe('HearingResultsActions.loadAllOrganisationUnitsSuccess', () => {
it('should add the all court centres to the state', () => {
const action = ReferenceDataActions.loadAllOrganisationUnitsSuccess({
allOrganisationUnits: [{ id: '*' }] as OrganisationUnit[]
});
const result = fromReferenceData.referenceDataReducer(undefined, action);

expect(result).toMatchSnapshot();
});
});

describe('HearingResultsActions.loadAllOrganisationUnitsError', () => {
it('should remove the all court centres key', () => {
const action = ReferenceDataActions.loadAllOrganisationUnitsError({ error: {} as any });
const result = fromReferenceData.referenceDataReducer(
{ allOrganisationUnits: null },
action
);

expect(result).toMatchSnapshot();
});
});
});

describe('Local Justice Areas', () => {
describe('ReferenceDataActions.loadLocalJusticeAreas', () => {
it('should install the localJusticeAreas', () => {
Expand Down
6 changes: 6 additions & 0 deletions projects/reference-data/src/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ export const getOrganisationUnits = (state: ReferenceDataState) =>
export const getOrganisationUnitsFetching = (state: ReferenceDataState) =>
state.referenceData.organisationUnits === null;

export const getAllOrganisationUnits = (state: ReferenceDataState) =>
state.referenceData.allOrganisationUnits;

export const getAllOrganisationUnitsFetching = (state: ReferenceDataState) =>
state.referenceData.allOrganisationUnits === null;

export const getPoliceForceList = (state: ReferenceDataState) =>
state.referenceData.policeForceList;

Expand Down
21 changes: 21 additions & 0 deletions projects/reference-data/src/reducers/reference-data.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface ReferenceDataState {
judiciaryGroupTypes?: JudiciaryGroupType[] | null;
localJusticeAreas?: LocalJusticeArea[] | null;
organisationUnits?: OrganisationUnit[] | null;
allOrganisationUnits?: OrganisationUnit[] | null;
policeForceList?: PoliceForce[] | null;
prosecutors?: Prosecutor[] | null;
resultDefinitions?: ResultDefinition[] | null;
Expand Down Expand Up @@ -276,6 +277,26 @@ export const referenceData = createReducer(
)
),

// All organisation units (including expired)

on(ReferenceDataActions.loadAllOrganisationUnits, (state) => ({
...state,
allOrganisationUnits: state.allOrganisationUnits || null
})),
on(ReferenceDataActions.loadAllOrganisationUnitsSuccess, (state, { allOrganisationUnits }) => ({
...state,
allOrganisationUnits
})),
on(ReferenceDataActions.loadAllOrganisationUnitsError, (state) =>
Object.keys(state).reduce(
(nextState, key) =>
key !== 'allOrganisationUnits' || state[key as keyof ReferenceDataState] !== null
? { ...nextState, [key]: state[key as keyof ReferenceDataState] }
: nextState,
{}
)
),

// Plea types

on(ReferenceDataActions.loadPleaTypes, (state) => ({
Expand Down
Loading