class ReservationsController < ApplicationController before_action :set_company before_action :set_reservation, only: %i[show edit update destroy] layout :determine_layout # GET /reservations or /reservations.json def index @reservations = Reservation.includes(:team, :customer).where(company: @company) @reservations = ActiveModelSerializers::SerializableResource.new(@reservations).as_json end # GET /reservations/1 or /reservations/1.json def show; end # GET /reservations/new def new @reservation = Reservation.new @reservation.team = @company.teams.first @customers = @company.customers end # GET /reservations/1/edit def edit; end # POST /reservations or /reservations.json def create @reservation = @company.reservations.new( reservation_params.except(:customer_id, :customer_birth_year) ) # 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 def update respond_to do |format| if @reservation.update(reservation_params) format.html { redirect_to reservation_url(@reservation), notice: t('.reservation_updated') } format.json { render :show, status: :ok, location: @reservation } else format.html { render :edit, status: :unprocessable_entity } format.json { render json: @reservation.errors, status: :unprocessable_entity } end end end # DELETE /reservations/1 or /reservations/1.json def destroy @reservation.destroy! respond_to do |format| format.html { redirect_to reservations_url, notice: t('.reservation_destroyed') } format.json { head :no_content } end end private # Use callbacks to share common setup or constraints between actions. def set_reservation @reservation = Reservation.find(params[:id]) end # Only allow a list of trusted parameters through. def reservation_params 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