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 (edit-draft-invoice): add adjusted fee mutation #1568

Merged
merged 3 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions app/graphql/concerns/execution_error_responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ def not_allowed_error(code:)
)
end

def forbidden_error(code:)
execution_error(
error: 'forbidden',
status: 403,
code:,
)
end

def validation_error(messages:)
execution_error(
error: 'Unprocessable Entity',
Expand All @@ -57,6 +65,8 @@ def result_error(service_result)
not_allowed_error(code: service_result.error.code)
when BaseService::ValidationFailure
validation_error(messages: service_result.error.messages)
when BaseService::ForbiddenFailure
forbidden_error(code: service_result.error.code)
else
execution_error(
error: 'Internal error',
Expand Down
31 changes: 31 additions & 0 deletions app/graphql/mutations/adjusted_fees/create.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

module Mutations
module AdjustedFees
class Create < BaseMutation
include AuthenticableApiUser
include RequiredOrganization

graphql_name 'CreateAdjustedFee'
description 'Creates Adjusted Fee'

input_object_class Types::AdjustedFees::CreateInput

type Types::Fees::Object

def resolve(**args)
validate_organization!

fee = Fee.find_by(id: args[:fee_id])

result = ::AdjustedFees::CreateService.call(
organization: current_organization,
fee:,
params: args,
)

result.success? ? result.fee : result_error(result)
end
end
end
end
14 changes: 14 additions & 0 deletions app/graphql/types/adjusted_fees/create_input.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module Types
module AdjustedFees
class CreateInput < Types::BaseInputObject
description 'Create Adjusted Fee Input'

argument :fee_id, ID, required: true
argument :units, GraphQL::Types::Float, required: true
argument :unit_amount_cents, GraphQL::Types::BigInt, required: false
lovrocolic marked this conversation as resolved.
Show resolved Hide resolved
argument :invoice_display_name, String, required: false
lovrocolic marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
2 changes: 2 additions & 0 deletions app/graphql/types/mutation_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class MutationType < Types::BaseObject
field :destroy_billable_metric, mutation: Mutations::BillableMetrics::Destroy
field :update_billable_metric, mutation: Mutations::BillableMetrics::Update

field :create_adjusted_fee, mutation: Mutations::AdjustedFees::Create

field :create_plan, mutation: Mutations::Plans::Create
field :destroy_plan, mutation: Mutations::Plans::Destroy
field :update_plan, mutation: Mutations::Plans::Update
Expand Down
2 changes: 2 additions & 0 deletions app/models/adjusted_fee.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

class AdjustedFee < ApplicationRecord
belongs_to :invoice
belongs_to :subscription
belongs_to :fee, optional: true
belongs_to :charge, optional: true

enum fee_type: Fee::FEE_TYPES
end
45 changes: 45 additions & 0 deletions app/services/adjusted_fees/create_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

module AdjustedFees
class CreateService < BaseService
def initialize(organization:, fee:, params:)
@organization = organization
@fee = fee
@params = params

super
end

def call
return result.forbidden_failure! if !License.premium? || !fee.invoice.draft?

adjusted_fee = AdjustedFee.new(
fee:,
invoice: fee.invoice,
subscription: fee.subscription,
charge: fee.charge,
adjusted_units: params[:unit_amount_cents]&.blank?,
adjusted_amount: params[:unit_amount_cents]&.present?,
invoice_display_name: params[:invoice_display_name],
fee_type: fee.fee_type,
properties: fee.properties,
units: params[:units],
unit_amount_cents: params[:unit_amount_cents],
)

adjusted_fee.save!

Invoices::RefreshBatchJob.perform_later([fee.invoice_id])

result.adjusted_fee = adjusted_fee
result.fee = fee
result
rescue ActiveRecord::RecordInvalid => e
result.record_validation_failure!(record: e.record)
end

private

attr_reader :organization, :fee, :params
end
end
24 changes: 24 additions & 0 deletions schema.graphql

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

108 changes: 108 additions & 0 deletions schema.json

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

82 changes: 82 additions & 0 deletions spec/graphql/mutations/adjusted_fees/create_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Mutations::AdjustedFees::Create, type: :graphql do
around { |test| lago_premium!(&test) }

let(:membership) { create(:membership) }
let(:fee) { create(:charge_fee) }
let(:input) do
{
feeId: fee.id,
units: 4,
unitAmountCents: 1000,
invoiceDisplayName: 'Hello',
}
end

let(:mutation) do
<<-GQL
mutation($input: CreateAdjustedFeeInput!) {
createAdjustedFee(input: $input) {
id,
units,
invoiceDisplayName
}
}
GQL
end

before { fee.invoice.draft! }

it 'creates an adjusted fee' do
result = execute_graphql(
current_user: membership.user,
current_organization: membership.organization,
query: mutation,
variables: { input: },
)

expect(result['data']['createAdjustedFee']['id']).to eq(fee.id)
end

context 'with finalized invoice' do
before { fee.invoice.finalized! }

it 'returns an error' do
result = execute_graphql(
current_user: membership.user,
current_organization: membership.organization,
query: mutation,
variables: { input: },
)

expect_forbidden_error(result)
end
end

context 'without current user' do
it 'returns an error' do
result = execute_graphql(
current_organization: membership.organization,
query: mutation,
variables: { input: },
)

expect_unauthorized_error(result)
end
end

context 'without current organization' do
it 'returns an error' do
result = execute_graphql(
current_user: membership.user,
query: mutation,
variables: { input: },
)

expect_forbidden_error(result)
end
end
end
Loading
Loading