Skip to content

Commit

Permalink
Merge pull request #84 from RandyInVictoria/mmtihookup
Browse files Browse the repository at this point in the history
MEM-453 Project model update and script for MMTI hook-up
  • Loading branch information
marklise authored Sep 12, 2017
2 parents 7a46ac7 + 5ccf734 commit 151e86d
Show file tree
Hide file tree
Showing 4 changed files with 276 additions and 30 deletions.
95 changes: 65 additions & 30 deletions modules/projects/server/models/project.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// =========================================================================
module.exports = require ('../../../core/server/controllers/core.schema.controller')
('Project', {
__audit : true,
__audit: true,
__access: [
'addUsersToContext',
'createRole',
Expand Down Expand Up @@ -46,35 +46,35 @@ module.exports = require ('../../../core/server/controllers/core.schema.controll
'manageFolders',
'manageDocumentPermissions'
],
__tracking : true,
__status : ['Initiated', 'Submitted', 'In Progress', 'Certified', 'Not Certified', 'Decommissioned'],
__codename : 'unique',
duration : { type:Number, default: 90 },
epicProjectID : { type:Number, default: 0, index:true}, // Used to relate ePIC imports
shortName : { type:String, default: '' },
eacDecision : { type:String, default: '' },
CEAALink : { type:String, default: '' },
type : { type:String, default:'', index:true },
sector : { type:String, default:'' }, // This is actually sub-type now.
region : { type:String, default:'' }, // object id
locSpatial : { type:String, default:'' }, // incoming ePIC
location : { type:String, default:'' },
provElecDist : { type:String, default:'' },
fedElecDist : { type:String, default:'' },
__tracking : true,
__status : ['Initiated', 'Submitted', 'In Progress', 'Certified', 'Not Certified', 'Decommissioned'],
__codename : 'unique',
duration : { type:Number, default: 90 },
epicProjectID : { type:Number, default: 0, index:true}, // Used to relate ePIC imports
shortName : { type:String, default: '' },
eacDecision : { type:String, default: '' },
CEAALink : { type:String, default: '' },
type : { type:String, default:'', index:true },
sector : { type:String, default:'' }, // This is actually sub-type now.
region : { type:String, default:'' }, // object id
locSpatial : { type:String, default:'' }, // incoming ePIC
location : { type:String, default:'' },
provElecDist : { type:String, default:'' },
fedElecDist : { type:String, default:'' },
//
// all the phases that make this project up, in order
//
phases : [ { type:'ObjectId', ref:'Phase'} ],
phases: [ { type:'ObjectId', ref:'Phase'} ],
//
// ancestry
//
stream : { type:'ObjectId', ref:'Stream', index:true, default:null },
stream: { type:'ObjectId', ref:'Stream', index:true, default:null },
//
// the proponent and their orgCode, plus the primary contact for same
//
proponent : { type:'ObjectId', ref:'Organization', index:true, default:null },
orgCode : { type:String, default: '' },
primaryContact : { type:'ObjectId', ref:'User', default:null },
proponent : { type:'ObjectId', ref:'Organization', index:true, default:null },
orgCode : { type:String, default: '' },
primaryContact : { type:'ObjectId', ref:'User', default:null },
//
// these are here as helpers for assigning things throughout the system
//
Expand All @@ -101,7 +101,7 @@ module.exports = require ('../../../core/server/controllers/core.schema.controll
operatingjobsNotes : { type:String, default:'' },
section7optin : { type:String, default:'' }
},
isTermsAgreed : {type:Boolean, default:false},
isTermsAgreed : { type:Boolean, default:false },
build : { type:String, default:'' },
//
// location is a free form string entry
Expand All @@ -120,18 +120,18 @@ module.exports = require ('../../../core/server/controllers/core.schema.controll

// OLD DATA
responsibleEPD : { type: String, default: '' },
responsibleEPDPhone : { type: String, default: '' },
responsibleEPDPhone : { type: String, default: '' },
responsibleEPDEmail : { type: String, default: '' },
projectLead : { type: String, default: '' },
projectLead : { type: String, default: '' },
projectLeadPhone : { type: String, default: '' },
projectLeadEmail : { type: String, default: '' },
projectAnalyst : { type: String, default: '' },
projectAssistant : { type: String, default: '' },
administrativeAssistant : { type: String, default: '' },
CELead : { type: String, default: '' },
CELeadPhone : { type: String, default: '' },
CELeadEmail : { type: String, default: '' },
teamNotes : { type: String, default: '' },
CELead : { type: String, default: '' },
CELeadPhone : { type: String, default: '' },
CELeadEmail : { type: String, default: '' },
teamNotes : { type: String, default: '' },

// Migrated epic data wanting to be made available
eaActive : { type: String, default: '' },
Expand All @@ -140,11 +140,46 @@ module.exports = require ('../../../core/server/controllers/core.schema.controll
CEAAInvolvement : { type: String, default: '' },
projectNotes : { type: String, default: '' }, // Formal notes about the project

// MMTI hook-up data
isMajorMine : { type: Boolean, default: false },
epicProjectCodes : [ { type: String } ],

morePermitsLinkYear : { type: String, default: null },
morePermitsLink : { type: String, default: null },
moreInspectionsLink : { type: String, default: null },
moreInspectionsLinkYear : { type: String, default: null },

activities: [{
_id : false,
name : { type: String, default: '' },
status : { type: String, default: '', enum: ['Active', 'Inactive', 'Pending', 'Complete', 'Suspended', 'N/A', ''] },
order : { type: Number } // display order, not any business rules order
}],

externalLinks: [{
_id : false,
source : { type: String, default: '' }, // ex. IMPORT, MEM, EPIC
type : { type: String, default: '' }, // ex. EXTERNAL_LINK ?
page : { type: String, default: '' }, // ex. Compliance, Authorization, Mine, further grouping for different areas of concern
title : { type: String, default: '' }, // title for hyperlink...
link : { type: String, default: '' } // hyperlink...
}],

content: [{
_id : false,
source : { type: String, default: '' }, // ex. IMPORT, MEM, EPIC
type : { type: String, default: '' }, // ex. SUBTITLE, INTRO_TEXT, OVERVIEW_INTRO_TEXT
page : { type: String, default: '' }, // ex. Compliance, Authorization, Mine, further grouping for different areas of concern
title : { type: String, default: '' }, // ex Project Details, Inspections / Compliance Oversight ?
text : { type: String, default: '' },
html : { type: String, default: '' } // should be the same text as text, but with html markup as needed
}],

// MEM data
ownership : { type: String, default: '' },
memPermitID : { type:String, default: '', index:true}, // Used to relate mem permitID on import
memPermitID : { type: String, default: '', index: true }, // Used to relate mem permitID on import
commodity : { type: String, default: '' },
tailingsImpoundments : { type: String, default: '' },
epicStream : { type: String, default: '' },
epicStream : { type: String, default: '' },
directoryStructure : { type: Object, default: null }
});
7 changes: 7 additions & 0 deletions modules/projects/server/routes/project.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ module.exports = function (app) {
return obj;
});
}));

app.route ('/api/projects/major')
.all(policy ('guest'))
.get(routes.setAndRun (Project, function (model, req) {
return model.list ({ isMajorMine: true });
}));

app.route ('/api/projects/for/org/:orgid')
.all (policy ('user'))
.get (routes.setAndRun (Project , function (model, req) {
Expand Down
1 change: 1 addition & 0 deletions scripts/mmti-hookup/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npm install mongodb promise request lodash
203 changes: 203 additions & 0 deletions scripts/mmti-hookup/update-major-mines.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
'use strict';

var MongoClient = require('mongodb').MongoClient;
var ObjectID = require('mongodb').ObjectID;
var Promise = require('promise');
var request = require('request');
var _ = require('lodash');

var defaultConnectionString = 'mongodb://localhost:27017/mem-dev';
var username = '';
var password = '';
var host = '';
var db = '';
var url = '';

var args = process.argv.slice(2);
if (args.length !== 4) {
console.log('Using default localhost connection:', defaultConnectionString);
url = defaultConnectionString;
} else {
username = args[0];
password = args[1];
host = args[2];
db = args[3];
url = 'mongodb://' + username + ':' + password + '@' + host + ':27017/' + db;
}

var getProjectsFromMMTI = function() {
return new Promise(function(resolve, reject) {
request({
url : 'http://mines.nrs.gov.bc.ca/api/project/',
method : 'GET',
}, function(err, res, body) {
if (err) {
console.log('x error fetching projects from MMTI' + err);
resolve();
} else if (res.statusCode != 200) {
console.log('x ' + res.statusCode + ' while fetching projects from MMTI');
resolve();
} else if (!body) {
console.log('x failed to fetch projects from MMTI');
resolve();
} else {
var project = JSON.parse(body)
console.log(': successfully fetched projects from MMTI');
resolve(project);
}
});
});
}

var mapToMemProjectCode = function(code) {
switch (code) {
case 'brule':
return 'brule-dillon';
case 'copper-mountain':
return 'copper-mountain-similco';
case 'highland-valley-copper':
return 'highland-valley-copper-hvc';
default:
return code;
};
};

var getRelatedEpicProjects = function(code) {
switch (code) {
case 'brucejack':
return ['brucejack-gold-mine'];
case 'brule':
return ['brule-mine'];
case 'coal-mountain-operations':
return ['coal-mountain-phase-2'];
case 'copper-mountain':
return [];
case 'elkview-operations':
return [];
case 'fording-river-operations':
return ['fording-river-operations-swift'];
case 'gibraltar':
return [];
case 'greenhills-operations':
return [];
case 'highland-valley-copper':
return [];
case 'kemess':
return ['kemess-south', 'kemess-underground', 'kemess-north-copper-gold-mine'];
case 'line-creek-operations':
return ['line-creek-coal', 'line-creek-operations-phase-ii'];
case 'mount-milligan':
return ['mount-milligan-copper-gold', 'mt-milligan-copper-gold'];
case 'mount-polley':
return ['mt-polley-copper'];
case 'new-afton':
return [];
case 'red-chris':
return ['red-chris-copper-and-gold-mine', 'red-chris-porphyry-copper-gold-mine'];
case 'silvertip':
return ['silvertip-silver-lead-zinc-mine'];
case 'trend-roman':
return ['roman-coal-mine'];
case 'tulsequah-chief':
return ['tulsequah-chief-mine'];
case 'wolverine':
return ['wolverine-coal-mine'];
default:
return [];
};
};

var update = function(memProject, mmtiProject) {
return new Promise(function(resolve, reject) {
MongoClient.connect(url, function(err, db) {

var collection = db.collection('documents');

collection.updateOne({ _id: ObjectID.createFromHexString(id) }, { $set: { displayName: displayName } } , { }, function(err, obj) {
db.close();
if (err) {
console.log('x Failed to update document ' + id + ' to ' + displayName);
reject(err);
} else if (obj.result.n === 0) {
console.log('x Failed to find document ' + id);
resolve(obj);
} else {
console.log(': Successfully updated document ' + id + ' to ' + displayName);
resolve(obj);
}
});

});
});
};

var updateProject = function(db, mmtiProject) {
return new Promise(function(resolve, reject) {
var memCode = mapToMemProjectCode(mmtiProject.code);
console.log(': updating project ' + memCode);

db.collection('projects').updateOne({ code: memCode }, { $set: {
isMajorMine : true,
morePermitsLinkYear : mmtiProject.morePermitsLinkYear || '',
morePermitsLink : mmtiProject.morePermitsLink || '',
moreInspectionsLink : mmtiProject.moreInspectionsLink || '',
moreInspectionsLinkYear : mmtiProject.moreInspectionsLinkYear || '',
activities : mmtiProject.activities || [],
externalLinks : mmtiProject.externalLinks || [],
content : mmtiProject.content || [],
epicProjectCodes : getRelatedEpicProjects(mmtiProject.code),
}}, {}, function(err, obj) {
if (err) {
console.log('x Failed to update project ' + memCode);
reject(err);
} else if (obj.result.n === 0) {
console.log('x Failed to find project ' + memCode);
resolve(obj);
} else {
console.log(': Successfully updated project ' + memCode);
resolve(obj);
}
});
});
}

var run = function () {
var database = null;

return new Promise(function (resolve, reject) {
console.log('start');
MongoClient.connect(url)
.then(function(db) {
console.log(': db connected');
database = db;
})
.then(function() {
console.log(': getting projects from MMTI');
return getProjectsFromMMTI();
})
.then(function(projects) {
console.log(': updating projects...');
var projectPromises = []
_.each(projects, function(mmtiProject) {
projectPromises.push(updateProject(database, mmtiProject));
});
return Promise.all(projectPromises);
})
.then(function() {
database.close();
console.log('end');
resolve(':)');
}, function (err) {
console.log('ERROR: end err = ', JSON.stringify(err));
reject(err);
});
});
};

run().then(function(success) {
console.log('success ', success);
process.exit();
}).catch(function(error) {
console.error('error ', error);
process.exit();
});

0 comments on commit 151e86d

Please sign in to comment.