Skip to content

Commit

Permalink
feat(dunning): Mutation to delete a dunning campaign
Browse files Browse the repository at this point in the history
  • Loading branch information
rsempe committed Nov 20, 2024
1 parent 851b1c4 commit c181101
Show file tree
Hide file tree
Showing 7 changed files with 353 additions and 0 deletions.
26 changes: 26 additions & 0 deletions app/graphql/mutations/dunning_campaigns/destroy.rb
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
1 change: 1 addition & 0 deletions app/graphql/types/mutation_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ class MutationType < Types::BaseObject
field :okta_login, mutation: Mutations::Auth::Okta::Login

field :create_dunning_campaign, mutation: Mutations::DunningCampaigns::Create
field :destroy_dunning_campaign, mutation: Mutations::DunningCampaigns::Destroy
field :update_dunning_campaign, mutation: Mutations::DunningCampaigns::Update

field :create_api_key, mutation: Mutations::ApiKeys::Create
Expand Down
30 changes: 30 additions & 0 deletions app/services/dunning_campaigns/destroy_service.rb
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
33 changes: 33 additions & 0 deletions schema.graphql

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

127 changes: 127 additions & 0 deletions schema.json

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

59 changes: 59 additions & 0 deletions spec/graphql/mutations/dunning_campaigns/destroy_spec.rb
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
Loading

0 comments on commit c181101

Please sign in to comment.