Commit 5780df0f authored by Oswaldo Ferreira's avatar Oswaldo Ferreira

Return related issues after create and destroy interactions

parent cf2ec7d8
......@@ -4,14 +4,18 @@ module Projects
before_action :authorize_admin_related_issue!, only: [:create, :destroy]
def index
render json: RelatedIssues::ListService.new(issue, current_user).execute
render json: issues
end
def create
opts = { issue_references: params[:issue_references] }
result = RelatedIssues::CreateService.new(issue, current_user, opts).execute
render json: result, status: result['http_status']
if result['status'] == 'success'
render json: { result: result, issues: issues }, status: result['http_status']
else
render json: { result: result }, status: result['http_status']
end
end
def destroy
......@@ -23,11 +27,15 @@ module Projects
result = RelatedIssues::DestroyService.new(related_issue, current_user).execute
render json: result
render json: { result: result, issues: issues }
end
private
def issues
RelatedIssues::ListService.new(issue, current_user).execute
end
def authorize_admin_related_issue!
return render_404 unless can?(current_user, :admin_related_issue, @project)
end
......
......@@ -33,7 +33,9 @@ describe Projects::RelatedIssuesController, type: :controller do
describe 'POST #create' do
let(:service) { double(RelatedIssues::CreateService, execute: service_response) }
let(:service_response) { { 'message' => 'yay' } }
let(:list_service) { double(RelatedIssues::ListService, execute: list_service_response) }
let(:service_response) { { 'message' => 'yay', 'status' => 'success' } }
let(:list_service_response) { [{ 'foo' => 'bar' }] }
let(:issue_references) { double }
let(:user_role) { :developer }
......@@ -41,6 +43,10 @@ describe Projects::RelatedIssuesController, type: :controller do
project.team << [user, user_role]
sign_in user
allow(RelatedIssues::ListService).to receive(:new)
.with(issue, user)
.and_return(list_service)
allow(RelatedIssues::CreateService).to receive(:new)
.with(issue, user, { issue_references: issue_references })
.and_return(service)
......@@ -57,7 +63,8 @@ describe Projects::RelatedIssuesController, type: :controller do
context 'with success' do
it 'returns success JSON' do
is_expected.to have_http_status(200)
expect(json_response).to eq(service_response)
expect(json_response).to eq('result' => service_response,
'issues' => list_service_response)
end
end
......@@ -70,12 +77,12 @@ describe Projects::RelatedIssuesController, type: :controller do
end
end
context 'when failure service result' do
let(:service_response) { { 'http_status' => 401 } }
context 'when failing service result' do
let(:service_response) { { 'http_status' => 401, 'status' => 'error' } }
it 'returns failure JSON' do
is_expected.to have_http_status(401)
expect(json_response).to eq(service_response)
expect(json_response).to eq('result' => service_response)
end
end
end
......@@ -85,7 +92,9 @@ describe Projects::RelatedIssuesController, type: :controller do
let(:referenced_issue) { create :issue, project: project }
let(:related_issue) { create :related_issue, related_issue: referenced_issue }
let(:service) { double(RelatedIssues::DestroyService, execute: service_response) }
let(:list_service) { double(RelatedIssues::ListService, execute: list_service_response) }
let(:service_response) { { 'message' => 'yay' } }
let(:list_service_response) { [{ 'foo' => 'bar' }] }
let(:current_project_user_role) { :developer }
subject do
......@@ -100,6 +109,10 @@ describe Projects::RelatedIssuesController, type: :controller do
project.team << [user, current_project_user_role]
sign_in user
allow(RelatedIssues::ListService).to receive(:new)
.with(issue, user)
.and_return(list_service)
allow(RelatedIssues::DestroyService).to receive(:new)
.with(related_issue, user)
.and_return(service)
......@@ -129,7 +142,7 @@ describe Projects::RelatedIssuesController, type: :controller do
it 'returns success JSON' do
is_expected.to have_http_status(200)
expect(json_response).to eq(service_response)
expect(json_response).to eq('result' => service_response, 'issues' => list_service_response)
end
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment