DEV: Allow with_service in jobs

This patch introduces a new `ServiceJob` class allowing the use of
`with_service` in jobs.

This way, it’s easier to use the chat service objects in jobs and
provides the same level of functionality than the one we have in
controllers.
This commit is contained in:
Loïc Guitaut
2023-02-21 16:02:10 +01:00
committed by Loïc Guitaut
parent 925a7bd48b
commit b8762172e4
6 changed files with 84 additions and 50 deletions

View File

@ -12,4 +12,18 @@ class Chat::Api < Chat::ChatBaseController
raise Discourse::NotFound unless SiteSetting.chat_enabled
guardian.ensure_can_chat!
end
def default_actions_for_service
proc do
on_success { render(json: success_json) }
on_failure { render(json: failed_json, status: 422) }
on_failed_policy(:invalid_access) { raise Discourse::InvalidAccess }
on_failed_contract do
render(
json: failed_json.merge(errors: result[:"result.contract.default"].errors.full_messages),
status: 400,
)
end
end
end
end