add createStakeRemoveMutation

This commit is contained in:
João Geonizeli
2021-08-27 10:39:39 -03:00
parent e4bf220185
commit 3967e182cf
4 changed files with 183 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
# frozen_string_literal: true
module Mutations
class CreateStakeRemoveOrder < 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
record = StakeOrder.find_or_initialize_by(
pool_name: order[:pool_name],
user_id: current_user.id,
currency_id: currency_id,
status: :processing
)
record.amount += amount
record.save!
{ order: record }
rescue ActiveRecord::RecordInvalid => e
{ errors: Resolvers::ModelErrors.from_active_record_model(e.record) }
end
end
end
end