<commit_before>class User < ApplicationRecord
  has_many :roles
  has_many :organizations, through: :roles
  has_many :service_versions
  has_many :schema_versions

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :trackable, :omniauthable, omniauth_providers: [:clave_unica]

  FALLBACK_URL = 'http://private-5f0326-microserviciosderolesv4.apiary-mock.com'
  URL = ENV['ROLE_SERVICE_URL'] || 'http://thawing-shore-28727.herokuapp.com'
  APP_ID = ENV['ROLE_APP_ID'] || 'AB01'

  def self.from_omniauth(auth)
    rut = auth.extra.raw_info.RUT
    sub = auth.extra.raw_info.sub
    id_token = auth.credentials.id_token
    new_user = where(rut: rut).first
    if new_user.nil?
      new_user = create!(rut: rut, sub: sub, id_token: id_token)
    else
      new_user.update!(sub: sub, id_token: id_token)
    end
    new_user.refresh_user_roles_and_email!
    new_user
  end

  def refresh_user_roles_and_email!
    response = call_roles_service(URL)
    if response.code == 200
      response = JSON.parse(response)
      if response.has_key?('nada')
        response = JSON.parse(call_roles_service(FALLBACK_URL))
      else
        response
      end
    else
      response = JSON.parse(call_roles_service(FALLBACK_URL))
    end

    self.name = refresh_name(response['nombre'])
    parse_organizations_and_roles(response['instituciones'])

    save!
  end

  def parse_organizations_and_roles(organizations)
    organizations.each do |organization|
      org = organization['institucion']
      role = organization['rol']
      email = parse_email(organization['email'])
      parse_organization_role(org, role, email)
    end
  end

  def parse_organization_role(organization, role, email)
    org_id = organization['id'].empty? ? "AB01" : organization['id']
    self.can_create_schemas = (org_id == "AB01")
    org = Organization.where(dipres_id: org_id ).first_or_create!(
      name: organization['nombre'].empty? ?
              'Secretaría General de la Presidencia' : organization['nombre'],
      initials: organization['sigla'].empty? ?
              'SEGPRES' : organization['sigla'])

    self.roles.where(organization: org).delete_all
    self.roles.first_or_create!(organization: org,<commit_msg>Delete all Roles from user before login<commit_after>class User < ApplicationRecord
  has_many :roles
  has_many :organizations, through: :roles
  has_many :service_versions
  has_many :schema_versions

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :trackable, :omniauthable, omniauth_providers: [:clave_unica]

  FALLBACK_URL = 'http://private-5f0326-microserviciosderolesv4.apiary-mock.com'
  URL = ENV['ROLE_SERVICE_URL'] || 'http://thawing-shore-28727.herokuapp.com'
  APP_ID = ENV['ROLE_APP_ID'] || 'AB01'

  def self.from_omniauth(auth)
    rut = auth.extra.raw_info.RUT
    sub = auth.extra.raw_info.sub
    id_token = auth.credentials.id_token
    new_user = where(rut: rut).first
    if new_user.nil?
      new_user = create!(rut: rut, sub: sub, id_token: id_token)
    else
      new_user.update!(sub: sub, id_token: id_token)
    end
    new_user.refresh_user_roles_and_email!
    new_user
  end

  def refresh_user_roles_and_email!
    response = call_roles_service(URL)
    if response.code == 200
      response = JSON.parse(response)
      if response.has_key?('nada')
        response = JSON.parse(call_roles_service(FALLBACK_URL))
      else
        response
      end
    else
      response = JSON.parse(call_roles_service(FALLBACK_URL))
    end

    self.name = refresh_name(response['nombre'])
    parse_organizations_and_roles(response['instituciones'])

    save!
  end

  def parse_organizations_and_roles(organizations)
    self.roles.delete_all
    organizations.each do |organization|
      org = organization['institucion']
      role = organization['rol']
      email = parse_email(organization['email'])
      parse_organization_role(org, role, email)
    end
  end

  def parse_organization_role(organization, role, email)
    org_id = organization['id'].empty? ? "AB01" : organization['id']
    self.can_create_schemas = (org_id == "AB01")
    org = Organization.where(dipres_id: org_id ).first_or_create!(
      name: organization['nombre'].empty? ?
              'Secretaría General de la Presidencia' : organization['nombre'],
      initials: organization['sigla'].empty? ?
              'SEGPRES' : organization['sigla'])
    self.roles.first_or_create!(organization: org,
{'fraction_non_alphanumeric': 0.10965487112276102, 'fraction_numerical': 0.00873743993010048, 'mean_word_length': 3.830168776371308, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3663995', 'n_tokens_mistral': 1582, 'n_tokens_neox': 1507, 'n_words': 282}