Added customer composite key

This commit is contained in:
Nedim
2025-02-27 07:45:54 +01:00
parent a28cdc6a6d
commit 92a61159ca
35 changed files with 1776 additions and 115 deletions

View File

@@ -21,7 +21,7 @@ class CustomersController < ApplicationController
# POST /customers or /customers.json
def create
@customer = Customer.new(customer_params)
@customer.company = @company
@customer.company = Company.first # Set the first company
respond_to do |format|
if @customer.save
@@ -57,15 +57,35 @@ class CustomersController < ApplicationController
end
end
def search
@customers = @company.customers.where(
"LOWER(first_name) LIKE :query OR LOWER(surname) LIKE :query OR original_phone LIKE :query",
query: "%#{params[:q].downcase}%"
).limit(10)
render json: @customers.map { |c|
{
id: "#{c.first_name}_#{c.surname}_#{c.original_phone}",
label: "#{c.full_name} (#{c.original_phone})",
birthyear: c.birthyear
}
}
end
private
# Use callbacks to share common setup or constraints between actions.
def set_customer
@customer = Customer.find(params[:id])
first_name, surname, original_phone = params[:composite_key].split('_')
@customer = Customer.find_by!(
first_name: first_name,
surname: surname,
original_phone: original_phone
)
end
# Only allow a list of trusted parameters through.
def customer_params
params.require(:customer).permit(:name, :phone, :notes, :email, :birthyear)
params.require(:customer).permit(:first_name, :surname, :phone, :notes, :email, :birthyear)
end
end

View File

@@ -16,6 +16,7 @@ class ReservationsController < ApplicationController
def new
@reservation = Reservation.new
@reservation.team = @company.teams.first
@customers = @company.customers
end
# GET /reservations/1/edit
@@ -23,18 +24,24 @@ class ReservationsController < ApplicationController
# POST /reservations or /reservations.json
def create
@reservation = Reservation.new(reservation_params)
@reservation.company = @company
@reservation = @company.reservations.new(
reservation_params.except(:customer_id, :customer_birth_year)
)
respond_to do |format|
if @reservation.save
format.html { redirect_to reservation_url(@reservation), notice: t('.reservation_created') }
format.json { render :show, status: :created, location: @reservation }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @reservation.errors, status: :unprocessable_entity }
end
# Find or create customer
find_or_create_customer
assign_customer_to_reservation
if @reservation.save
redirect_to @reservation, notice: t('.reservation_created')
else
@customers = @company.customers
render :new, status: :unprocessable_entity
end
rescue ActiveRecord::RecordInvalid => e
@reservation.errors.add(:base, "Failed to save customer: #{e.message}")
@customers = @company.customers
render :new, status: :unprocessable_entity
end
# PATCH/PUT /reservations/1 or /reservations/1.json
@@ -69,11 +76,63 @@ class ReservationsController < ApplicationController
# Only allow a list of trusted parameters through.
def reservation_params
params.require(:reservation).permit(:company_id, :customer_id, :team_id, :title, :description, :start_time,
:end_time)
params.require(:reservation).permit(
:customer_id,
:team_id,
:start_time,
:end_time,
:title,
:description,
:customer_first_name,
:customer_surname,
:customer_original_phone,
:customer_birth_year # Keep this in permitted params
)
end
def determine_layout
action_name == 'index' ? 'calendar' : 'application'
end
def find_or_create_customer
customer_params = build_customer_attributes
@customer = if new_customer?
Customer.create!(customer_params)
else
find_or_initialize_customer(customer_params)
end
end
def build_customer_attributes
{
first_name: params[:reservation][:customer_first_name],
surname: params[:reservation][:customer_surname],
original_phone: params[:reservation][:customer_original_phone],
phone: params[:reservation][:customer_original_phone],
birthyear: params[:reservation][:customer_birth_year],
company_id: @company.id
}
end
def new_customer?
params[:reservation][:customer_id].present? &&
params[:reservation][:customer_id].end_with?('__new')
end
def find_or_initialize_customer(attributes)
Customer.find_or_create_by!(
first_name: attributes[:first_name],
surname: attributes[:surname],
original_phone: attributes[:original_phone]
) do |customer|
customer.assign_attributes(attributes)
end
end
def assign_customer_to_reservation
@reservation.customer_first_name = @customer.first_name
@reservation.customer_surname = @customer.surname
@reservation.customer_original_phone = @customer.original_phone
end
end