add create sell and buy crypto order mutations
This commit is contained in:
@@ -6,8 +6,16 @@ module Mutations
|
||||
input_object_class Types::BaseInputObject
|
||||
object_class Types::BaseObject
|
||||
|
||||
field :errors, [String],
|
||||
field :errors, [Types::RecordInvalidType],
|
||||
null: true,
|
||||
description: "Errors encountered during execution of the mutation."
|
||||
|
||||
def current_user
|
||||
context[:current_user]
|
||||
end
|
||||
|
||||
def decode_id(encoded_id)
|
||||
GraphQL::Schema::UniqueWithinType.decode(encoded_id).last
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
26
app/graphql/mutations/create_buy_crypto_order.rb
Normal file
26
app/graphql/mutations/create_buy_crypto_order.rb
Normal file
@@ -0,0 +1,26 @@
|
||||
# frozen_string_literal: true
|
||||
module Mutations
|
||||
class CreateBuyCryptoOrder < BaseMutation
|
||||
field :order, Types::BuyCryptoOrderType, null: true
|
||||
|
||||
argument :order, Inputs::CreateBuyCryptoOrderAttributesInput, required: true
|
||||
|
||||
def resolve(order:)
|
||||
currency_id = decode_id(order[:currency_id])
|
||||
|
||||
ActiveRecord::Base.transaction do
|
||||
current_user.fiat_balance.withdrawal!(order[:amount_cents])
|
||||
|
||||
record = BuyCryptoOrder.create!(
|
||||
paid_amount_cents: order[:amount_cents],
|
||||
currency_id: currency_id,
|
||||
user_id: current_user.id,
|
||||
)
|
||||
|
||||
{ order: record }
|
||||
rescue ActiveRecord::RecordInvalid => e
|
||||
{ errors: Resolvers::ModelErrors.from_active_record_model(e.record) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
28
app/graphql/mutations/create_sell_crypto_order.rb
Normal file
28
app/graphql/mutations/create_sell_crypto_order.rb
Normal file
@@ -0,0 +1,28 @@
|
||||
# frozen_string_literal: true
|
||||
module Mutations
|
||||
class CreateSellCryptoOrder < BaseMutation
|
||||
field :order, Types::SellCryptoOrderType, null: true
|
||||
|
||||
argument :order, Inputs::CreateSellCryptoOrderAttributesInput, 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 = SellCryptoOrder.create(
|
||||
paid_amount: amount,
|
||||
currency_id: currency_id,
|
||||
user_id: current_user.id,
|
||||
)
|
||||
|
||||
{ order: record }
|
||||
rescue ActiveRecord::RecordInvalid => e
|
||||
{ errors: Resolvers::ModelErrors.from_active_record_model(e.record) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user