<commit_before>class Policy
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Versioning
  include Mongoid::Paranoia
  include AASM

  extend Mongorder

  auto_increment :_id

  field :eg_id, as: :enrollment_group_id, type: String
  field :preceding_enrollment_group_id, type: String
#  field :r_id, as: :hbx_responsible_party_id, type: String

  field :allocated_aptc, type: BigDecimal, default: 0.00
  field :elected_aptc, type: BigDecimal, default: 0.00
  field :applied_aptc, type: BigDecimal, default: 0.00
  field :csr_amt, type: BigDecimal

  field :pre_amt_tot, as: :total_premium_amount, type: BigDecimal, default: 0.00
  field :tot_res_amt, as: :total_responsible_amount, type: BigDecimal, default: 0.00
  field :tot_emp_res_amt, as: :employer_contribution, type: BigDecimal, default: 0.00
  field :sep_reason, type: String, default: :open_enrollment
  field :carrier_to_bill, type: Boolean, default: false
  field :aasm_state, type: String

  validates_presence_of :eg_id

  index({:eg_id => 1})
  index({:aasm_state => 1})
  index({:eg_id => 1, :carrier_id => 1, :plan_id => 1})

  embeds_many :enrollees
  accepts_nested_attributes_for :enrollees, reject_if: :all_blank, allow_destroy: true
  index({ "enrollees.hbx_member_id" => 1 })
  index({ "enrollees.carrier_member_id" => 1})
  index({ "enrollees.carrier_policy_id" => 1})
  index({ "enrollees.rel_code" => 1})
  index({ "enrollees.coverage_start" => 1})
  index({ "enrollees.coverage_end" => 1})

  belongs_to :carrier, counter_cache: true, index: true 
  belongs_to :broker, counter_cache: true, index: true # Assumes that broker change triggers new enrollment group
  belongs_to :plan, counter_cache: true, index: true
  belongs_to :employer, counter_cache: true, index: true
  belongs_to :responsible_party

  has_many :transaction_set_enrollments, 
              class_name: "Protocols::X12::TransactionSetEnrollment", 
              order: { submitted_at: :desc }
  has_many :premium_payments, order: { paid_at: 1 }

  before_create :generate_enrollment_group_id 
  before_save :invalidate_find_cache
  before_save :check_for_cancel_or_term

  scope :all_active_states,   where(:aasm_state.in => %w[submitted resubmitted effectuated])
  scope :all_inactive_states, where(:aasm_state.in => %w[canceled carrier_canceled terminated])

  aasm do
    state :submitted, initial: true
    state :resubmitted
    state :effectuated
    state :carrier_rejected
    state :carrier_canceled
    state :canceled
    state :terminated

    event :effectuate do
      transitions from: :submitted, to: :effectuated
      transitions from: :resubmitted, to: :effectuated
    end

    # Use when Carriers cancel for non-payment and initial enrollment is resent
    event :resubmit do
      transitions from: :carrier_canceled, to: :resubmitted
      transitions from: :canceled, to: :resubmitted
    end

    # Use when Carrier naks a transmission or reports a functional error
    event :carrier_reject do
      transitions from: :submitted, to: :carrier_rejected
    end

    event :carrier_cancel do
      transitions from: :submitted, to: :carrier_canceled
    end

    event :cancel do
      transitions from: :submitted, to: :canceled
      transitions from: :effectuated, to: :canceled
      transitions from: :carrier_rejected, to: :canceled
    end

    event :terminate do
      transitions from: :submitted, to: :terminated
      transitions from: :effectuated, to: :terminated
    end
  end

  def subscriber
    enrollees.detect { |m| m.relationship_status_code == "self" }
  end

  def has_responsible_person?
    !self.responsible_party_id.blank?
  end

  def responsible_person
    query_proxy.responsible_person
  end

  def people
    query_proxy.people
  end

  def merge_enrollee(m_enrollee, p_action)
    found_enrollee = self.enrollees.detect do |enr|
      enr.m_id == m_enrollee.m_id
    end
    if found_enrollee.nil?
      if p_action == :stop
        m_enrollee.coverage_status = 'inactive'
      end
      self.enrollees << m_enrollee
    else
      found_enrollee.merge_enrollee(m_enrollee, p_action)
    end
  end


  def edi_transaction_sets
    Protocols::X12::TransactionSetEnrollment.where({"policy_id" => self._id})
  end

  def invalidate_find_cache
    Rails.cache.delete("Policy/find/subkeys.#{enrollment_group_id}.#{carrier_id}.#{plan_id}")
    if !subscriber.nil?
      Rails.cache.delete("Policy/find/sub_plan.#{subscriber.m_id}.#{plan_id}")
    end
    true
  end

  def hios_plan_id
    self.plan.hios_plan_id
  end

  def coverage_type
    self.plan.coverage_type
  end

  def enrollee_for_member_id(m_id)
    self.enrollees.detect { |en| en.m_id == m_id }
  end

  def to_cv
    CanonicalVocabulary::EnrollmentSerializer.new(self).serialize
  end

  def self.default_search_order
    [[:eg_id, 1]]
  end

  def self.find_all_enrollees_for_member_id(m_id)
    Enrollee.where(
      "m_id" => m_id
    )
  end  

  def self.find_all_policies_for_member_id(m_id)
    self.where(
      "enrollees.m_id" => m_id
    ).order_by([:eg_id])
  end
<commit_msg>remove Policy#find_all_enrollees_for_member_id as it is invalid (Enrollee is an embedded document)<commit_after>class Policy
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Versioning
  include Mongoid::Paranoia
  include AASM

  extend Mongorder

  auto_increment :_id

  field :eg_id, as: :enrollment_group_id, type: String
  field :preceding_enrollment_group_id, type: String
#  field :r_id, as: :hbx_responsible_party_id, type: String

  field :allocated_aptc, type: BigDecimal, default: 0.00
  field :elected_aptc, type: BigDecimal, default: 0.00
  field :applied_aptc, type: BigDecimal, default: 0.00
  field :csr_amt, type: BigDecimal

  field :pre_amt_tot, as: :total_premium_amount, type: BigDecimal, default: 0.00
  field :tot_res_amt, as: :total_responsible_amount, type: BigDecimal, default: 0.00
  field :tot_emp_res_amt, as: :employer_contribution, type: BigDecimal, default: 0.00
  field :sep_reason, type: String, default: :open_enrollment
  field :carrier_to_bill, type: Boolean, default: false
  field :aasm_state, type: String

  validates_presence_of :eg_id

  index({:eg_id => 1})
  index({:aasm_state => 1})
  index({:eg_id => 1, :carrier_id => 1, :plan_id => 1})

  embeds_many :enrollees
  accepts_nested_attributes_for :enrollees, reject_if: :all_blank, allow_destroy: true
  index({ "enrollees.hbx_member_id" => 1 })
  index({ "enrollees.carrier_member_id" => 1})
  index({ "enrollees.carrier_policy_id" => 1})
  index({ "enrollees.rel_code" => 1})
  index({ "enrollees.coverage_start" => 1})
  index({ "enrollees.coverage_end" => 1})

  belongs_to :carrier, counter_cache: true, index: true 
  belongs_to :broker, counter_cache: true, index: true # Assumes that broker change triggers new enrollment group
  belongs_to :plan, counter_cache: true, index: true
  belongs_to :employer, counter_cache: true, index: true
  belongs_to :responsible_party

  has_many :transaction_set_enrollments, 
              class_name: "Protocols::X12::TransactionSetEnrollment", 
              order: { submitted_at: :desc }
  has_many :premium_payments, order: { paid_at: 1 }

  before_create :generate_enrollment_group_id 
  before_save :invalidate_find_cache
  before_save :check_for_cancel_or_term

  scope :all_active_states,   where(:aasm_state.in => %w[submitted resubmitted effectuated])
  scope :all_inactive_states, where(:aasm_state.in => %w[canceled carrier_canceled terminated])

  aasm do
    state :submitted, initial: true
    state :resubmitted
    state :effectuated
    state :carrier_rejected
    state :carrier_canceled
    state :canceled
    state :terminated

    event :effectuate do
      transitions from: :submitted, to: :effectuated
      transitions from: :resubmitted, to: :effectuated
    end

    # Use when Carriers cancel for non-payment and initial enrollment is resent
    event :resubmit do
      transitions from: :carrier_canceled, to: :resubmitted
      transitions from: :canceled, to: :resubmitted
    end

    # Use when Carrier naks a transmission or reports a functional error
    event :carrier_reject do
      transitions from: :submitted, to: :carrier_rejected
    end

    event :carrier_cancel do
      transitions from: :submitted, to: :carrier_canceled
    end

    event :cancel do
      transitions from: :submitted, to: :canceled
      transitions from: :effectuated, to: :canceled
      transitions from: :carrier_rejected, to: :canceled
    end

    event :terminate do
      transitions from: :submitted, to: :terminated
      transitions from: :effectuated, to: :terminated
    end
  end

  def subscriber
    enrollees.detect { |m| m.relationship_status_code == "self" }
  end

  def has_responsible_person?
    !self.responsible_party_id.blank?
  end

  def responsible_person
    query_proxy.responsible_person
  end

  def people
    query_proxy.people
  end

  def merge_enrollee(m_enrollee, p_action)
    found_enrollee = self.enrollees.detect do |enr|
      enr.m_id == m_enrollee.m_id
    end
    if found_enrollee.nil?
      if p_action == :stop
        m_enrollee.coverage_status = 'inactive'
      end
      self.enrollees << m_enrollee
    else
      found_enrollee.merge_enrollee(m_enrollee, p_action)
    end
  end


  def edi_transaction_sets
    Protocols::X12::TransactionSetEnrollment.where({"policy_id" => self._id})
  end

  def invalidate_find_cache
    Rails.cache.delete("Policy/find/subkeys.#{enrollment_group_id}.#{carrier_id}.#{plan_id}")
    if !subscriber.nil?
      Rails.cache.delete("Policy/find/sub_plan.#{subscriber.m_id}.#{plan_id}")
    end
    true
  end

  def hios_plan_id
    self.plan.hios_plan_id
  end

  def coverage_type
    self.plan.coverage_type
  end

  def enrollee_for_member_id(m_id)
    self.enrollees.detect { |en| en.m_id == m_id }
  end

  def to_cv
    CanonicalVocabulary::EnrollmentSerializer.new(self).serialize
  end

  def self.default_search_order
    [[:eg_id, 1]]
  end

  def self.find_all_policies_for_member_id(m_id)
    self.where(
      "enrollees.m_id" => m_id
    ).order_by([:eg_id])
  end

{'fraction_non_alphanumeric': 0.09318607632383394, 'fraction_numerical': 0.006902672320283996, 'mean_word_length': 3.996059113300493, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 34, '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': '3472259', 'n_tokens_mistral': 3647, 'n_tokens_neox': 3532, 'n_words': 826}