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(groups): Expose flat groups to graphql #536

Merged
merged 2 commits into from
Oct 13, 2022
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
7 changes: 7 additions & 0 deletions app/graphql/types/billable_metrics/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Object < Types::BaseObject
field :aggregation_type, Types::BillableMetrics::AggregationTypeEnum, null: false
field :field_name, String, null: true
field :group, GraphQL::Types::JSON, null: true
field :flat_groups, [GraphQL::Types::JSON], null: true

field :created_at, GraphQL::Types::ISO8601DateTime, null: false
field :updated_at, GraphQL::Types::ISO8601DateTime, null: false
Expand All @@ -29,6 +30,12 @@ def can_be_deleted
def group
object.groups_as_tree
end

def flat_groups
object.groups.active.children.map do |group|
{ id: group.id, key: group.parent.value, value: group.value }
end
end
end
end
end
5 changes: 5 additions & 0 deletions app/models/charge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Charge < ApplicationRecord
belongs_to :billable_metric

has_many :fees
has_many :group_properties, dependent: :destroy

CHARGE_MODELS = %i[
standard
Expand All @@ -24,6 +25,10 @@ class Charge < ApplicationRecord
validate :validate_percentage, if: :percentage?
validate :validate_volume, if: :volume?

def properties(group_id: nil)
group_properties.find_by(group_id: group_id)&.values || read_attribute(:properties)
end

private

def validate_amount
Expand Down
3 changes: 3 additions & 0 deletions app/models/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ class Group < ApplicationRecord
belongs_to :billable_metric
belongs_to :parent, class_name: 'Group', foreign_key: 'parent_group_id', optional: true
has_many :children, class_name: 'Group', foreign_key: 'parent_group_id'
has_many :properties, class_name: 'GroupProperty'

STATUS = %i[active inactive].freeze
enum status: STATUS

validates :key, :value, presence: true

scope :parents, -> { where(parent_group_id: nil) }
scope :children, -> { where.not(parent_group_id: nil) }
end
8 changes: 8 additions & 0 deletions app/models/group_property.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class GroupProperty < ApplicationRecord
belongs_to :charge
belongs_to :group

validates :values, presence: true
end
12 changes: 12 additions & 0 deletions db/migrate/20221011133055_create_group_properties.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class CreateGroupProperties < ActiveRecord::Migration[7.0]
def change
create_table :group_properties, id: :uuid do |t|
t.references :charge, type: :uuid, index: true, foreign_key: { on_delete: :cascade }, null: false
t.references :group, type: :uuid, index: true, foreign_key: { on_delete: :cascade }, null: false
t.jsonb :values, null: false, default: {}
t.timestamps
end
end
end
14 changes: 13 additions & 1 deletion db/schema.rb

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

2 changes: 2 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ type BillableMetric {
createdAt: ISO8601DateTime!
description: String
fieldName: String
flatGroups: [JSON!]
group: JSON
id: ID!
name: String!
Expand All @@ -144,6 +145,7 @@ type BillableMetricDetail {
createdAt: ISO8601DateTime!
description: String
fieldName: String
flatGroups: [JSON!]
group: JSON
id: ID!
name: String!
Expand Down
44 changes: 44 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,28 @@

]
},
{
"name": "flatGroups",
"description": null,
"type": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "JSON",
"ofType": null
}
}
},
"isDeprecated": false,
"deprecationReason": null,
"args": [

]
},
{
"name": "group",
"description": null,
Expand Down Expand Up @@ -1264,6 +1286,28 @@

]
},
{
"name": "flatGroups",
"description": null,
"type": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "JSON",
"ofType": null
}
}
},
"isDeprecated": false,
"deprecationReason": null,
"args": [

]
},
{
"name": "group",
"description": null,
Expand Down
12 changes: 11 additions & 1 deletion spec/factories/charges.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
billable_metric
plan

trait :with_group do
after(:build) do |charge|
create(
:group_property,
charge: charge,
values: charge.properties,
)
end
end

factory :standard_charge do
charge_model { 'standard' }
properties do
Expand Down Expand Up @@ -33,7 +43,7 @@
properties do
{
rate: '0.0555',
fixed_amount: '2'
fixed_amount: '2',
}
end
end
Expand Down
11 changes: 11 additions & 0 deletions spec/factories/group_properties.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

FactoryBot.define do
factory :group_property do
charge
group
values do
{ amount: Faker::Number.between(from: 100, to: 500).to_s }
end
end
end
8 changes: 7 additions & 1 deletion spec/graphql/resolvers/billable_metrics_resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<<~GQL
query {
billableMetrics(limit: 5) {
collection { id, name, canBeDeleted }
collection { id, name, canBeDeleted, flatGroups }
metadata { currentPage totalCount }
}
}
Expand All @@ -20,6 +20,9 @@
it 'returns a list of billable metrics' do
metric = create(:billable_metric, organization: organization)

group1 = create(:group, billable_metric: metric, key: 'cloud', value: 'aws')
group2 = create(:group, billable_metric: metric, key: 'region', value: 'usa', parent_group_id: group1.id)

result = execute_graphql(
current_user: membership.user,
current_organization: organization,
Expand All @@ -30,6 +33,9 @@
expect(result['data']['billableMetrics']['collection'].count).to eq(organization.billable_metrics.count)
expect(result['data']['billableMetrics']['collection'].first['id']).to eq(metric.id)
expect(result['data']['billableMetrics']['collection'].first['canBeDeleted']).to eq(true)
expect(result['data']['billableMetrics']['collection'].first['flatGroups']).to eq(
[{ id: group2.id, key: 'aws', value: 'usa' }],
)

expect(result['data']['billableMetrics']['metadata']['currentPage']).to eq(1)
expect(result['data']['billableMetrics']['metadata']['totalCount']).to eq(1)
Expand Down
19 changes: 19 additions & 0 deletions spec/models/charge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@
require 'rails_helper'

RSpec.describe Charge, type: :model do
describe '#properties' do
context 'with group properties' do
it 'returns the group properties' do
charge = create(:standard_charge)
property = create(:group_property, charge: charge, values: { foo: 'bar' })

expect(charge.properties(group_id: property.group_id)).to eq(property.values)
end
end

context 'without group properties' do
it 'returns the charge properties' do
charge = create(:standard_charge)

expect(charge.properties).to eq(charge.properties)
end
end
end

describe '.validate_graduated_range' do
subject(:charge) do
build(:graduated_charge, properties: charge_properties)
Expand Down