class Readiness::SupportSuperFormProcessor::USGovSetOrgASE

@since 1.0.42
@author Jason Colyer
Defines the class USGovSetOrgASE within the module {Readiness::Zendesk}.
#

def self.agent

Other tags:
    Since: - 1.0.42

Other tags:
    Author: - Jason Colyer
def self.agent
  @agent ||= Readiness::Zendesk::Users.find(@zendesk_client, agent_id)
end

def self.agent_id

Other tags:
    Since: - 1.0.42

Other tags:
    Author: - Jason Colyer
def self.agent_id
  @agent_id ||= ENV.fetch('ZENDESK_USGOV_ORGANIZATION_ASE_ID')
end

def self.agent_info

Other tags:
    Since: - 1.0.42

Other tags:
    Author: - Jason Colyer
def self.agent_info
  return '' if agent_id == 'None'
  agent.id.to_s
end

def self.agent_valid?

Other tags:
    Since: - 1.0.42

Other tags:
    Author: - Jason Colyer
def self.agent_valid?
  return true if agent_id == 'None'
  return false if agent.is_a? Hash
  return false if agent.role == 'end-user'
  return false if agent.suspended
  return false if agent.custom_role_id == 360008074111
  true
end

def self.approved_requester?

Other tags:
    Since: - 1.0.42

Other tags:
    Author: - Jason Colyer
def self.approved_requester?
  return true if approved_requester_list.map { |t| t['email'] }.include? requester_email
  false
end

def self.approved_requester_list

Other tags:
    Since: - 1.0.42

Other tags:
    Author: - Jason Colyer
def self.approved_requester_list
  @approved_requester_list ||= determine_approved_requesters
end

def self.bad_requester

def self.bad_requester
  issue = Readiness::GitLab::Issues.new
  issue.title = 'Invalid Request - Not approved to make this request'
  issue.description = bad_requester_message
  create = Readiness::GitLab::Issues.create!(@gitlab_client, project, issue)
  puts "Issue created: #{create.web_url}"
  exit 0
end

def self.bad_requester_message

def self.bad_requester_message
  <<~STRING
    ## Invalid Request - Not approved to make this request
    Greetings @#{requester.username} !
    We received a request from you recently to set an ASE on a Zendesk US Goverment organization, however this type of request can only be made by specifc approved persons.
    Please contact one fo the following via [#support_leadership](https://gitlab.enterprise.slack.com/archives/C01F9S37AKT) to discuss this request:
    #{approved_requester_list.map { |a| "- #{a['email']}" }.join("\n")}
    /label ~"AutomatedRequests::Invalid Permissions"
    /assign @#{requester.username}
  STRING
end

def self.determine_approved_requesters

Other tags:
    Since: - 1.0.42

Other tags:
    Author: - Jason Colyer
def self.determine_approved_requesters
  members = []
  directors = team.select { |t| t['title'] =~ /Director/ }
  members += directors.select { |t| t['region'] =~ /AMER/ }
  managers = team.select { |t| t['title'] =~ /Manager/ }
  members += managers.select { |t| t['zendesk']['us-federal']['role'] == 'Administrator' }
  members
end

def self.invalid_agent

def self.invalid_agent
  issue = Readiness::GitLab::Issues.new
  issue.title = 'Invalid Request - Invalid agent provided'
  issue.description = invalid_user_message
  create = Readiness::GitLab::Issues.create!(@gitlab_client, project, issue)
  puts "Issue created: #{create.web_url}"
  exit 0
end

def self.invalid_user_message

Other tags:
    Since: - 1.0.42

Other tags:
    Author: - Jason Colyer
def self.invalid_user_message
  <<~STRING
    ## Set an Assigned Support Engineer for a Zendesk US Goverment organization - #{org.id}
    Greetings @#{requester.username} !
    We were unable to set the ASE on the Zendesk US Goverment organization at this time, as we were encountered one of the following issues with the Zendesk US Government agent with the ID of #{agent_id}:
    - The agent does not exist
    - The agent does not have write permissions on tickets
    - The agent is suspended
    - The agent is an end-user
    Please review the Zendesk agent you were wanting to use once more and file a new submission.
    If you need assistance in locating the agent's ID, please ask in the Slack channel [#support_operations](https://gitlab.slack.com/archives/C018ZGZAMPD)
    /label ~"AutomatedRequests::Set Org ASE" ~"SupportOps::Blocked"
    /assign @#{requester.username}
  STRING
end

def self.message

Other tags:
    Since: - 1.0.42

Other tags:
    Author: - Jason Colyer
def self.message
  <<~STRING
    ## Set an Assigned Support Engineer for a Zendesk US Goverment organization - #{org.id}
    Greetings @#{requester.username} !
    We have set the Assigned Support Engineer of the Zendesk US Goverment organization to that of #{agent_id} at this time.
    If that is not the ID of the agent you were intending to use, please review the Zendesk agent you were wanting to use once more and file a new submission.
    If you need assistance in locating the agent's ID, please ask in the Slack channel [#support_operations](https://gitlab.slack.com/archives/C018ZGZAMPD)
    /label ~"AutomatedRequests::Set Org ASE"
    /assign @#{requester.username}
  STRING
end

def self.org

Other tags:
    Since: - 1.0.42

Other tags:
    Author: - Jason Colyer
def self.org
  @org ||= Readiness::Zendesk::Organizations.find(@zendesk_client, org_id)
end

def self.org_does_not_exist

def self.org_does_not_exist
  issue = Readiness::GitLab::Issues.new
  issue.title = 'Invalid Request - Organization Not Found'
  issue.description = org_does_not_exist_message
  create = Readiness::GitLab::Issues.create!(@gitlab_client, project, issue)
  puts "Issue created: #{create.web_url}"
  exit 0
end

def self.org_does_not_exist_message

Other tags:
    Since: - 1.0.42

Other tags:
    Author: - Jason Colyer
def self.org_does_not_exist_message
  <<~STRING
    ## Invalid Request Organization Not Found
    Greetings @#{requester.username} !
    We received a request from you recently, however the organization ID given #{org_id} is not a valid Zendesk US Goverment Organization ID. As such, we are not able to proceed.
    Please review the Zendesk US Goverment organization you were wanting to use once more and file a new submission.
    If you need assistance in locating the organization's ID, please ask in the Slack channel [#support_operations](https://gitlab.slack.com/archives/C018ZGZAMPD)
    /label ~"AutomatedRequests::Invalid Organization"
    /label ~"Invalid Zendesk US Goverment organization given"
    /assign @#{requester.username}
  STRING
end

def self.org_id

Other tags:
    Since: - 1.0.42

Other tags:
    Author: - Jason Colyer
def self.org_id
  @org_id ||= ENV.fetch('ZENDESK_USGOV_ORGANIZATION_ID')
end

def self.process!(gitlab_client, gitlab_admin_client, zendesk_client, redis_client)

Other tags:
    Since: - 1.0.42

Other tags:
    Author: - Jason Colyer
def self.process!(gitlab_client, gitlab_admin_client, zendesk_client, redis_client)
  @gitlab_client = gitlab_client
  @gitlab_admin_client = gitlab_admin_client
  @zendesk_client = zendesk_client
  @redis_client = redis_client
  requester
  bad_requester unless approved_requester?
  org_does_not_exist if org.is_a? Hash
  invalid_agent unless agent_valid?
  new_org = Readiness::Zendesk::Organizations.new
  new_org.id = org.id
  new_org.organization_fields = {
    "assigned_se" => agent_info
  }
  Readiness::Zendesk::Organizations.update!(@zendesk_client, new_org)
  issue = Readiness::GitLab::Issues.new
  issue.title = "Set an Assigned Support Engineer for a Zendesk US Goverment organization - #{org.id}"
  issue.description = message
  create = Readiness::GitLab::Issues.create!(@gitlab_client, project, issue)
  puts "Issue created: #{create.web_url}"
  exit 0
end

def self.project

Other tags:
    Since: - 1.0.42

Other tags:
    Author: - Jason Colyer
def self.project
  @project ||= Readiness::GitLab::Projects.find!(@gitlab_client, 12811526)
end

def self.requester

Other tags:
    Since: - 1.0.42

Other tags:
    Author: - Jason Colyer
def self.requester
  @requester ||= Readiness::SupportSuperFormProcessor::Shared.gitlab_user_check(@gitlab_admin_client, requester_email)
end

def self.requester_email

Other tags:
    Since: - 1.0.42

Other tags:
    Author: - Jason Colyer
def self.requester_email
  @requester_email ||= ENV.fetch('REQUESTER_EMAIL')
end

def self.team

Other tags:
    Since: - 1.0.42

Other tags:
    Author: - Jason Colyer
def self.team
  @team ||= Readiness::Redis.get(@redis_client, 'support_team')
end