2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-09-07 10:36:09 +02:00
|
|
|
class DossierTransfer < ApplicationRecord
|
2023-01-31 14:36:51 +01:00
|
|
|
include EmailSanitizableConcern
|
2021-09-07 10:36:09 +02:00
|
|
|
has_many :dossiers, dependent: :nullify
|
|
|
|
|
|
|
|
EXPIRATION_LIMIT = 2.weeks
|
|
|
|
|
2024-02-13 10:37:03 +01:00
|
|
|
validates :email, strict_email: true, presence: true
|
2023-01-31 14:36:51 +01:00
|
|
|
before_validation -> { sanitize_email(:email) }
|
2022-08-02 12:37:29 +02:00
|
|
|
|
2021-09-07 10:36:09 +02:00
|
|
|
scope :pending, -> { where('created_at > ?', (Time.zone.now - EXPIRATION_LIMIT)) }
|
|
|
|
scope :stale, -> { where('created_at < ?', (Time.zone.now - EXPIRATION_LIMIT)) }
|
2022-03-09 10:27:43 +01:00
|
|
|
scope :with_dossiers, -> { joins(:dossiers).merge(Dossier.visible_by_user) }
|
2023-01-31 14:36:51 +01:00
|
|
|
scope :for_email, -> (email) { includes(dossiers: :user).with_dossiers.where(email: email) }
|
2021-09-07 10:36:09 +02:00
|
|
|
|
|
|
|
after_create_commit :send_notification
|
|
|
|
|
|
|
|
def self.initiate(email, dossiers)
|
|
|
|
create(email: email, dossiers: dossiers)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.accept(id, current_user)
|
|
|
|
transfer = pending.find_by(id: id, email: current_user.email)
|
|
|
|
|
|
|
|
if transfer && transfer.dossiers.present?
|
|
|
|
Invite
|
|
|
|
.where(dossier: transfer.dossiers, email: transfer.email)
|
|
|
|
.destroy_all
|
|
|
|
DossierTransferLog.create(transfer.dossiers.map do |dossier|
|
|
|
|
{
|
|
|
|
dossier: dossier,
|
2024-04-18 10:21:32 +02:00
|
|
|
from: dossier.user_email_for(:notification),
|
2023-11-28 11:11:28 +01:00
|
|
|
from_support: transfer.from_support,
|
2021-09-07 10:36:09 +02:00
|
|
|
to: transfer.email
|
|
|
|
}
|
|
|
|
end)
|
|
|
|
transfer.dossiers.update_all(user_id: current_user.id)
|
2021-10-22 10:55:10 +02:00
|
|
|
transfer.destroy_and_nullify
|
2021-09-07 10:36:09 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_locale
|
|
|
|
User.find_by(email: email)&.locale || I18n.default_locale
|
|
|
|
end
|
|
|
|
|
2021-10-21 12:19:40 +02:00
|
|
|
def destroy_and_nullify
|
|
|
|
transaction do
|
|
|
|
# Rails cascading is not working with default scopes. Doing nullify cascade manually.
|
2022-03-09 10:27:43 +01:00
|
|
|
dossiers.update_all(dossier_transfer_id: nil)
|
2021-10-21 12:19:40 +02:00
|
|
|
destroy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.destroy_stale
|
|
|
|
transaction do
|
|
|
|
# Rails cascading is not working with default scopes. Doing nullify cascade manually.
|
2022-03-09 10:27:43 +01:00
|
|
|
Dossier.where(transfer: stale).update_all(dossier_transfer_id: nil)
|
2021-10-21 12:19:40 +02:00
|
|
|
stale.destroy_all
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-12-21 16:27:50 +01:00
|
|
|
def sender
|
|
|
|
if from_support?
|
|
|
|
I18n.t("views.users.dossiers.transfers.sender_from_support")
|
|
|
|
else
|
|
|
|
sender_email
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def sender_email
|
|
|
|
dossiers.last.user.email
|
|
|
|
end
|
|
|
|
|
2021-09-07 10:36:09 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def send_notification
|
2024-01-24 16:02:09 +01:00
|
|
|
DossierMailer.with(dossier_transfer: self).notify_transfer.deliver_later
|
2021-09-07 10:36:09 +02:00
|
|
|
end
|
|
|
|
end
|