Skip to content

Commit

Permalink
Add Map Layer Info controller
Browse files Browse the repository at this point in the history
  • Loading branch information
BcGovNeal committed Feb 25, 2021
1 parent 18abcdf commit c458f2b
Show file tree
Hide file tree
Showing 6 changed files with 638 additions and 3 deletions.
6 changes: 3 additions & 3 deletions api/materialized_views/search/redactedRecordSubset.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async function update(defaultLog) {
skipRedact: {
$cond: {
if: {
$in: [{ $arrayElemAt: ['$fullRecord._schemaName', 0] }, ['MineBCMI', 'CollectionBCMI']]
$in: [{ $arrayElemAt: ['$fullRecord._schemaName', 0] }, ['MineBCMI', 'CollectionBCMI', 'MapLayerInfo']]
},
then: true,
else: false
Expand Down Expand Up @@ -164,7 +164,7 @@ async function update(defaultLog) {
$addFields: {
'issuedTo.read': {
$cond: {
if: notAuthorizedCondition,
if: { $and: [{ $eq: ['$skipRedact', false] }, notAuthorizedCondition] },
then: {
$filter: {
input: '$issuedTo.read',
Expand All @@ -184,7 +184,7 @@ async function update(defaultLog) {
$addFields: {
documents: {
$cond: {
if: notAuthorizedCondition,
if: { $and: [{ $eq: ['$skipRedact', false] }, notAuthorizedCondition] },
then: [],
else: '$documents'
}
Expand Down
220 changes: 220 additions & 0 deletions api/src/controllers/map-info-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
// const mongoose = require('mongoose');
// const ObjectID = require('mongodb').ObjectID;

const defaultLog = require('../utils/logger')('mapLayerInfo');
const queryActions = require('../utils/query-actions');
const { ApplicationRoles } = require('../utils/constants/misc');
const Post = require('../controllers/post/post');
const Get = require('../controllers/get/get');
const Delete = require('../controllers/delete/delete');
const PutUtils = require('../utils/put-utils');
const { mapLayerInfo: MapLayerInfo } = require('../models/index');

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

exports.publicGet = async function(args, res, next) {
let mapInfoId = null;
let errorMsg = null;
if (args.swagger.params.mapInfoId && args.swagger.params.mapInfoId.value) {
mapInfoId = args.swagger.params.mapInfoId.value;
} else {
errorMsg = `publicGet - you must provide an id to get`;
defaultLog.info(errorMsg);
return queryActions.sendResponse(res, 400, errorMsg);
}

let obj = null;
try {
obj = await Get.findById(mapInfoId);
} catch (error) {
errorMsg = `publicGet - error getting map info: ${mapInfoId}`;
defaultLog.info(errorMsg);
defaultLog.debug(error);
return queryActions.sendResponse(res, 400, errorMsg);
}

if (!obj) {
errorMsg = `protectedGet - map info not found: ${mapInfoId}`;
defaultLog.info(errorMsg);
return queryActions.sendResponse(res, 404, errorMsg);
}

return queryActions.sendResponse(res, 200, obj);
};

exports.protectedGet = async function(args, res, next) {
let mapInfoId = null;
let errorMsg = null;
if (args.swagger.params.mapInfoId && args.swagger.params.mapInfoId.value) {
mapInfoId = args.swagger.params.mapInfoId.value;
} else {
errorMsg = `protectedGet - you must provide an id to get`;
defaultLog.info(errorMsg);
return queryActions.sendResponse(res, 400, errorMsg);
}

let obj = null;
try {
obj = await Get.findById(mapInfoId);
} catch (error) {
errorMsg = `protectedGet - error getting map info: ${mapInfoId}`;
defaultLog.info(errorMsg);
defaultLog.debug(error);
return queryActions.sendResponse(res, 400, errorMsg);
}

if (!obj) {
errorMsg = `protectedGet - map info not found: ${mapInfoId}`;
defaultLog.info(errorMsg);
return queryActions.sendResponse(res, 404, errorMsg);
}

return queryActions.sendResponse(res, 200, obj);
};

async function getMapLayerInfoSegment(segment) {
return await MapLayerInfo.findOne({ _schemaName: 'MapLayerInfo', segment });
}

exports.protectedPost = async function(args, res, next) {
let incomingObj = {};
let errorMsg = null;
// Only accpet applicaiton: 'LNG' for now
if (args.swagger.params.mapInfo && args.swagger.params.mapInfo.value) {
incomingObj = args.swagger.params.mapInfo.value;
} else {
errorMsg = `protectedPost - error: invalid post body`;
defaultLog.info(errorMsg);
return queryActions.sendResponse(res, 400, errorMsg);
}

if (!(incomingObj.application && incomingObj.application === 'LNG')) {
errorMsg = `protectedPost - error: application type not accepted`;
defaultLog.info(errorMsg);
return queryActions.sendResponse(res, 400, errorMsg);
}

if (!(incomingObj.data && incomingObj.data.segment)) {
errorMsg = `protectedPost - error: missing segment`;
defaultLog.info(errorMsg);
return queryActions.sendResponse(res, 400, errorMsg);
}

const data = incomingObj.data;

// Check for existing segment info and prevent creating duplicates
const existingMapInfo = await getMapLayerInfoSegment(data.segment);
if (existingMapInfo) {
errorMsg = `protectedPost - error: segment info already exists`;
defaultLog.info(errorMsg);
return queryActions.sendResponse(res, 400, errorMsg);
}

let map = new MapLayerInfo();

data.segment && (map.segment = data.segment);
data.description && (map.description = data.description);
data.location && (map.location = data.location);
data.length && (map.length = data.length);

map.read = [ApplicationRoles.ADMIN, ApplicationRoles.ADMIN_LNG, ApplicationRoles.PUBLIC];
map.write = [ApplicationRoles.ADMIN, ApplicationRoles.ADMIN_LNG];

map.dateAdded = Date.now();
map.dateUpdated = Date.now();
map.updatedBy = args.swagger.params.auth_payload.displayName;

let obj = null;
try {
obj = await Post.insert(map);
} catch (error) {
errorMsg = `protectedPost - error inserting map layer info: ${map}`;
defaultLog.info(errorMsg);
defaultLog.debug(error);
return queryActions.sendResponse(res, 400, errorMsg);
}

return queryActions.sendResponse(res, 200, obj.ops[0]);
};

exports.protectedPut = async function(args, res, next) {
let incomingObj = {};
let errorMsg = null;

if (args.swagger.params.mapInfo && args.swagger.params.mapInfo.value) {
incomingObj = args.swagger.params.mapInfo.value;
} else {
errorMsg = `protectedPut - you must provide mapInfo data to put`;
defaultLog.info(errorMsg);
return queryActions.sendResponse(res, 400, errorMsg);
}

let mapInfoId = null;
if (args.swagger.params.mapInfoId && args.swagger.params.mapInfoId.value) {
mapInfoId = args.swagger.params.mapInfoId.value;
} else {
errorMsg = `protectedPut - you must provide mapInfoId to put`;
defaultLog.info(errorMsg);
return queryActions.sendResponse(res, 400, errorMsg);
}

// Prevent changing the segment value
delete incomingObj.segment;
delete incomingObj._id;
delete incomingObj.read;
delete incomingObj.write;

const sanitizedObj = PutUtils.validateObjectAgainstModel(MapLayerInfo, incomingObj);

if (!sanitizedObj || sanitizedObj === {}) {
// skip, as there are no changes to master record
return;
}

sanitizedObj.dateUpdated = new Date();
sanitizedObj.updatedBy = args.swagger.params.auth_payload.displayName;
const dotNotatedObj = PutUtils.getDotNotation(sanitizedObj);
const updateObj = { $set: dotNotatedObj };

let obj = null;
try {
obj = await MapLayerInfo.findOneAndUpdate(
{
_id: mapInfoId,
write: { $in: args.swagger.params.auth_payload.realm_access.roles }
},
updateObj,
{ new: true }
);
} catch (error) {
errorMsg = `protectedPut - error updating map info: ${updateObj}`;
defaultLog.info(errorMsg);
defaultLog.debug(error);
return queryActions.sendResponse(res, 400, errorMsg);
}

return queryActions.sendResponse(res, 200, obj);
};

exports.protectedDelete = async function(args, res, next) {
let mapInfoId = null;
if (args.swagger.params.mapInfoId && args.swagger.params.mapInfoId.value) {
mapInfoId = args.swagger.params.mapInfoId.value;
} else {
defaultLog.info(`protectedDelete - you must provide an id to delete`);
return queryActions.sendResponse(res, 400, {});
}

let obj = null;
try {
obj = await Delete.deleteById(mapInfoId);
} catch (error) {
defaultLog.info(`protectedDelete - error deleting mapInfo: ${mapInfoId}`);
defaultLog.debug(error);
return queryActions.sendResponse(res, 400, {});
}

return queryActions.sendResponse(res, 200, obj);
};
1 change: 1 addition & 0 deletions api/src/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ exports.task = require('./task');
exports.document = require('./document');
exports.epicProject = require('./epicProject');
exports.communicationPackage = require('./communicationPackage');
exports.mapLayerInfo = require('./mapLayerInfo');

// master
require('./master');
Expand Down
26 changes: 26 additions & 0 deletions api/src/models/mapLayerInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = require('../utils/model-schema-generator')(
'MapLayerInfo',
{
_schemaName: { type: String, default: 'MapLayerInfo', index: true },

segment: {
type: String,
required: true,
enum: ['Section 1', 'Section 2', 'Section 3', 'Section 4', 'Section 5', 'Section 6', 'Section 7', 'Section 8']
},

location: { type: String, default: null },
length: { type: String, default: null },
description: { type: String, default: null },

dateAdded: { type: Date, default: Date.now() },

updatedBy: { type: String, default: null },
dateUpdated: { type: Date, default: null },

// Permissions
write: [{ type: String, trim: true, default: 'sysadmin' }],
read: [{ type: String, trim: true, default: 'sysadmin' }]
},
'nrpti'
);
Loading

0 comments on commit c458f2b

Please sign in to comment.