Skip to content

Commit

Permalink
feat: T-922 Edit billable metric mutation (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-pochet authored Mar 21, 2022
1 parent a0fc8e6 commit 6fd72b9
Show file tree
Hide file tree
Showing 14 changed files with 464 additions and 45 deletions.
28 changes: 28 additions & 0 deletions app/graphql/mutations/billable_metrics/create.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

module Mutations
module BillableMetrics
class Create < BaseMutation
include AuthenticableApiUser

graphql_name 'CreateBillableMetric'

argument :organization_id, String, required: true
argument :name, String, required: true
argument :code, String, required: true
argument :description, String
argument :billable_period, Types::BillableMetrics::BillablePeriodEnum, required: true
argument :aggregation_type, Types::BillableMetrics::AggregationTypeEnum, required: true
argument :pro_rata, GraphQL::Types::Boolean, required: true
argument :properties, GraphQL::Types::JSON

type Types::BillableMetrics::Object

def resolve(**args)
result = BillableMetricsService.new(context[:current_user]).create(**args)

result.success? ? result.billable_metric : execution_error(code: result.error_code, message: result.error)
end
end
end
end
28 changes: 28 additions & 0 deletions app/graphql/mutations/billable_metrics/update.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

module Mutations
module BillableMetrics
class Update < BaseMutation
include AuthenticableApiUser

graphql_name 'UpdateBillableMetric'

argument :id, String, required: true
argument :name, String, required: true
argument :code, String, required: true
argument :description, String
argument :billable_period, Types::BillableMetrics::BillablePeriodEnum, required: true
argument :aggregation_type, Types::BillableMetrics::AggregationTypeEnum, required: true
argument :pro_rata, GraphQL::Types::Boolean, required: true
argument :properties, GraphQL::Types::JSON

type Types::BillableMetrics::Object

def resolve(**args)
result = BillableMetricsService.new(context[:current_user]).update(**args)

result.success? ? result.billable_metric : execution_error(code: result.error_code, message: result.error)
end
end
end
end
24 changes: 0 additions & 24 deletions app/graphql/mutations/create_billable_metric.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/graphql/resolvers/billable_metrics_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class BillableMetricsResolver < GraphQL::Schema::Resolver
argument :page, Integer, required: false
argument :limit, Integer, required: false

type Types::BillableMetrics::BillableMetricObject.collection_type, null: false
type Types::BillableMetrics::Object.collection_type, null: false

def resolve(page: nil, limit: nil)
current_organization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

module Types
module BillableMetrics
class BillableMetricObject < Types::BaseObject
class Object < Types::BaseObject
graphql_name 'BillableMetric'

field :id, ID, null: false
field :organization, Types::OrganizationType

Expand Down
9 changes: 8 additions & 1 deletion app/graphql/types/error_enum.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
# frozen_string_literal: true

module Types
class ErrorEnum < BaseEnum
# Generic errors
value 'internal_error'
value 'unauthorized'
value 'forbidden'
value 'not_found'

# Authentication & authentication errors
value 'token_encoding_error'
value 'expired_jwt_token'
value 'incorrect_login_or_password'
value 'not_organization_member'

value 'incorrect_login_or_password'
# Validation errors
value 'user_already_exists'
end
end
3 changes: 2 additions & 1 deletion app/graphql/types/mutation_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Types
class MutationType < Types::BaseObject
field :login_user, mutation: Mutations::LoginUser
field :register_user, mutation: Mutations::RegisterUser
field :create_billable_metric, mutation: Mutations::CreateBillableMetric
field :create_billable_metric, mutation: Mutations::BillableMetrics::Create
field :update_billable_metric, mutation: Mutations::BillableMetrics::Update
end
end
6 changes: 6 additions & 0 deletions app/services/base_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ def fail!(code, message = nil)
@failure = true
@error_code = code
@error = message || code

# Return self to return result immediately in case of failure:
# ```
# return result.fail!('not_found')
# ```
self
end

private
Expand Down
26 changes: 22 additions & 4 deletions app/services/billable_metrics_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

class BillableMetricsService < BaseService
def create(**args)
if !organization_member?(args[:organization_id])
result.fail!('not_organization_member')
return result
end
return result.fail!('not_organization_member') unless organization_member?(args[:organization_id])

metric = BillableMetric.new(
organization_id: args[:organization_id],
Expand All @@ -18,6 +15,27 @@ def create(**args)
properties: args[:properties]
)

# TODO: better handling of validation errors
metric.save!

result.billable_metric = metric
result
end

def update(**args)
metric = BillableMetric.find_by(id: args[:id])
return result.fail!('not_found') unless metric
return result.fail!('not_organization_member') unless organization_member?(metric.organization_id)

metric.name = args[:name]
metric.code = args[:code]
metric.description = args[:description] if args[:description]
metric.billable_period = args[:billable_period]&.to_sym
metric.aggregation_type = args[:aggregation_type]&.to_sym
metric.pro_rata = args[:pro_rata]
metric.properties = args[:properties] if args[:properties]

# TODO: better handling of validation errors
metric.save!

result.billable_metric = metric
Expand Down
35 changes: 30 additions & 5 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ enum AggregationTypeEnum {
unique_count_agg
}

type BillableMetricObject {
type BillableMetric {
aggregationType: AggregationTypeEnum!
billablePeriod: BillablePeriodEnum!
code: String!
Expand All @@ -19,8 +19,8 @@ type BillableMetricObject {
updatedAt: ISO8601DateTime!
}

type BillableMetricObjectCollection {
collection: [BillableMetricObject!]!
type BillableMetricCollection {
collection: [BillableMetric!]!
metadata: CollectionMetadata!
}

Expand Down Expand Up @@ -97,7 +97,7 @@ type Mutation {
Parameters for CreateBillableMetric
"""
input: CreateBillableMetricInput!
): BillableMetricObject
): BillableMetric
loginUser(
"""
Parameters for LoginUser
Expand All @@ -110,6 +110,12 @@ type Mutation {
"""
input: RegisterUserInput!
): RegisterUser
updateBillableMetric(
"""
Parameters for UpdateBillableMetric
"""
input: UpdateBillableMetricInput!
): BillableMetric
}

type Organization {
Expand All @@ -124,7 +130,7 @@ type Query {
"""
Query billable metrics of an organization
"""
billableMetrics(limit: Int, page: Int): BillableMetricObjectCollection!
billableMetrics(limit: Int, page: Int): BillableMetricCollection!
currentUser: User!
}

Expand All @@ -148,6 +154,25 @@ input RegisterUserInput {
password: String!
}

"""
Autogenerated input type of UpdateBillableMetric
"""
input UpdateBillableMetricInput {
aggregationType: AggregationTypeEnum!
billablePeriod: BillablePeriodEnum!

"""
A unique identifier for the client performing the mutation.
"""
clientMutationId: String
code: String!
description: String!
id: String!
name: String!
proRata: Boolean!
properties: JSON!
}

type User {
createdAt: ISO8601DateTime!
email: String
Expand Down
Loading

0 comments on commit 6fd72b9

Please sign in to comment.