Created
January 24, 2019 03:58
-
-
Save martiuh/6bf490c66e231810d9b117ffda7a3858 to your computer and use it in GitHub Desktop.
Object Rank Role Resolver
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
const ranks = require('./ranks') | |
module.exports = (ownRole, otherRole) => { | |
if (!ownRole) { | |
console.error('ownRole es necesario') | |
return false | |
} | |
let activeRanks = Object.keys(ranks).map(R => ({ | |
name: R, content: ranks[R], solved: false, from: [], parent: null | |
})) | |
let pendingRanks = [] | |
let ways = {} | |
let found = false | |
let wayNumber = 0 | |
while (!found) { | |
if (activeRanks.length === 0) { | |
break | |
} | |
activeRanks.forEach((A, i) => { | |
// TODO: Si el rol no existe, entonces te manda al chile | |
const { content, name } = A | |
let { from } = A | |
from = [...from, name] | |
const wayName = from.join('XXX') | |
if (name === ownRole) { | |
found = wayName | |
} | |
ways[wayName] = { path: from, content: content } | |
if (typeof content === 'object') { | |
pendingRanks.push({ content, from }) | |
} | |
}) | |
activeRanks = [] | |
pendingRanks.forEach(P => { | |
Object.keys(P.content).forEach(C => { | |
activeRanks.push({ | |
name: C, | |
content: P.content[C], | |
from: [...P.from], | |
parent: P.from[P.from.length - 1] | |
}) | |
}) | |
}) | |
pendingRanks = [] | |
} | |
if (!found) { | |
return false | |
} | |
// Ya que tenemos el objeto exacto donde está, ahora vamos a hacer todo el objeto un array | |
let rolesBellow = Object.keys(ways[found].content).map(R => ({ | |
content: ways[found].content[R], name: R | |
})) | |
let grantRoles = [] | |
if (rolesBellow.length === 0) { | |
return false | |
} | |
let pendingBellow = [] | |
while (rolesBellow.length !== 0) { | |
rolesBellow.forEach(R => { | |
if (otherRole === R.name) { | |
otherRole = true | |
} | |
grantRoles.push(R.name) | |
if (typeof R.content === 'object') { | |
pendingBellow.push({ content: R.content, name: R.name }) | |
} | |
}) | |
rolesBellow = [] | |
pendingBellow.forEach(P => { | |
Object.keys(P.content).forEach(C => rolesBellow.push({ | |
content: P.content[C], | |
name: C | |
})) | |
}) | |
pendingBellow = [] | |
} | |
if (otherRole) { | |
if (otherRole === true) { | |
return otherRole | |
} | |
if (typeof otherRole === 'string') { | |
return false | |
} | |
} | |
return grantRoles | |
} |
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
// No puede haber nombres repetidos | |
module.exports = { | |
superadmin: { | |
administrador: { | |
calidad: true, | |
ventas: true | |
} | |
} | |
} | |
const mockRank = { | |
superadmin: { | |
administrador: { | |
compras: true, | |
calidad: true, | |
contabilidad: { | |
contador: { | |
auxiliar: true | |
} | |
} | |
}, | |
soporte: { | |
tecnico: true, | |
computacional: { | |
ingeniero: true, | |
loquerote: true | |
} | |
} | |
}, | |
administradorPlus: true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment