Created
April 18, 2020 01:04
-
-
Save MattGoldwater/c2ddbfde7e5543ac5b2b69e5762c03a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function EmployeeGraph() { | |
this._nextEmployeeId = 0; | |
this._employeeGraph = {} | |
this.allowedWriteFields = { | |
employee: ['firstName', 'lastName'], | |
organization: ['workEmail', 'jobTitle'] | |
} | |
this.allowedReadFields = { | |
employee: new Set(['firstName', 'lastName']), | |
} | |
this.log = []; | |
} | |
EmployeeGraph.prototype.addEmployee = function (firstName, lastName, dateOfBirth, jobTitle, workEmail, personalEmail, specialistNotes) { | |
this._employeeGraph[this._nextEmployeeId] = {firstName, lastName, dateOfBirth, jobTitle, workEmail, personalEmail, specialistNotes} | |
this._nextEmployeeId++; | |
} | |
EmployeeGraph.prototype.modification = function(title, modifications, id) { | |
// make sure id is valid | |
const allowedFields = new Set(this.allowedWriteFields[title]); // all but specialistNotes | |
for (let field in modifications) { | |
// console.log(field); | |
if (allowedFields.has(field)) { | |
// console.log(this._employeeGraph[id][field]) | |
this._employeeGraph[id][field] = modifications[field]; | |
} | |
} | |
this.log.push({type: 'write', fields: modifications, id}); | |
} | |
EmployeeGraph.prototype.viewEmployee = function(title, fields, id) { | |
const results = {}; | |
fields.forEach(field => { | |
if (this.allowedReadFields[title].has(field)) { | |
results[field] = this._employeeGraph[id][field]; | |
} | |
}) | |
this.log.push({type: 'read', fields, id}); | |
return results; | |
} | |
const newCompany = new EmployeeGraph(); | |
newCompany.addEmployee('Matt', 'Goldwater', '07171991', 'Software Engineer', '[email protected]', '[email protected]', 'fooBar') | |
newCompany.addEmployee('Matthias', 'Silverwater', '07101900', 'Software Engineering Manager', '[email protected]', '[email protected]', 'fooBarfa') | |
// console.log(newCompany._employeeGraph); | |
newCompany.modification('employee', {firstName: 'Sebastian', middleName: 'Jordan', specialistNotes: 'foofoo'}, 0) | |
newCompany.modification('organization', {firstName: 'Matt', middleName: 'Jordan', workEmail: '[email protected]'}, 0) | |
newCompany.modification('organization', {firstName: 'Bob', middleName: 'Jordan', workEmail: '[email protected]'}, 1) | |
console.log(newCompany.viewEmployee('employee', ['firstName', 'middleName', 'specialistNotes'], 0)); | |
console.log(newCompany.viewEmployee('employee', ['firstName', 'middleName', 'specialistNotes'], 1)); | |
// console.log(newCompany._employeeGraph); | |
console.log(newCompany.log); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment