PLEASE READ Tentative update fixing validation errors with the Participant and Employer models. Had to delete database and reclone last git push.

This commit is contained in:
Ben 2024-02-07 05:13:12 -06:00
parent 35d144327b
commit c1deea8463
7 changed files with 537 additions and 916 deletions

View File

@ -8,11 +8,11 @@ class EmployersController < ApplicationController
# GET /employers/:id # GET /employers/:id
def show def show
Rails.logger.debug "Employer ID: #{params[:id]}" Rails.logger.debug "Employer ID: #{params[:id]}"
@employer = Employer.includes(:employer_records, :direct_participants).find(params[:id]) @employer = Employer.includes(employer_records: :participant).find(params[:id])
Rails.logger.debug "Employer: #{@employer.inspect}" Rails.logger.debug "Employer: #{@employer.inspect}"
@employer_records = @employer.employer_records.includes(:participant)
end end
# GET /employers/new # GET /employers/new
@ -45,15 +45,16 @@ class EmployersController < ApplicationController
# DELETE /employers/:id # DELETE /employers/:id
def destroy def destroy
employer_record = EmployerRecord.find_by(id: params[:id]) @employer = Employer.find_by(id: params[:id])
if employer_record if @employer
employer_record.destroy @employer.destroy
redirect_to employers_url, notice: 'Employer record was successfully deleted.' redirect_to employers_url, notice: 'Employer was successfully deleted.'
else else
redirect_to employers_url, alert: 'Employer record not found.' redirect_to employers_url, alert: 'Employer record not found.'
end end
end end
# GET /employers/search # GET /employers/search
# This action responds to the auto-complete AJAX requests # This action responds to the auto-complete AJAX requests

View File

@ -22,50 +22,59 @@ class ParticipantsController < ApplicationController
def create def create
@participant = Participant.new(participant_params) @participant = Participant.new(participant_params)
Participant.transaction do 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' if params[:is_employer] == 'yes'
create_employer_and_employer_record_for(@participant) # 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? elsif params[:employer_id].present?
# Directly assign the existing employer_id to the participant
@participant.employer_id = params[:employer_id] @participant.employer_id = params[:employer_id]
EmployerRecord.create!(participant: @participant, employer_id: @participant.employer_id, start_date: Date.today)
end end
if @participant.save 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.' redirect_to @participant, notice: 'Participant was successfully created.'
else else
flash.now[:alert] = 'There was an error creating the participant.'
render :new, status: :unprocessable_entity render :new, status: :unprocessable_entity
end end
end end
rescue ActiveRecord::RecordInvalid => e rescue ActiveRecord::RecordInvalid => e
flash.now[:alert] = "Error: #{e.message}" flash.now[:alert] = "Failed to create participant: #{e.message}"
render :new, status: :unprocessable_entity render :new, status: :unprocessable_entity
end end
def edit def edit
end end
def update def update
@participant = Participant.find(params[:id]) @participant = Participant.find(params[:id])
Participant.transaction do if @participant.update(participant_params)
if @participant.update(participant_params) update_employer_details(@participant)
if @participant.employer_id.present?
update_employer_details(@participant) if @participant.errors.any?
end # If there are errors (e.g., from updating employer details), re-render the edit form.
redirect_to @participant, notice: 'Participant was successfully updated.'
else
render :edit 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 end
else
render :edit
end end
rescue => e
flash.now[:alert] = "Error: #{e.message}"
render :edit
end end
def search def search
if params[:term].present? if params[:term].present?
@ -117,10 +126,23 @@ class ParticipantsController < ApplicationController
private 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 def set_participant
@participant = Participant.find(params[:id]) @participant = Participant.find(params[:id])
end end
def ssn_already_taken?(ssn)
Participant.exists?(ssn: ssn) || Employer.exists?(ssn: ssn)
end
def participant_params def participant_params
params.require(:participant).permit( params.require(:participant).permit(
:first_name, :first_name,
@ -166,38 +188,22 @@ class ParticipantsController < ApplicationController
params.require(:service_contract).permit(:vendor_id, :start_date, :end_date) params.require(:service_contract).permit(:vendor_id, :start_date, :end_date)
end end
def create_employer_and_employer_record_for(participant) def create_new_employer_for_participant(participant)
# Check if an employer with the same email or similar details already exists employer = Employer.create!(employer_params_from_participant(participant))
existing_employer = Employer.find_by(email: participant.email) || participant.employer_id = employer.id
Employer.where(first_name: participant.first_name, employer.persisted?
last_name: participant.last_name, end
ssn: participant.ssn).first
def create_employer_record(participant)
# If an existing employer is found, assign it to the participant EmployerRecord.create!(participant: participant, employer_id: participant.employer_id, start_date: Date.today)
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 end
def update_employer_details(participant) def update_employer_details(participant)
employer = Employer.find(participant.employer_id) employer = Employer.find(participant.employer_id)
employer.update(
update_attrs = {
# Include all fields you want to update, excluding SSN
first_name: participant.first_name, first_name: participant.first_name,
last_name: participant.last_name, last_name: participant.last_name,
address_line_1: participant.address_line_1, address_line_1: participant.address_line_1,
@ -208,10 +214,15 @@ class ParticipantsController < ApplicationController
phone: participant.phone, phone: participant.phone,
email: participant.email, email: participant.email,
dob: participant.dob, dob: participant.dob,
ssn: participant.ssn,
gender: participant.gender, gender: participant.gender,
}
# Add other fields that need to be updated
) 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
end end

View File

@ -15,17 +15,20 @@ class Employer < ApplicationRecord
end end
validate :unique_as_participant class Employer < ApplicationRecord
validates :ssn, uniqueness: true, allow_blank: true, presence: true # Associations and other methods as before...
validates :tin, uniqueness: true, allow_blank: true, presence: true
private validate :unique_as_participant, if: -> { ssn_changed? || new_record? }
validates :ssn, uniqueness: { allow_blank: true }, if: -> { ssn.present? }
validates :tin, uniqueness: { allow_blank: true }, if: -> { tin.present? }
def unique_as_participant private
# Check if there's a Participant with the same details
existing_participant = Participant.find_by(ssn: ssn) def unique_as_participant
if existing_participant && (existing_participant.id != id) if Participant.exists?(ssn: ssn) && (id.blank? || Participant.where.not(id: id).exists?(ssn: ssn))
errors.add(:base, 'A participant with the same SSN already exists') errors.add(:ssn, 'A participant with the same SSN already exists')
end end
end end
end end
end

View File

@ -1,6 +1,6 @@
class Participant < ApplicationRecord class Participant < ApplicationRecord
# Associations # Associations
belongs_to :employer, optional: true belongs_to :employer, optional: false
has_many :employments has_many :employments
has_many :workers, through: :employments has_many :workers, through: :employments
has_many :service_contracts has_many :service_contracts
@ -11,7 +11,8 @@ class Participant < ApplicationRecord
# Validations # Validations
validates :first_name, :last_name, :ssn, presence: true validates :first_name, :last_name, :ssn, presence: true
validate :ssn_must_be_unique_across_models validates :ssn, uniqueness: { message: "SSN is already taken by another participant" }, unless: :ssn_already_taken_by_employer?
validates :employer_id, presence: true
def full_name def full_name
"#{first_name} #{last_name}" "#{first_name} #{last_name}"
@ -19,9 +20,8 @@ class Participant < ApplicationRecord
private private
def ssn_must_be_unique_across_models def ssn_already_taken_by_employer?
if Participant.where.not(id: id).exists?(ssn: ssn) || Employer.exists?(ssn: ssn) # Check if the SSN already exists in the Employer model, excluding the current participant's employer
errors.add(:ssn, 'SSN is already taken by another participant or employer') Employer.where.not(id: employer_id).exists?(ssn: ssn)
end
end end
end end

3
db/schema.rb generated
View File

@ -79,9 +79,6 @@ ActiveRecord::Schema[7.1].define(version: 2024_02_06_215805) do
t.date "start_date" t.date "start_date"
t.date "end_date" t.date "end_date"
t.index ["employer_id"], name: "index_participants_on_employer_id" t.index ["employer_id"], name: "index_participants_on_employer_id"
t.index ["last_name"], name: "index_participants_on_last_name"
t.index ["mci"], name: "index_participants_on_mci"
t.index ["ssn"], name: "index_participants_on_ssn"
t.index ["worker_id"], name: "index_participants_on_worker_id" t.index ["worker_id"], name: "index_participants_on_worker_id"
end end

View File

@ -12,8 +12,8 @@
"@babel/core": "^7.23.9", "@babel/core": "^7.23.9",
"@babel/plugin-proposal-optional-chaining": "^7.21.0", "@babel/plugin-proposal-optional-chaining": "^7.21.0",
"@babel/preset-env": "^7.23.9", "@babel/preset-env": "^7.23.9",
"webpack": "^5.90.0", "webpack": "^4.46.0",
"webpack-cli": "^5.1.4", "webpack-cli": "^3.3.12",
"webpack-dev-server": "^4.15.1" "webpack-dev-server": "^3"
} }
} }

1287
yarn.lock

File diff suppressed because it is too large Load Diff