2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2017-05-02 15:21:13 +00:00
|
|
|
module ChecksConditionValidation
|
2017-04-27 14:57:19 +00:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
before_create :validate_condition
|
|
|
|
before_update :validate_condition
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_condition
|
|
|
|
# use Marshal to do a deep copy of the condition hash
|
|
|
|
validate_condition = Marshal.load(Marshal.dump(condition))
|
|
|
|
|
|
|
|
# check if a valid condition got inserted.
|
|
|
|
validate_condition.delete('ticket.action')
|
2020-02-20 12:34:16 +00:00
|
|
|
validate_condition.delete('execution_time.calendar_id')
|
2017-04-27 14:57:19 +00:00
|
|
|
validate_condition.each do |key, value|
|
|
|
|
next if !value
|
|
|
|
next if !value['operator']
|
|
|
|
next if !value['operator']['has changed']
|
|
|
|
|
|
|
|
validate_condition.delete(key)
|
|
|
|
end
|
|
|
|
|
|
|
|
validate_condition['ticket.id'] = {
|
|
|
|
operator: 'is',
|
2018-12-19 17:31:51 +00:00
|
|
|
value: 1,
|
2017-04-27 14:57:19 +00:00
|
|
|
}
|
|
|
|
|
2019-06-28 11:38:49 +00:00
|
|
|
ticket_count, _tickets = Ticket.selectors(validate_condition, limit: 1, current_user: User.find(1))
|
2017-06-16 22:53:20 +00:00
|
|
|
return true if ticket_count.present?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-04-27 14:57:19 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, 'Invalid ticket selector conditions'
|
|
|
|
end
|
|
|
|
end
|