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) Participant.transaction do if params[:is_employer] == 'yes' create_employer_and_employer_record_for(@participant) elsif params[:employer_id].present? @participant.employer_id = params[:employer_id] EmployerRecord.create!(participant: @participant, employer_id: @participant.employer_id, start_date: Date.today) end if @participant.save redirect_to @participant, notice: 'Participant was successfully created.' else flash.now[:alert] = 'There was an error creating the participant.' render :new, status: :unprocessable_entity end end rescue ActiveRecord::RecordInvalid => e flash.now[:alert] = "Error: #{e.message}" render :new, status: :unprocessable_entity end def edit end def update @participant = Participant.find(params[:id]) Participant.transaction do if @participant.update(participant_params) if @participant.employer_id.present? update_employer_details(@participant) end redirect_to @participant, notice: 'Participant was successfully updated.' else render :edit end end rescue => e flash.now[:alert] = "Error: #{e.message}" render :edit 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 private def set_participant @participant = Participant.find(params[:id]) 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_employer_and_employer_record_for(participant) # Check if an employer with the same email or similar details already exists existing_employer = Employer.find_by(email: participant.email) || Employer.where(first_name: participant.first_name, last_name: participant.last_name, ssn: participant.ssn).first # If an existing employer is found, assign it to the participant if existing_employer.present? participant.employer_id = existing_employer.id else # Create a new employer with participant's details employer = Employer.new(employer_params_from_participant(participant)) if employer.save participant.employer_id = employer.id else participant.errors.add(:base, "Employer creation failed: #{employer.errors.full_messages.join(', ')}") raise ActiveRecord::Rollback end end # Create an EmployerRecord if it doesn't exist for this combination unless EmployerRecord.exists?(participant: participant, employer_id: participant.employer_id) EmployerRecord.create!(participant: participant, employer_id: participant.employer_id, start_date: Date.today) end end def update_employer_details(participant) employer = Employer.find(participant.employer_id) employer.update( 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, # Add other fields that need to be updated ) end end