Skip to content

Commit

Permalink
feat: T 961 api add pay in advance to plan (getlago#55)
Browse files Browse the repository at this point in the history
* feat: T-961-api-add-pay_in_advance-to-plan

* fix plan service spec

* missing new line in Makefile

Co-authored-by: Jérémy Denquin <[email protected]>
  • Loading branch information
jdenquin and Jérémy Denquin authored Apr 6, 2022
1 parent a778701 commit 45a2b24
Show file tree
Hide file tree
Showing 19 changed files with 123 additions and 32 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
RUN_ARGS := $(wordlist 2, $(words $(MAKECMDGOALS)), $(MAKECMDGOALS))

test:
RAILS_ENV=test bundle exec rspec $(RUN_ARGS)
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ touch ./config/master.key
cp .env.dist .env
```

- Install Rubocop locally (required for all developers)
```shell
gem install rubocop rubocop-rspec rubocop-performance rubocop-rails rubocop-thread_safety
```

- Start the database by running `docker-compose up db`
- Prepare the database and run migrations
```shell
Expand Down
1 change: 1 addition & 0 deletions app/graphql/mutations/plans/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Create < BaseMutation
argument :frequency, Types::Plans::FrequencyEnum, required: true
argument :billing_period, Types::Plans::BillingPeriodEnum, required: true
argument :pro_rata, Boolean, required: true
argument :pay_in_advance, Boolean, required: true
argument :amount_cents, Integer, required: true
argument :amount_currency, Types::CurrencyEnum
argument :vat_rate, Float, required: false
Expand Down
1 change: 1 addition & 0 deletions app/graphql/mutations/plans/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Update < BaseMutation
argument :frequency, Types::Plans::FrequencyEnum, required: true
argument :billing_period, Types::Plans::BillingPeriodEnum, required: true
argument :pro_rata, Boolean, required: true
argument :pay_in_advance, Boolean, required: true
argument :amount_cents, Integer, required: true
argument :amount_currency, Types::CurrencyEnum, required: true
argument :vat_rate, Float, required: false
Expand Down
1 change: 1 addition & 0 deletions app/graphql/types/plans/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Object < Types::BaseObject
field :frequency, Types::Plans::FrequencyEnum, null: false
field :billing_period, Types::Plans::BillingPeriodEnum, null: false
field :pro_rata, Boolean, null: false
field :pay_in_advance, Boolean, null: false
field :amount_cents, Integer, null: false
field :amount_currency, Types::CurrencyEnum, null: false
field :vat_rate, Float
Expand Down
6 changes: 4 additions & 2 deletions app/services/plans_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ def create(**args)
frequency: args[:frequency].to_sym,
billing_period: args[:billing_period].to_sym,
pro_rata: args[:pro_rata],
pay_in_advance: args[:pay_in_advance],
amount_cents: args[:amount_cents],
amount_currency: args[:amount_currency],
vat_rate: args[:vat_rate],
trial_period: args[:trial_period]
trial_period: args[:trial_period],
)

# Validates billable metrics
Expand Down Expand Up @@ -44,6 +45,7 @@ def update(**args)
plan.frequency = args[:frequency].to_sym
plan.billing_period = args[:billing_period].to_sym
plan.pro_rata = args[:pro_rata]
plan.pay_in_advance = args[:pay_in_advance]
plan.amount_cents = args[:amount_cents]
plan.amount_currency = args[:amount_currency]
plan.vat_rate = args[:vat_rate]
Expand All @@ -67,7 +69,7 @@ def update(**args)
created_charges_ids.push(created_charge.id)
end

# Note: Delete charges that are no more linked to the plan
# NOTE: Delete charges that are no more linked to the plan
sanitize_charges(plan, args[:charges], created_charges_ids)
end

Expand Down
1 change: 1 addition & 0 deletions config/initializers/money.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
MoneyRails.configure do |config|
config.default_currency = :eur
config.locale_backend = :i18n
config.rounding_mode = BigDecimal::ROUND_HALF_UP
end
7 changes: 7 additions & 0 deletions db/migrate/20220406084258_add_pay_in_advance_on_plans.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddPayInAdvanceOnPlans < ActiveRecord::Migration[7.0]
def change
add_column :plans, :pay_in_advance, :boolean, default: false, null: false
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.

3 changes: 3 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ input CreatePlanInput {
description: String
frequency: PlanFrequency!
name: String!
payInAdvance: Boolean!
proRata: Boolean!
trialPeriod: Float
vatRate: Float
Expand Down Expand Up @@ -337,6 +338,7 @@ type Plan {
id: ID!
name: String!
organization: Organization
payInAdvance: Boolean!
proRata: Boolean!
trialPeriod: Float
updatedAt: ISO8601DateTime!
Expand Down Expand Up @@ -462,6 +464,7 @@ input UpdatePlanInput {
frequency: PlanFrequency!
id: String!
name: String!
payInAdvance: Boolean!
proRata: Boolean!
trialPeriod: Float
vatRate: Float
Expand Down
50 changes: 50 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,22 @@
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "payInAdvance",
"description": null,
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "Boolean",
"ofType": null
}
},
"defaultValue": null,
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "amountCents",
"description": null,
Expand Down Expand Up @@ -2291,6 +2307,24 @@

]
},
{
"name": "payInAdvance",
"description": null,
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "Boolean",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null,
"args": [

]
},
{
"name": "proRata",
"description": null,
Expand Down Expand Up @@ -3220,6 +3254,22 @@
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "payInAdvance",
"description": null,
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "Boolean",
"ofType": null
}
},
"defaultValue": null,
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "amountCents",
"description": null,
Expand Down
1 change: 1 addition & 0 deletions scripts/start.dev.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash

rm -f ./tmp/pids/server.pid
bundle install
rake db:prepare
rails s -b 0.0.0.0
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions spec/factories/plan_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
frequency { 'monthly' }
billing_period { 'beginning_of_period' }
pro_rata { false }
pay_in_advance { false }
amount_cents { 100 }
amount_currency { 'EUR' }
end
Expand Down
5 changes: 5 additions & 0 deletions spec/graphql/mutations/plans/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
frequency,
billingPeriod,
proRata,
payInAdvance,
amountCents,
amountCurrency,
charges { id, billableMetric { id name code } }
Expand All @@ -39,6 +40,7 @@
frequency: 'monthly',
billingPeriod: 'beginning_of_period',
proRata: false,
payInAdvance: false,
amountCents: 200,
amountCurrency: 'EUR',
charges: [
Expand Down Expand Up @@ -73,6 +75,7 @@
expect(result_data['frequency']).to eq('monthly')
expect(result_data['billingPeriod']).to eq('beginning_of_period')
expect(result_data['proRata']).to eq(false)
expect(result_data['payInAdvance']).to eq(false)
expect(result_data['amountCents']).to eq(200)
expect(result_data['amountCurrency']).to eq('EUR')
expect(result_data['charges'].count).to eq(2)
Expand All @@ -91,6 +94,7 @@
frequency: 'monthly',
billingPeriod: 'beginning_of_period',
proRata: false,
payInAdvance: false,
amountCents: 200,
amountCurrency: 'EUR',
charges: [],
Expand All @@ -114,6 +118,7 @@
frequency: 'monthly',
billingPeriod: 'beginning_of_period',
proRata: false,
payInAdvance: false,
amountCents: 200,
amountCurrency: 'EUR',
charges: [],
Expand Down
4 changes: 4 additions & 0 deletions spec/graphql/mutations/plans/update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
frequency,
billingPeriod,
proRata,
payInAdvance,
amountCents,
amountCurrency,
charges { id, billableMetric { id name code } }
Expand All @@ -40,6 +41,7 @@
frequency: 'monthly',
billingPeriod: 'beginning_of_period',
proRata: false,
payInAdvance: false,
amountCents: 200,
amountCurrency: 'EUR',
charges: [
Expand Down Expand Up @@ -74,6 +76,7 @@
expect(result_data['frequency']).to eq('monthly')
expect(result_data['billingPeriod']).to eq('beginning_of_period')
expect(result_data['proRata']).to eq(false)
expect(result_data['payInAdvance']).to eq(false)
expect(result_data['amountCents']).to eq(200)
expect(result_data['amountCurrency']).to eq('EUR')
expect(result_data['charges'].count).to eq(2)
Expand All @@ -92,6 +95,7 @@
frequency: 'monthly',
billingPeriod: 'beginning_of_period',
proRata: false,
payInAdvance: false,
amountCents: 200,
amountCurrency: 'EUR',
charges: [],
Expand Down
10 changes: 5 additions & 5 deletions spec/graphql/resolvers/plans_resolver_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: true
# frozen_string_literal: true

require 'rails_helper'

Expand All @@ -23,7 +23,7 @@
result = execute_graphql(
current_user: membership.user,
current_organization: organization,
query: query
query: query,
)

plans_response = result['data']['plans']
Expand All @@ -43,7 +43,7 @@

expect_graphql_error(
result: result,
message: 'Missing organization id'
message: 'Missing organization id',
)
end
end
Expand All @@ -53,12 +53,12 @@
result = execute_graphql(
current_user: membership.user,
current_organization: create(:organization),
query: query
query: query,
)

expect_graphql_error(
result: result,
message: 'Not in organization'
message: 'Not in organization',
)
end
end
Expand Down
Loading

0 comments on commit 45a2b24

Please sign in to comment.