Skip to content
Open
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
10 changes: 3 additions & 7 deletions src/main/controllers/SummaryOfPublicationsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ import { PipRequest } from '../models/request/PipRequest';
import { Response } from 'express';
import { cloneDeep } from 'lodash';
import { LocationService } from '../service/LocationService';
import { SummaryOfPublicationsService } from '../service/SummaryOfPublicationsService';
import { PublicationService } from '../service/PublicationService';

const summaryOfPublicationsService = new SummaryOfPublicationsService();
const locationService = new LocationService();
const publicationService = new PublicationService();

export default class SummaryOfPublicationsController {
public async get(req: PipRequest, res: Response): Promise<void> {
const locationId = req.query['locationId'] as string;
if (locationId) {

if (locationId && !isNaN(parseInt(locationId))) {
const court = await locationService.getLocationById(parseInt(locationId));
const locationName = locationService.findCourtName(court, req.lng, 'summary-of-publications');
const locationMetadata = await locationService.getLocationMetadata(parseInt(locationId));
Expand All @@ -26,10 +25,7 @@ export default class SummaryOfPublicationsController {
req.lng === 'cy' ? locationMetadata.welshCautionMessage : locationMetadata.cautionMessage;
}

const publications = await summaryOfPublicationsService.getPublications(
parseInt(locationId.toString()),
req.user?.['userId']
);
const publications = await publicationService.getPublicationsByLocation(locationId, req.user?.['userId']);

const publicationsWithName = [];
publications.forEach(publication => {
Expand Down
12 changes: 6 additions & 6 deletions src/main/controllers/admin/RemoveListSearchResultsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ import { PipRequest } from '../../models/request/PipRequest';
import { Response } from 'express';
import { cloneDeep } from 'lodash';
import { LocationService } from '../../service/LocationService';
import { SummaryOfPublicationsService } from '../../service/SummaryOfPublicationsService';
import { ManualUploadService } from '../../service/ManualUploadService';
import * as url from 'url';
import { checkIfUrl } from '../../helpers/urlHelper';
import { PublicationService } from '../../service/PublicationService';

const courtService = new LocationService();
const summaryOfPublicationsService = new SummaryOfPublicationsService();
const publicationsService = new PublicationService();
const manualUploadService = new ManualUploadService();

export default class RemoveListSearchResultsController {
public async get(req: PipRequest, res: Response): Promise<void> {
const locationId = parseInt(req.query?.locationId as string);
const locationId = req.query?.locationId as string;
const noOptionSelectedError = req.query?.error;
locationId
locationId && !isNaN(parseInt(locationId))
? res.render('admin/remove-list-search-results', {
...cloneDeep(req.i18n.getDataByLanguage(req.lng)['remove-list-search-results']),
court: await courtService.getLocationById(locationId),
court: await courtService.getLocationById(parseInt(locationId)),
removalList: manualUploadService.formatListRemovalValues(
await summaryOfPublicationsService.getPublications(locationId, req.user?.['userId'], true)
await publicationsService.getPublicationsByLocation(locationId, req.user?.['userId'], true)
),
noOptionSelectedError: noOptionSelectedError,
})
Expand Down
42 changes: 23 additions & 19 deletions src/main/controllers/system-admin/BlobViewPublicationsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { PipRequest } from '../../models/request/PipRequest';
import { Response } from 'express';
import { cloneDeep } from 'lodash';
import { LocationService } from '../../service/LocationService';
import { SummaryOfPublicationsService } from '../../service/SummaryOfPublicationsService';
import { PublicationService } from '../../service/PublicationService';

const summaryOfPublicationsService = new SummaryOfPublicationsService();
const publicationService = new PublicationService();
const locationService = new LocationService();
export default class BlobViewPublicationsController {
public async get(req: PipRequest, res: Response): Promise<void> {
Expand All @@ -16,25 +16,29 @@ export default class BlobViewPublicationsController {

// reusing summary-of-pubs language file and service as this is essentially the same kind of page.
// If the location being asked for is noMatch we do not need to request data from the API as it is not a real location
const noMatchArtefact = locationId.toString() === 'noMatch';
if (!noMatchArtefact) {
court = await locationService.getLocationById(parseInt(locationId.toString()));
locationName = locationService.findCourtName(court, req.lng, 'summary-of-publications');
listOfPublications = await summaryOfPublicationsService.getPublications(
parseInt(locationId.toString()),
req.user['userId']
);
} else {
const noMatchArtefact = locationId === 'noMatch';
if (locationId === 'noMatch') {
locationName = 'No match artefacts';
listOfPublications = await summaryOfPublicationsService.getNoMatchPublications(req.user['userId']);
listOfPublications = await publicationService.getNoMatchPublications(req.user['userId']);
res.render('system-admin/blob-view-publications', {
...cloneDeep(req.i18n.getDataByLanguage(req.lng)['blob-view-publications']),
listOfPublications: listOfPublications,
locationName,
noMatchArtefact,
});
} else if (isNaN(parseInt(locationId))) {
res.render('error', req.i18n.getDataByLanguage(req.lng).error);
} else {
court = await locationService.getLocationById(parseInt(locationId));
locationName = locationService.findCourtName(court, req.lng, 'summary-of-publications');
listOfPublications = await publicationService.getPublicationsByLocation(locationId, req.user['userId']);
res.render('system-admin/blob-view-publications', {
...cloneDeep(req.i18n.getDataByLanguage(req.lng)['blob-view-publications']),
listOfPublications: listOfPublications,
locationName,
noMatchArtefact,
});
}

res.render('system-admin/blob-view-publications', {
...cloneDeep(req.i18n.getDataByLanguage(req.lng)['blob-view-publications']),
listOfPublications: listOfPublications,
locationName,
noMatchArtefact,
});
} else {
res.render('error', req.i18n.getDataByLanguage(req.lng).error);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/requests/PublicationRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class PublicationRequests {
}
}

public async getPublicationsByCourt(locationId: string, userId: string, admin: boolean): Promise<Artefact[]> {
public async getPublicationsByLocation(locationId: string, userId: string, admin: boolean): Promise<Artefact[]> {
try {
let header;
if (userId) {
Expand Down
8 changes: 6 additions & 2 deletions src/main/service/PublicationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ export class PublicationService {
return this.getCaseFromArtefact(artefact[0], 'caseUrn', urn);
}

public async getPublicationsByCourt(locationId: string, userId: string): Promise<Artefact[]> {
return await publicationRequests.getPublicationsByCourt(locationId, userId, false);
public async getPublicationsByLocation(locationId: string, userId: string, admin = false): Promise<Artefact[]> {
return await publicationRequests.getPublicationsByLocation(locationId, userId, admin);
}

public async getNoMatchPublications(userId: string): Promise<Artefact[]> {
return publicationRequests.getNoMatchPublications(userId);
}

private getCaseFromArtefact(artefact: Artefact, term: string, value: string): SearchObject {
Expand Down
14 changes: 0 additions & 14 deletions src/main/service/SummaryOfPublicationsService.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/test/a11y/tests/admin-routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const userData = testUserData();
sinon.stub(LocationRequests.prototype, 'getLocation').resolves(locationData[0]);
sinon.stub(LocationRequests.prototype, 'getFilteredCourts').resolves(locationData);
sinon.stub(LocationRequests.prototype, 'getAllLocations').resolves(locationData);
sinon.stub(PublicationRequests.prototype, 'getPublicationsByCourt').resolves(metadata);
sinon.stub(PublicationRequests.prototype, 'getPublicationsByLocation').resolves(metadata);
sinon.stub(PublicationRequests.prototype, 'getIndividualPublicationMetadata').resolves(metadata[0]);
sinon.stub(AccountManagementRequests.prototype, 'getPendingMediaApplications').resolves(mediaApplications);
sinon.stub(AccountManagementRequests.prototype, 'getMediaApplicationById').resolves(mediaApplications[0]);
Expand Down
2 changes: 1 addition & 1 deletion src/test/a11y/tests/media-routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const subscriptionSearchResults = {
sinon.stub(LocationRequests.prototype, 'getLocation').resolves(locationData[0]);
sinon.stub(LocationRequests.prototype, 'getFilteredCourts').resolves(locationData);
sinon.stub(LocationRequests.prototype, 'getAllLocations').resolves(locationData);
sinon.stub(PublicationRequests.prototype, 'getPublicationsByCourt').resolves(metadata);
sinon.stub(PublicationRequests.prototype, 'getPublicationsByLocation').resolves(metadata);
sinon.stub(PublicationRequests.prototype, 'getIndividualPublicationMetadata').returns(metadata[0]);
sinon.stub(PublicationService.prototype, 'getCasesByCaseName').resolves([subscriptionSearchResults]);
sinon.stub(PublicationService.prototype, 'getCaseByCaseNumber').resolves(subscriptionSearchResults);
Expand Down
5 changes: 2 additions & 3 deletions src/test/a11y/tests/public-routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { testArtefactMetadata, testLocationData } from '../common/testData';
import { filterRoutes, testAccessibility } from '../common/pa11yHelper';
import { app } from '../../../main/app';
import { ssoNotAuthorised } from '../../../main/helpers/consts';
import { SummaryOfPublicationsService } from '../../../main/service/SummaryOfPublicationsService';
import { PublicationService } from '../../../main/service/PublicationService';

const publicRoutes = [
Expand Down Expand Up @@ -35,8 +34,8 @@ const metadata = testArtefactMetadata();
sinon.stub(LocationRequests.prototype, 'getLocation').resolves(locationData[0]);
sinon.stub(LocationRequests.prototype, 'getFilteredCourts').resolves(locationData);
sinon.stub(LocationRequests.prototype, 'getAllLocations').resolves(locationData);
sinon.stub(PublicationRequests.prototype, 'getPublicationsByCourt').resolves(metadata[0]);
sinon.stub(SummaryOfPublicationsService.prototype, 'getPublications').resolves(metadata);
sinon.stub(PublicationRequests.prototype, 'getPublicationsByLocation').resolves(metadata[0]);
sinon.stub(PublicationService.prototype, 'getPublicationsByLocation').resolves(metadata);
sinon.stub(PublicationService.prototype, 'getListTypes').returns(
new Map([
['CROWN_WARNED_LIST', { friendlyName: 'List A' }],
Expand Down
2 changes: 1 addition & 1 deletion src/test/a11y/tests/system-admin-routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ sinon.stub(LocationRequests.prototype, 'getLocation').resolves(locationData[0]);
sinon.stub(LocationRequests.prototype, 'getLocationMetadata').resolves({ locationMetadataId: '123' });
sinon.stub(LocationRequests.prototype, 'getFilteredCourts').resolves(locationData);
sinon.stub(LocationRequests.prototype, 'getAllLocations').resolves(locationData);
sinon.stub(PublicationRequests.prototype, 'getPublicationsByCourt').resolves(metadata);
sinon.stub(PublicationRequests.prototype, 'getPublicationsByLocation').resolves(metadata);
sinon.stub(PublicationRequests.prototype, 'getIndividualPublicationMetadata').returns(metadata[0]);
sinon.stub(PublicationRequests.prototype, 'getPubsPerLocation').returns(countPerLocation);
sinon.stub(AccountManagementRequests.prototype, 'getUserByUserId').resolves(userDataThirdParty);
Expand Down
4 changes: 2 additions & 2 deletions src/test/routes/admin/remove-list-search-results.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { expect } from 'chai';
import request from 'supertest';
import sinon from 'sinon';
import { LocationService } from '../../../main/service/LocationService';
import { SummaryOfPublicationsService } from '../../../main/service/SummaryOfPublicationsService';
import { ManualUploadService } from '../../../main/service/ManualUploadService';
import { request as expressRequest } from 'express';
import { PublicationService } from '../../../main/service/PublicationService';

const URL = '/remove-list-search';

const courtServiceStub = sinon.stub(LocationService.prototype, 'getLocationById');
sinon.stub(SummaryOfPublicationsService.prototype, 'getPublications').withArgs('2', true, true).resolves([]);
sinon.stub(PublicationService.prototype, 'getPublicationsByLocation').withArgs('2').resolves([]);
sinon.stub(ManualUploadService.prototype, 'formatListRemovalValues').withArgs([]).returns([]);
courtServiceStub.withArgs('2').resolves(true);
courtServiceStub.withArgs('888').resolves(false);
Expand Down
3 changes: 1 addition & 2 deletions src/test/routes/summary-of-publications.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import { app } from '../../main/app';
import request from 'supertest';
import { PublicationService } from '../../main/service/PublicationService';
import sinon from 'sinon';
import { SummaryOfPublicationsService } from '../../main/service/SummaryOfPublicationsService';

const mockJSON = '{"data":"false"}';
const mockArray = '[{"listType": "List_A"}]';
sinon.stub(PublicationService.prototype, 'getIndividualPublicationJson').resolves(mockJSON);
sinon.stub(SummaryOfPublicationsService.prototype, 'getPublications').resolves(JSON.parse(mockArray));
sinon.stub(PublicationService.prototype, 'getPublicationsByLocation').resolves(JSON.parse(mockArray));
sinon
.stub(PublicationService.prototype, 'getListTypes')
.returns(new Map([['List_A', { friendlyName: 'List name A' }]]));
Expand Down
25 changes: 22 additions & 3 deletions src/test/unit/controllers/SummaryOfPublicationsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import sinon from 'sinon';
import fs from 'fs';
import path from 'path';
import { LocationService } from '../../../main/service/LocationService';
import { SummaryOfPublicationsService } from '../../../main/service/SummaryOfPublicationsService';
import { PublicationService } from '../../../main/service/PublicationService';

const publicationController = new SummaryOfPublicationsController();
const i18n = {
'list-option': {},
error: { title: 'error' },
};
const court = { name: 'New Court', email: '[email protected]', contactNo: '0123456789' };

Expand All @@ -28,7 +28,7 @@ const additionalLocationInfo = {
sinon
.stub(LocationService.prototype, 'getLocationById')
.resolves(JSON.parse('{"name":"New Court", "email": "[email protected]", "contactNo": "0123456789"}'));
sinon.stub(SummaryOfPublicationsService.prototype, 'getPublications').resolves(metadata);
sinon.stub(PublicationService.prototype, 'getPublicationsByLocation').resolves(metadata);

const additionalLocationInfoStub = sinon.stub(LocationService.prototype, 'getLocationMetadata');
additionalLocationInfoStub.withArgs(1).returns(null);
Expand Down Expand Up @@ -139,6 +139,22 @@ describe('Get publications', () => {
responseMock.verify();
});

it('should render the error page if location ID is no a number', async () => {
const response = {
render: () => {
return '';
},
} as unknown as Response;
const request = mockRequest(i18n);
request.query = { locationId: 'Test2' };
request.user = { userId: 1 };
const responseMock = sinon.mock(response);
responseMock
.expects('render')
.once()
.withArgs('error', { ...i18n.error });
});

it('should render the error screen if there is no locationId passed as a param', async () => {
const response = {
render: () => {
Expand All @@ -148,6 +164,9 @@ describe('Get publications', () => {
const request = mockRequest(i18n);
request.user = { userId: 1 };
const responseMock = sinon.mock(response);
responseMock.expects('render').once().withArgs('error');
responseMock
.expects('render')
.once()
.withArgs('error', { ...i18n.error });
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import sinon from 'sinon';
import { LocationService } from '../../../../main/service/LocationService';
import { SummaryOfPublicationsService } from '../../../../main/service/SummaryOfPublicationsService';
import { ManualUploadService } from '../../../../main/service/ManualUploadService';
import { Response } from 'express';
import { mockRequest } from '../../mocks/mockRequest';
Expand Down Expand Up @@ -62,7 +61,7 @@ const removeListFormData = { courtLists: ['valid-artefact', 'valid-artefact'], l
sinon.stub(PublicationService.prototype, 'getIndividualPublicationMetadata').resolves(mockArtefactsArray);
sinon.stub(ManualUploadService.prototype, 'formatListRemovalValues').returns(mockArtefactsArray);
sinon.stub(LocationService.prototype, 'getLocationById').resolves(mockCourt);
sinon.stub(SummaryOfPublicationsService.prototype, 'getPublications').withArgs('5', true, true).resolves([]);
sinon.stub(PublicationService.prototype, 'getPublicationsByLocation').withArgs('5').resolves([]);

const removeListSearchResultsController = new RemoveListSearchResultsController();

Expand All @@ -84,7 +83,7 @@ describe('Remove List Summary Controller', () => {
await responseMock.verify();
});

it('should render error page', async () => {
it('should render error page if no lopcation ID', async () => {
const request = mockRequest(i18n);
const responseMock = sinon.mock(response);
request.query = {};
Expand All @@ -95,6 +94,19 @@ describe('Remove List Summary Controller', () => {
await removeListSearchResultsController.get(request, response);
await responseMock.verify();
});

it('should render error page if location ID not an integer', async () => {
const responseMock = sinon.mock(response);
const request = mockRequest(i18n);
request.query = { locationId: 'Test5' };

responseMock
.expects('render')
.once()
.withArgs('error', { ...i18n.error });
await removeListSearchResultsController.get(request, response);
await responseMock.verify();
});
});

describe('POST request', () => {
Expand Down
Loading