2024-08-06 14:16:40 +02:00
|
|
|
class Reservation < ApplicationRecord
|
|
|
|
|
belongs_to :company
|
2025-02-27 07:45:54 +01:00
|
|
|
belongs_to :customer,
|
|
|
|
|
primary_key: %i[first_name surname original_phone],
|
|
|
|
|
query_constraints: %i[customer_first_name customer_surname customer_original_phone]
|
2024-08-24 07:06:09 +02:00
|
|
|
belongs_to :team
|
2024-08-06 14:16:40 +02:00
|
|
|
|
|
|
|
|
validates :company_id, presence: true
|
2024-08-24 07:06:09 +02:00
|
|
|
validates :team_id, presence: true
|
2025-02-27 07:45:54 +01:00
|
|
|
|
|
|
|
|
# Remove customer_id validation since we're using composite key
|
|
|
|
|
validates :customer_first_name, :customer_surname, :customer_original_phone, presence: true
|
|
|
|
|
|
|
|
|
|
# Add validation to ensure customer exists
|
|
|
|
|
validate :customer_must_exist
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def customer_must_exist
|
|
|
|
|
unless Customer.exists?(first_name: customer_first_name,
|
|
|
|
|
surname: customer_surname,
|
|
|
|
|
original_phone: customer_original_phone)
|
|
|
|
|
errors.add(:base, "Selected customer does not exist")
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-08-06 14:16:40 +02:00
|
|
|
end
|
2024-09-08 14:00:15 +02:00
|
|
|
|
2025-04-24 07:04:42 +02:00
|
|
|
# Moved to app/serializers/reservation_serializer.rb
|
|
|
|
|
# class ReservationSerializer < ActiveModel::Serializer
|
|
|
|
|
# attributes :id, :company, :customer, :team, :start_time, :end_time
|
|
|
|
|
# end
|