blob: 7ff8b731e0d11c0525b8dbe8b6c0453dc453501c (
plain)
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
|
# frozen_string_literal: true
module CustomerRelations
module Contacts
class CreateService < BaseService
def execute
return error_no_permissions unless allowed?
return error_organization_invalid unless organization_valid?
contact = Contact.create(params.merge(group_id: group.id))
return error_creating(contact) unless contact.persisted?
ServiceResponse.success(payload: contact)
end
private
def organization_valid?
return true unless params[:organization_id]
organization = Organization.find(params[:organization_id])
organization.group_id == group.id
rescue ActiveRecord::RecordNotFound
false
end
def error_organization_invalid
error('The specified organization was not found or does not belong to this group')
end
def error_no_permissions
error('You have insufficient permissions to create a contact for this group')
end
def error_creating(contact)
error(contact&.errors&.full_messages || 'Failed to create contact')
end
end
end
end
|