Skip to content

Commit

Permalink
[LEVWEB-1396] Pass roles on to API
Browse files Browse the repository at this point in the history
This is necessary for testing with newer versions of the mock.
  • Loading branch information
daniel-ac-martin committed Oct 16, 2020
1 parent d1a7c8f commit 2a42a4e
Show file tree
Hide file tree
Showing 17 changed files with 171 additions and 136 deletions.
118 changes: 60 additions & 58 deletions api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,186 +12,188 @@ const marriageSearch = `${baseURL}/v1/registration/marriage`;
const partnershipSearch = `${baseURL}/v1/registration/partnership`;
const userActivity = `${baseURL}/api/v0/audit/user-activity`;

const requestData = (url, accessToken) =>
const requestData = (url, requestInfo) =>
new Promise((resolve, reject) =>
levRequest.get({
'url': url
},
accessToken,
requestInfo,
helpers.responseHandler(resolve, reject)
));

const findByNameDOB = (searchFields, accessToken) => {
const findByNameDOB = (searchFields, requestInfo) => {
if (searchFields === undefined) {
throw new ReferenceError('findByNameDOB(): first argument, searchFields, was not defined');
} else if (!(searchFields instanceof Object)) {
throw new TypeError('findByNameDOB(): first argument, searchFields, must be an object');
} else if (accessToken !== undefined && typeof accessToken !== 'string') {
throw new TypeError('findByNameDOB(): second argument, accessToken, must be a string');
} else if (!(requestInfo instanceof Object)) {
throw new TypeError('findByNameDOB(): second argument, requestInfo, must be an object');
}

return requestData(helpers.buildQueryUri(birthSearch, searchFields), accessToken)
return requestData(helpers.buildQueryUri(birthSearch, searchFields), requestInfo)
.then((data) => data.map(helpers.processRecord));
};

const findBySystemNumber = (systemNumber, accessToken) => {
const findBySystemNumber = (systemNumber, requestInfo) => {
if (systemNumber === undefined) {
throw new ReferenceError('findBySystemNumber(): first argument, systemNumber, was not defined');
} else if ((!Number.isInteger(systemNumber))) {
throw new TypeError('findBySystemNumber(): first argument, systemNumber, must be an integer');
} else if (accessToken !== undefined && typeof accessToken !== 'string') {
throw new TypeError('findBySystemNumber(): second argument, accessToken, must be a string');
} else if (!(requestInfo instanceof Object)) {
throw new TypeError('findBySystemNumber(): second argument, requestInfo, must be an object');
}

return requestData(birthSearch + '/' + systemNumber, accessToken)
return requestData(birthSearch + '/' + systemNumber, requestInfo)
.then(helpers.processRecord);
};

const findBirths = (searchFields, accessToken) => {
const findBirths = (searchFields, requestInfo) => {
if (searchFields === undefined) {
throw new ReferenceError('findBirths(): first argument, searchFields, was not defined');
} else if (!(searchFields instanceof Object)) {
throw new TypeError('findBirths(): first argument, searchFields, must be an object');
} else if (accessToken !== undefined && typeof accessToken !== 'string') {
throw new TypeError('findBirths(): second argument, accessToken, must be a string');
} else if (!(requestInfo instanceof Object)) {
throw new TypeError('findBirths(): second argument, requestInfo, must be an object');
}

const systemNumber = searchFields['system-number'] && Number.parseInt(searchFields['system-number'], 10);

return systemNumber
? findBySystemNumber(systemNumber, accessToken).then((data) => [data])
: findByNameDOB(searchFields, accessToken);
? findBySystemNumber(systemNumber, requestInfo).then((data) => [data])
: findByNameDOB(searchFields, requestInfo);
};

const findDeathsByNameDate = (searchFields, accessToken) => {
const findDeathsByNameDate = (searchFields, requestInfo) => {
if (searchFields === undefined) {
throw new ReferenceError('findDeathsByNameDate(): first argument, searchFields, was not defined');
} else if (!(searchFields instanceof Object)) {
throw new TypeError('findDeathsByNameDate(): first argument, searchFields, must be an object');
} else if (accessToken !== undefined && typeof accessToken !== 'string') {
throw new TypeError('findDeathsByNameDate(): second argument, accessToken, must be a string');
} else if (!(requestInfo instanceof Object)) {
throw new TypeError('findDeathsByNameDate(): second argument, requestInfo, must be an object');
}

return requestData(helpers.buildQueryUri(deathSearch, searchFields), accessToken)
return requestData(helpers.buildQueryUri(deathSearch, searchFields), requestInfo)
.then((data) => data.map(helpers.processDeathRecord));
};

const findDeathBySystemNumber = (systemNumber, accessToken) => {
const findDeathBySystemNumber = (systemNumber, requestInfo) => {
if (systemNumber === undefined) {
throw new ReferenceError('findBySystemNumber(): first argument, systemNumber, was not defined');
} else if ((!Number.isInteger(systemNumber))) {
throw new TypeError('findDeathsBySystemNumber(): first argument, systemNumber, must be an integer');
} else if (accessToken !== undefined && typeof accessToken !== 'string') {
throw new TypeError('findDeathsBySystemNumber(): second argument, accessToken, must be a string');
} else if (!(requestInfo instanceof Object)) {
throw new TypeError('findDeathsBySystemNumber(): second argument, requestInfo, must be an object');
}

return requestData(deathSearch + '/' + systemNumber, accessToken)
return requestData(deathSearch + '/' + systemNumber, requestInfo)
.then(helpers.processDeathRecord);
};

const findDeaths = (searchFields, accessToken) => {
const findDeaths = (searchFields, requestInfo) => {
if (searchFields === undefined) {
throw new ReferenceError('findDeaths(): first argument, searchFields, was not defined');
} else if (!(searchFields instanceof Object)) {
throw new TypeError('findDeaths(): first argument, searchFields, must be an object');
} else if (accessToken !== undefined && typeof accessToken !== 'string') {
throw new TypeError('findDeaths(): second argument, accessToken, must be a string');
} else if (!(requestInfo instanceof Object)) {
throw new TypeError('findDeaths(): second argument, requestInfo, must be an object');
}

const systemNumber = searchFields['system-number'] && Number.parseInt(searchFields['system-number'], 10);

return systemNumber
? findDeathBySystemNumber(systemNumber, accessToken).then((data) => [data])
: findDeathsByNameDate(searchFields, accessToken);
? findDeathBySystemNumber(systemNumber, requestInfo).then((data) => [data])
: findDeathsByNameDate(searchFields, requestInfo);
};

const findMarriagesByNameDOM = (searchFields, accessToken) => {
const findMarriagesByNameDOM = (searchFields, requestInfo) => {
if (searchFields === undefined) {
throw new ReferenceError('findMarriagesByNameDOM(): first argument, searchFields, was not defined');
} else if (!(searchFields instanceof Object)) {
throw new TypeError('findMarriagesByNameDOM(): first argument, searchFields, must be an object');
} else if (accessToken !== undefined && typeof accessToken !== 'string') {
throw new TypeError('findMarriagesByNameDOM(): second argument, accessToken, must be a string');
} else if (!(requestInfo instanceof Object)) {
throw new TypeError('findMarriagesByNameDOM(): second argument, requestInfo, must be an object');
}

return requestData(helpers.buildQueryUri(marriageSearch, searchFields), accessToken)
return requestData(helpers.buildQueryUri(marriageSearch, searchFields), requestInfo)
.then((data) => data.map(helpers.processMarriageRecord));
};

const findMarriageBySystemNumber = (systemNumber, accessToken) => {
const findMarriageBySystemNumber = (systemNumber, requestInfo) => {
if (systemNumber === undefined) {
throw new ReferenceError('findMarriagesBySystemNumber(): first argument, systemNumber, was not defined');
} else if ((!Number.isInteger(systemNumber))) {
throw new TypeError('findMarriagesBySystemNumber(): first argument, systemNumber, must be an integer');
} else if (accessToken !== undefined && typeof accessToken !== 'string') {
throw new TypeError('findMarriagesBySystemNumber(): second argument, accessToken, must be a string');
} else if (!(requestInfo instanceof Object)) {
throw new TypeError('findMarriagesBySystemNumber(): second argument, requestInfo, must be an object');
}

return requestData(marriageSearch + '/' + systemNumber, accessToken)
return requestData(marriageSearch + '/' + systemNumber, requestInfo)
.then(helpers.processMarriageRecord);
};

const findMarriages = (searchFields, accessToken) => {
const findMarriages = (searchFields, requestInfo) => {
if (searchFields === undefined) {
throw new ReferenceError('findMarriages(): first argument, searchFields, was not defined');
} else if (!(searchFields instanceof Object)) {
throw new TypeError('findMarriages(): first argument, searchFields, must be an object');
} else if (accessToken !== undefined && typeof accessToken !== 'string') {
throw new TypeError('findMarriages(): second argument, accessToken, must be a string');
} else if (!(requestInfo instanceof Object)) {
throw new TypeError('findMarriages(): second argument, requestInfo, must be an object');
}

const systemNumber = searchFields['system-number'] && Number.parseInt(searchFields['system-number'], 10);

return systemNumber
? findMarriageBySystemNumber(systemNumber, accessToken).then((data) => [data])
: findMarriagesByNameDOM(searchFields, accessToken);
? findMarriageBySystemNumber(systemNumber, requestInfo).then((data) => [data])
: findMarriagesByNameDOM(searchFields, requestInfo);
};

const findPartnershipsByNameDOP = (searchFields, accessToken) => {
const findPartnershipsByNameDOP = (searchFields, requestInfo) => {
if (searchFields === undefined) {
throw new ReferenceError('findPartnershipsByNameDOP(): first argument, searchFields, was not defined');
} else if (!(searchFields instanceof Object)) {
throw new TypeError('findPartnershipsByNameDOP(): first argument, searchFields, must be an object');
} else if (accessToken !== undefined && typeof accessToken !== 'string') {
throw new TypeError('findPartnershipsByNameDOP(): second argument, accessToken, must be a string');
} else if (!(requestInfo instanceof Object)) {
throw new TypeError('findPartnershipsByNameDOP(): second argument, requestInfo, must be an object');
}

return requestData(helpers.buildQueryUri(partnershipSearch, searchFields), accessToken)
return requestData(helpers.buildQueryUri(partnershipSearch, searchFields), requestInfo)
.then((data) => data.map(helpers.processPartnershipRecord));
};

const findPartnershipBySystemNumber = (systemNumber, accessToken) => {
const findPartnershipBySystemNumber = (systemNumber, requestInfo) => {
if (systemNumber === undefined) {
throw new ReferenceError('findPartnershipsBySystemNumber(): first argument, systemNumber, was not defined');
} else if ((!Number.isInteger(systemNumber))) {
throw new TypeError('findPartnershipsBySystemNumber(): first argument, systemNumber, must be an integer');
} else if (accessToken !== undefined && typeof accessToken !== 'string') {
throw new TypeError('findPartnershipsBySystemNumber(): second argument, accessToken, must be a string');
} else if (!(requestInfo instanceof Object)) {
throw new TypeError('findPartnershipsBySystemNumber(): second argument, requestInfo, must be an object');
}

return requestData(partnershipSearch + '/' + systemNumber, accessToken)
return requestData(partnershipSearch + '/' + systemNumber, requestInfo)
.then(helpers.processPartnershipRecord);
};

const findPartnerships = (searchFields, accessToken) => {
const findPartnerships = (searchFields, requestInfo) => {
if (searchFields === undefined) {
throw new ReferenceError('findPartnerships(): first argument, searchFields, was not defined');
} else if (!(searchFields instanceof Object)) {
throw new TypeError('findPartnerships(): first argument, searchFields, must be an object');
} else if (accessToken !== undefined && typeof accessToken !== 'string') {
throw new TypeError('findPartnerships(): second argument, accessToken, must be a string');
} else if (!(requestInfo instanceof Object)) {
throw new TypeError('findPartnerships(): second argument, requestInfo, must be an object');
}

const systemNumber = searchFields['system-number'] && Number.parseInt(searchFields['system-number'], 10);

return systemNumber
? findPartnershipBySystemNumber(systemNumber, accessToken).then((data) => [data])
: findPartnershipsByNameDOP(searchFields, accessToken);
? findPartnershipBySystemNumber(systemNumber, requestInfo).then((data) => [data])
: findPartnershipsByNameDOP(searchFields, requestInfo);
};

const userActivityReport = (accessToken, from, to, userFilter) => { // eslint-disable-line complexity
if (accessToken !== undefined && typeof accessToken !== 'string') {
throw new TypeError('The "accessToken" parameter must be a string');
const userActivityReport = (requestInfo, from, to, userFilter) => { // eslint-disable-line complexity
if (requestInfo === undefined) {
throw new ReferenceError('The "requestInfo" parameter must be an object');
} else if (!(requestInfo instanceof Object)) {
throw new TypeError('The "requestInfo" parameter must be an object');
}
if (!from || !to) {
throw new ReferenceError('"from" and "to" dates must be provided for the User Activity report');
Expand All @@ -218,7 +220,7 @@ const userActivityReport = (accessToken, from, to, userFilter) => { // eslint-di
user: userFilter
};

return requestData(helpers.buildQueryUri(userActivity, data), accessToken);
return requestData(helpers.buildQueryUri(userActivity, data), requestInfo);
};

module.exports = {
Expand Down
5 changes: 3 additions & 2 deletions controllers/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const moment = require('moment');
const Parent = require('../lib/hof-standalone');
const api = require('../api');
const reqInfo = require('../lib/req-info');
const validators = require('../lib/custom-validators');
const util = require('util');

Expand Down Expand Up @@ -68,7 +69,7 @@ const expandUsers = (records, days) => {
};

AuditController.prototype.successHandler = function successHandler(req, res, callback) {
const accessToken = req.headers['X-Auth-Token'] || req.headers['x-auth-token'];
const ri = reqInfo(req);
const from = validators.parseDate(req.form.values.from).floor(24, 'hours');
const to = validators.parseDate(req.form.values.to).floor(24, 'hours');
const toInclusive = moment(to).add(1, 'day');
Expand Down Expand Up @@ -100,7 +101,7 @@ AuditController.prototype.successHandler = function successHandler(req, res, cal
callback(error, req, res);
};

api.userActivityReport(accessToken, from, toInclusive, userFilter).then(resolved, rejected);
api.userActivityReport(ri, from, toInclusive, userFilter).then(resolved, rejected);
};

const form = new AuditController({
Expand Down
2 changes: 1 addition & 1 deletion controllers/death-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = function renderDetails(req, res, next) {

const canRedirectToResults = (req.query && req.query.multipleResults) !== undefined;

return api.findDeathBySystemNumber(Number(systemNumber), ri.token)
return api.findDeathBySystemNumber(Number(systemNumber), ri)
.then(result => res.render('pages/death-details', {
record: result,
showAll: helpers.showFullDetails(ri),
Expand Down
5 changes: 3 additions & 2 deletions controllers/death-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const api = require('../api');
const helpers = require('../lib/helpers');
const validators = require('../lib/custom-validators');
const fields = require('../fields/death');
const reqInfo = require('../lib/req-info');
const util = require('util');
const _ = require('lodash');

Expand All @@ -19,9 +20,9 @@ util.inherits(SearchController, Parent);
// Only redirect when a single result is returned, otherwise render
// results page
SearchController.prototype.successHandler = function successHandler(req, res, callback) {
const accessToken = req.headers['X-Auth-Token'] || req.headers['x-auth-token'];
const query = _.pick(req.query, _.keys(fields));
const querystring = helpers.serialize(query);
const ri = reqInfo(req);

const resolved = (records) => {
if (records.length === 1) {
Expand Down Expand Up @@ -53,7 +54,7 @@ SearchController.prototype.successHandler = function successHandler(req, res, ca
}
};

api.findDeaths(req.form.values, accessToken)
api.findDeaths(req.form.values, ri)
.then(resolved, rejected);
this.emit('complete', req, res);
};
Expand Down
5 changes: 3 additions & 2 deletions controllers/details.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const api = require('../api');
const helpers = require('../lib/helpers');
const fields = require('../fields');
const reqInfo = require('../lib/req-info');
const _ = require('lodash');

const handleError = (err, next) => {
Expand All @@ -16,6 +17,7 @@ const handleError = (err, next) => {
module.exports = function renderDetails(req, res, next) {
req.params = req.params || {};
const systemNumber = req.params.sysnum;
const ri = reqInfo(req);

if (systemNumber === undefined) {
return next(new ReferenceError('The parameter \'id\' was not defined'), req, res);
Expand All @@ -24,10 +26,9 @@ module.exports = function renderDetails(req, res, next) {
return next(new TypeError('The parameter \'id\' was not an integer'), req, res);
}

const accessToken = req.headers['X-Auth-Token'] || req.headers['x-auth-token'];
const canRedirectToResults = (req.query && req.query.multipleResults) !== undefined;

return api.findBySystemNumber(Number(systemNumber), accessToken)
return api.findBySystemNumber(Number(systemNumber), ri)
.then(result => res.render('pages/details', {
record: result,
querystring: helpers.serialize(_.pick(req.query, _.keys(fields))),
Expand Down
2 changes: 1 addition & 1 deletion controllers/marriage-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = function renderDetails(req, res, next) {

const canRedirectToResults = (req.query && req.query.multipleResults) !== undefined;

return api.findMarriageBySystemNumber(Number(systemNumber), ri.token)
return api.findMarriageBySystemNumber(Number(systemNumber), ri)
.then(result => res.render('pages/marriage-details', {
record: result,
showAll: helpers.showFullDetails(ri),
Expand Down
5 changes: 3 additions & 2 deletions controllers/marriage-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const api = require('../api');
const helpers = require('../lib/helpers');
const validators = require('../lib/custom-validators');
const fields = require('../fields/marriage');
const reqInfo = require('../lib/req-info');
const util = require('util');
const _ = require('lodash');

Expand All @@ -19,9 +20,9 @@ util.inherits(SearchController, Parent);
// Only redirect when a single result is returned, otherwise render
// results page
SearchController.prototype.successHandler = function successHandler(req, res, callback) {
const accessToken = req.headers['X-Auth-Token'] || req.headers['x-auth-token'];
const query = _.pick(req.query, _.keys(fields));
const querystring = helpers.serialize(query);
const ri = reqInfo(req);

const resolved = (records) => {
if (records.length === 1) {
Expand Down Expand Up @@ -53,7 +54,7 @@ SearchController.prototype.successHandler = function successHandler(req, res, ca
}
};

api.findMarriages(req.form.values, accessToken)
api.findMarriages(req.form.values, ri)
.then(resolved, rejected);
this.emit('complete', req, res);
};
Expand Down
2 changes: 1 addition & 1 deletion controllers/partnership-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = function renderDetails(req, res, next) {

const canRedirectToResults = (req.query && req.query.multipleResults) !== undefined;

return api.findPartnershipBySystemNumber(Number(systemNumber), ri.token)
return api.findPartnershipBySystemNumber(Number(systemNumber), ri)
.then(result => res.render('pages/partnership-details', {
record: result,
showAll: helpers.showFullDetails(ri),
Expand Down
Loading

0 comments on commit 2a42a4e

Please sign in to comment.