Skip to content

Commit

Permalink
feat: Add ability to soft delete fees (getlago#2233)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunomiguelpinto authored and abdussamadbello committed Aug 8, 2024
1 parent 0a2b2ac commit b55bab3
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 1 deletion.
20 changes: 20 additions & 0 deletions app/controllers/api/v1/fees_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,32 @@ def index
end
end

def destroy
fee = Fee.from_organization(current_organization).find_by(id: params[:id])
result = ::Fees::DestroyService.call(fee:)

if result.success?
render_fee(result.fee)
else
render_error_response(result)
end
end

private

def update_params
params.require(:fee).permit(:payment_status)
end

def render_fee(fee)
render(
json: ::V1::FeeSerializer.new(
fee,
root_name: 'fee'
)
)
end

def index_filters
params.permit(
:fee_type,
Expand Down
3 changes: 3 additions & 0 deletions app/models/fee.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

class Fee < ApplicationRecord
include Currencies
include Discard::Model
self.discard_column = :deleted_at
default_scope -> { kept }

belongs_to :invoice, optional: true
belongs_to :charge, -> { with_discarded }, optional: true
Expand Down
25 changes: 25 additions & 0 deletions app/services/fees/destroy_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module Fees
class DestroyService < BaseService
def initialize(fee:)
@fee = fee

super
end

def call
return result.not_found_failure!(resource: 'fee') unless fee
return result.not_allowed_failure!(code: 'invoiced_fee') if fee.invoice_id

fee.discard!

result.fee = fee
result
end

private

attr_reader :fee
end
end
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
post :estimate_fees, on: :collection
end
resources :applied_coupons, only: %i[create index]
resources :fees, only: %i[show update index]
resources :fees, only: %i[show update index destroy]
resources :invoices, only: %i[create update show index] do
post :download, on: :member
post :void, on: :member
Expand Down
8 changes: 8 additions & 0 deletions db/migrate/20240701184757_add_deleted_at_to_fees.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class AddDeletedAtToFees < ActiveRecord::Migration[7.1]
def change
add_column :fees, :deleted_at, :datetime
add_index :fees, :deleted_at
end
end
2 changes: 2 additions & 0 deletions db/schema.rb

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

28 changes: 28 additions & 0 deletions spec/requests/api/v1/fees_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,34 @@
end
end

describe 'DELETE /fees/:id' do
let(:customer) { create(:customer, organization:) }
let(:subscription) { create(:subscription, customer:) }
let(:update_params) { {payment_status: 'succeeded'} }
let(:fee) do
create(:charge_fee, fee_type: 'charge', pay_in_advance: true, subscription:, invoice: nil)
end

context 'when fee exist' do
it 'deletes the fee' do
delete_with_token(organization, "/api/v1/fees/#{fee.id}")
expect(response).to have_http_status(:ok)
end
end

context 'when fee exist but is attached to an invoice' do
let(:invoice) { create(:invoice, organization:, customer:) }
let(:fee) do
create(:charge_fee, fee_type: 'charge', pay_in_advance: true, subscription:, invoice:)
end

it 'dont delete the fee' do
delete_with_token(organization, "/api/v1/fees/#{fee.id}")
expect(response).to have_http_status(:method_not_allowed)
end
end
end

describe 'GET /fees' do
let(:customer) { create(:customer, organization:) }
let(:subscription) { create(:subscription, customer:) }
Expand Down

0 comments on commit b55bab3

Please sign in to comment.