2022-03-18 19:44:30 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-02-13 17:31:57 +01:00
|
|
|
# Provides methods for SupportingDocumentFile
|
|
|
|
class SupportingDocumentFileService
|
2022-03-18 19:44:30 +01:00
|
|
|
def self.list(operator, filters = {})
|
|
|
|
files = []
|
2023-02-13 14:08:42 +01:00
|
|
|
if filters[:user_id].present? && (operator.privileged? || filters[:user_id].to_i == operator.id)
|
2023-02-13 17:31:57 +01:00
|
|
|
files = SupportingDocumentFile.where(user_id: filters[:user_id])
|
2022-03-18 19:44:30 +01:00
|
|
|
end
|
|
|
|
files
|
|
|
|
end
|
|
|
|
|
2023-02-13 17:31:57 +01:00
|
|
|
def self.create(supporting_document_file)
|
|
|
|
saved = supporting_document_file.save
|
2022-03-18 19:44:30 +01:00
|
|
|
|
|
|
|
if saved
|
2023-02-13 17:31:57 +01:00
|
|
|
user = User.find(supporting_document_file.user_id)
|
2022-03-18 19:44:30 +01:00
|
|
|
all_files_are_upload = true
|
2023-02-13 17:31:57 +01:00
|
|
|
user.group.supporting_document_types.each do |type|
|
|
|
|
file = type.supporting_document_files.find_by(user_id: supporting_document_file.user_id)
|
2023-02-13 14:08:42 +01:00
|
|
|
all_files_are_upload = false unless file
|
2022-03-18 19:44:30 +01:00
|
|
|
end
|
|
|
|
if all_files_are_upload
|
2023-02-13 14:08:42 +01:00
|
|
|
NotificationCenter.call type: 'notify_admin_user_supporting_document_files_created',
|
2022-03-18 19:44:30 +01:00
|
|
|
receiver: User.admins_and_managers,
|
|
|
|
attached_object: user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
saved
|
|
|
|
end
|
|
|
|
|
2023-02-13 17:31:57 +01:00
|
|
|
def self.update(supporting_document_file, params)
|
|
|
|
updated = supporting_document_file.update(params)
|
2022-03-18 19:44:30 +01:00
|
|
|
if updated
|
2023-02-13 17:31:57 +01:00
|
|
|
user = supporting_document_file.user
|
2022-03-18 19:44:30 +01:00
|
|
|
all_files_are_upload = true
|
2023-02-13 17:31:57 +01:00
|
|
|
user.group.supporting_document_types.each do |type|
|
|
|
|
file = type.supporting_document_files.find_by(user_id: supporting_document_file.user_id)
|
2023-02-13 14:08:42 +01:00
|
|
|
all_files_are_upload = false unless file
|
2022-03-18 19:44:30 +01:00
|
|
|
end
|
2023-02-17 13:26:58 +01:00
|
|
|
if all_files_are_upload
|
2023-02-13 14:08:42 +01:00
|
|
|
NotificationCenter.call type: 'notify_admin_user_supporting_document_files_updated',
|
2022-03-18 19:44:30 +01:00
|
|
|
receiver: User.admins_and_managers,
|
2023-02-13 17:31:57 +01:00
|
|
|
attached_object: supporting_document_file
|
2022-03-18 19:44:30 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
updated
|
|
|
|
end
|
|
|
|
end
|