<commit_before>class Allocation < ActiveRecord::Base
  belongs_to :job
  belongs_to :seeker
  belongs_to :provider

  has_many :todos

  enum state: {application_open: 0, application_rejected: 1, proposal: 2, active: 3, finished: 4, cancelled: 5, application_retracted: 6}

  validates :job, presence: true
  validates :seeker, presence: true
  validates :seeker, uniqueness: { scope: :job_id }, if: proc { |p| p.job && p.seeker }

  before_save :set_state_last_change, if: proc { |s| s.state_changed?}

  after_save :adjust_todo

  def adjust_todo
    job.adjust_todo unless job.nil?
    Todo.where(record_type: :allocation, record_id: id).find_each &:destroy!
    Todotype.allocation.find_each do |todotype|
      begin
        result = Allocation.joins(:seeker, job: :provider).find_by(Allocation.replace_state_with_number(todotype.where) + " AND allocations.id = #{id}")
        unless result.nil?
          Todo.create(record_id: id, record_type: :allocation, todotype: todotype, allocation_id: id, seeker_id: seeker_id, job_id: job_id, provider_id: job.provider_id)
        end
      rescue
        nil
      end
    end
  end

  def self.replace_state_with_number(string)
    new_str = string
    states = {application_open: 0, application_rejected: 1, proposal: 2, active: 3, finished: 4, cancelled: 5, application_retracted: 6}
    states.each do |name, number|
      new_str = new_str.gsub(name.to_s, number.to_s)
    end

    new_str
  end

  def name
    "#{seeker.try(:name)} #{job.try(:title)}"
  end

  # Sets date of last change of state
  def set_state_last_change
    self.last_change_of_state = DateTime.now()
  end
end<commit_msg>Added sending refresh request when changing allocation state<commit_after>class Allocation < ActiveRecord::Base
  belongs_to :job
  belongs_to :seeker
  belongs_to :provider

  has_many :todos

  enum state: {application_open: 0, application_rejected: 1, proposal: 2, active: 3, finished: 4, cancelled: 5, application_retracted: 6}

  validates :job, presence: true
  validates :seeker, presence: true
  validates :seeker, uniqueness: { scope: :job_id }, if: proc { |p| p.job && p.seeker }

  before_save :set_state_last_change, if: proc { |s| s.state_changed?}
  before_save :send_request_refresh, if: proc { |s| s.state_changed?}

  after_save :adjust_todo

  def adjust_todo
    job.adjust_todo unless job.nil?
    Todo.where(record_type: :allocation, record_id: id).find_each &:destroy!
    Todotype.allocation.find_each do |todotype|
      begin
        result = Allocation.joins(:seeker, job: :provider).find_by(Allocation.replace_state_with_number(todotype.where) + " AND allocations.id = #{id}")
        unless result.nil?
          Todo.create(record_id: id, record_type: :allocation, todotype: todotype, allocation_id: id, seeker_id: seeker_id, job_id: job_id, provider_id: job.provider_id)
        end
      rescue
        nil
      end
    end
  end

  def self.replace_state_with_number(string)
    new_str = string
    states = {application_open: 0, application_rejected: 1, proposal: 2, active: 3, finished: 4, cancelled: 5, application_retracted: 6}
    states.each do |name, number|
      new_str = new_str.gsub(name.to_s, number.to_s)
    end

    new_str
  end

  def name
    "#{seeker.try(:name)} #{job.try(:title)}"
  end

  # Sets date of last change of state
  def set_state_last_change
    self.last_change_of_state = DateTime.now()
  end

  # Make post to jugendarbeit requesting user in app to refresh job list
  #
  def send_request_refresh
    return if job.nil?
    require 'rest-client'
    dev = 'https://devadmin.jugendarbeit.digital/api/jugendinfo_smalljobs/refresh/'
    live = 'https://admin.jugendarbeit.digital/api/jugendinfo_smalljobs/refresh/'
    begin
      logger.info "Sending changes to jugendinfo"
      self.organization.regions.each do |region|
        logger.info "Sending: #{{token: '<KEY>', region_id: job.region.id}}"
        response = RestClient.post dev, {token: '<KEY>', region_id: job.region.id}
        logger.info "Response from jugendinfo: #{response}"
      end
    rescue
      logger.info "Failed sending changes to jugendinfo"
      nil
    end
  end
end
{'fraction_non_alphanumeric': 0.10460652591170826, 'fraction_numerical': 0.0067178502879078695, 'mean_word_length': 3.689538807649044, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '4192740', 'n_tokens_mistral': 1454, 'n_tokens_neox': 1400, 'n_words': 359}