2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2012-08-21 10:28:41 +00:00
|
|
|
class LinksController < ApplicationController
|
2017-02-15 12:29:25 +00:00
|
|
|
prepend_before_action :authentication_check
|
2012-08-21 10:28:41 +00:00
|
|
|
|
2013-08-06 22:10:28 +00:00
|
|
|
# GET /api/v1/links
|
2012-08-21 10:28:41 +00:00
|
|
|
def index
|
|
|
|
links = Link.list(
|
2018-12-19 17:31:51 +00:00
|
|
|
link_object: params[:link_object],
|
2015-04-27 13:42:53 +00:00
|
|
|
link_object_value: params[:link_object_value],
|
2021-04-27 08:17:15 +00:00
|
|
|
user: current_user,
|
2012-08-21 10:28:41 +00:00
|
|
|
)
|
|
|
|
|
2019-06-04 03:40:48 +00:00
|
|
|
linked_objects = links
|
2021-06-24 07:05:39 +00:00
|
|
|
.filter_map { |elem| elem['link_object']&.safe_constantize&.lookup(id: elem['link_object_value']) }
|
2012-08-21 10:28:41 +00:00
|
|
|
|
|
|
|
# return result
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: {
|
2019-06-04 03:40:48 +00:00
|
|
|
links: links,
|
|
|
|
assets: ApplicationModel::CanAssets.reduce(linked_objects),
|
2012-08-21 10:28:41 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-08-06 22:10:28 +00:00
|
|
|
# POST /api/v1/links/add
|
2012-08-21 10:28:41 +00:00
|
|
|
def add
|
2019-06-04 03:40:48 +00:00
|
|
|
object = case params[:link_object_source]
|
|
|
|
when 'Ticket'
|
|
|
|
Ticket.find_by(number: params[:link_object_source_number])
|
|
|
|
when 'KnowledgeBase::Answer::Translation'
|
|
|
|
KnowledgeBase::Answer::Translation.find_by(id: params[:link_object_source_number])
|
|
|
|
end
|
2012-08-24 18:54:57 +00:00
|
|
|
|
2017-03-15 10:29:56 +00:00
|
|
|
if !object
|
2021-11-15 15:58:19 +00:00
|
|
|
render json: { error: __('No such object!') }, status: :unprocessable_entity
|
2017-03-15 10:29:56 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2012-08-21 10:28:41 +00:00
|
|
|
link = Link.add(
|
2018-12-19 17:31:51 +00:00
|
|
|
link_type: params[:link_type],
|
|
|
|
link_object_target: params[:link_object_target],
|
2015-04-27 13:42:53 +00:00
|
|
|
link_object_target_value: params[:link_object_target_value],
|
2018-12-19 17:31:51 +00:00
|
|
|
link_object_source: params[:link_object_source],
|
2017-03-15 10:29:56 +00:00
|
|
|
link_object_source_value: object.id,
|
2012-08-21 10:28:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if link
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: link, status: :created
|
2012-08-21 10:28:41 +00:00
|
|
|
else
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: link.errors, status: :unprocessable_entity
|
2012-08-21 10:28:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-06 22:10:28 +00:00
|
|
|
# DELETE /api/v1/links/remove
|
2012-08-23 22:08:27 +00:00
|
|
|
def remove
|
|
|
|
link = Link.remove(params)
|
2012-08-21 10:28:41 +00:00
|
|
|
|
2012-08-23 22:08:27 +00:00
|
|
|
if link
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: link, status: :created
|
2012-08-21 10:28:41 +00:00
|
|
|
else
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: link.errors, status: :unprocessable_entity
|
2012-08-21 10:28:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|