wedding-planner/app/controllers/tables_arrangements_controller.rb
Manuel Bustillo 2147d7ad5e
All checks were successful
Run unit tests / rubocop (push) Successful in 27s
Run unit tests / check-licenses (push) Successful in 32s
Run unit tests / copyright_notice (push) Successful in 36s
Run unit tests / unit_tests (push) Successful in 1m22s
Run unit tests / build-static-assets (push) Successful in 10m9s
Redo simulations lifecycle (#222)
## Why

The current way of creating and deleting simulations doesn't scale for big instances. We cannot generate 50 simulations every time a guest confirms attendance, and we should not delete existing valuable simulations. For example, if a guest confirms attendance and declines right after, previously generated simulations should still be valid.

## What

In this PR we are introducing a series of changes that make simulations management easier:

1. We're removing the automatic creation of simulations.
2. Simulations are not removed anymore, neither manually nor automatically.
3. A new endpoint has been defined to create simulations on demand.
4. A digest property has been defined to determine whether a simulation is still valid (meaning there have not been any change in the list of guests involved).

Reviewed-on: #222
Co-authored-by: Manuel Bustillo <bustikiller@bustikiller.com>
Co-committed-by: Manuel Bustillo <bustikiller@bustikiller.com>
2025-01-26 12:53:21 +00:00

55 lines
1.5 KiB
Ruby

# Copyright (C) 2024-2025 LibreWeddingPlanner contributors
# frozen_string_literal: true
class TablesArrangementsController < ApplicationController
def index
current_digest = Tables::Distribution.digest(current_tenant)
render json: TablesArrangement
.order(valid: :desc)
.order(discomfort: :asc)
.select(:id, :name, :discomfort)
.select("digest = '#{current_digest}'::uuid as valid")
.limit(20)
.as_json(only: %i[id name discomfort valid])
end
def show
Guest.joins(:seats, :group)
.where(seats: { tables_arrangement_id: params[:id] })
.select('guests.*', 'groups.color', 'seats.table_number')
.group_by(&:table_number)
.map { |number, guests| format(number:, guests:) }
.then { |result| render json: { id: params[:id], tables: result } }
end
def create
TableSimulatorJob.perform_later(current_tenant.id)
render json: {}, status: :created
end
private
def format(number:, guests:)
{
number: number,
discomfort: discomfort(guests: guests),
guests: guests.as_json(only: %i[id name color])
}
end
def discomfort(guests:)
table = Tables::Table.new(guests)
table.min_per_table = TableSimulatorJob::MIN_PER_TABLE
table.max_per_table = TableSimulatorJob::MAX_PER_TABLE
calculator = Tables::DiscomfortCalculator.new(table:)
{
discomfort: calculator.calculate,
breakdown: calculator.breakdown
}
end
end