From c7a799af1e1ea6d3bd8c09f6ec17ccd39e2f7d66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Geonizeli?= Date: Sun, 5 Sep 2021 22:32:15 -0300 Subject: [PATCH] add createDepositOrder mutation --- .../create_deposit_order_attributes_input.rb | 6 ++++ app/graphql/mutations/create_deposit_order.rb | 19 +++++++++++ app/graphql/types/mutation_type.rb | 1 + app/javascript/__generated__/schema.graphql | 33 +++++++++++++++++++ app/services/build_deposit_order.rb | 19 +++++++++++ 5 files changed, 78 insertions(+) create mode 100644 app/graphql/inputs/create_deposit_order_attributes_input.rb create mode 100644 app/graphql/mutations/create_deposit_order.rb create mode 100644 app/services/build_deposit_order.rb diff --git a/app/graphql/inputs/create_deposit_order_attributes_input.rb b/app/graphql/inputs/create_deposit_order_attributes_input.rb new file mode 100644 index 0000000..245eac7 --- /dev/null +++ b/app/graphql/inputs/create_deposit_order_attributes_input.rb @@ -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 diff --git a/app/graphql/mutations/create_deposit_order.rb b/app/graphql/mutations/create_deposit_order.rb new file mode 100644 index 0000000..8be66e6 --- /dev/null +++ b/app/graphql/mutations/create_deposit_order.rb @@ -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 diff --git a/app/graphql/types/mutation_type.rb b/app/graphql/types/mutation_type.rb index c4eee07..3458594 100644 --- a/app/graphql/types/mutation_type.rb +++ b/app/graphql/types/mutation_type.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true module Types class MutationType < Types::BaseObject + field :create_deposit_order, mutation: Mutations::CreateDepositOrder field :create_stake_remove_order, mutation: Mutations::CreateStakeRemoveOrder field :create_stake_order, mutation: Mutations::CreateStakeOrder field :create_sell_crypto_order, mutation: Mutations::CreateSellCryptoOrder diff --git a/app/javascript/__generated__/schema.graphql b/app/javascript/__generated__/schema.graphql index fc23c6a..e895691 100644 --- a/app/javascript/__generated__/schema.graphql +++ b/app/javascript/__generated__/schema.graphql @@ -76,6 +76,33 @@ type CreateBuyCryptoOrderPayload { 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 { """ Amount to be paid @@ -233,6 +260,12 @@ type Mutation { """ input: CreateBuyCryptoOrderInput! ): CreateBuyCryptoOrderPayload + createDepositOrder( + """ + Parameters for CreateDepositOrder + """ + input: CreateDepositOrderInput! + ): CreateDepositOrderPayload createSellCryptoOrder( """ Parameters for CreateSellCryptoOrder diff --git a/app/services/build_deposit_order.rb b/app/services/build_deposit_order.rb new file mode 100644 index 0000000..9f19023 --- /dev/null +++ b/app/services/build_deposit_order.rb @@ -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