<commit_before>require 'aasm'
require File.expand_path("../../validators/email_validator", __FILE__)

class ReferenceSheet < ActiveRecord::Base
  include AnswerSheetConcern
  include Rails.application.routes.url_helpers
  include AASM
  self.table_name = "#{Qe.table_name_prefix}references"
  self.inheritance_column = 'fake'

  belongs_to :question,
    :class_name => 'Element',
    :foreign_key => 'question_id'

  belongs_to :applicant_answer_sheet,
    :class_name => Qe.answer_sheet_class,
    :foreign_key => "applicant_answer_sheet_id"

  validates_presence_of :first_name, :last_name, :phone, :email, :relationship, :on => :update, :message => "can't be blank"
  validates :email, email: true, :on => :update

  delegate :style, :to => :question

  before_save :check_email_change

  after_destroy :notify_reference_of_deletion

  aasm :initial => :created, :column => :status do

    state :started, :enter => Proc.new {|ref|
                                ref.started_at = Time.now
                              }
    state :created
    state :completed, :enter => Proc.new {|ref|
                                  ref.submitted_at = Time.now
                                  # SpReferenceMailer.deliver_completed(ref)
                                  # SpReferenceMailer.deliver_completed_confirmation(ref)
                                  ref.applicant_answer_sheet.complete(ref)
                                }

    event :start do
      transitions :to => :started, :from => :created
    end

    event :submit do
      transitions :to => :completed, :from => :started
      transitions :to => :completed, :from => :created
    end

    event :unsubmit do
      transitions :to => :started, :from => :completed
    end
  end

  alias_method :applicant, :applicant_answer_sheet
  def generate_access_key
    self.access_key = Digest::MD5.hexdigest(email + Time.now.to_s)
  end

  def frozen?
    !%w(started created).include?(self.status)
  end

  def email_sent?() !self.email_sent_at.nil? end

  # send email invite
  def send_invite
    return if self.email.blank?

    application = self.applicant_answer_sheet

    Notifier.notification(self.email,
                                  application.email,
                                  "Reference Invite",
                                  {'reference_full_name' => self.name,
                                   'applicant_full_name' => application.name,
                                   'applicant_email' => application.email,
                                   'applicant_home_phone' => application.phone,
                                   'reference_url' => edit_reference_sheet_url(self, :a => self.access_key, :host => ActionMailer::Base.default_url_options[:host])}).deliver
    # Send notification to applicant
    Notifier.notification(applicant_answer_sheet.email, # RECIPIENTS
                                  Qe.from_email, # FROM
                                  "Reference Notification to Applicant", # LIQUID TEMPLATE NAME
                                  {'applicant_full_name' => applicant_answer_sheet.name,
                                   'reference_full_name' => self.name,
                                   'reference_email' => self.email,
                                   'application_url' => edit_answer_sheet_url(applicant_answer_sheet, :host => ActionMailer::Base.default_url_options[:host])}).deliver

    self.email_sent_at = Time.now
    self.save(:validate => false)

    true
  end

  def name
    [first_name, last_name].join(' ')
  end

  def reference
    self
  end<commit_msg>Send references from Qe.from_email instead of applicant email to decrease risk of blocked emails due to spoofing<commit_after>require 'aasm'
require File.expand_path("../../validators/email_validator", __FILE__)

class ReferenceSheet < ActiveRecord::Base
  include AnswerSheetConcern
  include Rails.application.routes.url_helpers
  include AASM
  self.table_name = "#{Qe.table_name_prefix}references"
  self.inheritance_column = 'fake'

  belongs_to :question,
    :class_name => 'Element',
    :foreign_key => 'question_id'

  belongs_to :applicant_answer_sheet,
    :class_name => Qe.answer_sheet_class,
    :foreign_key => "applicant_answer_sheet_id"

  validates_presence_of :first_name, :last_name, :phone, :email, :relationship, :on => :update, :message => "can't be blank"
  validates :email, email: true, :on => :update

  delegate :style, :to => :question

  before_save :check_email_change

  after_destroy :notify_reference_of_deletion

  aasm :initial => :created, :column => :status do

    state :started, :enter => Proc.new {|ref|
                                ref.started_at = Time.now
                              }
    state :created
    state :completed, :enter => Proc.new {|ref|
                                  ref.submitted_at = Time.now
                                  # SpReferenceMailer.deliver_completed(ref)
                                  # SpReferenceMailer.deliver_completed_confirmation(ref)
                                  ref.applicant_answer_sheet.complete(ref)
                                }

    event :start do
      transitions :to => :started, :from => :created
    end

    event :submit do
      transitions :to => :completed, :from => :started
      transitions :to => :completed, :from => :created
    end

    event :unsubmit do
      transitions :to => :started, :from => :completed
    end
  end

  alias_method :applicant, :applicant_answer_sheet
  def generate_access_key
    self.access_key = Digest::MD5.hexdigest(email + Time.now.to_s)
  end

  def frozen?
    !%w(started created).include?(self.status)
  end

  def email_sent?() !self.email_sent_at.nil? end

  # send email invite
  def send_invite
    return if self.email.blank?

    application = self.applicant_answer_sheet

    Notifier.notification(self.email,
                                  Qe.from_email,
                                  "Reference Invite",
                                  {'reference_full_name' => self.name,
                                   'applicant_full_name' => application.name,
                                   'applicant_email' => application.email,
                                   'applicant_home_phone' => application.phone,
                                   'reference_url' => edit_reference_sheet_url(self, :a => self.access_key, :host => ActionMailer::Base.default_url_options[:host])}).deliver
    # Send notification to applicant
    Notifier.notification(applicant_answer_sheet.email, # RECIPIENTS
                                  Qe.from_email, # FROM
                                  "Reference Notification to Applicant", # LIQUID TEMPLATE NAME
                                  {'applicant_full_name' => applicant_answer_sheet.name,
                                   'reference_full_name' => self.name,
                                   'reference_email' => self.email,
                                   'application_url' => edit_answer_sheet_url(applicant_answer_sheet, :host => ActionMailer::Base.default_url_options[:host])}).deliver

    self.email_sent_at = Time.now
    self.save(:validate => false)

    true
  end

  def name
    [first_name, last_name].join(' ')
  end

  def reference
    self
  end
{'fraction_non_alphanumeric': 0.09534915626286185, 'fraction_numerical': 0.00027438606118809165, 'mean_word_length': 2.045112781954887, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 34, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4013090', 'n_tokens_mistral': 2136, 'n_tokens_neox': 1994, 'n_words': 415}