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(dunning): Add DunningCampaignsResolver #2692

Merged
merged 2 commits into from
Oct 17, 2024
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
1 change: 1 addition & 0 deletions app/config/permissions/definition.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ customers:
update:
delete:
dunning_campaigns:
view:
create:
subscriptions:
view: true
Expand Down
1 change: 1 addition & 0 deletions app/config/permissions/role-finance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ customers:
update: false
delete: false
dunning_campaigns:
view: true
create: true
subscriptions:
create: false
Expand Down
1 change: 1 addition & 0 deletions app/config/permissions/role-manager.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ customers:
update: true
delete: true
dunning_campaigns:
view: false
create: false
subscriptions:
create: true
Expand Down
38 changes: 38 additions & 0 deletions app/graphql/resolvers/dunning_campaigns_resolver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

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

description "Query dunning campaigns of an organization"

REQUIRED_PERMISSION = "dunning_campaigns:view"

argument :applied_to_organization, Boolean, required: false
argument :limit, Integer, required: false
argument :order, String, required: false
argument :page, Integer, required: false
argument :search_term, String, required: false

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

def resolve( # rubocop:disable Metrics/ParameterLists
applied_to_organization: nil,
order: nil,
page: nil,
limit: nil,
search_term: nil
)
result = ::DunningCampaignsQuery.call(
organization: current_organization,
search_term:,
order:,
pagination: {page:, limit:},
filters: {applied_to_organization:}
)

result.dunning_campaigns
end
end
end
1 change: 1 addition & 0 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class QueryType < Types::BaseObject
field :customer_portal_wallets, resolver: Resolvers::CustomerPortal::WalletsResolver
field :customer_usage, resolver: Resolvers::Customers::UsageResolver
field :customers, resolver: Resolvers::CustomersResolver
field :dunning_campaigns, resolver: Resolvers::DunningCampaignsResolver
field :events, resolver: Resolvers::EventsResolver
field :google_auth_url, resolver: Resolvers::Auth::Google::AuthUrlResolver
field :gross_revenues, resolver: Resolvers::Analytics::GrossRevenuesResolver
Expand Down
6 changes: 6 additions & 0 deletions app/models/dunning_campaign.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
class DunningCampaign < ApplicationRecord
include PaperTrailTraceable

ORDERS = %w[name code].freeze

belongs_to :organization
has_many :thresholds, class_name: "DunningCampaignThreshold", dependent: :destroy
accepts_nested_attributes_for :thresholds
Expand All @@ -11,6 +13,10 @@ class DunningCampaign < ApplicationRecord
validates :days_between_attempts, numericality: {greater_than: 0}
validates :max_attempts, numericality: {greater_than: 0}
validates :code, uniqueness: {scope: :organization_id}

def self.ransackable_attributes(_auth_object = nil)
%w[name code]
end
end

# == Schema Information
Expand Down
40 changes: 40 additions & 0 deletions app/queries/dunning_campaigns_query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

class DunningCampaignsQuery < BaseQuery
DEFAULT_ORDER = "name"

def call
dunning_campaigns = base_scope.result
dunning_campaigns = paginate(dunning_campaigns)
dunning_campaigns = dunning_campaigns.order(order)

dunning_campaigns = with_applied_to_organization(dunning_campaigns) unless filters.applied_to_organization.nil?

result.dunning_campaigns = dunning_campaigns
result
end

private

def base_scope
DunningCampaign.where(organization:).ransack(search_params)
end

def search_params
return if search_term.blank?

{
m: "or",
name_cont: search_term,
code_cont: search_term
}
end

def order
DunningCampaign::ORDERS.include?(@order) ? @order : DEFAULT_ORDER
end

def with_applied_to_organization(scope)
scope.where(applied_to_organization: filters.applied_to_organization)
end
end
21 changes: 21 additions & 0 deletions schema.graphql

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

152 changes: 152 additions & 0 deletions schema.json

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

Loading