Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Wallets Migration and GraphQL Endpoints #337

Merged
merged 6 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions app/graphql/mutations/wallets/create.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module Mutations
module Wallets
class Create < BaseMutation
include AuthenticableApiUser
include RequiredOrganization

graphql_name 'CreateCustomerWallet'
description 'Creates a new Customer Wallet'

argument :customer_id, String, required: true
argument :rate_amount, String, required: true
argument :name, String, required: false
argument :paid_credits, String, required: true
argument :granted_credits, String, required: true
argument :expiration_date, GraphQL::Types::ISO8601Date, required: false

type Types::Wallets::Object

def resolve(**args)
# Empty
end
end
end
end
21 changes: 21 additions & 0 deletions app/graphql/mutations/wallets/terminate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module Mutations
module Wallets
class Terminate < BaseMutation
include AuthenticableApiUser
include RequiredOrganization

graphql_name 'TerminateCustomerWallet'
description 'Terminates a new Customer Wallet'

argument :id, String, required: true

type Types::Wallets::Object

def resolve(**args)
# Empty
end
end
end
end
22 changes: 22 additions & 0 deletions app/graphql/mutations/wallets/update.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module Mutations
module Wallets
class Update < BaseMutation
include AuthenticableApiUser
include RequiredOrganization

graphql_name 'UpdateCustomerWallet'
description 'Updates a new Customer Wallet'

argument :id, String, required: true
argument :expiration_date, GraphQL::Types::ISO8601Date, required: false

type Types::Wallets::Object

def resolve(**args)
# Empty
end
end
end
end
22 changes: 22 additions & 0 deletions app/graphql/resolvers/wallet_resolver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module Resolvers
class WalletResolver < Resolvers::BaseResolver
include AuthenticableApiUser
include RequiredOrganization

description 'Query a single wallet'

argument :id, ID, required: true, description: 'Uniq ID of the wallet'

type Types::Wallets::SingleObject, null: true

def resolve(id: nil)
jdenquin marked this conversation as resolved.
Show resolved Hide resolved
validate_organization!

Wallet.find(id)
rescue ActiveRecord::RecordNotFound
not_found_error
end
end
end
36 changes: 36 additions & 0 deletions app/graphql/resolvers/wallets_resolver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal

module Resolvers
class WalletsResolver < Resolvers::BaseResolver
include AuthenticableApiUser
include RequiredOrganization

description 'Query wallets'

argument :ids, [ID], required: false, description: 'List of wallet IDs to fetch'
argument :customer_id, ID, required: true, description: 'Uniq ID of the customer'
argument :page, Integer, required: false
argument :limit, Integer, required: false
argument :status, Types::Wallets::StatusEnum, required: false

type Types::Wallets::Object.collection_type, null: false

def resolve(customer_id: nil, ids: nil, page: nil, limit: nil, status: nil)
validate_organization!

current_customer = Customer.find(customer_id)

wallets = current_customer
.wallets
.page(page)
.limit(limit)

wallets = wallets.where(status: status) if status.present?
wallets = wallets.where(id: ids) if ids.present?

wallets
rescue ActiveRecord::RecordNotFound
not_found_error
end
end
end
4 changes: 4 additions & 0 deletions app/graphql/types/mutation_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,9 @@ class MutationType < Types::BaseObject
field :add_stripe_payment_provider, mutation: Mutations::PaymentProviders::Stripe

field :download_invoice, mutation: Mutations::Invoices::Download

field :create_customer_wallet, mutation: Mutations::Wallets::Create
field :update_customer_wallet, mutation: Mutations::Wallets::Update
field :terminate_customer_wallet, mutation: Mutations::Wallets::Terminate
end
end
2 changes: 2 additions & 0 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ class QueryType < Types::BaseObject
field :plans, resolver: Resolvers::PlansResolver
field :plan, resolver: Resolvers::PlanResolver
field :current_version, resolver: Resolvers::VersionResolver
field :wallets, resolver: Resolvers::WalletsResolver
field :wallet, resolver: Resolvers::WalletResolver
end
end
27 changes: 27 additions & 0 deletions app/graphql/types/wallets/object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

module Types
module Wallets
class Object < Types::BaseObject
graphql_name 'Wallet'

field :id, ID, null: false
field :customer, Types::Customers::Object

field :name, String, null: false
field :status, Types::Wallets::StatusEnum, null: false
field :rate_amount, String, null: false
field :currency, Types::CurrencyEnum, null: false
field :credits_balance, String, null: false
field :balance, String, null: false
field :consumed_credits, String, null: false

field :expiration_date, GraphQL::Types::ISO8601Date, null: true

field :created_at, GraphQL::Types::ISO8601DateTime, null: false
field :updated_at, GraphQL::Types::ISO8601DateTime, null: false
field :last_balance_sync_at, GraphQL::Types::ISO8601DateTime, null: true
field :last_consumed_credit_sync_at, GraphQL::Types::ISO8601DateTime, null: true
end
end
end
9 changes: 9 additions & 0 deletions app/graphql/types/wallets/single_object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module Types
module Wallets
class SingleObject < Types::Wallets::Object
graphql_name 'WalletDetails'
end
end
end
13 changes: 13 additions & 0 deletions app/graphql/types/wallets/status_enum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Types
module Wallets
class StatusEnum < Types::BaseEnum
graphql_name 'WalletStatusEnum'

Wallet::STATUSES.each do |type|
value type
end
end
end
end
1 change: 1 addition & 0 deletions app/models/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Customer < ApplicationRecord
has_many :coupons, through: :applied_coupons
has_many :applied_add_ons
has_many :add_ons, through: :applied_add_ons
has_many :wallets
has_many :payment_provider_customers,
class_name: 'PaymentProviderCustomers::BaseCustomer',
dependent: :destroy
Expand Down
14 changes: 14 additions & 0 deletions app/models/wallet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class Wallet < ApplicationRecord
jdenquin marked this conversation as resolved.
Show resolved Hide resolved
belongs_to :customer

has_one :organization, through: :customer

STATUSES = [
:active,
:terminated,
].freeze

enum status: STATUSES
end
25 changes: 25 additions & 0 deletions db/migrate/20220718083657_create_wallets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

class CreateWallets < ActiveRecord::Migration[7.0]
def change
create_table :wallets, id: :uuid do |t|
t.references :customer, type: :uuid, null: false, foreign_key: true, index: true

t.integer :status, null: false
t.string :currency, null: false

t.string :name
t.decimal :rate_amount, null: false, default: 0, precision: 5
t.decimal :credits_balance, null: false, default: 0, precision: 5
t.decimal :balance, null: false, default: 0, precision: 5
t.decimal :consumed_credits, null: false, default: 0, precision: 5

t.timestamp :expiration_date
t.timestamp :last_balance_sync_at
t.timestamp :last_consumed_credit_at
t.timestamp :terminated_at

t.timestamps
end
end
end
21 changes: 20 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading