Skip to content

Commit d55db18

Browse files
fix(conditional-approval): update condition to check if user can trigger the action (#604)
1 parent f34b3c5 commit d55db18

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

app/services/forest_liana/ability/permission/smart_action_checker.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ def can_trigger?
3737
elsif @smart_action['approvalRequired'].include?(@user['roleId'])
3838
if @smart_action['approvalRequiredConditions'].empty? || match_conditions('approvalRequiredConditions')
3939
raise ForestLiana::Ability::Exceptions::RequireApproval.new(@smart_action['userApprovalEnabled'])
40+
else
41+
return true if @smart_action['triggerConditions'].empty? || match_conditions('triggerConditions')
4042
end
4143
end
4244

spec/services/forest_liana/ability/permission/smart_action_checker_spec.rb

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,67 @@ module Ability
205205
expect{smart_action_checker.can_execute?}.to raise_error(ForestLiana::Ability::Exceptions::TriggerForbidden)
206206
end
207207

208-
it 'should raise error when triggerConditions not match' do
208+
it 'should trigger action when approvalRequiredCondition not match but with triggerConditions matched' do
209+
parameters = ActionController::Parameters.new(params).permit!
210+
action['approvalRequired'] = [1]
211+
action['triggerEnabled'] = [1]
212+
action['triggerConditions'] = [
213+
{ 'filter' =>
214+
{ 'aggregator' => 'and',
215+
'conditions' =>
216+
[
217+
{
218+
'field' => 'name',
219+
'value' => 'foo',
220+
'source' => 'data',
221+
'operator' => 'equal'
222+
}
223+
]
224+
},
225+
'roleId' => 1
226+
}
227+
]
228+
action['approvalRequiredConditions'] = [
229+
{ 'filter' =>
230+
{ 'aggregator' => 'and',
231+
'conditions' =>
232+
[
233+
{
234+
'field' => 'name',
235+
'value' => 'fake island',
236+
'source' => 'data',
237+
'operator' => 'equal'
238+
}
239+
]
240+
},
241+
'roleId' => 1
242+
}
243+
]
244+
smart_action_checker = ForestLiana::Ability::Permission::SmartActionChecker.new(parameters, Island, action, user)
245+
246+
expect(smart_action_checker.can_execute?).to equal true
247+
end
248+
249+
it 'should raise error when approvalRequiredConditions and triggerConditions not match' do
209250
parameters = ActionController::Parameters.new(params).permit!
210251
action['approvalRequired'] = [1]
252+
action['triggerEnabled'] = [1]
253+
action['triggerConditions'] = [
254+
{ 'filter' =>
255+
{ 'aggregator' => 'and',
256+
'conditions' =>
257+
[
258+
{
259+
'field' => 'name',
260+
'value' => 'fake island',
261+
'source' => 'data',
262+
'operator' => 'equal'
263+
}
264+
]
265+
},
266+
'roleId' => 1
267+
}
268+
]
211269
action['approvalRequiredConditions'] = [
212270
{ 'filter' =>
213271
{ 'aggregator' => 'and',

0 commit comments

Comments
 (0)