Skip to content

Commit

Permalink
feat: Add invoice type to invoice (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-pochet authored Jun 13, 2022
1 parent c295549 commit 5c978a3
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 2 deletions.
11 changes: 11 additions & 0 deletions app/graphql/types/invoices/invoice_type_enum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Types
module Invoices
class InvoiceTypeEnum < Types::BaseEnum
Invoice::INVOICE_TYPES.each do |type|
value type
end
end
end
end
1 change: 1 addition & 0 deletions app/graphql/types/invoices/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Object < Types::BaseObject
field :total_amount_currency, Types::CurrencyEnum, null: false
field :vat_amount_cents, Integer, null: false
field :vat_amount_currency, Types::CurrencyEnum, null: false
field :invoice_type, Types::Invoices::InvoiceTypeEnum, null: false

field :from_date, GraphQL::Types::ISO8601Date, null: false
field :to_date, GraphQL::Types::ISO8601Date, null: false
Expand Down
4 changes: 4 additions & 0 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class Invoice < ApplicationRecord
monetize :amount_cents
monetize :vat_amount_cents

INVOICE_TYPES = %i[subscription add_on].freeze

enum invoice_type: INVOICE_TYPES

sequenced scope: ->(invoice) { invoice.organization.invoices }

validates :from_date, presence: true
Expand Down
1 change: 1 addition & 0 deletions app/serializers/v1/invoice_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def serialize
to_date: model.to_date.iso8601,
charges_from_date: model.to_date.iso8601,
issuing_date: model.issuing_date.iso8601,
invoice_type: model.invoice_type,
amount_cents: model.amount_cents,
amount_currency: model.amount_currency,
vat_amount_cents: model.vat_amount_cents,
Expand Down
1 change: 1 addition & 0 deletions app/services/invoices/add_on_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def create
from_date: date,
to_date: date,
issuing_date: date,
invoice_type: :add_on,
)

create_add_on_fee(invoice)
Expand Down
1 change: 1 addition & 0 deletions app/services/invoices/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def create
to_date: to_date,
charges_from_date: charges_from_date,
issuing_date: issuing_date,
invoice_type: :subscription,
)

create_subscription_fee(invoice) if should_create_subscription_fee?
Expand Down
7 changes: 7 additions & 0 deletions db/migrate/20220613130634_add_invoice_type_to_invoices.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddInvoiceTypeToInvoices < ActiveRecord::Migration[7.0]
def change
add_column :invoices, :invoice_type, :integer, null: false, default: 0
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

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

6 changes: 6 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -1875,6 +1875,7 @@ type Invoice {
createdAt: ISO8601DateTime!
fromDate: ISO8601Date!
id: ID!
invoiceType: InvoiceTypeEnum!
issuingDate: ISO8601Date!
plan: Plan
sequentialId: ID!
Expand All @@ -1887,6 +1888,11 @@ type Invoice {
vatAmountCurrency: CurrencyEnum!
}

enum InvoiceTypeEnum {
add_on
subscription
}

"""
Represents untyped JSON
"""
Expand Down
41 changes: 41 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6551,6 +6551,24 @@

]
},
{
"name": "invoiceType",
"description": null,
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "ENUM",
"name": "InvoiceTypeEnum",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null,
"args": [

]
},
{
"name": "issuingDate",
"description": null,
Expand Down Expand Up @@ -6727,6 +6745,29 @@
"inputFields": null,
"enumValues": null
},
{
"kind": "ENUM",
"name": "InvoiceTypeEnum",
"description": null,
"interfaces": null,
"possibleTypes": null,
"fields": null,
"inputFields": null,
"enumValues": [
{
"name": "subscription",
"description": null,
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "add_on",
"description": null,
"isDeprecated": false,
"deprecationReason": null
}
]
},
{
"kind": "SCALAR",
"name": "JSON",
Expand Down
2 changes: 1 addition & 1 deletion spec/graphql/resolvers/customer_resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
query($customerId: ID!) {
customer(id: $customerId) {
id customerId name
invoices { id }
invoices { id invoiceType }
subscriptions(status: [active]) { id, status }
appliedCoupons { id amountCents amountCurrency coupon { id name } }
appliedAddOns { id amountCents amountCurrency addOn { id name } }
Expand Down
1 change: 1 addition & 0 deletions spec/services/invoices/add_on_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
expect(result.invoice.from_date).to eq(date)
expect(result.invoice.subscription).to eq(subscription)
expect(result.invoice.issuing_date).to eq(date)
expect(result.invoice.invoice_type).to eq('add_on')

expect(result.invoice.amount_cents).to eq(200)
expect(result.invoice.amount_currency).to eq('EUR')
Expand Down
1 change: 1 addition & 0 deletions spec/services/invoices/create_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
expect(result.invoice.from_date).to eq(timestamp - 1.month)
expect(result.invoice.subscription).to eq(subscription)
expect(result.invoice.issuing_date.to_date).to eq(timestamp - 1.day)
expect(result.invoice.invoice_type).to eq('subscription')
expect(result.invoice.fees.subscription_kind.count).to eq(1)
expect(result.invoice.fees.charge_kind.count).to eq(1)

Expand Down

0 comments on commit 5c978a3

Please sign in to comment.