Commit 15b02da6 authored by Luke Duncalfe's avatar Luke Duncalfe

Use AwardEmojis services in GraphQL mutations

https://gitlab.com/gitlab-org/gitlab-ce/issues/63372
parent 37b17fa6
...@@ -10,14 +10,11 @@ module Mutations ...@@ -10,14 +10,11 @@ module Mutations
check_object_is_awardable!(awardable) check_object_is_awardable!(awardable)
# TODO this will be handled by AwardEmoji::AddService service = ::AwardEmojis::AddService.new(awardable, args[:name], current_user).execute
# See https://gitlab.com/gitlab-org/gitlab-ce/issues/63372 and
# https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/29782
award = awardable.create_award_emoji(args[:name], current_user)
{ {
award_emoji: (award if award.persisted?), award_emoji: (service[:award] if service[:status] == :success),
errors: errors_on_object(award) errors: service[:errors] || []
} }
end end
end end
......
...@@ -10,22 +10,11 @@ module Mutations ...@@ -10,22 +10,11 @@ module Mutations
check_object_is_awardable!(awardable) check_object_is_awardable!(awardable)
# TODO this check can be removed once AwardEmoji services are available. service = ::AwardEmojis::DestroyService.new(awardable, args[:name], current_user).execute
# See https://gitlab.com/gitlab-org/gitlab-ce/issues/63372 and
# https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/29782
unless awardable.awarded_emoji?(args[:name], current_user)
raise Gitlab::Graphql::Errors::ResourceNotAvailable,
'You have not awarded emoji of type name to the awardable'
end
# TODO this will be handled by AwardEmoji::DestroyService
# See https://gitlab.com/gitlab-org/gitlab-ce/issues/63372 and
# https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/29782
awardable.remove_award_emoji(args[:name], current_user)
{ {
# Mutation response is always a `nil` award_emoji # Mutation response is always a `nil` award_emoji
errors: [] errors: service[:errors] || []
} }
end end
end end
......
...@@ -15,23 +15,15 @@ module Mutations ...@@ -15,23 +15,15 @@ module Mutations
check_object_is_awardable!(awardable) check_object_is_awardable!(awardable)
# TODO this will be handled by AwardEmoji::ToggleService service = ::AwardEmojis::ToggleService.new(awardable, args[:name], current_user).execute
# See https://gitlab.com/gitlab-org/gitlab-ce/issues/63372 and
# https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/29782
award = awardable.toggle_award_emoji(args[:name], current_user)
# Destroy returns a collection :(
award = award.first if award.is_a?(Array)
errors = errors_on_object(award)
toggled_on = awardable.awarded_emoji?(args[:name], current_user) toggled_on = awardable.awarded_emoji?(args[:name], current_user)
{ {
# For consistency with the AwardEmojis::Remove mutation, only return # For consistency with the AwardEmojis::Remove mutation, only return
# the AwardEmoji if it was created and not destroyed # the AwardEmoji if it was created and not destroyed
award_emoji: (award if toggled_on), award_emoji: (service[:award] if toggled_on),
errors: errors, errors: service[:errors] || [],
toggled_on: toggled_on toggled_on: toggled_on
} }
end end
......
...@@ -5,9 +5,9 @@ require 'spec_helper' ...@@ -5,9 +5,9 @@ require 'spec_helper'
describe 'Adding an AwardEmoji' do describe 'Adding an AwardEmoji' do
include GraphqlHelpers include GraphqlHelpers
let(:current_user) { create(:user) } set(:current_user) { create(:user) }
let(:awardable) { create(:note) } set(:project) { create(:project) }
let(:project) { awardable.project } set(:awardable) { create(:note, project: project) }
let(:emoji_name) { 'thumbsup' } let(:emoji_name) { 'thumbsup' }
let(:mutation) do let(:mutation) do
variables = { variables = {
...@@ -43,7 +43,7 @@ describe 'Adding an AwardEmoji' do ...@@ -43,7 +43,7 @@ describe 'Adding an AwardEmoji' do
end end
context 'when the given awardable is not an Awardable' do context 'when the given awardable is not an Awardable' do
let(:awardable) { create(:label) } let(:awardable) { create(:label, project: project) }
it_behaves_like 'a mutation that does not create an AwardEmoji' it_behaves_like 'a mutation that does not create an AwardEmoji'
...@@ -52,7 +52,7 @@ describe 'Adding an AwardEmoji' do ...@@ -52,7 +52,7 @@ describe 'Adding an AwardEmoji' do
end end
context 'when the given awardable is an Awardable but still cannot be awarded an emoji' do context 'when the given awardable is an Awardable but still cannot be awarded an emoji' do
let(:awardable) { create(:system_note) } let(:awardable) { create(:system_note, project: project) }
it_behaves_like 'a mutation that does not create an AwardEmoji' it_behaves_like 'a mutation that does not create an AwardEmoji'
...@@ -73,6 +73,13 @@ describe 'Adding an AwardEmoji' do ...@@ -73,6 +73,13 @@ describe 'Adding an AwardEmoji' do
expect(mutation_response['awardEmoji']['name']).to eq(emoji_name) expect(mutation_response['awardEmoji']['name']).to eq(emoji_name)
end end
describe 'marking Todos as done' do
let(:user) { current_user}
subject { post_graphql_mutation(mutation, current_user: user) }
include_examples 'creating award emojis marks Todos as done'
end
context 'when there were active record validation errors' do context 'when there were active record validation errors' do
before do before do
expect_next_instance_of(AwardEmoji) do |award| expect_next_instance_of(AwardEmoji) do |award|
......
...@@ -5,9 +5,9 @@ require 'spec_helper' ...@@ -5,9 +5,9 @@ require 'spec_helper'
describe 'Toggling an AwardEmoji' do describe 'Toggling an AwardEmoji' do
include GraphqlHelpers include GraphqlHelpers
let(:current_user) { create(:user) } set(:current_user) { create(:user) }
let(:awardable) { create(:note) } set(:project) { create(:project) }
let(:project) { awardable.project } set(:awardable) { create(:note, project: project) }
let(:emoji_name) { 'thumbsup' } let(:emoji_name) { 'thumbsup' }
let(:mutation) do let(:mutation) do
variables = { variables = {
...@@ -40,7 +40,7 @@ describe 'Toggling an AwardEmoji' do ...@@ -40,7 +40,7 @@ describe 'Toggling an AwardEmoji' do
end end
context 'when the given awardable is not an Awardable' do context 'when the given awardable is not an Awardable' do
let(:awardable) { create(:label) } let(:awardable) { create(:label, project: project) }
it_behaves_like 'a mutation that does not create or destroy an AwardEmoji' it_behaves_like 'a mutation that does not create or destroy an AwardEmoji'
...@@ -49,7 +49,7 @@ describe 'Toggling an AwardEmoji' do ...@@ -49,7 +49,7 @@ describe 'Toggling an AwardEmoji' do
end end
context 'when the given awardable is an Awardable but still cannot be awarded an emoji' do context 'when the given awardable is an Awardable but still cannot be awarded an emoji' do
let(:awardable) { create(:system_note) } let(:awardable) { create(:system_note, project: project) }
it_behaves_like 'a mutation that does not create or destroy an AwardEmoji' it_behaves_like 'a mutation that does not create or destroy an AwardEmoji'
...@@ -81,6 +81,13 @@ describe 'Toggling an AwardEmoji' do ...@@ -81,6 +81,13 @@ describe 'Toggling an AwardEmoji' do
expect(mutation_response['toggledOn']).to eq(true) expect(mutation_response['toggledOn']).to eq(true)
end end
describe 'marking Todos as done' do
let(:user) { current_user}
subject { post_graphql_mutation(mutation, current_user: user) }
include_examples 'creating award emojis marks Todos as done'
end
context 'when there were active record validation errors' do context 'when there were active record validation errors' do
before do before do
expect_next_instance_of(AwardEmoji) do |award| expect_next_instance_of(AwardEmoji) do |award|
......
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