From 0acc5b717ed93a0f8fafea3dea6d6f99e7f58ff8 Mon Sep 17 00:00:00 2001 From: Jean-PhilippeD Date: Mon, 20 Aug 2018 21:20:53 +0200 Subject: [PATCH] improve condition in launcher --- api/core/scenario/scenario.export.js | 3 ++- api/core/scenario/scenario.import.js | 3 ++- api/core/scenario/scenario.queries.js | 4 ++-- api/core/scenario/scenario.verifyconditions.js | 9 ++++++--- api/core/state/state.create.js | 2 +- api/models/State.js | 5 +++++ assets/js/app/scenario/scenario.controller.js | 1 + assets/js/app/state/state.service.js | 5 +++-- config/locales/en.json | 6 +++++- config/locales/fr.json | 8 ++++++-- test/fixtures/state.json | 8 +++++--- views/scenario/modal.ejs | 9 +++++++-- 12 files changed, 45 insertions(+), 18 deletions(-) diff --git a/api/core/scenario/scenario.export.js b/api/core/scenario/scenario.export.js index 407c2984fb..7082f2eff3 100644 --- a/api/core/scenario/scenario.export.js +++ b/api/core/scenario/scenario.export.js @@ -29,6 +29,7 @@ function normalize(data) { } else { dictionnary[elem.id] = { code: elem.code, + trueIf: elem.trueIf, params: { [elem.variablename]: elem.value } @@ -37,4 +38,4 @@ function normalize(data) { } }); return newArray; -} \ No newline at end of file +} diff --git a/api/core/scenario/scenario.import.js b/api/core/scenario/scenario.import.js index 85030f4419..14d7a70dab 100644 --- a/api/core/scenario/scenario.import.js +++ b/api/core/scenario/scenario.import.js @@ -38,6 +38,7 @@ module.exports = function(params) { return gladys.stateType.getByServiceFunction({service: splitted[0], function: splitted[1]}) .then(stateType => gladys.state.create({ state: stateType.id, + trueIf: condition.trueIf, condition_template: condition.condition_template, active: condition.active, launcher: launcher.id, @@ -72,4 +73,4 @@ module.exports = function(params) { }; }); -}; \ No newline at end of file +}; diff --git a/api/core/scenario/scenario.queries.js b/api/core/scenario/scenario.queries.js index eb795da7e0..5163ad3ac5 100644 --- a/api/core/scenario/scenario.queries.js +++ b/api/core/scenario/scenario.queries.js @@ -15,7 +15,7 @@ module.exports = { JOIN actiontypeparam ON (actionparam.actiontypeparam = actiontypeparam.id) WHERE action = ?;`, getStatesLauncher: ` - SELECT statetype.*, state.id AS stateId, state.condition_template AS condition_template + SELECT statetype.*, state.id AS stateId, state.condition_template AS condition_template, trueIf FROM state INNER JOIN statetype ON state.state = statetype.id WHERE launcher = ?; @@ -27,7 +27,7 @@ module.exports = { WHERE launcher.id = ?; `, getStates: ` - SELECT state.id, CONCAT(statetype.service, '.', statetype.function) as code, state.condition_template, state.active, + SELECT state.id, CONCAT(statetype.service, '.', statetype.function) as code, state.condition_template, state.active, state.trueIf, stateparam.value, statetypeparam.variablename FROM state JOIN statetype ON state.state = statetype.id diff --git a/api/core/scenario/scenario.verifyconditions.js b/api/core/scenario/scenario.verifyconditions.js index 4eeee3b447..8e76df83c4 100644 --- a/api/core/scenario/scenario.verifyconditions.js +++ b/api/core/scenario/scenario.verifyconditions.js @@ -53,10 +53,13 @@ function verify(statetype, params) { return verifyFunction(paramsToSend); }) .then(function(scope) { - + + var trueIf = true + if(statetype.trueIf === 0) trueIf = false + // module can send raw response if he don't need the template - if(scope === true) return Promise.resolve(scope); - if(scope === false) return Promise.reject(new Error('conditions_not_verified')); + if(scope === trueIf) return Promise.resolve(scope); + if(scope === !trueIf) return Promise.reject(new Error('conditions_not_verified')); // if the scope is not a boolean, we inject it in the condition template try { diff --git a/api/core/state/state.create.js b/api/core/state/state.create.js index deaf002405..d9a844b9ef 100644 --- a/api/core/state/state.create.js +++ b/api/core/state/state.create.js @@ -35,4 +35,4 @@ module.exports = function(state){ return gladys.stateParam.create(paramsArray); }) .then(() => createdState); -}; \ No newline at end of file +}; diff --git a/api/models/State.js b/api/models/State.js index 0035adb3db..972af3d10a 100644 --- a/api/models/State.js +++ b/api/models/State.js @@ -17,6 +17,11 @@ module.exports = { defaultsTo: true }, + trueIf: { + type: 'boolean', + defaultsTo: true + }, + launcher: { model: 'Launcher', required: true diff --git a/assets/js/app/scenario/scenario.controller.js b/assets/js/app/scenario/scenario.controller.js index 149ff82ad5..b08609f4c9 100644 --- a/assets/js/app/scenario/scenario.controller.js +++ b/assets/js/app/scenario/scenario.controller.js @@ -143,6 +143,7 @@ var newState = { launcher: vm.newLauncher.id, state: stateType.id, + trueIf: true, name: stateType.name }; diff --git a/assets/js/app/state/state.service.js b/assets/js/app/state/state.service.js index 56a8a8c965..9aadf24c30 100644 --- a/assets/js/app/state/state.service.js +++ b/assets/js/app/state/state.service.js @@ -49,7 +49,8 @@ var newState = { state: state.state, - launcher: state.launcher + launcher: state.launcher, + trueIf: state.trueIf }; // we create the condition template @@ -82,4 +83,4 @@ } } -})(); \ No newline at end of file +})(); diff --git a/config/locales/en.json b/config/locales/en.json index b7ecf8fc28..aa7b6f80ca 100755 --- a/config/locales/en.json +++ b/config/locales/en.json @@ -132,6 +132,7 @@ "parameters-box-table-delete": "Delete", "parameters-box-left": "Left", "parameters-box-right": "Right", + "scenario-if-condition": "Condition is validated if", "scenario-list": "My scenario", "scenario-help": "Help", "scenario-table-event": "Trigger", @@ -398,5 +399,8 @@ "reset-password-new-password": "New Password", "reset-password-button": "Change my password", "reset-password-minimum-size-warning": "(Minimum 8 characters)", - "forgot-password-reset-failed": "Your password was not changed. Maybe the link was not valid anymore (It is valid only 2 hours), or maybe your password length was too low. Please try again." + "forgot-password-reset-failed": "Your password was not changed. Maybe the link was not valid anymore (It is valid only 2 hours), or maybe your password length was too low. Please try again.", + "True": "True", + "False": "False", + "is": "is" } diff --git a/config/locales/fr.json b/config/locales/fr.json index 9f6881fc54..396df89a3b 100755 --- a/config/locales/fr.json +++ b/config/locales/fr.json @@ -298,6 +298,7 @@ "parameters-box-table-delete": "Supprimer", "parameters-box-left": "Gauche", "parameters-box-right": "Droite", + "scenario-if-condition": "La condition est validée si", "scenario-list": "Mes scénarios", "scenario-help": "Aide", "scenario-table-event": "Déclencheur", @@ -562,5 +563,8 @@ "reset-password-new-password": "Nouveau mot de passe", "reset-password-button": "Changer mon mot de passe", "reset-password-minimum-size-warning": "(Minimum 8 caractères)", - "forgot-password-reset-failed": "Le changement de votre mot de passe a échoué. Le lien n'était peut être plus valide (il n'est valide que 2 heures) ou le mot de passe était trop court. Merci de ré-essayer." -} \ No newline at end of file + "forgot-password-reset-failed": "Le changement de votre mot de passe a échoué. Le lien n'était peut être plus valide (il n'est valide que 2 heures) ou le mot de passe était trop court. Merci de ré-essayer.", + "True": "Vraie", + "False": "Fausse", + "is": "est" +} diff --git a/test/fixtures/state.json b/test/fixtures/state.json index 9265a48937..c560c0273f 100644 --- a/test/fixtures/state.json +++ b/test/fixtures/state.json @@ -3,12 +3,14 @@ "id": 1, "launcher": 1, "state": 1, - "condition_template": "true" + "condition_template": "true", + "trueIf": 0 }, { "id": 2, "launcher": 3, "state": 2, - "condition_template": "true" + "condition_template": "true", + "trueIf": 0 } -] \ No newline at end of file +] diff --git a/views/scenario/modal.ejs b/views/scenario/modal.ejs index 149b6092f4..6d3e94a610 100644 --- a/views/scenario/modal.ejs +++ b/views/scenario/modal.ejs @@ -182,7 +182,12 @@
-

{{state.name}}

+
+

+ <%= __('scenario-if-condition') %>   <%= __('is') %>    +

+
+
@@ -317,4 +322,4 @@
- \ No newline at end of file +