-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
457 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
module Mutations | ||
module Invites | ||
class Accept < BaseMutation | ||
graphql_name 'AcceptInvite' | ||
description 'Accepts a new Invite' | ||
|
||
argument :email, String, required: true | ||
argument :password, String, required: true | ||
argument :token, String, required: true, description: 'Uniq token of the Invite' | ||
|
||
type Types::Payloads::RegisterUserType | ||
|
||
def resolve(**args) | ||
result = ::Invites::AcceptService.new.call(**args) | ||
|
||
result.success? ? result : result_error(result) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
module Invites | ||
class AcceptService < BaseService | ||
def call(**args) | ||
invite = Invite.find_by(token: args[:token], status: :pending) | ||
return result.not_found_failure!(resource: 'invite') unless invite | ||
|
||
ActiveRecord::Base.transaction do | ||
result = UsersService.new.register_from_invite( | ||
args[:email], | ||
args[:password], | ||
invite.organization_id, | ||
) | ||
|
||
invite.recipient = result.membership | ||
|
||
invite.mark_as_accepted! | ||
|
||
result | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Mutations::Invites::Accept, type: :graphql do | ||
let(:membership) { create(:membership) } | ||
let(:organization) { membership.organization } | ||
let(:password) { Faker::Internet.password } | ||
|
||
let(:mutation) do | ||
<<~GQL | ||
mutation($input: AcceptInviteInput!) { | ||
acceptInvite(input: $input) { | ||
token | ||
user { | ||
id | ||
} | ||
} | ||
} | ||
GQL | ||
end | ||
|
||
describe 'Invite revoke mutation' do | ||
context 'with a new user' do | ||
let(:invite) { create(:invite, organization: organization) } | ||
|
||
it 'accepts the invite ' do | ||
result = execute_graphql( | ||
current_user: membership.user, | ||
current_organization: organization, | ||
query: mutation, | ||
variables: { | ||
input: { | ||
email: invite.email, | ||
password: password, | ||
token: invite.token, | ||
}, | ||
}, | ||
) | ||
|
||
data = result['data']['acceptInvite'] | ||
|
||
expect(data['user']['email']).to eq(invite.email) | ||
expect(data['token']).to be_present | ||
end | ||
end | ||
|
||
context 'when invite is revoked' do | ||
let(:invite) { create(:invite, organization: organization, status: :revoked) } | ||
|
||
it 'returns an error' do | ||
result = execute_graphql( | ||
current_user: membership.user, | ||
current_organization: organization, | ||
query: mutation, | ||
variables: { | ||
input: { | ||
email: invite.email, | ||
password: password, | ||
token: invite.token, | ||
}, | ||
}, | ||
) | ||
|
||
expect(result['errors'].first['extensions']['status']).to eq(404) | ||
expect(result['errors'].first['extensions']['code']).to eq('invite_not_found') | ||
end | ||
end | ||
|
||
context 'when invite is already accepted' do | ||
let(:invite) { create(:invite, organization: organization, status: :accepted) } | ||
|
||
it 'returns an error' do | ||
result = execute_graphql( | ||
current_user: membership.user, | ||
current_organization: organization, | ||
query: mutation, | ||
variables: { | ||
input: { | ||
email: invite.email, | ||
password: password, | ||
token: invite.token, | ||
}, | ||
}, | ||
) | ||
|
||
expect(result['errors'].first['extensions']['status']).to eq(404) | ||
expect(result['errors'].first['extensions']['code']).to eq('invite_not_found') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.