Files
old-zsterminator/app/controllers/reservations_controller.rb

208 lines
7.9 KiB
Ruby
Raw Normal View History

2024-08-06 14:16:40 +02:00
class ReservationsController < ApplicationController
before_action :set_company
2025-02-17 19:12:40 +01:00
before_action :set_reservation, only: %i[show edit update destroy]
2024-08-06 14:16:40 +02:00
2024-08-11 14:18:36 +02:00
layout :determine_layout
2024-08-06 14:16:40 +02:00
# GET /reservations or /reservations.json
def index
2025-02-17 19:12:40 +01:00
@reservations = Reservation.includes(:team, :customer).where(company: @company)
2025-04-24 07:04:42 +02:00
@reservations = ActiveModelSerializers::SerializableResource.new(
@reservations,
each_serializer: ReservationSerializer
).as_json
2024-08-06 14:16:40 +02:00
end
# GET /reservations/1 or /reservations/1.json
2025-02-17 19:12:40 +01:00
def show; end
2024-08-06 14:16:40 +02:00
# GET /reservations/new
def new
# logger.debug "--- Reservations#new --- Params received: #{params.inspect}"
# Use Time.zone.parse to interpret times within the application's configured timezone
start_time_param = params[:start_time]
end_time_param = params[:end_time]
# logger.debug "--- Reservations#new --- Start Param: #{start_time_param}, End Param: #{end_time_param}"
start_time = start_time_param ? Time.zone.parse(start_time_param) : Time.current
end_time = end_time_param ? Time.zone.parse(end_time_param) : Time.current + 30.minutes
# logger.debug "--- Reservations#new --- Parsed Start: #{start_time}, Parsed End: #{end_time}"
@reservation = Reservation.new(start_time: start_time, end_time: end_time)
# logger.debug "--- Reservations#new --- Reservation object initialized: #{@reservation.attributes.inspect}"
@reservation.team = @company.teams.first # Assign default team
@customers = @company.customers # Preload customers for potential dropdown
2024-08-06 14:16:40 +02:00
end
# GET /reservations/1/edit
2025-02-17 19:12:40 +01:00
def edit; end
2024-08-06 14:16:40 +02:00
# POST /reservations or /reservations.json
def create
2025-02-27 07:45:54 +01:00
@reservation = @company.reservations.new(
reservation_params.except(:customer_id, :customer_birth_year)
)
2024-08-06 14:16:40 +02:00
# Find or create customer based on submitted attributes
2025-02-27 07:45:54 +01:00
find_or_create_customer
# Associate the reservation with the found/created customer's primary key attributes
2025-02-27 07:45:54 +01:00
assign_customer_to_reservation
if @reservation.save
redirect_to reservations_url, notice: t('.reservation_created')
2025-02-27 07:45:54 +01:00
else
@customers = @company.customers
render :new, status: :unprocessable_entity
2024-08-06 14:16:40 +02:00
end
2025-02-27 07:45:54 +01:00
rescue ActiveRecord::RecordInvalid => e
# If customer creation/validation fails
2025-02-27 07:45:54 +01:00
@reservation.errors.add(:base, "Failed to save customer: #{e.message}")
@customers = @company.customers
render :new, status: :unprocessable_entity
2024-08-06 14:16:40 +02:00
end
# PATCH/PUT /reservations/1 or /reservations/1.json
def update
# Separate reservation attributes from customer attributes
customer_attrs = build_customer_attributes # Use existing helper
reservation_attrs = reservation_params.except(
:customer_id, :customer_first_name, :customer_surname,
:customer_original_phone, :customer_birth_year
)
# Find the customer identified by the submitted name/phone
@customer = Customer.find_by(
first_name: customer_attrs[:first_name],
surname: customer_attrs[:surname],
original_phone: customer_attrs[:original_phone],
company_id: @company.id
)
# Update customer phone/birthyear if found (identifiers assumed immutable here)
customer_updated = @customer ? @customer.update(customer_attrs.slice(:phone, :birthyear)) : false
# Assign the correct customer foreign keys to the reservation attributes
if @customer
reservation_attrs[:customer_first_name] = @customer.first_name
reservation_attrs[:customer_surname] = @customer.surname
reservation_attrs[:customer_original_phone] = @customer.original_phone
else
# Fall back to original keys if form customer wasn't found (e.g., identifiers changed)
# Consider adding an error or different handling if customer *must* be found.
reservation_attrs[:customer_first_name] = @reservation.customer_first_name
reservation_attrs[:customer_surname] = @reservation.customer_surname
reservation_attrs[:customer_original_phone] = @reservation.customer_original_phone
end
reservation_updated = @reservation.update(reservation_attrs)
2024-08-06 14:16:40 +02:00
respond_to do |format|
# Check if reservation update was successful
# (We might ignore customer_updated status for now, or add more complex checks)
if reservation_updated
format.html { redirect_to reservations_url, notice: t('.reservation_updated') }
2024-08-06 14:16:40 +02:00
format.json { render :show, status: :ok, location: @reservation }
else
@customers = @company.customers # Reload for form
2024-08-06 14:16:40 +02:00
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @reservation.errors, status: :unprocessable_entity }
end
end
rescue ActiveRecord::RecordInvalid => e # Catch potential customer update errors
@reservation.errors.add(:base, "Failed to save customer: #{e.message}") if @customer&.invalid?
@customers = @company.customers
render :edit, status: :unprocessable_entity
2024-08-06 14:16:40 +02:00
end
# DELETE /reservations/1 or /reservations/1.json
def destroy
@reservation.destroy!
respond_to do |format|
2025-02-17 19:12:40 +01:00
format.html { redirect_to reservations_url, notice: t('.reservation_destroyed') }
2024-08-06 14:16:40 +02:00
format.json { head :no_content }
end
end
private
2025-02-17 19:12:40 +01:00
# Use callbacks to share common setup or constraints between actions.
def set_reservation
@reservation = Reservation.find(params[:id])
end
2024-08-11 14:18:36 +02:00
2025-02-17 19:12:40 +01:00
# Only allow a list of trusted parameters through.
def reservation_params
# Permit composite key if form uses it, otherwise permit individual fields
# params.require(:reservation).permit(:customer_composite_key, ...)
2025-02-27 07:45:54 +01:00
params.require(:reservation).permit(
:team_id,
:start_time,
:end_time,
:customer_first_name,
:customer_surname,
:customer_original_phone,
:customer_birth_year,
:customer_id # Allow this if select still sends it sometimes
2025-02-27 07:45:54 +01:00
)
2025-02-17 19:12:40 +01:00
end
2024-08-11 14:18:36 +02:00
2025-02-17 19:12:40 +01:00
def determine_layout
action_name == 'index' ? 'calendar' : 'application'
end
2025-02-27 07:45:54 +01:00
# Finds or creates customer based on submitted fields
2025-02-27 07:45:54 +01:00
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
# Extracts customer attributes from reservation form parameters
2025-02-27 07:45:54 +01:00
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], # Assuming phone is same as original_phone for now
2025-02-27 07:45:54 +01:00
birthyear: params[:reservation][:customer_birth_year],
company_id: @company.id
}
end
# Checks if the submitted customer ID indicates a new customer
2025-02-27 07:45:54 +01:00
def new_customer?
# Check based on the specific value format used by TomSelect create function
params[:reservation][:customer_composite_key]&.end_with?('__new') ||
params[:reservation][:customer_id]&.end_with?('__new') # Fallback if old key is used
2025-02-27 07:45:54 +01:00
end
# Finds customer by composite key or creates them
2025-02-27 07:45:54 +01:00
def find_or_initialize_customer(attributes)
# Find using the composite key fields
2025-02-27 07:45:54 +01:00
Customer.find_or_create_by!(
first_name: attributes[:first_name],
surname: attributes[:surname],
original_phone: attributes[:original_phone]
# company_id: attributes[:company_id] # Scope to company if needed
2025-02-27 07:45:54 +01:00
) do |customer|
# Assign other attributes only if creating
customer.assign_attributes(attributes.slice(:phone, :birthyear, :company_id))
2025-02-27 07:45:54 +01:00
end
end
# Sets the foreign key fields on the reservation based on the found/created customer
2025-02-27 07:45:54 +01:00
def assign_customer_to_reservation
return unless @customer # Guard clause
2025-02-27 07:45:54 +01:00
@reservation.customer_first_name = @customer.first_name
@reservation.customer_surname = @customer.surname
@reservation.customer_original_phone = @customer.original_phone
end
2024-08-06 14:16:40 +02:00
end