From cf0a7b71ca129a152ac50ef2abede1a6bf05c0f4 Mon Sep 17 00:00:00 2001
From: Manuel Bustillo <bustikiller@bustikiller.com>
Date: Sun, 26 Jan 2025 12:16:53 +0100
Subject: [PATCH] Store the digest along with the calculated discomfort

---
 app/models/wedding.rb               |  2 ++
 app/services/tables/distribution.rb | 13 +++++++++++--
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/app/models/wedding.rb b/app/models/wedding.rb
index 83525b5..5f0dabf 100644
--- a/app/models/wedding.rb
+++ b/app/models/wedding.rb
@@ -19,4 +19,6 @@ class Wedding < ApplicationRecord
   SLUG_REGEX = /[a-z\d-]+/
 
   validates :slug, presence: true, uniqueness: true, format: { with: /\A#{SLUG_REGEX}\z/ }
+
+  has_many :guests, dependent: :delete_all
 end
diff --git a/app/services/tables/distribution.rb b/app/services/tables/distribution.rb
index 87dccb0..6a12d70 100644
--- a/app/services/tables/distribution.rb
+++ b/app/services/tables/distribution.rb
@@ -6,7 +6,13 @@ require_relative '../../extensions/tree_node_extension'
 
 module Tables
   class Distribution
-    attr_accessor :tables, :min_per_table, :max_per_table
+    class << self
+      def digest(wedding)
+        Digest::UUID.uuid_v5(wedding.id, wedding.guests.potential.order(:id).pluck(:id).join)
+      end
+    end
+
+    attr_accessor :tables, :min_per_table, :max_per_table, :hierarchy
 
     def initialize(min_per_table:, max_per_table:)
       @min_per_table = min_per_table
@@ -54,7 +60,10 @@ module Tables
 
         Seat.insert_all!(records_to_store)
 
-        arrangement.update!(discomfort:)
+        arrangement.update!(
+          discomfort:,
+          guests_digest: self.class.digest(tables.first.first.wedding)
+        )
       end
     end