aboutsummaryrefslogtreecommitdiff
path: root/app/jobs/ensure_agent_job.rb
blob: a37b6faf70d9386d74110b60ac47a58dfbab2d62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# SPDX-FileCopyrightText: 2021 IN COMMON Collective
#
# SPDX-License-Identifier: AGPL-3.0-or-later

class EnsureAgentJob < ApplicationJob
  queue_as :default

  include AgentsHelper

  def perform(user, groups)
    # Ensure the logged in user has a current agent
    # In order to do this, we first check the existing agents against the user's
    # groups. If none match, we assign the user to the default Anonymous agent.
    existing_agents = Agent.where(name: groups)
    if existing_agents.nil?
      user.agents << default_agent unless user.agents.include? default_agent
    else
      # Update user agents
      user.agents << existing_agents - user.agents
    end
  end
end