Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add ability to soft delete fees #2233

Merged
merged 5 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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