-
-
Notifications
You must be signed in to change notification settings - Fork 572
Expand file tree
/
Copy pathpartner_profile_update_service.rb
More file actions
75 lines (64 loc) · 2.21 KB
/
partner_profile_update_service.rb
File metadata and controls
75 lines (64 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class PartnerProfileUpdateService
include ServiceObjectErrorsMixin
attr_reader :error
def initialize(old_partner, new_partner_params, new_profile_params)
@partner = old_partner
@profile = @partner.profile
@partner_params = new_partner_params
@profile_params = new_profile_params
@error_messages = []
end
def call
return self unless validation
perform_profile_service do
@partner.update!(@partner_params)
@profile.served_areas.destroy_all
@profile.attributes = @profile_params
@profile.save!(context: :edit)
end
end
def perform_profile_service(&block)
begin
@profile.transaction do
yield block
end
@profile.reload
rescue ActiveRecord::RecordNotFound => e
Rails.logger.error "[!] #{self.class.name} failed to update profile #{@profile.id} because it does not exist"
set_error(e)
rescue => e
Rails.logger.error "[!] #{self.class.name} failed to update profile for #{@profile.id}: #{@profile.errors.full_messages} [#{e.inspect}]"
set_error(e)
end
self
end
def success?
@error.nil?
end
def set_error(error)
@error = error.to_s
end
private
def validation
return true unless %w[awaiting_review approved].include?(@partner.status)
return true if @partner.organization.one_step_partner_invite
check_social_media
check_mandatory_fields
@error = @error_messages.join(". ") if @error_messages.any?
@error.nil?
end
def check_mandatory_fields
mandatory_fields = %i[agency_type address1 city state zip_code program_name program_description]
missing_fields = mandatory_fields.select { |field| @profile_params[field].blank? }
missing_fields.prepend :agency_name if @partner_params[:name].blank?
if missing_fields.any?
@error_messages << "Missing mandatory fields: #{missing_fields.join(", ")}"
end
end
def check_social_media
social_media_fields = %i[website facebook twitter instagram]
if social_media_fields.all? { |field| @profile_params[field].blank? } && !@profile_params[:no_social_media_presence]
@error_messages << "At least one social media field must be filled out or 'No social media presence' must be checked."
end
end
end