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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'app/controllers/concerns/collavre/comments/approval_actions.rb', line 34
def update_action
executed_error = false
update_success = false
approver_mismatch_error = false
status_error_key = nil
status_http_status = nil
validation_error_message = nil
@comment.with_lock do
@comment.reload
status_in_lock = @comment.approval_status(Current.user)
if status_in_lock == :invalid_action_format
if @comment.approver_id == Current.user&.id
status_in_lock = :ok
else
status_in_lock = :not_allowed
end
end
if status_in_lock != :ok && @creative.has_permission?(Current.user, :admin)
status_in_lock = :ok
end
if status_in_lock != :ok
approver_mismatch_error = true
status_error_key = case status_in_lock
when :invalid_action_format then "collavre.comments.approve_invalid_format"
when :missing_action then "collavre.comments.approve_missing_action"
when :missing_approver then "collavre.comments.approve_missing_approver"
when :admin_required then "collavre.comments.approve_admin_required"
else "collavre.comments.approve_not_allowed"
end
status_http_status = case status_in_lock
when :invalid_action_format, :missing_action, :missing_approver
:unprocessable_entity
else
:forbidden
end
elsif @comment.action_executed_at.present?
executed_error = true
else
action_payload = params.dig(:comment, :action)
if action_payload.blank?
validation_error_message = I18n.t("collavre.comments.approve_missing_action")
else
begin
validator = ::Comments::ActionValidator.new(comment: @comment)
parsed_payload = validator.validate!(action_payload)
normalized_action = JSON.pretty_generate(parsed_payload)
update_success = @comment.update(action: normalized_action)
rescue ::Comments::ActionValidator::ValidationError => e
validation_error_message = e.message
end
end
end
end
if approver_mismatch_error
render json: { error: I18n.t(status_error_key) }, status: status_http_status
elsif validation_error_message
render json: { error: validation_error_message }, status: :unprocessable_entity
elsif executed_error
render json: { error: I18n.t("collavre.comments.approve_already_executed") }, status: :unprocessable_entity
elsif update_success
@comment = Comment.with_attached_images.includes(:comment_reactions, :comment_versions, :selected_version).find(@comment.id)
render partial: "collavre/comments/comment", locals: { comment: @comment, current_topic_id: current_topic_context }
else
error_message = @comment.errors.full_messages.to_sentence.presence || I18n.t("collavre.comments.action_update_error")
render json: { error: error_message }, status: :unprocessable_entity
end
rescue ::Comments::ActionValidator::ValidationError => e
render json: { error: e.message }, status: :unprocessable_entity
end
|