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

View File

@@ -4,26 +4,30 @@
#
# Table name: stake_orders
#
# id :bigint not null, primary key
# amount :decimal(20, 10) default(0.0), not null
# pool_name :string not null
# status :string not null
# created_at :datetime not null
# updated_at :datetime not null
# user_id :bigint not null
# id :bigint not null, primary key
# amount :decimal(20, 10) default(0.0), not null
# pool_name :string not null
# status :string not null
# created_at :datetime not null
# updated_at :datetime not null
# currency_id :bigint
# user_id :bigint not null
#
# Indexes
#
# index_stake_orders_on_user_id (user_id)
# index_stake_orders_on_currency_id (currency_id)
# index_stake_orders_on_user_id (user_id)
#
# Foreign Keys
#
# fk_rails_... (currency_id => currencies.id)
# fk_rails_... (user_id => users.id)
#
class StakeOrder < ApplicationRecord
include Processable
belongs_to :user
belongs_to :currency
validates :pool_name, presence: true
validates :amount, presence: true