-
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): Mutation to delete a dunning campaign
- Loading branch information
Showing
7 changed files
with
353 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
|
||
module Mutations | ||
module DunningCampaigns | ||
class Destroy < BaseMutation | ||
include AuthenticableApiUser | ||
include RequiredOrganization | ||
|
||
REQUIRED_PERMISSION = "dunning_campaigns:delete" | ||
|
||
graphql_name "DestroyDunningCampaign" | ||
description "Deletes a dunning campaign" | ||
|
||
argument :id, ID, required: true | ||
|
||
field :id, ID, null: true | ||
|
||
def resolve(id:) | ||
dunning_campaign = current_organization.dunning_campaigns.find_by(id:) | ||
result = ::DunningCampaigns::DestroyService.call(dunning_campaign:) | ||
|
||
result.success? ? result.dunning_campaign : result_error(result) | ||
end | ||
end | ||
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
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,30 @@ | ||
# frozen_string_literal: true | ||
|
||
module DunningCampaigns | ||
class DestroyService < BaseService | ||
def initialize(dunning_campaign:) | ||
@dunning_campaign = dunning_campaign | ||
|
||
super | ||
end | ||
|
||
def call | ||
return result.not_found_failure!(resource: "dunning_campaign") unless dunning_campaign | ||
return result.forbidden_failure! unless dunning_campaign.organization.auto_dunning_enabled? | ||
|
||
ActiveRecord::Base.transaction do | ||
dunning_campaign.discard! | ||
dunning_campaign.thresholds.discard_all | ||
|
||
# TODO: Reset counters for customers that were in the dunning campaign | ||
end | ||
|
||
result.dunning_campaign = dunning_campaign | ||
result | ||
end | ||
|
||
private | ||
|
||
attr_reader :dunning_campaign | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,59 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe Mutations::DunningCampaigns::Destroy, type: :graphql do | ||
let(:required_permissions) { "dunning_campaigns:delete" } | ||
let(:membership) { create(:membership, organization:) } | ||
let(:organization) { create(:organization, premium_integrations: ["auto_dunning"]) } | ||
let(:dunning_campaign) { create(:dunning_campaign, organization:) } | ||
|
||
let(:mutation) do | ||
<<-GQL | ||
mutation($input: DestroyDunningCampaignInput!) { | ||
destroyDunningCampaign(input: $input) { | ||
id | ||
} | ||
} | ||
GQL | ||
end | ||
|
||
around { |test| lago_premium!(&test) } | ||
|
||
it_behaves_like "requires current user" | ||
it_behaves_like "requires current organization" | ||
it_behaves_like "requires permission", "dunning_campaigns:delete" | ||
|
||
it "deletes a dunning campaign" do | ||
result = execute_graphql( | ||
current_user: membership.user, | ||
current_organization: organization, | ||
permissions: required_permissions, | ||
query: mutation, | ||
variables: { | ||
input: {id: dunning_campaign.id} | ||
} | ||
) | ||
|
||
data = result["data"]["destroyDunningCampaign"] | ||
expect(data["id"]).to eq(dunning_campaign.id) | ||
end | ||
|
||
context "when dunnign campaign is not found" do | ||
let(:dunning_campaign) { create(:dunning_campaign) } | ||
|
||
it "returns an error" do | ||
result = execute_graphql( | ||
current_user: membership.user, | ||
current_organization: organization, | ||
permissions: required_permissions, | ||
query: mutation, | ||
variables: { | ||
input: {id: dunning_campaign.id} | ||
} | ||
) | ||
|
||
expect_graphql_error(result:, message: "Resource not found") | ||
end | ||
end | ||
end |
Oops, something went wrong.