Class: CommandTower::Deserializers::Messaging::Preferences::UpdateDeserializer

Inherits:
ApplicationDeserializer show all
Defined in:
app/deserializers/command_tower/deserializers/messaging/preferences/update_deserializer.rb

Defined Under Namespace

Classes: Input

Constant Summary collapse

ROUTING_KEYS =
%w[controller action format notification_type_key preference].freeze
ALLOWED_PREFERENCE_KEYS =
%w[inboxEnabled channels].freeze

Instance Method Summary collapse

Methods inherited from ApplicationDeserializer

call

Instance Method Details

#call(params) ⇒ Object



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
# File 'app/deserializers/command_tower/deserializers/messaging/preferences/update_deserializer.rb', line 13

def call(params)
  raw = normalize_hash(params)
  return failure(errors: { message: "invalid_notification_type_key" }) if raw.nil?

  notification_type_key = raw["notification_type_key"]
  if notification_type_key.nil? || !notification_type_key.is_a?(String) || notification_type_key.strip.empty?
    return failure(errors: { message: "invalid_notification_type_key" })
  end

  extras = raw.keys - ROUTING_KEYS - ["preferences"]
  return failure(errors: { message: "unexpected_fields" }) if extras.any?
  return failure(errors: { message: "missing_preferences" }) unless raw.key?("preferences")

  preferences = raw["preferences"]
  return failure(errors: { message: "invalid_preferences" }) unless preferences.is_a?(Hash)

  preference_state = parse_preferences(preferences)
  return preference_state if preference_state.is_a?(DeserializerResult)

  success(
    Input.new(
      notification_type_key: notification_type_key.strip,
      preference_state:,
    ),
  )
end