add create sell and buy crypto order mutations

This commit is contained in:
João Geonizeli
2021-08-15 01:59:59 -03:00
parent 4b1341677f
commit a3d32ee13a
25 changed files with 688 additions and 15 deletions

View File

@@ -0,0 +1,12 @@
# frozen_string_literal: true
require "rails_helper"
describe Inputs::CreateBuyCryptoOrderAttributesInput do
subject { described_class }
describe "fields" do
it { is_expected.to(accept_argument("currency_id").of_type("ID!")) }
it { is_expected.to(accept_argument("amount_cents").of_type("Int!")) }
end
end

View File

@@ -0,0 +1,12 @@
# frozen_string_literal: true
require "rails_helper"
describe Inputs::CreateSellCryptoOrderAttributesInput do
subject { described_class }
describe "fields" do
it { is_expected.to(accept_argument("currency_id").of_type("ID!")) }
it { is_expected.to(accept_argument("amount").of_type("String!")) }
end
end

View File

@@ -0,0 +1,121 @@
# frozen_string_literal: true
require "rails_helper"
RSpec.describe(Mutations::CreateBuyCryptoOrder, type: :mutation) do
let(:query_string) do
<<~GQL
mutation($currencyId: ID!, $amountCents: Int!) {
createBuyCryptoOrder(input: {
order: {
currencyId: $currencyId,
amountCents: $amountCents,
}
}) {
errors {
fullMessages
fieldName
messages
path
}
order {
status
paidAmountCents
receivedAmount
currency {
name
}
}
}
}
GQL
end
context "when the user has enough balance" do
it "withdraws from his account and creates a buy order" do
currency = create(:currency)
user = create(
:user,
fiat_balance: build(
:fiat_balance,
amount_cents: 100_00
),
)
currency_global_id = GraphQL::Schema::UniqueWithinType.encode("Currency", currency.id)
variables = {
"currencyId": currency_global_id,
"amountCents": 90_00,
}
context = { current_user: user }
result = XStakeSchema.execute(
query_string,
variables: variables,
context: context
).to_h.with_indifferent_access
expect(result).to(eq({
"data" => {
"createBuyCryptoOrder" => {
"errors" => nil,
"order" => {
"status" => "PROCESSING",
"paidAmountCents" => 90_00,
"receivedAmount" => nil,
"currency" => {
"name" => "CAKE",
},
},
},
},
}))
expect(user.fiat_balance.reload.amount_cents).to(eq(10_00))
end
end
context "when the user does not have enough balance" do
it "returns withdrawl error" do
currency = create(:currency)
user = create(
:user,
fiat_balance: build(
:fiat_balance,
amount_cents: 80_00
),
)
currency_global_id = GraphQL::Schema::UniqueWithinType.encode("Currency", currency.id)
variables = {
"currencyId": currency_global_id,
"amountCents": 90_00,
}
context = { current_user: user }
result = XStakeSchema.execute(
query_string,
variables: variables,
context: context
).to_h.with_indifferent_access
expect(result).to(eq({
"data" => {
"createBuyCryptoOrder" => {
"errors" => [{
"fullMessages" => ["Quantia saldo insuficiente"],
"fieldName" => "amount_cents",
"messages" => ["saldo insuficiente"],
"path" => ["attributes", "amount_cents"],
}],
"order" => nil,
},
},
}))
end
end
end

View File

@@ -0,0 +1,121 @@
# frozen_string_literal: true
require "rails_helper"
RSpec.describe(Mutations::CreateSellCryptoOrder, type: :mutation) do
let(:query_string) do
<<~GQL
mutation($currencyId: ID!, $amount: String!) {
createSellCryptoOrder(input: {
order: {
currencyId: $currencyId,
amount: $amount,
}
}) {
errors {
fullMessages
fieldName
messages
path
}
order {
status
paidAmount
receivedAmountCents
currency {
name
}
}
}
}
GQL
end
context "when the user has enough balance" do
it "withdraws from his account and creates a buy order" do
currency = create(:currency)
user = create(
:user,
balances: [
build(:balance, currency: currency, amount: 1.0034),
]
)
currency_global_id = GraphQL::Schema::UniqueWithinType.encode("Currency", currency.id)
variables = {
"currencyId": currency_global_id,
"amount": "0.80",
}
context = { current_user: user }
result = XStakeSchema.execute(
query_string,
variables: variables,
context: context
).to_h.with_indifferent_access
expect(result).to(eq({
"data" => {
"createSellCryptoOrder" => {
"errors" => nil,
"order" => {
"status" => "PROCESSING",
"paidAmount" => "0.8",
"receivedAmountCents" => nil,
"currency" => {
"name" => "CAKE",
},
},
},
},
}))
expect(user.balances.first.reload.amount.to_s).to(eq("0.2034"))
end
end
context "when the user does not have enough balance" do
it "returns withdrawl error" do
currency = create(:currency)
user = create(
:user,
balances: [
build(:balance, currency: currency, amount: 0.0034),
]
)
currency_global_id = GraphQL::Schema::UniqueWithinType.encode("Currency", currency.id)
variables = {
"currencyId": currency_global_id,
"amount": "0.80",
}
context = { current_user: user }
result = XStakeSchema.execute(
query_string,
variables: variables,
context: context
).to_h.with_indifferent_access
expect(result).to(eq({
"data" => {
"createSellCryptoOrder" => {
"errors" => [{
"fullMessages" => ["Quantia saldo insuficiente"],
"fieldName" => "amount",
"messages" => ["saldo insuficiente"],
"path" => ["attributes", "amount"],
}],
"order" => nil,
},
},
}))
expect(user.balances.first.reload.amount.to_s).to(eq("0.0034"))
end
end
end

View File

@@ -0,0 +1,16 @@
# frozen_string_literal: true
require "rails_helper"
RSpec.describe(Types::BuyCryptoOrderType) do
subject { described_class }
describe "arguments" do
it { is_expected.to(have_a_field(:id).of_type("ID!")) }
it { is_expected.to(have_a_field(:currency).of_type("Currency!")) }
it { is_expected.to(have_a_field(:paid_amount_cents).of_type("Int!")) }
it { is_expected.to(have_a_field(:received_amount).of_type("String")) }
it { is_expected.to(have_a_field(:status).of_type("ProcessStatus!")) }
it { is_expected.to(have_a_field(:created_at).of_type("ISO8601DateTime!")) }
it { is_expected.to(have_a_field(:updated_at).of_type("ISO8601DateTime!")) }
end
end

View File

@@ -0,0 +1,9 @@
# frozen_string_literal: true
require "rails_helper"
describe Types::ProcessStatusEnum do
describe "values" do
it { expect(described_class.values.keys).to(match(["PROCESSING", "COMPLETED", "CANCELED"])) }
end
end

View File

@@ -0,0 +1,14 @@
# frozen_string_literal: true
require "rails_helper"
describe Types::RecordInvalidType do
subject { described_class }
describe "fields" do
it { is_expected.to(have_field(:full_messages).of_type("[String!]!")) }
it { is_expected.to(have_field(:field_name).of_type("String")) }
it { is_expected.to(have_field(:messages).of_type("[String!]")) }
it { is_expected.to(have_field(:path).of_type("[String!]")) }
end
end

View File

@@ -0,0 +1,16 @@
# frozen_string_literal: true
require "rails_helper"
RSpec.describe(Types::SellCryptoOrderType) do
subject { described_class }
describe "arguments" do
it { is_expected.to(have_a_field(:id).of_type("ID!")) }
it { is_expected.to(have_a_field(:currency).of_type("Currency!")) }
it { is_expected.to(have_a_field(:paid_amount).of_type("String!")) }
it { is_expected.to(have_a_field(:received_amount_cents).of_type("Int")) }
it { is_expected.to(have_a_field(:status).of_type("ProcessStatus!")) }
it { is_expected.to(have_a_field(:created_at).of_type("ISO8601DateTime!")) }
it { is_expected.to(have_a_field(:updated_at).of_type("ISO8601DateTime!")) }
end
end

View File

@@ -31,7 +31,7 @@ RSpec.describe(User, type: :model) do
describe "associations" do
it { is_expected.to(have_many(:documents)) }
it { is_expected.to(have_one(:balance)) }
it { is_expected.to(have_many(:balances)) }
it { is_expected.to(have_one(:fiat_balance)) }
end
end