2024-01-16 20:54:44 -06:00
|
|
|
class Participant < ApplicationRecord
|
2024-02-16 23:36:48 -06:00
|
|
|
after_create :create_onboarding_items_for_forms
|
|
|
|
|
2024-01-22 22:57:37 -06:00
|
|
|
# Associations
|
2024-02-07 05:13:12 -06:00
|
|
|
belongs_to :employer, optional: false
|
2024-01-22 22:57:37 -06:00
|
|
|
has_many :employments
|
|
|
|
has_many :workers, through: :employments
|
2024-01-31 02:19:24 -06:00
|
|
|
has_many :service_contracts
|
|
|
|
has_many :vendors, through: :service_contracts
|
2024-02-02 23:12:36 -06:00
|
|
|
has_many :employer_records
|
|
|
|
has_many :employers, through: :employer_records
|
2024-02-06 17:36:08 -06:00
|
|
|
has_and_belongs_to_many :programs
|
2024-02-12 17:26:45 -06:00
|
|
|
has_many :bank_accounts, as: :owner
|
2024-02-16 23:36:48 -06:00
|
|
|
has_many :onboarding_items, as: :owner
|
|
|
|
accepts_nested_attributes_for :onboarding_items
|
2024-01-22 22:57:37 -06:00
|
|
|
|
|
|
|
# Validations
|
2024-02-06 17:36:08 -06:00
|
|
|
validates :first_name, :last_name, :ssn, presence: true
|
2024-02-07 05:13:12 -06:00
|
|
|
validates :ssn, uniqueness: { message: "SSN is already taken by another participant" }, unless: :ssn_already_taken_by_employer?
|
|
|
|
validates :employer_id, presence: true
|
2024-02-06 17:36:08 -06:00
|
|
|
|
2024-02-02 16:44:50 -06:00
|
|
|
def full_name
|
|
|
|
"#{first_name} #{last_name}"
|
|
|
|
end
|
2024-01-19 23:38:15 -06:00
|
|
|
|
2024-02-06 17:36:08 -06:00
|
|
|
private
|
|
|
|
|
2024-02-07 05:13:12 -06:00
|
|
|
def ssn_already_taken_by_employer?
|
|
|
|
# Check if the SSN already exists in the Employer model, excluding the current participant's employer
|
|
|
|
Employer.where.not(id: employer_id).exists?(ssn: ssn)
|
2024-02-06 17:36:08 -06:00
|
|
|
end
|
2024-02-16 23:36:48 -06:00
|
|
|
|
|
|
|
def create_onboarding_items_for_forms
|
2024-04-02 18:19:58 -05:00
|
|
|
create_participant_onboarding_items
|
|
|
|
create_employer_onboarding_items_if_applicable
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_participant_onboarding_items
|
|
|
|
participant_role = FormRole.find_by(name: 'Participant')
|
|
|
|
forms = Form.joins(:form_roles).where(form_roles: { id: participant_role.id })
|
2024-02-16 23:36:48 -06:00
|
|
|
forms.each do |form|
|
2024-04-02 18:19:58 -05:00
|
|
|
onboarding_items.find_or_create_by(form: form)
|
2024-02-16 23:36:48 -06:00
|
|
|
end
|
|
|
|
end
|
2024-04-02 18:19:58 -05:00
|
|
|
|
|
|
|
def create_employer_onboarding_items_if_applicable
|
|
|
|
# Assuming there is a method or indicator to check if the participant is also an employer
|
|
|
|
return unless self.is_also_employer?
|
|
|
|
|
|
|
|
employer_role = FormRole.find_by(name: 'Employer')
|
|
|
|
forms = Form.joins(:form_roles).where(form_roles: { id: employer_role.id })
|
|
|
|
forms.each do |form|
|
|
|
|
# Here you need to decide how you want to associate the forms with the employer record
|
|
|
|
# If the employer record is separate from the participant, you may need something like:
|
|
|
|
self.employer.onboarding_items.find_or_create_by(form: form) if self.employer.present?
|
|
|
|
# If the participant itself acts as an employer, then you might directly create onboarding items as done for participant forms
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# A method to determine if the participant should also have employer forms
|
|
|
|
def is_also_employer?
|
|
|
|
# Implement logic to determine if the participant is also an employer
|
|
|
|
# This might involve checking if the participant has an associated employer record
|
|
|
|
# or any other logic specific to your application
|
|
|
|
self.employer.present? # Example logic, adjust as needed
|
|
|
|
end
|
2024-02-06 17:36:08 -06:00
|
|
|
end
|