-
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.
feat: T-983-api-create-a-subscription-graphql-mutation (#63)
* feat: T-983-api-create-a-subscription-graphql-mutation * remove useless file
- Loading branch information
Showing
6 changed files
with
249 additions
and
13 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,31 @@ | ||
# frozen_string_literal: true | ||
|
||
module Mutations | ||
module Subscriptions | ||
class Create < BaseMutation | ||
include AuthenticableApiUser | ||
include RequiredOrganization | ||
|
||
graphql_name 'CreateSubscription' | ||
description 'Create a new Subscription' | ||
|
||
argument :customer_id, String, required: true | ||
argument :plan_code, String, required: true | ||
|
||
type Types::Subscriptions::Object | ||
|
||
def resolve(**args) | ||
validate_organization! | ||
|
||
result = SubscriptionsService | ||
.new | ||
.create( | ||
organization: current_organization, | ||
params: args, | ||
) | ||
|
||
result.success? ? result.subscription : execution_error(code: result.error_code, message: result.error) | ||
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
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,85 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Mutations::Plans::Create, type: :graphql do | ||
let(:membership) { create(:membership) } | ||
let(:organization) { membership.organization } | ||
let(:plan) { create(:plan, organization: organization) } | ||
let(:customer) { create(:customer, organization: organization) } | ||
let(:mutation) do | ||
<<~GQL | ||
mutation($input: CreateSubscriptionInput!) { | ||
createSubscription(input: $input) { | ||
id, | ||
status, | ||
startedAt, | ||
customer { | ||
id | ||
}, | ||
plan { | ||
id | ||
} | ||
} | ||
} | ||
GQL | ||
end | ||
|
||
it 'creates a subscription' do | ||
result = execute_graphql( | ||
current_user: membership.user, | ||
current_organization: organization, | ||
query: mutation, | ||
variables: { | ||
input: { | ||
customerId: customer.customer_id, | ||
planCode: plan.code, | ||
}, | ||
}, | ||
) | ||
|
||
result_data = result['data']['createSubscription'] | ||
|
||
aggregate_failures do | ||
expect(result_data['id']).to be_present | ||
expect(result_data['status'].to_sym).to eq(:active) | ||
expect(result_data['startedAt']).to be_present | ||
expect(result_data['customer']['id']).to eq(customer.id) | ||
expect(result_data['plan']['id']).to eq(plan.id) | ||
end | ||
end | ||
|
||
context 'without current user' do | ||
it 'returns an error' do | ||
result = execute_graphql( | ||
current_organization: membership.organization, | ||
query: mutation, | ||
variables: { | ||
input: { | ||
customerId: customer.customer_id, | ||
planCode: plan.code, | ||
}, | ||
}, | ||
) | ||
|
||
expect_unauthorized_error(result) | ||
end | ||
end | ||
|
||
context 'without current organization' do | ||
it 'returns an error' do | ||
result = execute_graphql( | ||
current_user: membership.user, | ||
query: mutation, | ||
variables: { | ||
input: { | ||
customerId: customer.customer_id, | ||
planCode: plan.code, | ||
}, | ||
}, | ||
) | ||
|
||
expect_forbidden_error(result) | ||
end | ||
end | ||
end |