2017-01-26 10:27:24 +00:00
|
|
|
// Séparation artificielle, temporaire, entre ces deux types de règles
|
|
|
|
import rawRules from './load-rules'
|
|
|
|
import rawEntityRules from './load-entity-rules'
|
|
|
|
import R from 'ramda'
|
2017-01-23 18:06:46 +00:00
|
|
|
import possibleVariableTypes from './possibleVariableTypes.yaml'
|
2017-01-26 10:27:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
/***********************************
|
|
|
|
Méthodes agissant sur une règle */
|
|
|
|
|
|
|
|
export let enrichRule = rule => {
|
|
|
|
let
|
|
|
|
type = possibleVariableTypes.find(t => rule[t]),
|
|
|
|
name = rule[type],
|
2017-01-26 12:19:04 +00:00
|
|
|
dottedName = rule.attache && [
|
|
|
|
rule.attache,
|
|
|
|
rule.alias || name
|
|
|
|
].join(' . ')
|
2017-01-26 10:27:24 +00:00
|
|
|
|
|
|
|
return {...rule, type, name, dottedName}
|
|
|
|
}
|
|
|
|
|
|
|
|
export let hasKnownRuleType = rule => rule && enrichRule(rule).type
|
|
|
|
|
2017-01-26 12:19:04 +00:00
|
|
|
let splitName = R.split(' . ')
|
2017-01-26 10:27:24 +00:00
|
|
|
|
2017-01-26 12:19:04 +00:00
|
|
|
export let parentName = R.pipe(
|
|
|
|
splitName,
|
|
|
|
R.dropLast(1),
|
|
|
|
R.join(' . ')
|
|
|
|
)
|
|
|
|
export let nameLeaf = R.pipe(
|
|
|
|
splitName,
|
|
|
|
R.last
|
|
|
|
)
|
2017-01-26 10:27:24 +00:00
|
|
|
|
|
|
|
// On enrichit la base de règles avec des propriétés dérivées de celles du YAML
|
2017-02-08 16:50:22 +00:00
|
|
|
let [rules, entityRules] =
|
2017-01-26 10:27:24 +00:00
|
|
|
[rawRules, rawEntityRules].map(rules => rules.map(enrichRule))
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************
|
|
|
|
Méthodes de recherche d'une règle */
|
2016-12-07 18:08:10 +00:00
|
|
|
|
2017-02-09 17:15:25 +00:00
|
|
|
export let findRuleByName = search =>
|
2017-02-08 16:50:22 +00:00
|
|
|
[...rules, ...entityRules]
|
2017-01-24 15:22:40 +00:00
|
|
|
.map(enrichRule)
|
2017-02-09 17:15:25 +00:00
|
|
|
.find( ({name}) =>
|
2017-01-10 18:22:44 +00:00
|
|
|
name === search
|
2016-12-07 18:08:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
export let searchRules = searchInput =>
|
|
|
|
rules
|
|
|
|
.filter( rule =>
|
|
|
|
rule && hasKnownRuleType(rule) &&
|
2017-01-26 16:30:15 +00:00
|
|
|
JSON.stringify(rule).toLowerCase().indexOf(searchInput) > -1)
|
2017-01-24 15:22:40 +00:00
|
|
|
.map(enrichRule)
|
2016-12-07 18:08:10 +00:00
|
|
|
|
|
|
|
|
2017-01-23 18:06:46 +00:00
|
|
|
|
2017-01-26 12:19:04 +00:00
|
|
|
export let findRuleByDottedName = dottedName => do {
|
|
|
|
let found = entityRules.find(rule => rule.dottedName == dottedName)
|
2017-02-07 19:10:04 +00:00
|
|
|
found
|
2017-01-26 12:19:04 +00:00
|
|
|
}
|
2017-01-24 15:22:40 +00:00
|
|
|
|
|
|
|
export let findGroup = R.pipe(
|
|
|
|
findRuleByDottedName,
|
|
|
|
found => found && found['choix exclusifs'] && found,
|
|
|
|
// Is there a way to express this more litterally in ramda ?
|
|
|
|
// R.unless(
|
|
|
|
// R.isNil,
|
|
|
|
// R.when(
|
|
|
|
// R.has('choix exclusifs'),
|
|
|
|
// R.identity
|
|
|
|
// )
|
|
|
|
// )
|
|
|
|
)
|