add createDepositOrder mutation

This commit is contained in:
João Geonizeli
2021-09-05 22:32:15 -03:00
parent 3bedd32502
commit c7a799af1e
5 changed files with 78 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
# frozen_string_literal: true
module Inputs
class CreateDepositOrderAttributesInput < Types::BaseInputObject
argument :amount_cents, Integer, "Amount to be paid", required: true
end
end

View File

@@ -0,0 +1,19 @@
# frozen_string_literal: true
module Mutations
class CreateDepositOrder < BaseMutation
field :order, Types::DepositOrderType, null: true
argument :order, Inputs::CreateStakeOrderAttributesInput, required: true
def resolve(order:)
ActiveRecord::Base.transaction do
record = BuildDepositOrder.new(paid_amount_cents: order[:amount_cents], user: current_user.id)
record.save!
{ 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 # frozen_string_literal: true
module Types module Types
class MutationType < Types::BaseObject class MutationType < Types::BaseObject
field :create_deposit_order, mutation: Mutations::CreateDepositOrder
field :create_stake_remove_order, mutation: Mutations::CreateStakeRemoveOrder field :create_stake_remove_order, mutation: Mutations::CreateStakeRemoveOrder
field :create_stake_order, mutation: Mutations::CreateStakeOrder field :create_stake_order, mutation: Mutations::CreateStakeOrder
field :create_sell_crypto_order, mutation: Mutations::CreateSellCryptoOrder field :create_sell_crypto_order, mutation: Mutations::CreateSellCryptoOrder

View File

@@ -76,6 +76,33 @@ type CreateBuyCryptoOrderPayload {
order: BuyCryptoOrder order: BuyCryptoOrder
} }
"""
Autogenerated input type of CreateDepositOrder
"""
input CreateDepositOrderInput {
"""
A unique identifier for the client performing the mutation.
"""
clientMutationId: String
order: CreateStakeOrderAttributesInput!
}
"""
Autogenerated return type of CreateDepositOrder
"""
type CreateDepositOrderPayload {
"""
A unique identifier for the client performing the mutation.
"""
clientMutationId: String
"""
Errors encountered during execution of the mutation.
"""
errors: [RecordInvalid!]
order: DepositOrder
}
input CreateSellCryptoOrderAttributesInput { input CreateSellCryptoOrderAttributesInput {
""" """
Amount to be paid Amount to be paid
@@ -233,6 +260,12 @@ type Mutation {
""" """
input: CreateBuyCryptoOrderInput! input: CreateBuyCryptoOrderInput!
): CreateBuyCryptoOrderPayload ): CreateBuyCryptoOrderPayload
createDepositOrder(
"""
Parameters for CreateDepositOrder
"""
input: CreateDepositOrderInput!
): CreateDepositOrderPayload
createSellCryptoOrder( createSellCryptoOrder(
""" """
Parameters for CreateSellCryptoOrder Parameters for CreateSellCryptoOrder

View File

@@ -0,0 +1,19 @@
# frozen_string_literal: true
class BuildDepositOrder
DEPOSIT_FEE = 0.05
attr_reader :paid_amount_cents, :user_id
def initilize(paid_amount_cents:, user_id:)
@paid_amount_cents = paid_amount_cents
@user_id = user_id
end
def build
DepositOrder.new(
user_id: user_id,
paid_amount_cents: paid_amount_cents,
received_amount_cents: paid_amount_cents + (paid_amount_cents * DEPOSIT_FEE)
)
end
end