From bf9aeaddc46e732c5ea246c505e36449ccc46cd8 Mon Sep 17 00:00:00 2001 From: Greg McCullough Date: Wed, 25 Jul 2018 19:04:23 -0400 Subject: [PATCH] Support setting retires_on and retirement_warn for services from dialogs --- app/models/service/dialog_properties.rb | 62 ++++++++++++++++++- spec/models/service/dialog_properties_spec.rb | 42 +++++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-) diff --git a/app/models/service/dialog_properties.rb b/app/models/service/dialog_properties.rb index 239c000de4d3..2e8efc470b15 100644 --- a/app/models/service/dialog_properties.rb +++ b/app/models/service/dialog_properties.rb @@ -1,5 +1,8 @@ class Service class DialogProperties + RETIREMENT_FIELD_NAMES = %w(retires_on retires_in_hours retires_in_days retire_on_date).freeze + RETIREMENT_WARN_FIELD_NAMES = %w(warn_on warn_in_days warn_in_hours warn_offset_days warn_offset_hours).freeze + def initialize(options) @attributes = {} @options = options || {} @@ -16,7 +19,7 @@ def parse private def parse_options - %w(name description).each do |field_name| + (%w(name description) + RETIREMENT_FIELD_NAMES).each do |field_name| dialog_field_name = "dialog_service_#{field_name}" dialog_field_value = @options[dialog_field_name] parse_value(field_name, dialog_field_name, dialog_field_value) if @options.key?(dialog_field_name) @@ -36,5 +39,62 @@ def name(value) def description(value) @attributes[:description] = value if value.present? end + + def retires_on(value) + self.retire_on_date = Time.zone.parse(value) + end + + def retires_in_hours(value) + retires_in_duration(value, :hours) + end + + def retires_in_days(value) + retires_in_duration(value, :days) + end + + def retires_in_duration(value, modifier) + self.retire_on_date = Time.zone.now + offset_set(value, modifier) + end + + def offset_set(value, modifier) + value.to_i.send(modifier).tap do |offset| + raise "Offset cannot be a zero or negative value" if offset.zero? || offset.negative? + end + end + + def retire_on_date=(value) + @attributes[:retires_on] = value + retirement_warning + end + + def retirement_warning + warn_value = parse_retirement_warn + @attributes[:retirement_warn] = warn_value if warn_value + end + + def parse_retirement_warn + warn_key, value = retirement_warn_properties + + case warn_key + when 'warn_on' + Time.zone.parse(value) + when 'warn_in_days' + Time.zone.now + offset_set(value, :days) + when 'warn_in_hours' + Time.zone.now + offset_set(value, :hours) + when 'warn_offset_days' + @attributes[:retires_on] - offset_set(value, :days) + when 'warn_offset_hours' + @attributes[:retires_on] - offset_set(value, :hours) + end + end + + def retirement_warn_properties + RETIREMENT_WARN_FIELD_NAMES.each do |field_name| + dialog_field_name = "dialog_service_retirement_#{field_name}" + + return field_name, @options[dialog_field_name] if @options[dialog_field_name].present? + end + end end end diff --git a/spec/models/service/dialog_properties_spec.rb b/spec/models/service/dialog_properties_spec.rb index 7225d84e20c0..521db2da10af 100644 --- a/spec/models/service/dialog_properties_spec.rb +++ b/spec/models/service/dialog_properties_spec.rb @@ -32,4 +32,46 @@ expect(described_class.parse(options)).to eq(:description => 'test description from dialog') end end + + context 'retires_on' do + it 'with invalid time' do + options = {'dialog_service_retires_on' => 'xyz'} + parsed_results = described_class.parse(options) + + expect(parsed_results[:retires_on]).to be_nil + expect(parsed_results[:retirement_warn]).to be_nil + end + + it 'with valid time' do + time = Time.new(2018, 7, 21, 12, 20, 0, 0) + Timecop.freeze(time) do + options = {'dialog_service_retires_on' => time.to_s} + parsed_results = described_class.parse(options) + + expect(parsed_results[:retires_on]).to eq(time) + expect(parsed_results[:retirement_warn]).to be_nil + end + end + end + + context 'retires_in_hours' do + it 'with invalid time' do + options = {'dialog_service_retires_in_hours' => 'xyz'} + parsed_results = described_class.parse(options) + + expect(parsed_results[:retires_on]).to be_nil + expect(parsed_results[:retirement_warn]).to be_nil + end + + it 'with valid time' do + time = Time.new(2018, 7, 21, 12, 20, 0, 0) + Timecop.freeze(time) do + options = {'dialog_service_retires_in_hours' => 5} + parsed_results = described_class.parse(options) + + expect(parsed_results[:retires_on]).to eq(time + 5.hours) + expect(parsed_results[:retirement_warn]).to be_nil + end + end + end end