Skip to content

Commit

Permalink
Merge pull request #144 from marklise/scale-kc-master
Browse files Browse the repository at this point in the history
NOBUG: Scale branch => master.
  • Loading branch information
marklise authored Nov 7, 2018
2 parents 36591c0 + 213c6e8 commit 6a71b62
Show file tree
Hide file tree
Showing 21 changed files with 1,697 additions and 645 deletions.
261 changes: 155 additions & 106 deletions api/controllers/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,88 +6,210 @@ var Actions = require('../helpers/actions');
var Utils = require('../helpers/utils');
var request = require('request');

var DEFAULT_PAGESIZE = 100;
var MAX_LIMIT = 1000;
var getSanitizedFields = function (fields) {
return _.remove(fields, function (f) {
return (_.indexOf(['_proponent',
'agency',
'areaHectares',
'businessUnit',
'centroid',
'cl_file',
'client',
'description',
'internal',
'legalDescription',
'location',
'name',
'publishDate',
'purpose',
'status',
'subpurpose',
'subtype',
'tantalisID',
'tenureStage',
'type'], f) !== -1);
});
}

exports.protectedOptions = function (args, res, rest) {
res.status(200).send();
}

exports.publicHead = function (args, res, next) {
// Build match query if on appId route
var query = {};
var skip = null;
var limit = null;

if (args.swagger.params.appId) {
query = Utils.buildQuery("_id", args.swagger.params.appId.value, query);
} else {
// Could be a bunch of results - enable pagination
var processedParameters = Utils.getSkipLimitParameters(args.swagger.params.pageSize, args.swagger.params.pageNum);
skip = processedParameters.skip;
limit = processedParameters.limit;

if (args.swagger.params.tantalisId && args.swagger.params.tantalisId.value !== undefined) {
_.assignIn(query, { tantalisID: args.swagger.params.tantalisId.value });
}
if (args.swagger.params.cl_file && args.swagger.params.cl_file.value !== undefined) {
_.assignIn(query, { cl_file: args.swagger.params.cl_file.value });
}
}

_.assignIn(query, { isDeleted: false });

Utils.runDataQuery('Application',
['public'],
query,
['_id',
'tags'], // Fields
null, // sort warmup
null, // sort
skip, // skip
limit, // limit
true) // count
.then(function (data) {
// /api/comment/ route, return 200 OK with 0 items if necessary
if (!(args.swagger.params.appId && args.swagger.params.appId.value) || (data && data.length > 0)) {
res.setHeader('x-total-count', data && data.length > 0 ? data[0].total_items: 0);
return Actions.sendResponse(res, 200, data);
} else {
return Actions.sendResponse(res, 404, data);
}
});
};

exports.publicGet = function (args, res, next) {
// Build match query if on appId route
var query = {};
var skip = null;
var limit = null;
var query = {};
var skip = null;
var limit = null;

if (args.swagger.params.appId) {
query = Utils.buildQuery("_id", args.swagger.params.appId.value, query);
} else {
// Could be a bunch of results - enable pagination
var pageSize = DEFAULT_PAGESIZE;
if (args.swagger.params.pageSize && args.swagger.params.pageSize.value !== undefined) {
if (args.swagger.params.pageSize.value > 0) {
pageSize = args.swagger.params.pageSize.value;
}
var processedParameters = Utils.getSkipLimitParameters(args.swagger.params.pageSize, args.swagger.params.pageNum);
skip = processedParameters.skip;
limit = processedParameters.limit;

if (args.swagger.params.tantalisId && args.swagger.params.tantalisId.value !== undefined) {
_.assignIn(query, { tantalisID: args.swagger.params.tantalisId.value });
}
if (args.swagger.params.pageNum && args.swagger.params.pageNum.value !== undefined) {
if (args.swagger.params.pageNum.value >= 0) {
skip = (args.swagger.params.pageNum.value * pageSize);
limit = pageSize;
}
if (args.swagger.params.cl_file && args.swagger.params.cl_file.value !== undefined) {
_.assignIn(query, { cl_file: args.swagger.params.cl_file.value });
}
}

_.assignIn(query, { isDeleted: false });

getApplications(['public'], query, args.swagger.params.fields.value, skip, limit)
Utils.runDataQuery('Application',
['public'],
query,
getSanitizedFields(args.swagger.params.fields.value), // Fields
null, // sort warmup
null, // sort
skip, // skip
limit, // limit
false) // count
.then(function (data) {
return Actions.sendResponse(res, 200, data);
});
};

exports.protectedGet = function(args, res, next) {
var self = this;
self.scopes = args.swagger.params.auth_payload.scopes;
var skip = null;
var limit = null;

var Application = mongoose.model('Application');

defaultLog.info("args.swagger.params:", args.swagger.params.auth_payload.scopes);
defaultLog.info("args.swagger.params:", args.swagger.operation["x-security-scopes"]);

// Build match query if on appId route
var query = {};
if (args.swagger.params.appId) {
query = Utils.buildQuery("_id", args.swagger.params.appId.value, query);
} else {
// Could be a bunch of results - enable pagination
var pageSize = DEFAULT_PAGESIZE;
if (args.swagger.params.pageSize && args.swagger.params.pageSize.value !== undefined) {
if (args.swagger.params.pageSize.value > 0) {
pageSize = args.swagger.params.pageSize.value;
}
var processedParameters = Utils.getSkipLimitParameters(args.swagger.params.pageSize, args.swagger.params.pageNum);
skip = processedParameters.skip;
limit = processedParameters.limit;

if (args.swagger.params.tantalisId && args.swagger.params.tantalisId.value !== undefined) {
_.assignIn(query, { tantalisID: args.swagger.params.tantalisId.value });
}
if (args.swagger.params.pageNum && args.swagger.params.pageNum.value !== undefined) {
if (args.swagger.params.pageNum.value >= 0) {
skip = (args.swagger.params.pageNum.value * pageSize);
limit = pageSize;
}
if (args.swagger.params.cl_file && args.swagger.params.cl_file.value !== undefined) {
_.assignIn(query, { cl_file: args.swagger.params.cl_file.value });
}
}
if (args.swagger.params.tantalisId && args.swagger.params.tantalisId.value !== undefined) {
_.assignIn(query, { tantalisID: args.swagger.params.tantalisId.value });
}

// Unless they specifically ask for it, hide deleted results.
if (args.swagger.params.isDeleted && args.swagger.params.isDeleted.value !== undefined) {
_.assignIn(query, { isDeleted: args.swagger.params.isDeleted.value });
} else {
_.assignIn(query, { isDeleted: false });
}

getApplications(args.swagger.params.auth_payload.scopes, query, args.swagger.params.fields.value, skip, limit)
Utils.runDataQuery('Application',
args.swagger.operation["x-security-scopes"],
query,
getSanitizedFields(args.swagger.params.fields.value), // Fields
null, // sort warmup
null, // sort
skip, // skip
limit, // limit
false) // count
.then(function (data) {
return Actions.sendResponse(res, 200, data);
});
};

exports.protectedHead = function (args, res, next) {
defaultLog.info("args.swagger.params:", args.swagger.operation["x-security-scopes"]);

// Build match query if on appId route
var query = {};
if (args.swagger.params.appId) {
query = Utils.buildQuery("_id", args.swagger.params.appId.value, query);
} else {
if (args.swagger.params.tantalisId && args.swagger.params.tantalisId.value !== undefined) {
_.assignIn(query, { tantalisID: args.swagger.params.tantalisId.value });
}
if (args.swagger.params.cl_file && args.swagger.params.cl_file.value !== undefined) {
_.assignIn(query, { cl_file: args.swagger.params.cl_file.value });
}
}

// Unless they specifically ask for it, hide deleted results.
if (args.swagger.params.isDeleted && args.swagger.params.isDeleted.value !== undefined) {
_.assignIn(query, { isDeleted: args.swagger.params.isDeleted.value });
} else {
_.assignIn(query, { isDeleted: false });
}

Utils.runDataQuery('Application',
args.swagger.operation["x-security-scopes"],
query,
['_id',
'tags'], // Fields
null, // sort warmup
null, // sort
null, // skip
null, // limit
true) // count
.then(function (data) {
// /api/comment/ route, return 200 OK with 0 items if necessary
if (!(args.swagger.params.appId && args.swagger.params.appId.value) || (data && data.length > 0)) {
res.setHeader('x-total-count', data && data.length > 0 ? data[0].total_items: 0);
return Actions.sendResponse(res, 200, data);
} else {
return Actions.sendResponse(res, 404, data);
}
});
};

exports.protectedDelete = function (args, res, next) {
var appId = args.swagger.params.appId.value;
defaultLog.info("Delete Application:", appId);
Expand Down Expand Up @@ -178,7 +300,7 @@ exports.protectedPost = function (args, res, next) {
// Define security tag defaults
app.tags = [['sysadmin']];
app.internal.tags = [['sysadmin']];
app._addedBy = args.swagger.params.auth_payload.userID;
app._addedBy = args.swagger.params.auth_payload.preferred_username.value;
app.save()
.then(function (savedApp) {
// Get the shapes from BCGW for this DISPOSITION and save them into the feature collection
Expand Down Expand Up @@ -335,76 +457,3 @@ exports.protectedUnPublish = function (args, res, next) {
}
});
};
var getApplications = function (role, query, fields, skip, limit) {
return new Promise(function (resolve, reject) {
var Application = mongoose.model('Application');
var projection = {};

// Fields we always return
var defaultFields = ['_id',
'code',
'tags'];
_.each(defaultFields, function (f) {
projection[f] = 1;
});

// Add requested fields - sanitize first by including only those that we can/want to return
var sanitizedFields = _.remove(fields, function (f) {
return (_.indexOf(['_proponent',
'agency',
'areaHectares',
'businessUnit',
'centroid',
'cl_file',
'client',
'description',
'internal',
'legalDescription',
'location',
'name',
'publishDate',
'purpose',
'status',
'subpurpose',
'subtype',
'tantalisID',
'tenureStage',
'type'], f) !== -1);
});
_.each(sanitizedFields, function (f) {
projection[f] = 1;
});

Application.aggregate([
{
"$match": query
},
{
"$project": projection
},
{
$redact: {
$cond: {
if: {
$anyElementTrue: {
$map: {
input: "$tags" ,
as: "fieldTag",
in: { $setIsSubset: [ "$$fieldTag", role ] }
}
}
},
then: "$$DESCEND",
else: "$$PRUNE"
}
}
},
{ $skip: skip || 0 },
{ $limit: limit || MAX_LIMIT }
]).exec()
.then(function (data) {
defaultLog.info("data:", data);
resolve(data);
});
});
};
Loading

0 comments on commit 6a71b62

Please sign in to comment.