Class: HasHelpers::Mutations::ToggleFavorite

Inherits:
Base
  • Object
show all
Defined in:
app/graphql/has_helpers/mutations/toggle_favorite.rb

Instance Method Summary collapse

Methods included from HasHelpers::Middleware::ApplicationAccess

#authorized?

Instance Method Details

#resolve(**foreign_keys) ⇒ 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
# File 'app/graphql/has_helpers/mutations/toggle_favorite.rb', line 13

def resolve(**foreign_keys)
  has_values = foreign_keys.values.compact.present?
  return { is_favorite: false } unless has_values

  favorite = ::HasHelpers::Favorite.find_or_initialize_by(
    created_by_id: current_user.id,
    organization_id: current_organization.id,
    **foreign_keys
  )

  # == Toggle favorite
  # If favorite exists, delete it
  # Else create a favorite
  is_favorite =
    if favorite.persisted?
      !favorite.destroy.destroyed?
    else
      favorite.save
    end

  {
    is_favorite: is_favorite
  }
end