diff --git a/angular/projects/admin-nrpti/src/app/records/records-resolver.spec.ts b/angular/projects/admin-nrpti/src/app/records/records-resolver.spec.ts index f67b0efae..c691df445 100644 --- a/angular/projects/admin-nrpti/src/app/records/records-resolver.spec.ts +++ b/angular/projects/admin-nrpti/src/app/records/records-resolver.spec.ts @@ -6,21 +6,18 @@ 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', ['getRecords']); const spyTableTemplateUtils = jasmine.createSpyObj('TableTemplateUtils', [ 'updateTableObjectWithUrlParams' ]); - const spyRecordUtils = jasmine.createSpyObj('RecordUtils', ['appendActCodesToActNames']); beforeEach(() => { TestBed.configureTestingModule({ providers: [ { provide: FactoryService, useValue: spyFactoryService }, - { provide: TableTemplateUtils, useValue: spyTableTemplateUtils }, - { provide: RecordUtils, useValue: spyRecordUtils } + { provide: TableTemplateUtils, useValue: spyTableTemplateUtils } ] }).compileComponents(); }); @@ -28,9 +25,8 @@ describe('RecordsResolver', () => { 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(); })); @@ -38,7 +34,6 @@ describe('RecordsResolver', () => { describe('resolve', () => { let factoryServiceSpy: jasmine.SpyObj; let tableTemplateUtilsSpy: jasmine.SpyObj; - let recordUtilsSpy: jasmine.SpyObj; describe('with empty/null route parameters', () => { const activatedRouteSnapshot: ActivatedRouteSnapshot = { @@ -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); })); @@ -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); })); @@ -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); })); @@ -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); })); @@ -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); })); @@ -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); })); diff --git a/angular/projects/admin-nrpti/src/app/records/records-resolver.ts b/angular/projects/admin-nrpti/src/app/records/records-resolver.ts index 8f0703653..f7eae2b86 100644 --- a/angular/projects/admin-nrpti/src/app/records/records-resolver.ts +++ b/angular/projects/admin-nrpti/src/app/records/records-resolver.ts @@ -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> { - constructor( - private factoryService: FactoryService, - private tableTemplateUtils: TableTemplateUtils, - private recordUtils: RecordUtils - ) {} + constructor(private factoryService: FactoryService, private tableTemplateUtils: TableTemplateUtils) {} resolve(route: ActivatedRouteSnapshot): Observable { const params = { ...route.params }; @@ -59,7 +54,7 @@ export class RecordsResolver implements Resolve> { } if (params.act) { - or['legislation.act'] = this.recordUtils.appendActCodesToActNames(params.act, this.factoryService); + or['legislation.act'] = params.act; } if (params.regulation) { diff --git a/angular/projects/admin-nrpti/src/app/records/utils/record-utils.ts b/angular/projects/admin-nrpti/src/app/records/utils/record-utils.ts index 3f9b6b417..3ec13b205 100644 --- a/angular/projects/admin-nrpti/src/app/records/utils/record-utils.ts +++ b/angular/projects/admin-nrpti/src/app/records/utils/record-utils.ts @@ -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 diff --git a/angular/projects/public-nrpti/src/app/records/records-list/records-list-resolver.ts b/angular/projects/public-nrpti/src/app/records/records-list/records-list-resolver.ts index 4d7fa0ced..aa2cb0045 100644 --- a/angular/projects/public-nrpti/src/app/records/records-list/records-list-resolver.ts +++ b/angular/projects/public-nrpti/src/app/records/records-list/records-list-resolver.ts @@ -25,10 +25,6 @@ export class RecordsListResolver implements Resolve> { schemaList = params.activityType.split(','); } - if (params.act) { - params.act = RecordUtils.appendActCodesToActNames(params.act, this.factoryService); - } - let keywords = ''; if (params.keywords) { keywords = params.keywords; diff --git a/angular/projects/public-nrpti/src/app/records/utils/record-utils.ts b/angular/projects/public-nrpti/src/app/records/utils/record-utils.ts index 3a189c642..017b359eb 100644 --- a/angular/projects/public-nrpti/src/app/records/utils/record-utils.ts +++ b/angular/projects/public-nrpti/src/app/records/utils/record-utils.ts @@ -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 = []; @@ -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'])); @@ -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; - } } /** diff --git a/api/src/controllers/acts-regulations-controller.js b/api/src/controllers/acts-regulations-controller.js index 37d10d5bd..d453980b7 100644 --- a/api/src/controllers/acts-regulations-controller.js +++ b/api/src/controllers/acts-regulations-controller.js @@ -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 diff --git a/api/src/controllers/search.js b/api/src/controllers/search.js index 16b707a2e..9b0cf25bf 100644 --- a/api/src/controllers/search.js +++ b/api/src/controllers/search.js @@ -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) { @@ -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 => {