234 lines
7.2 KiB
Ruby
234 lines
7.2 KiB
Ruby
class ParticipantsController < ApplicationController
|
|
before_action :set_participant, only: [:show, :edit, :update, :destroy]
|
|
|
|
def index
|
|
@participants = Participant.order(:last_name).page(params[:page]).per(5) # Adjust the number per page as needed
|
|
end
|
|
|
|
|
|
def show
|
|
@participant = Participant.includes(:employments, :service_contracts).find(params[:id])
|
|
@workers = @participant.workers # Fetch associated workers
|
|
@employments = @participant.employments.includes(:worker).order('workers.last_name')
|
|
@service_contracts = @participant.service_contracts.includes(:vendor) # Fetch associated service contracts
|
|
@employment = Employment.new # Initialize a new Employment object
|
|
@service_contract = ServiceContract.new # Initialize a new Service Contract object
|
|
end
|
|
|
|
def new
|
|
@participant = Participant.new
|
|
end
|
|
|
|
def create
|
|
@participant = Participant.new(participant_params)
|
|
|
|
if ssn_already_taken?(@participant.ssn)
|
|
flash.now[:alert] = 'SSN is already taken by another participant or employer.'
|
|
render :new, status: :unprocessable_entity and return
|
|
end
|
|
|
|
ActiveRecord::Base.transaction do
|
|
if params[:is_employer] == 'yes'
|
|
# This assumes you have a method `create_new_employer_for_participant` that handles creating a new employer and setting `@participant.employer_id`.
|
|
unless create_new_employer_for_participant(@participant)
|
|
render :new, status: :unprocessable_entity and return
|
|
end
|
|
elsif params[:employer_id].present?
|
|
# Directly assign the existing employer_id to the participant
|
|
@participant.employer_id = params[:employer_id]
|
|
end
|
|
|
|
if @participant.save
|
|
# Create an EmployerRecord only if necessary.
|
|
create_employer_record(@participant) if @participant.employer_id.present?
|
|
|
|
redirect_to @participant, notice: 'Participant was successfully created.'
|
|
else
|
|
render :new, status: :unprocessable_entity
|
|
end
|
|
end
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
flash.now[:alert] = "Failed to create participant: #{e.message}"
|
|
render :new, status: :unprocessable_entity
|
|
end
|
|
|
|
|
|
def edit
|
|
end
|
|
|
|
def update
|
|
@participant = Participant.find(params[:id])
|
|
|
|
if @participant.update(participant_params)
|
|
update_employer_details(@participant)
|
|
|
|
if @participant.errors.any?
|
|
# If there are errors (e.g., from updating employer details), re-render the edit form.
|
|
render :edit
|
|
else
|
|
# If everything went well, redirect to the participant's show page.
|
|
redirect_to @participant, notice: 'Participant and associated employer details were successfully updated.'
|
|
end
|
|
else
|
|
render :edit
|
|
end
|
|
end
|
|
|
|
|
|
|
|
def search
|
|
if params[:term].present?
|
|
@participants = Participant.where("first_name LIKE ? OR last_name LIKE ?", "%#{params[:term]}%", "%#{params[:term]}%")
|
|
else
|
|
@participants = Participant.none
|
|
end
|
|
|
|
respond_to do |format|
|
|
format.json { render json: @participants.map { |participant| { label: participant.full_name, value: participant.id } } }
|
|
end
|
|
end
|
|
|
|
def destroy
|
|
@participant = Participant.find(params[:id])
|
|
begin
|
|
@participant.destroy
|
|
redirect_to participants_url, notice: 'Participant was successfully destroyed.'
|
|
rescue ActiveRecord::InvalidForeignKey
|
|
# Redirect with an error message if foreign key constraints fail
|
|
redirect_to participants_url, alert: 'Participant cannot be deleted because it is linked to other records.'
|
|
end
|
|
end
|
|
|
|
def link_worker
|
|
@participant = Participant.find(params[:id])
|
|
@employment = @participant.employments.new(employment_params)
|
|
|
|
if @employment.save
|
|
redirect_to @participant, notice: 'Worker was successfully linked.'
|
|
else
|
|
Rails.logger.debug @employment.errors.full_messages
|
|
flash[:alert] = @employment.errors.full_messages.to_sentence
|
|
render 'show'
|
|
end
|
|
end
|
|
|
|
def link_vendor
|
|
@participant = Participant.find(params[:id])
|
|
@service_contract = @participant.service_contracts.new(service_contract_params)
|
|
|
|
if @service_contract.save
|
|
redirect_to @participant, notice: 'Vendor was successfully linked.'
|
|
else
|
|
flash[:alert] = @service_contract.errors.full_messages.to_sentence
|
|
render 'show'
|
|
end
|
|
end
|
|
|
|
def onboarding
|
|
@participant = Participant.find(params[:id])
|
|
@onboarding_items = @participant.onboarding_items.includes(:form)
|
|
end
|
|
|
|
|
|
private
|
|
|
|
def handle_employer_creation_for_participant(participant)
|
|
if params[:is_employer] == 'yes'
|
|
employer = Employer.create!(employer_params_from_participant(participant))
|
|
participant.employer_id = employer.id
|
|
elsif params[:employer_id].present?
|
|
participant.employer_id = params[:employer_id]
|
|
end
|
|
end
|
|
|
|
def set_participant
|
|
@participant = Participant.find(params[:id])
|
|
end
|
|
|
|
def ssn_already_taken?(ssn)
|
|
Participant.exists?(ssn: ssn) || Employer.exists?(ssn: ssn)
|
|
end
|
|
|
|
def participant_params
|
|
params.require(:participant).permit(
|
|
:first_name,
|
|
:last_name,
|
|
:address_line_1,
|
|
:address_line_2,
|
|
:city,
|
|
:state,
|
|
:zip,
|
|
:phone,
|
|
:email,
|
|
:mci,
|
|
:dob,
|
|
:ssn,
|
|
:gender,
|
|
:employer_id
|
|
)
|
|
end
|
|
|
|
|
|
def employer_params_from_participant(participant)
|
|
{
|
|
first_name: participant.first_name,
|
|
last_name: participant.last_name,
|
|
address_line_1: participant.address_line_1,
|
|
address_line_2: participant.address_line_2,
|
|
city: participant.city,
|
|
state: participant.state,
|
|
zip: participant.zip,
|
|
phone: participant.phone,
|
|
email: participant.email,
|
|
dob: participant.dob,
|
|
ssn: participant.ssn,
|
|
gender: participant.gender
|
|
}
|
|
end
|
|
|
|
def employment_params
|
|
params.require(:employment).permit(:worker_id, :start_date, :end_date)
|
|
end
|
|
|
|
def service_contract_params
|
|
params.require(:service_contract).permit(:vendor_id, :start_date, :end_date)
|
|
end
|
|
|
|
def create_new_employer_for_participant(participant)
|
|
employer = Employer.create!(employer_params_from_participant(participant))
|
|
participant.employer_id = employer.id
|
|
employer.persisted?
|
|
end
|
|
|
|
def create_employer_record(participant)
|
|
EmployerRecord.create!(participant: participant, employer_id: participant.employer_id, start_date: Date.today)
|
|
end
|
|
|
|
|
|
def update_employer_details(participant)
|
|
employer = Employer.find(participant.employer_id)
|
|
|
|
update_attrs = {
|
|
# Include all fields you want to update, excluding SSN
|
|
first_name: participant.first_name,
|
|
last_name: participant.last_name,
|
|
address_line_1: participant.address_line_1,
|
|
address_line_2: participant.address_line_2,
|
|
city: participant.city,
|
|
state: participant.state,
|
|
zip: participant.zip,
|
|
phone: participant.phone,
|
|
email: participant.email,
|
|
dob: participant.dob,
|
|
gender: participant.gender,
|
|
}
|
|
|
|
unless employer.update(update_attrs)
|
|
# If the update fails, add a custom error to the participant object.
|
|
participant.errors.add(:base, "Employer details could not be updated: #{employer.errors.full_messages.join(', ')}")
|
|
# You might not need to throw :abort if you're handling this logic in the controller.
|
|
end
|
|
end
|
|
end
|
|
|