diff --git a/app/controllers/guests_controller.rb b/app/controllers/guests_controller.rb index ecf5d34..de6127d 100644 --- a/app/controllers/guests_controller.rb +++ b/app/controllers/guests_controller.rb @@ -26,7 +26,7 @@ class GuestsController < ApplicationController end def bulk_update - Guest.where(id: params[:guest_ids]).update!(params.require(:properties).permit(:status)) + Guests::UpdateUseCase.new(guest_ids: params[:guest_ids], params: params.require(:properties).permit(:status)).call render json: {}, status: :ok end end diff --git a/app/jobs/table_simulator_job.rb b/app/jobs/table_simulator_job.rb index c382b1a..0b90396 100644 --- a/app/jobs/table_simulator_job.rb +++ b/app/jobs/table_simulator_job.rb @@ -9,7 +9,7 @@ class TableSimulatorJob < ApplicationJob engine.add_perturbation(Tables::Swap) initial_solution = Tables::Distribution.new(min_per_table: 8, max_per_table: 10) - initial_solution.random_distribution(Guest.all.shuffle) + initial_solution.random_distribution(Guest.potential.shuffle) engine.initial_solution = initial_solution diff --git a/app/models/guest.rb b/app/models/guest.rb index 39d27b3..87680bf 100644 --- a/app/models/guest.rb +++ b/app/models/guest.rb @@ -8,9 +8,11 @@ class Guest < ApplicationRecord invited: 10, confirmed: 20, declined: 30, - tentative: 40, + tentative: 40 } + scope :potential, -> { where.not(status: %i[declined considered]) } + def full_name "#{first_name} #{last_name}" end diff --git a/app/use_cases/guests/update_use_case.rb b/app/use_cases/guests/update_use_case.rb new file mode 100644 index 0000000..9a5cc66 --- /dev/null +++ b/app/use_cases/guests/update_use_case.rb @@ -0,0 +1,14 @@ +# Copyright (C) 2024 Manuel Bustillo + +module Guests + class UpdateUseCase + def initialize(guest_ids:, params:) + Guest.where(id: guest_ids).update!(params) + + # TODO: Not all status transitions may require a table re-arrangement + TablesArrangement.delete_all + + ActiveJob.perform_all_later(50.times.map { TableSimulatorJob.new }) + end + end +end