~singpolyma/sgx-jmp

sgx-jmp/lib/configure_calls_form.rb -rw-r--r-- 945 bytes
94298f5dStephen Paul Weber Refactor alt top up to use FormTemplate 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# frozen_string_literal: true

require_relative "form_to_h"

class ConfigureCallsForm
	using FormToH

	def initialize(customer)
		@customer = customer
	end

	def render
		FormTemplate.render("configure_calls", customer: @customer)
	end

	def parse(form)
		params = form.to_h
		{}.tap do |result|
			result[:fwd] = parse_fwd(params["fwd"]) if params.key?("fwd")
			if params.key?("voicemail_transcription")
				result[:transcription_enabled] =
					["1", "true"].include?(params["voicemail_transcription"])
			end
			result[:lidb_name] = params["lidb_name"].strip if lidb_guard(params)
		end
	end

protected

	def lidb_guard(params)
		!params["lidb_name"].to_s.strip.empty? &&
			!@customer.tndetails.dig(:features, :lidb)
	end

	def parse_fwd(fwd_from_form)
		@customer.fwd.with(fwd_from_form.each_with_object({}) { |(var, val), args|
			args[var.to_sym] =
				var == "voicemail_enabled" ? ["1", "true"].include?(val) : val&.strip
		})
	end
end