blob: dcadae8dc3bb9bc59f9268c40f8c22dcdb63b614 (
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 Achievements
class UpdateService
attr_reader :current_user, :achievement, :params
def initialize(current_user, achievement, params)
@current_user = current_user
@achievement = achievement
@params = params
end
def execute
return error_no_permissions unless allowed?
if achievement.update(params)
ServiceResponse.success(payload: achievement)
else
error_updating
end
end
private
def allowed?
current_user&.can?(:admin_achievement, achievement)
end
def error_no_permissions
error('You have insufficient permission to update this achievement')
end
def error(message)
ServiceResponse.error(payload: achievement, message: Array(message))
end
def error_updating
error(achievement&.errors&.full_messages || 'Failed to update achievement')
end
end
end
|