Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PRC-834/827/828/829/831:Adding api endpoints for ttls api integration. #168

Merged
merged 1 commit into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 47 additions & 59 deletions api/controllers/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,69 +318,57 @@ exports.protectedPost = function (args, res, next) {
app.createdDate = Date.now();
app.save()
.then(function (savedApp) {
// Get the shapes from BCGW for this DISPOSITION and save them into the feature collection
var searchURL = "https://openmaps.gov.bc.ca/geo/pub/WHSE_TANTALIS.TA_CROWN_TENURES_SVW/ows?service=wfs&version=2.0.0&request=getfeature&typename=PUB:WHSE_TANTALIS.TA_CROWN_TENURES_SVW&outputFormat=json&srsName=EPSG:4326&CQL_FILTER=DISPOSITION_TRANSACTION_SID=";
defaultLog.info("SEARCHING:", searchURL+ "'" + savedApp.tantalisID + "'");
return new Promise(function (resolve, reject) {
request({url: searchURL + "'" + savedApp.tantalisID + "'"}, function (err, res, body) {
if (err) {
reject(err);
} else if (res.statusCode !== 200) {
reject(res.statusCode+' '+body);
return Utils.loginWebADE()
.then(function (accessToken) {
_accessToken = accessToken;
console.log("TTLS API Logged in:", _accessToken);
// Disp lookup
return Utils.getApplicationByDispositionID(_accessToken, savedApp.tantalisID);
}).then(resolve, reject);
}).then(function (data) {
// Copy in the meta
savedApp.areaHectares = data.areaHectares;
savedApp.centroid = data.centroid;
savedApp.purpose = data.TENURE_PURPOSE;
savedApp.subpurpose = data.TENURE_SUBPURPOSE;
savedApp.type = data.TENURE_TYPE;
savedApp.subtype = data.TENURE_SUBTYPE;
savedApp.status = data.TENURE_STATUS;
savedApp.tenureStage = data.TENURE_STAGE;
savedApp.location = data.TENURE_LOCATION;
savedApp.businessUnit = data.RESPONSIBLE_BUSINESS_UNIT;
savedApp.cl_file = data.CROWN_LANDS_FILE;
savedApp.tantalisID = data.DISPOSITION_TRANSACTION_SID;

for(let [idx,client] of Object.entries(data.interestedParties)) {
if (idx > 0) {
savedApp.client += ", ";
}
if (client.interestedPartyType == 'O') {
savedApp.client += client.legalName;
} else {
var obj = {};
try {
defaultLog.info ('BCGW Call Complete.', body);
obj = JSON.parse(body);

// Store the features in the DB
var allFeaturesForDisp = [];
// don't clear previous value if no features
if (obj.features.length > 0) {
savedApp.areaHectares = 0.00;
}
var turf = require('@turf/turf');
var helpers = require('@turf/helpers');
var centroids = helpers.featureCollection([]);
_.each(obj.features, function (f) {
// Tags default public
f.tags = [['sysadmin'], ['public']];
allFeaturesForDisp.push(f);
// Get the polygon and put it for later centroid calculation
centroids.features.push(turf.centroid(f));
// Calculate Total Area (hectares) from all features
if (f.properties && f.properties.TENURE_AREA_IN_HECTARES) {
savedApp.areaHectares += parseFloat(f.properties.TENURE_AREA_IN_HECTARES);
}
});
// Centroid of all the shapes.
var featureCollectionCentroid;
if (centroids.features.length > 0) {
featureCollectionCentroid = turf.centroid(centroids).geometry.coordinates;
// Store the centroid.
savedApp.centroid = featureCollectionCentroid;
}

Promise.resolve()
.then(function () {
return allFeaturesForDisp.reduce(function (previousItem, currentItem) {
return previousItem.then(function () {
return doFeatureSave(currentItem, savedApp._id);
});
}, Promise.resolve());
}).then(function () {
// All done with promises in the array, return to the caller.
return savedApp.save();
})
.then(function (a) {
resolve(a);
});
} catch (e) {
defaultLog.error ('Parsing Failed.', e);
resolve(savedApp);
}
savedApp.client += client.firstName + " " + client.lastName;
}
}

Promise.resolve()
.then(function () {
return data.parcels.reduce(function (previousItem, currentItem) {
return previousItem.then(function () {
// publish
currentItem.tags = [['sysadmin'],['public']];
return doFeatureSave(currentItem, savedApp._id);
});
}, Promise.resolve());
}).then(function () {
// All done with promises in the array, return to the caller.
console.log("all done");
return savedApp.save();
});
}).catch(function (err) {
console.log("Error in API:", err);
return Actions.sendResponse(res, 400, err);
});
}).then(function (theApp) {
// defaultLog.info("Saved new application object:", a);
Expand Down
76 changes: 70 additions & 6 deletions api/controllers/search.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,73 @@
var auth = require("../helpers/auth");
var _ = require('lodash');
var defaultLog = require('winston').loggers.get('default');
var mongoose = require('mongoose');
var Actions = require('../helpers/actions');
var request = require('request');
var auth = require("../helpers/auth");
var _ = require('lodash');
var defaultLog = require('winston').loggers.get('default');
var mongoose = require('mongoose');
var Actions = require('../helpers/actions');
var Utils = require('../helpers/utils');
var request = require('request');
var _accessToken = null;

exports.protectedTTLSGetApplicationsByFileNumber = function (args, res, rest) {
var fileNumber = args.swagger.params.fileNumber.value;
defaultLog.info("Searching TTLS API for Crown Land FileNumber:", fileNumber);
return new Promise(function (r, j) {
return Utils.loginWebADE()
.then(function (accessToken) {
_accessToken = accessToken;
console.log("TTLS API Logged in:", _accessToken);
// fileNumber lookup
return Utils.getApplicationByFilenumber(_accessToken, fileNumber);
}).then(r, j);
}).then(function (promises) {
console.log("returning number of items:", promises.length);

// Call the api again but this time grab all the related information on each app
// returned form the CL file lookup.
var allApps = [];
Promise.resolve()
.then(function () {
return promises.reduce(function (previousItem, currentItem) {
return previousItem.then(function () {
// return Actions.publish(currentItem);
console.log("executing disp:", currentItem.DISPOSITION_TRANSACTION_SID);
return Utils.getApplicationByDispositionID(_accessToken, currentItem.DISPOSITION_TRANSACTION_SID)
.then(function (appData) {
allApps.push(appData);
return appData;
});
});
}, Promise.resolve());
}).then(function () {
// All done with promises in the array, return to the caller.
defaultLog.info("------------------------done with promises------------------------");
console.log(allApps);
return Actions.sendResponse(res, 200, allApps);
});
}).catch(function (err) {
console.log("Error in API:", err);
return Actions.sendResponse(res, 400, err);
});
};

exports.protectedTTLSGetApplicationByDisp = function (args, res, rest) {
var dtId = args.swagger.params.dtId.value;
defaultLog.info("Searching TTLS API for Disposition Transaction ID:", dtId);
return new Promise(function (resolve, reject) {
return Utils.loginWebADE()
.then(function (accessToken) {
_accessToken = accessToken;
console.log("TTLS API Logged in:", _accessToken);
// Disp lookup
return Utils.getApplicationByDispositionID(_accessToken, dtId);
}).then(resolve, reject);
}).then(function (data) {
console.log("returning:", data.DISPOSITION_TRANSACTION_SID);
return Actions.sendResponse(res, 200, data);
}).catch(function (err) {
console.log("Error in API:", err);
return Actions.sendResponse(res, 400, err);
});
};

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