Skip to content

Commit

Permalink
Merge branch 'master' into 1125-automated-publishing-of-redacted-mine…
Browse files Browse the repository at this point in the history
…s-act-inspection-reports
  • Loading branch information
sggerard authored Jun 19, 2024
2 parents dbf7abc + 74fee6d commit e93e177
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,34 @@ import { of } from 'rxjs';
import { ActivatedRouteSnapshot } from '@angular/router';
import { TableObject } from 'nrpti-angular-components';
import { EpicProjectIds } from '../../../../common/src/app/utils/record-constants';
import { RecordUtils } from './utils/record-utils';

describe('RecordsResolver', () => {
const spyFactoryService = jasmine.createSpyObj<FactoryService>('FactoryService', ['getRecords']);
const spyTableTemplateUtils = jasmine.createSpyObj<TableTemplateUtils>('TableTemplateUtils', [
'updateTableObjectWithUrlParams'
]);
const spyRecordUtils = jasmine.createSpyObj<RecordUtils>('RecordUtils', ['appendActCodesToActNames']);

beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{ provide: FactoryService, useValue: spyFactoryService },
{ provide: TableTemplateUtils, useValue: spyTableTemplateUtils },
{ provide: RecordUtils, useValue: spyRecordUtils }
{ provide: TableTemplateUtils, useValue: spyTableTemplateUtils }
]
}).compileComponents();
});

it('should create', async(() => {
const factoryService = TestBed.get(FactoryService);
const tableTemplateUtils = TestBed.get(TableTemplateUtils);
const recordUtils = TestBed.get(RecordUtils);

const recordsResolver = new RecordsResolver(factoryService, tableTemplateUtils, recordUtils);
const recordsResolver = new RecordsResolver(factoryService, tableTemplateUtils);

expect(recordsResolver).toBeTruthy();
}));

describe('resolve', () => {
let factoryServiceSpy: jasmine.SpyObj<FactoryService>;
let tableTemplateUtilsSpy: jasmine.SpyObj<TableTemplateUtils>;
let recordUtilsSpy: jasmine.SpyObj<RecordUtils>;

describe('with empty/null route parameters', () => {
const activatedRouteSnapshot: ActivatedRouteSnapshot = {
Expand All @@ -62,13 +57,12 @@ describe('RecordsResolver', () => {
beforeAll(async(() => {
factoryServiceSpy = TestBed.get(FactoryService);
tableTemplateUtilsSpy = TestBed.get(TableTemplateUtils);
recordUtilsSpy = TestBed.get(RecordUtils);

factoryServiceSpy.getRecords.calls.reset();
factoryServiceSpy.getRecords.and.returnValue(of({}));
tableTemplateUtilsSpy.updateTableObjectWithUrlParams.and.returnValue({ currentPage: 1, pageSize: 25 });

const recordsResolver = new RecordsResolver(factoryServiceSpy, tableTemplateUtilsSpy, recordUtilsSpy);
const recordsResolver = new RecordsResolver(factoryServiceSpy, tableTemplateUtilsSpy);

recordsResolver.resolve(activatedRouteSnapshot);
}));
Expand Down Expand Up @@ -139,13 +133,12 @@ describe('RecordsResolver', () => {
beforeAll(async(() => {
factoryServiceSpy = TestBed.get(FactoryService);
tableTemplateUtilsSpy = TestBed.get(TableTemplateUtils);
recordUtilsSpy = TestBed.get(RecordUtils);

factoryServiceSpy.getRecords.calls.reset();
factoryServiceSpy.getRecords.and.returnValue(of({}));
tableTemplateUtilsSpy.updateTableObjectWithUrlParams.and.returnValue({ currentPage: 1, pageSize: 25 });

const recordsResolver = new RecordsResolver(factoryServiceSpy, tableTemplateUtilsSpy, recordUtilsSpy);
const recordsResolver = new RecordsResolver(factoryServiceSpy, tableTemplateUtilsSpy);

recordsResolver.resolve(activatedRouteSnapshot);
}));
Expand Down Expand Up @@ -216,13 +209,12 @@ describe('RecordsResolver', () => {
beforeAll(async(() => {
factoryServiceSpy = TestBed.get(FactoryService);
tableTemplateUtilsSpy = TestBed.get(TableTemplateUtils);
recordUtilsSpy = TestBed.get(RecordUtils);

factoryServiceSpy.getRecords.calls.reset();
factoryServiceSpy.getRecords.and.returnValue(of({}));
tableTemplateUtilsSpy.updateTableObjectWithUrlParams.and.returnValue({ currentPage: 1, pageSize: 25 });

const recordsResolver = new RecordsResolver(factoryServiceSpy, tableTemplateUtilsSpy, recordUtilsSpy);
const recordsResolver = new RecordsResolver(factoryServiceSpy, tableTemplateUtilsSpy);

recordsResolver.resolve(activatedRouteSnapshot);
}));
Expand Down Expand Up @@ -293,13 +285,12 @@ describe('RecordsResolver', () => {
beforeAll(async(() => {
factoryServiceSpy = TestBed.get(FactoryService);
tableTemplateUtilsSpy = TestBed.get(TableTemplateUtils);
recordUtilsSpy = TestBed.get(RecordUtils);

factoryServiceSpy.getRecords.calls.reset();
factoryServiceSpy.getRecords.and.returnValue(of({}));
tableTemplateUtilsSpy.updateTableObjectWithUrlParams.and.returnValue({ currentPage: 1, pageSize: 25 });

const recordsResolver = new RecordsResolver(factoryServiceSpy, tableTemplateUtilsSpy, recordUtilsSpy);
const recordsResolver = new RecordsResolver(factoryServiceSpy, tableTemplateUtilsSpy);

recordsResolver.resolve(activatedRouteSnapshot);
}));
Expand Down Expand Up @@ -370,13 +361,12 @@ describe('RecordsResolver', () => {
beforeAll(async(() => {
factoryServiceSpy = TestBed.get(FactoryService);
tableTemplateUtilsSpy = TestBed.get(TableTemplateUtils);
recordUtilsSpy = TestBed.get(RecordUtils);

factoryServiceSpy.getRecords.calls.reset();
factoryServiceSpy.getRecords.and.returnValue(of({}));
tableTemplateUtilsSpy.updateTableObjectWithUrlParams.and.returnValue({ currentPage: 1, pageSize: 25 });

const recordsResolver = new RecordsResolver(factoryServiceSpy, tableTemplateUtilsSpy, recordUtilsSpy);
const recordsResolver = new RecordsResolver(factoryServiceSpy, tableTemplateUtilsSpy);

recordsResolver.resolve(activatedRouteSnapshot);
}));
Expand Down Expand Up @@ -447,13 +437,12 @@ describe('RecordsResolver', () => {
beforeAll(async(() => {
factoryServiceSpy = TestBed.get(FactoryService);
tableTemplateUtilsSpy = TestBed.get(TableTemplateUtils);
recordUtilsSpy = TestBed.get(RecordUtils);

factoryServiceSpy.getRecords.calls.reset();
factoryServiceSpy.getRecords.and.returnValue(of({}));
tableTemplateUtilsSpy.updateTableObjectWithUrlParams.and.returnValue({ currentPage: 1, pageSize: 25 });

const recordsResolver = new RecordsResolver(factoryServiceSpy, tableTemplateUtilsSpy, recordUtilsSpy);
const recordsResolver = new RecordsResolver(factoryServiceSpy, tableTemplateUtilsSpy);

recordsResolver.resolve(activatedRouteSnapshot);
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@ import { Observable } from 'rxjs/Observable';
import { TableTemplateUtils, TableObject } from 'nrpti-angular-components';
import { FactoryService } from '../services/factory.service';
import { EpicProjectIds, SchemaLists } from '../../../../common/src/app/utils/record-constants';
import { RecordUtils } from './utils/record-utils';

@Injectable()
export class RecordsResolver implements Resolve<Observable<object>> {
constructor(
private factoryService: FactoryService,
private tableTemplateUtils: TableTemplateUtils,
private recordUtils: RecordUtils
) {}
constructor(private factoryService: FactoryService, private tableTemplateUtils: TableTemplateUtils) {}

resolve(route: ActivatedRouteSnapshot): Observable<object> {
const params = { ...route.params };
Expand Down Expand Up @@ -59,7 +54,7 @@ export class RecordsResolver implements Resolve<Observable<object>> {
}

if (params.act) {
or['legislation.act'] = this.recordUtils.appendActCodesToActNames(params.act, this.factoryService);
or['legislation.act'] = params.act;
}

if (params.regulation) {
Expand Down
18 changes: 0 additions & 18 deletions angular/projects/admin-nrpti/src/app/records/utils/record-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,24 +273,6 @@ export class RecordUtils {
record.legislation[0].act = actCode;
}

/**
* Adds the act code to a list of act names for a search query
* @param {Object} actsSTring - a string of comma-seperated act names.
* @param {ServiceFactory} factoryService - The service factory used to create data service instances.
* @returns {string} a string with comma-serparated act names followed by comma sperated act codes
*/
appendActCodesToActNames(actsString, factoryService) {
const dataservice = new ActDataServiceNRPTI(factoryService);
const actList = actsString.split(',');
actList.forEach(actName => {
const actCode = dataservice.getCodeFromTitle(actName);
if (actCode) {
actsString += ',' + actCode;
}
});
return actsString;
}

/**
* Replaces the 'act' value in the given record object with a corresponding act code.
* @param {string} actCode - an intermediate code mapped to a title
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ export class RecordsListResolver implements Resolve<Observable<object>> {
schemaList = params.activityType.split(',');
}

if (params.act) {
params.act = RecordUtils.appendActCodesToActNames(params.act, this.factoryService);
}

let keywords = '';
if (params.keywords) {
keywords = params.keywords;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export class RecordUtils {
output = `${csvHeaders.join(',')}\n`;

const agencyDataService = new AgencyDataService(factoryService);
const dataService = new ActDataServiceNRCED(factoryService);

for (const row of data) {
let line = [];
Expand All @@ -151,7 +152,14 @@ export class RecordUtils {
const legislation = Array.isArray(row['legislation']) ? row['legislation'][0] : row['legislation'];

if (legislation) {
line.push(escapeCsvString(legislation['act']));
if (legislation['act'] && legislation['act'].startsWith('ACT_')) {
const actTitle = dataService.displayActTitleFull(legislation['act']);
if (actTitle) {
line.push(escapeCsvString(actTitle));
}
} else {
line.push(escapeCsvString(legislation['act']));
}
line.push(escapeCsvString(legislation['regulation']));
line.push(escapeCsvString(legislation['section']));
line.push(escapeCsvString(legislation['subSection']));
Expand Down Expand Up @@ -189,24 +197,6 @@ export class RecordUtils {

download(`nrced-export-${moment().format('YYYY-MM-DD')}.csv`, output);
}

/**
* Adds the act code to a list of act names for a search query
* @param {Object} actsSTring - a string of comma-seperated act names.
* @param {ServiceFactory} factoryService - The service factory used to create data service instances.
* @returns {string} a string with comma-serparated act names followed by comma sperated act codes
*/
static appendActCodesToActNames(actsString, factoryService) {
const dataservice = new ActDataServiceNRCED(factoryService);
const actList = actsString.split(',');
actList.forEach(actName => {
const actCode = dataservice.getCodeFromTitle(actName);
if (actCode) {
actsString += ',' + actCode;
}
});
return actsString;
}
}

/**
Expand Down
13 changes: 13 additions & 0 deletions api/src/controllers/acts-regulations-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@ exports.getActTitleFromDB = async function(actCode){
}
}

exports.getActCodeFromActTitle = async function(actName){
try{
const db = mongodb.connection.db(process.env.MONGODB_DATABASE || 'nrpti-dev');
const actsRegulationsCollection = db.collection('acts_regulations_mapping');
let act = await actsRegulationsCollection.find({ _schemaName: RECORD_TYPE.ActsRegulationsMapping._schemaName, actName: actName}).toArray();
let actCodeFromDB = act[0]['actCode'];
return (actCodeFromDB);
} catch (error) {
console.error("getActCodeFromActTitle: Failed to fetch data from DB:", error);
return actName;
}
}

/**
* @async
* @return {Object} an object that has the legislative act names as keys and an array of the regulations associated with those acts as values
Expand Down
16 changes: 16 additions & 0 deletions api/src/controllers/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ let moment = require('moment');
let fuzzySearch = require('../utils/fuzzySearch');
const { ApplicationAdminRoles, ApplicationLimitedAdminRoles, SKIP_REDACTION_SCHEMA_NAMES } = require('../utils/constants/misc');
const { userIsOnlyInRole } = require('../utils/auth-utils');
const {getActCodeFromActTitle} = require('../controllers/acts-regulations-controller');

function isEmpty(obj) {
for (const key in obj) {
Expand Down Expand Up @@ -39,6 +40,21 @@ let generateExpArray = async function (field, logicalOperator = '$or', compariso
Object.keys(queryString).map(async item => {
let entry = queryString[item];

//Append legislation codes from mapping table
if(item === "legislation.act"){
if(entry instanceof String || typeof(entry) === 'string'){
entry = [entry];
}
for(const title of entry){
if(!title.startsWith("ACT_")){
const actCode = await getActCodeFromActTitle(title);
if(actCode != title){
entry.push(actCode);
}
}
}
}

// If $in on an array, go through all the members of the array and convert their value
if (Array.isArray(entry) && comparisonOperator === '$in') {
let arrayExp = entry.map(element => {
Expand Down

0 comments on commit e93e177

Please sign in to comment.