2023-05-26 15:55:38 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# ChildService
|
|
|
|
class ChildService
|
|
|
|
def self.create(child)
|
|
|
|
if child.save
|
|
|
|
NotificationCenter.call type: 'notify_admin_child_created',
|
|
|
|
receiver: User.admins_and_managers,
|
|
|
|
attached_object: child
|
2023-05-29 19:32:51 +02:00
|
|
|
all_files_are_upload = true
|
|
|
|
SupportingDocumentType.where(document_type: 'Child').each do |sdt|
|
|
|
|
file = sdt.supporting_document_files.find_by(supportable: child)
|
|
|
|
all_files_are_upload = false if file.nil? || file.attachment_identifier.nil?
|
|
|
|
end
|
|
|
|
if all_files_are_upload
|
|
|
|
NotificationCenter.call type: 'notify_admin_user_child_supporting_document_files_created',
|
|
|
|
receiver: User.admins_and_managers,
|
|
|
|
attached_object: child
|
|
|
|
end
|
|
|
|
|
2023-05-26 15:55:38 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.update(child, child_params)
|
2023-05-29 19:32:51 +02:00
|
|
|
if child.update(child_params)
|
|
|
|
all_files_are_upload = true
|
|
|
|
SupportingDocumentType.where(document_type: 'Child').each do |sdt|
|
|
|
|
file = sdt.supporting_document_files.find_by(supportable: child)
|
|
|
|
all_files_are_upload = false if file.nil? || file.attachment_identifier.nil?
|
|
|
|
end
|
|
|
|
if all_files_are_upload
|
|
|
|
NotificationCenter.call type: 'notify_admin_user_child_supporting_document_files_updated',
|
|
|
|
receiver: User.admins_and_managers,
|
|
|
|
attached_object: child
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
false
|
2023-05-26 15:55:38 +02:00
|
|
|
end
|
2023-05-26 16:44:18 +02:00
|
|
|
|
|
|
|
def self.validate(child, is_valid)
|
|
|
|
is_updated = child.update(validated_at: is_valid ? Time.current : nil)
|
|
|
|
if is_updated
|
|
|
|
if is_valid
|
|
|
|
NotificationCenter.call type: 'notify_user_child_is_validated',
|
|
|
|
receiver: child.user,
|
|
|
|
attached_object: child
|
|
|
|
else
|
|
|
|
NotificationCenter.call type: 'notify_user_child_is_invalidated',
|
|
|
|
receiver: child.user,
|
|
|
|
attached_object: child
|
|
|
|
end
|
|
|
|
end
|
|
|
|
is_updated
|
|
|
|
end
|
2023-05-26 15:55:38 +02:00
|
|
|
end
|