feat: update controllers to create delete request

This commit is contained in:
Shivam Mishra
2025-03-06 20:05:59 +05:30
parent 82408a8e03
commit 1c2c8f26fc
4 changed files with 39 additions and 25 deletions
+28 -1
View File
@@ -19,5 +19,32 @@
# index_delete_requests_on_confirmation_code (confirmation_code) UNIQUE
#
class DeleteRequest < ApplicationRecord
belongs_to :account
belongs_to :account, optional: true
enum :status, %w[pending processing completed].index_by(&:itself), default: :pending
before_create :ensure_unqiue_confirmation_code
private
def ensure_unqiue_confirmation_code
max_retries = 3
retry_count = 0
begin
self.confirmation_code = generate_confirmation_code
raise ActiveRecord::RecordNotUnique if self.class.exists?(confirmation_code: confirmation_code)
rescue ActiveRecord::RecordNotUnique
retry_count += 1
if retry_count > max_retries
# it's really really unlikely that we'll ever ever hit this case, but just in case
self.confirmation_code = generate_confirmation_code + SecureRandom.alphanumeric(3)
else
retry
end
end
end
def generate_confirmation_code
SecureRandom.uuid.delete('-')
end
end