Skip to content

Commit

Permalink
feat(free-units): Add free units fields to GraphQL
Browse files Browse the repository at this point in the history
  • Loading branch information
rsempe committed Aug 16, 2022
1 parent 6708045 commit df2ca4a
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/graphql/concerns/charge_model_attributes_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module ChargeModelAttributesHandler
# - Standard model only has one property `amount_cents`
# - Graduated model relies on the the list of `GraduatedRange`
# - Package model has properties `amount_cents`, `package_size` and `free_units`
# - Percentage model has properties `rate`, `fixed_amount`, `free_units_per_events`, `free_units_per_total_aggregation`
def prepare_arguments(arguments)
return arguments if arguments[:charges].blank?

Expand All @@ -27,6 +28,8 @@ def prepare_arguments(arguments)
output[:properties] = {
rate: output[:rate],
fixed_amount: output[:fixed_amount],
free_units_per_events: output[:free_units_per_events],
free_units_per_total_aggregation: output[:free_units_per_total_aggregation],
}
end

Expand All @@ -37,6 +40,8 @@ def prepare_arguments(arguments)
output.delete(:package_size)
output.delete(:rate)
output.delete(:fixed_amount)
output.delete(:free_units_per_events)
output.delete(:free_units_per_total_aggregation)

output
end
Expand Down
2 changes: 2 additions & 0 deletions app/graphql/types/charges/input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Input < Types::BaseInputObject
# NOTE: Percentage charge model
argument :rate, String, required: false
argument :fixed_amount, String, required: false
argument :free_units_per_events, Integer, required: false
argument :free_units_per_total_aggregation, String, required: false
end
end
end
14 changes: 14 additions & 0 deletions app/graphql/types/charges/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class Object < Types::BaseObject
# NOTE: Percentage charge model
field :rate, String, null: true
field :fixed_amount, String, null: true
field :free_units_per_events, Integer, null: true
field :free_units_per_total_aggregation, String, null: true

def amount
return unless object.standard? || object.package?
Expand Down Expand Up @@ -62,6 +64,18 @@ def fixed_amount

object.properties['fixed_amount']
end

def free_units_per_events
return unless object.percentage?

object.properties['free_units_per_events']
end

def free_units_per_total_aggregation
return unless object.percentage?

object.properties['free_units_per_total_aggregation']
end
end
end
end
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ en:
invalid_package_size: invalid_package_size
invalid_rate: invalid_rate
invalid_fixed_amount: invalid_fixed_amount
invalid_free_units_per_events: invalid_free_units_per_events
invalid_free_units_per_total_aggregation: invalid_free_units_per_total_aggregation
invalid_content_type: invalid_content_type
invalid_size: invalid_size
value_already_exists: value_already_exists
4 changes: 4 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ type Charge {
createdAt: ISO8601DateTime!
fixedAmount: String
freeUnits: Int
freeUnitsPerEvents: Int
freeUnitsPerTotalAggregation: String
graduatedRanges: [GraduatedRange!]
id: ID!
packageSize: Int
Expand All @@ -150,6 +152,8 @@ input ChargeInput {
chargeModel: ChargeModelEnum!
fixedAmount: String
freeUnits: Int
freeUnitsPerEvents: Int
freeUnitsPerTotalAggregation: String
graduatedRanges: [GraduatedRangeInput!]
id: ID
packageSize: Int
Expand Down
52 changes: 52 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,34 @@

]
},
{
"name": "freeUnitsPerEvents",
"description": null,
"type": {
"kind": "SCALAR",
"name": "Int",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null,
"args": [

]
},
{
"name": "freeUnitsPerTotalAggregation",
"description": null,
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null,
"args": [

]
},
{
"name": "graduatedRanges",
"description": null,
Expand Down Expand Up @@ -1573,6 +1601,30 @@
"defaultValue": null,
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "freeUnitsPerEvents",
"description": null,
"type": {
"kind": "SCALAR",
"name": "Int",
"ofType": null
},
"defaultValue": null,
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "freeUnitsPerTotalAggregation",
"description": null,
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"defaultValue": null,
"isDeprecated": false,
"deprecationReason": null
}
],
"enumValues": null
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/charge_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
properties do
{
rate: '0.0555',
fixed_amount: '2',
fixed_amount: '2'
}
end
end
Expand Down
2 changes: 2 additions & 0 deletions spec/graphql/mutations/plans/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
chargeModel: 'percentage',
rate: '0.25',
fixedAmount: '2',
freeUnitsPerEvents: 5,
freeUnitsPerTotalAggregation: '50',
},
{
billableMetricId: billable_metrics.last.id,
Expand Down
2 changes: 2 additions & 0 deletions spec/graphql/mutations/plans/update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
chargeModel: 'percentage',
rate: '0.25',
fixedAmount: '2',
freeUnitsPerEvents: 5,
freeUnitsPerTotalAggregation: '50',
},
{
billableMetricId: billable_metrics.last.id,
Expand Down
4 changes: 4 additions & 0 deletions spec/models/charge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@
message: [
:invalid_rate,
:invalid_fixed_amount,
:invalid_free_units_per_events,
:invalid_free_units_per_total_aggregation,
],
)
end
Expand All @@ -187,6 +189,8 @@
expect(charge.errors.messages.keys).to include(:properties)
expect(charge.errors.messages[:properties]).to include('invalid_rate')
expect(charge.errors.messages[:properties]).to include('invalid_fixed_amount')
expect(charge.errors.messages[:properties]).to include('invalid_free_units_per_events')
expect(charge.errors.messages[:properties]).to include('invalid_free_units_per_total_aggregation')

expect(Charges::Validators::PercentageService).to have_received(:new)
.with(charge: charge)
Expand Down

0 comments on commit df2ca4a

Please sign in to comment.