Define an endpoint with table simulation stats #324

Open
bustikiller wants to merge 4 commits from tables-stats into main
3 changed files with 26 additions and 9 deletions

View File

@ -663,4 +663,4 @@ RUBY VERSION
ruby 3.4.3p32 ruby 3.4.3p32
BUNDLED WITH BUNDLED WITH
2.6.1 2.7.2

View File

@ -4,17 +4,28 @@
class TablesArrangementsController < ApplicationController class TablesArrangementsController < ApplicationController
def index def index
current_digest = Tables::Distribution.digest(current_tenant) query = TablesArrangement
.order(valid: :desc)
.order(discomfort: :asc)
.select(:id, :name, :discomfort, :status, :progress)
.select("digest = '#{current_digest}'::uuid OR discomfort IS NULL as valid")
.limit((params[:limit].presence&.to_i || 20).clamp(1..20))
render json: TablesArrangement query = query.where(status: params[:status]) if params[:status].present?
.order(valid: :desc)
.order(discomfort: :asc) render json: query
.select(:id, :name, :discomfort, :status, :progress)
.select("digest = '#{current_digest}'::uuid OR discomfort IS NULL as valid")
.limit(20)
.as_json(only: %i[id name discomfort valid status progress]) .as_json(only: %i[id name discomfort valid status progress])
end end
def stats
scope = TablesArrangement.where('digest = ?::uuid OR discomfort IS NULL', current_digest)
render json: {
count: scope.group(:status).count,
in_progress: scope.where(status: :in_progress).limit(10).order(progress: :desc).pluck(:progress)
}
end
def show def show
Guest.joins(:seats, :group) Guest.joins(:seats, :group)
.where(seats: { tables_arrangement_id: params[:id] }) .where(seats: { tables_arrangement_id: params[:id] })
@ -35,6 +46,10 @@ class TablesArrangementsController < ApplicationController
private private
def current_digest
@current_digest ||= Tables::Distribution.digest(current_tenant)
end
def format(number:, guests:) def format(number:, guests:)
{ {
number: number, number: number,

View File

@ -40,7 +40,9 @@ Rails.application.routes.draw do
resource :website, only: [:show, :update] resource :website, only: [:show, :update]
resources :tables_arrangements, only: %i[index show create] resources :tables_arrangements, only: %i[index show create] do
get :stats, on: :collection
end
resources :summary, only: :index resources :summary, only: :index
resources :invitations, only: %i[show index create update destroy] do resources :invitations, only: %i[show index create update destroy] do
post :email, on: :collection post :email, on: :collection