add createStakeOrder mutation

This commit is contained in:
João Geonizeli
2021-08-16 15:14:48 -03:00
parent 17d502430f
commit c1bd034f5a
11 changed files with 335 additions and 152 deletions

View File

@@ -0,0 +1,8 @@
# frozen_string_literal: true
module Inputs
class CreateStakeOrderAttributesInput < Types::BaseInputObject
argument :currency_id, ID, required: true
argument :pool_name, String, required: true
argument :amount, String, "Amount to be paid", required: true
end
end

View File

@@ -0,0 +1,31 @@
# frozen_string_literal: true
module Mutations
class CreateStakeOrder < BaseMutation
field :order, Types::StakeOrderType, null: true
argument :order, Inputs::CreateStakeOrderAttributesInput, required: true
def resolve(order:)
currency_id = decode_id(order[:currency_id])
amount = BigDecimal(order[:amount])
ActiveRecord::Base.transaction do
current_user
.balances
.find_by!(currency_id: currency_id)
.withdrawal!(amount)
record = StakeOrder.create!(
amount: amount,
currency_id: currency_id,
pool_name: order[:pool_name],
user_id: current_user.id,
)
{ order: record }
rescue ActiveRecord::RecordInvalid => e
{ errors: Resolvers::ModelErrors.from_active_record_model(e.record) }
end
end
end
end

View File

@@ -1,6 +1,7 @@
# frozen_string_literal: true
module Types
class MutationType < Types::BaseObject
field :create_stake_order, mutation: Mutations::CreateStakeOrder
field :create_sell_crypto_order, mutation: Mutations::CreateSellCryptoOrder
field :create_buy_crypto_order, mutation: Mutations::CreateBuyCryptoOrder
end