-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dunning): Add DunningCampaigns::CreateService
- Loading branch information
Showing
3 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |