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

misc: Refact some services to the call pattern #2641

Merged
merged 1 commit into from
Sep 30, 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
11 changes: 5 additions & 6 deletions app/controllers/api/v1/add_ons_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ module Api
module V1
class AddOnsController < Api::BaseController
def create
service = AddOns::CreateService.new
result = service.create(
**input_params
result = AddOns::CreateService.call(
input_params
.merge(organization_id: current_organization.id)
.to_h
.symbolize_keys
Expand Down Expand Up @@ -46,7 +45,7 @@ def show
code: params[:code]
)

return not_found_error(resource: 'add_on') unless add_on
return not_found_error(resource: "add_on") unless add_on

render_add_on(add_on)
end
Expand All @@ -61,7 +60,7 @@ def index
json: ::CollectionSerializer.new(
add_ons.includes(:taxes),
::V1::AddOnSerializer,
collection_name: 'add_ons',
collection_name: "add_ons",
meta: pagination_metadata(add_ons),
includes: %i[taxes]
)
Expand All @@ -86,7 +85,7 @@ def render_add_on(add_on)
render(
json: ::V1::AddOnSerializer.new(
add_on,
root_name: 'add_on',
root_name: "add_on",
includes: %i[taxes]
)
)
Expand Down
15 changes: 7 additions & 8 deletions app/controllers/api/v1/billable_metrics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ module Api
module V1
class BillableMetricsController < Api::BaseController
def create
service = ::BillableMetrics::CreateService.new
result = service.create(
result = ::BillableMetrics::CreateService.call(
input_params.merge(organization_id: current_organization.id).to_h.deep_symbolize_keys
)

if result.success?
render(
json: ::V1::BillableMetricSerializer.new(
result.billable_metric,
root_name: 'billable_metric'
root_name: "billable_metric"
)
)
else
Expand All @@ -36,7 +35,7 @@ def update
render(
json: ::V1::BillableMetricSerializer.new(
result.billable_metric,
root_name: 'billable_metric'
root_name: "billable_metric"
)
)
else
Expand All @@ -53,7 +52,7 @@ def destroy
render(
json: ::V1::BillableMetricSerializer.new(
result.billable_metric,
root_name: 'billable_metric'
root_name: "billable_metric"
)
)
else
Expand All @@ -66,12 +65,12 @@ def show
code: params[:code]
)

return not_found_error(resource: 'billable_metric') unless metric
return not_found_error(resource: "billable_metric") unless metric

render(
json: ::V1::BillableMetricSerializer.new(
metric,
root_name: 'billable_metric'
root_name: "billable_metric"
)
)
end
Expand All @@ -87,7 +86,7 @@ def index
json: ::CollectionSerializer.new(
metrics,
::V1::BillableMetricSerializer,
collection_name: 'billable_metrics',
collection_name: "billable_metrics",
meta: pagination_metadata(metrics)
)
)
Expand Down
9 changes: 4 additions & 5 deletions app/controllers/api/v1/coupons_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ module Api
module V1
class CouponsController < Api::BaseController
def create
service = Coupons::CreateService.new
result = service.create(
result = Coupons::CreateService.call(
input_params.merge(organization_id: current_organization.id).to_h
)

Expand Down Expand Up @@ -47,7 +46,7 @@ def show
code: params[:code]
)

return not_found_error(resource: 'coupon') unless coupon
return not_found_error(resource: "coupon") unless coupon

render_coupon(coupon)
end
Expand All @@ -62,7 +61,7 @@ def index
json: ::CollectionSerializer.new(
coupons,
::V1::CouponSerializer,
collection_name: 'coupons',
collection_name: "coupons",
meta: pagination_metadata(coupons)
)
)
Expand Down Expand Up @@ -95,7 +94,7 @@ def render_coupon(coupon)
render(
json: ::V1::CouponSerializer.new(
coupon,
root_name: 'coupon'
root_name: "coupon"
)
)
end
Expand Down
10 changes: 4 additions & 6 deletions app/graphql/mutations/add_ons/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@ class Create < BaseMutation
include AuthenticableApiUser
include RequiredOrganization

REQUIRED_PERMISSION = 'addons:create'
REQUIRED_PERMISSION = "addons:create"

graphql_name 'CreateAddOn'
description 'Creates a new add-on'
graphql_name "CreateAddOn"
description "Creates a new add-on"

input_object_class Types::AddOns::CreateInput

type Types::AddOns::Object

def resolve(**args)
result = ::AddOns::CreateService
.new(context[:current_user])
.create(**args.merge(organization_id: current_organization.id))
result = ::AddOns::CreateService.call(args.merge(organization_id: current_organization.id))

result.success? ? result.add_on : result_error(result)
end
Expand Down
9 changes: 4 additions & 5 deletions app/graphql/mutations/billable_metrics/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@ class Create < BaseMutation
include AuthenticableApiUser
include RequiredOrganization

REQUIRED_PERMISSION = 'billable_metrics:create'
REQUIRED_PERMISSION = "billable_metrics:create"

graphql_name 'CreateBillableMetric'
description 'Creates a new Billable metric'
graphql_name "CreateBillableMetric"
description "Creates a new Billable metric"

input_object_class Types::BillableMetrics::CreateInput

type Types::BillableMetrics::Object

def resolve(**args)
result = ::BillableMetrics::CreateService
.new(context[:current_user])
.create(**args.merge(organization_id: current_organization.id))
.call(**args.merge(organization_id: current_organization.id))

result.success? ? result.billable_metric : result_error(result)
end
Expand Down
10 changes: 4 additions & 6 deletions app/graphql/mutations/coupons/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@ class Create < BaseMutation
include AuthenticableApiUser
include RequiredOrganization

REQUIRED_PERMISSION = 'coupons:create'
REQUIRED_PERMISSION = "coupons:create"

graphql_name 'CreateCoupon'
description 'Creates a new Coupon'
graphql_name "CreateCoupon"
description "Creates a new Coupon"

input_object_class Types::Coupons::CreateInput

type Types::Coupons::Object

def resolve(**args)
result = ::Coupons::CreateService
.new(context[:current_user])
.create(args.merge(organization_id: current_organization.id))
result = ::Coupons::CreateService.call(args.merge(organization_id: current_organization.id))

result.success? ? result.coupon : result_error(result)
end
Expand Down
9 changes: 5 additions & 4 deletions app/graphql/mutations/coupons/terminate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ module Coupons
class Terminate < BaseMutation
include AuthenticableApiUser

REQUIRED_PERMISSION = 'coupons:update'
REQUIRED_PERMISSION = "coupons:update"

graphql_name 'TerminateCoupon'
description 'Deletes a coupon'
graphql_name "TerminateCoupon"
description "Deletes a coupon"

argument :id, ID, required: true

type Types::Coupons::Object

def resolve(id:)
result = ::Coupons::TerminateService.new(context[:current_user]).terminate(id)
coupon = context[:current_user].coupons.find_by(id:)
result = ::Coupons::TerminateService.call(coupon)

result.success? ? result.coupon : result_error(result)
end
Expand Down
4 changes: 2 additions & 2 deletions app/jobs/clock/terminate_coupons_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ module Clock
class TerminateCouponsJob < ApplicationJob
include SentryCronConcern

queue_as 'clock'
queue_as "clock"

def perform
Coupons::TerminateService.new.terminate_all_expired
Coupons::TerminateService.terminate_all_expired
end
end
end
11 changes: 9 additions & 2 deletions app/services/add_ons/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

module AddOns
class CreateService < BaseService
def create(**args)
def initialize(args)
@args = args
super
vincent-pochet marked this conversation as resolved.
Show resolved Hide resolved
end

def call
ActiveRecord::Base.transaction do
add_on = AddOn.create!(
organization_id: args[:organization_id],
Expand Down Expand Up @@ -32,10 +37,12 @@ def create(**args)

private

attr_reader :args

def track_add_on_created(add_on)
SegmentTrackJob.perform_later(
membership_id: CurrentContext.membership,
event: 'add_on_created',
event: "add_on_created",
properties: {
addon_code: add_on.code,
addon_name: add_on.name,
Expand Down
6 changes: 3 additions & 3 deletions app/services/base_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ def service_failure!(code:, message:)
fail_with_error!(ServiceFailure.new(self, code:, error_message: message))
end

def forbidden_failure!(code: 'feature_unavailable')
def forbidden_failure!(code: "feature_unavailable")
fail_with_error!(ForbiddenFailure.new(self, code:))
end

def unauthorized_failure!(message: 'unauthorized')
def unauthorized_failure!(message: "unauthorized")
fail_with_error!(UnauthorizedFailure.new(self, message:))
end

Expand Down Expand Up @@ -186,7 +186,7 @@ def graphql_context?
source&.to_sym == :graphql
end

def at_time_zone(customer: 'customers', organization: 'organizations')
def at_time_zone(customer: "customers", organization: "organizations")
Utils::Timezone.at_time_zone_sql(customer:, organization:)
end
end
11 changes: 9 additions & 2 deletions app/services/billable_metrics/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

module BillableMetrics
class CreateService < BaseService
def create(args)
def initialize(args = {})
@args = args
super
end

def call
organization = Organization.find_by(id: args[:organization_id])

if args[:aggregation_type]&.to_sym == :custom_agg && !organization&.custom_aggregation
Expand Down Expand Up @@ -40,10 +45,12 @@ def create(args)

private

attr_reader :args

def track_billable_metric_created(metric)
SegmentTrackJob.perform_later(
membership_id: CurrentContext.membership,
event: 'billable_metric_created',
event: "billable_metric_created",
properties: {
code: metric.code,
name: metric.name,
Expand Down
17 changes: 11 additions & 6 deletions app/services/coupons/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

module Coupons
class CreateService < BaseService
def create(args)
def initialize(args)
@args = args
super
end

def call
return result unless valid?(args)

@limitations = args[:applies_to]&.to_h&.deep_symbolize_keys || {}
Expand All @@ -29,15 +34,15 @@ def create(args)
)

if plan_identifiers.present? && plans.count != plan_identifiers.count
return result.not_found_failure!(resource: 'plans')
return result.not_found_failure!(resource: "plans")
end

if billable_metric_identifiers.present? && billable_metrics.count != billable_metric_identifiers.count
return result.not_found_failure!(resource: 'billable_metrics')
return result.not_found_failure!(resource: "billable_metrics")
end

if billable_metrics.present? && plans.present?
return result.not_allowed_failure!(code: 'only_one_limitation_type_per_coupon_allowed')
return result.not_allowed_failure!(code: "only_one_limitation_type_per_coupon_allowed")
end

ActiveRecord::Base.transaction do
Expand All @@ -59,12 +64,12 @@ def create(args)

private

attr_reader :limitations, :organization_id
attr_reader :args, :limitations, :organization_id

def track_coupon_created(coupon)
SegmentTrackJob.perform_later(
membership_id: CurrentContext.membership,
event: 'coupon_created',
event: "coupon_created",
properties: {
coupon_code: coupon.code,
coupon_name: coupon.name,
Expand Down
Loading