Skip to content

Commit

Permalink
feat(dunning): Add DunningCampaigns::CreateService
Browse files Browse the repository at this point in the history
  • Loading branch information
rsempe committed Oct 11, 2024
1 parent b8a5c70 commit 849f6dd
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/graphql/mutations/dunning_campaigns/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class Create < BaseMutation
type Types::DunningCampaigns::Object

def resolve(**args)
# TODO: Implement the resolver via ::DunningCampaigns::CreateService.call
result = ::DunningCampaigns::CreateService.call(organization: current_organization, params: args)
result.success? ? result.dunning_campaign : result_error(result)
end
end
end
Expand Down
34 changes: 34 additions & 0 deletions app/services/dunning_campaigns/create_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

module DunningCampaigns
class CreateService < BaseService
def initialize(organization:, params:)
@organization = organization
@params = params

super
end

def call
dunning_campaign = organization.dunning_campaigns.new(
applied_to_organization: params[:applied_to_organization],
code: params[:code],
days_between_attempts: params[:days_between_attempts],
max_attempts: params[:max_attempts],
name: params[:name],
description: params[:description]
)

dunning_campaign.save!

result.dunning_campaign = dunning_campaign
result
rescue ActiveRecord::RecordInvalid => e
result.record_validation_failure!(record: e.record)
end

private

attr_reader :organization, :params
end
end
52 changes: 52 additions & 0 deletions spec/graphql/mutations/dunning_campaigns/create_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Mutations::DunningCampaigns::Create, type: :graphql do
let(:required_permission) { "dunning_campaigns:create" }
let(:membership) { create(:membership) }
let(:input) do
{
name: "Dunning campaign name",
code: "dunning-campaign-code",
description: "Dunning campaign description",
maxAttempts: 3,
daysBetweenAttempts: 1,
appliedToOrganization: false
}
end

let(:mutation) do
<<-GQL
mutation($input: CreateDunningCampaignInput!) {
createDunningCampaign(input: $input) {
id name code description maxAttempts daysBetweenAttempts appliedToOrganization
}
}
GQL
end

it_behaves_like "requires current user"
it_behaves_like "requires current organization"
it_behaves_like "requires permission", "dunning_campaigns:create"

it "creates a dunning campaign" do
result = execute_graphql(
current_user: membership.user,
current_organization: membership.organization,
permissions: required_permission,
query: mutation,
variables: { input: }
)

expect(result["data"]["createDunningCampaign"]).to include(
"id" => String,
"name" => "Dunning campaign name",
"code" => "dunning-campaign-code",
"description" => "Dunning campaign description",
"maxAttempts" => 3,
"daysBetweenAttempts" => 1,
"appliedToOrganization" => false
)
end
end

0 comments on commit 849f6dd

Please sign in to comment.