2021-09-14 15:12:05 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module CustomerRelations
|
|
|
|
module Organizations
|
2021-09-17 18:11:44 +00:00
|
|
|
class CreateService < BaseService
|
2021-09-14 15:12:05 +00:00
|
|
|
# returns the created organization
|
|
|
|
def execute
|
|
|
|
return error_no_permissions unless allowed?
|
|
|
|
|
2021-10-07 00:09:20 +00:00
|
|
|
organization = Organization.create(params.merge(group_id: group.id))
|
2021-09-14 15:12:05 +00:00
|
|
|
|
|
|
|
return error_creating(organization) unless organization.persisted?
|
|
|
|
|
|
|
|
ServiceResponse.success(payload: organization)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def error_no_permissions
|
|
|
|
error('You have insufficient permissions to create an organization for this group')
|
|
|
|
end
|
|
|
|
|
|
|
|
def error_creating(organization)
|
|
|
|
error(organization&.errors&.full_messages || 'Failed to create organization')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|