Skip to content

Commit

Permalink
Merge pull request #120 from goinvo/fermion/upsertProject
Browse files Browse the repository at this point in the history
Adds upsertProject GraphQL mutation
  • Loading branch information
fermion authored Mar 6, 2024
2 parents 2707b7b + 3fea19f commit 1550f7b
Show file tree
Hide file tree
Showing 10 changed files with 365 additions and 11 deletions.
68 changes: 68 additions & 0 deletions app/graphql/mutations/upsert_project.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
module Mutations
class UpsertProject < BaseMutation
description "Create or update a project."

# arguments passed to the `resolve` method
argument :id, ID, required: false, description: "The ID of the project to update."
argument :client_id, ID, required: false, description: "The ID of the client for this project."
argument :name, String, required: false, description: "The name of the project."
argument :status, String, required: false, description: "The status of the assignment."
argument :cost, Float, required: false, description: "The cost of the project."
argument :payment_frequency, String, required: false, description: "The frequency of payment for the project."
argument :starts_on, GraphQL::Types::ISO8601Date, required: false, description: "The date this assignment starts."
argument :ends_on, GraphQL::Types::ISO8601Date, required: false, description: "The date this assignment ends."

# return type from the mutation
type Types::StaffPlan::ProjectType, null: true

def resolve(id: nil, client_id: nil, name: nil, status: nil, cost: nil, payment_frequency: nil, starts_on: nil, ends_on: nil)
current_company = context[:current_company]

# try and find the assignment
project = if id.present?
current_company.projects.find(id)
end

if project.blank?
client = current_company.clients.find_by(id: client_id)

# client must belong to the current company
if client.nil?
context.add_error(
GraphQL::ExecutionError.new("Client not found", extensions: { attribute: "client_id" })
)

return {}
end

project = client.projects.new
end

project.assign_attributes(name:) if name.present?
project.assign_attributes(status:) if status.present?
project.assign_attributes(cost:) if cost.present?
project.assign_attributes(payment_frequency:) if payment_frequency.present?
project.assign_attributes(starts_on:) if starts_on.present?
project.assign_attributes(ends_on:) if ends_on.present?

if project.valid?
project.save!
else
project.errors.group_by_attribute.each do |attribute, errors|
errors.each do |error|
context.add_error(
GraphQL::ExecutionError.new(
error.full_message,
extensions: {
attribute: attribute.to_s,
}
)
)
end
end
end

project
end
end
end
46 changes: 46 additions & 0 deletions app/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,51 @@ type Mutation {
userId: ID!
): Assignment!

"""
Create or update a project.
"""
upsertProject(
"""
The ID of the client for this project.
"""
clientId: ID

"""
The cost of the project.
"""
cost: Float

"""
The date this assignment ends.
"""
endsOn: ISO8601Date

"""
The ID of the project to update.
"""
id: ID

"""
The name of the project.
"""
name: String

"""
The frequency of payment for the project.
"""
paymentFrequency: String

"""
The date this assignment starts.
"""
startsOn: ISO8601Date

"""
The status of the assignment.
"""
status: String
): Project

"""
Create or update a work week record for a StaffPlan user.
"""
Expand Down Expand Up @@ -140,6 +185,7 @@ type Mutation {
type Project {
assignments: [Assignment!]!
client: Client!
cost: Float!
createdAt: ISO8601DateTime!
endsOn: ISO8601Date
id: ID!
Expand Down
1 change: 1 addition & 0 deletions app/graphql/types/mutation_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ class MutationType < Types::BaseObject
field :set_current_company, mutation: Mutations::SetCurrentCompany
field :upsert_work_week, mutation: Mutations::UpsertWorkWeek
field :upsert_assignment, mutation: Mutations::UpsertAssignment
field :upsert_project, mutation: Mutations::UpsertProject
end
end
1 change: 1 addition & 0 deletions app/graphql/types/staff_plan/project_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class ProjectType < Types::BaseObject
field :client, Types::StaffPlan::ClientType, null: false
field :name, String, null: false
field :status, String, null: false
field :cost, Float, null: false
field :payment_frequency, String, null: false
field :starts_on, GraphQL::Types::ISO8601Date, null: true
field :ends_on, GraphQL::Types::ISO8601Date, null: true
Expand Down
8 changes: 4 additions & 4 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class Project < ApplicationRecord
VALID_PAYMENT_FREQUENCIES = [WEEKLY, MONTHLY, FORTNIGHTLY, QUARTERLY, ANNUALLY].freeze

validates :client_id, presence: true
validates :name, presence: true, uniqueness: { case_sensitive: false }
validates :status, presence: true, inclusion: { in: VALID_STATUSES }
validates :cost, presence: true, numericality: { greater_than_or_equal_to: 0.0 }
validates :payment_frequency, presence: true, inclusion: { in: VALID_PAYMENT_FREQUENCIES }
validates :name, presence: true, uniqueness: { scope: :client_id, case_sensitive: false }
validates :status, inclusion: { in: VALID_STATUSES }, allow_blank: true
validates :cost, numericality: { greater_than_or_equal_to: 0.0 }, allow_blank: true
validates :payment_frequency, inclusion: { in: VALID_PAYMENT_FREQUENCIES }, allow_blank: true

def active?
status == ACTIVE
Expand Down
3 changes: 1 addition & 2 deletions app/sidekiq/sync_customer_subscription_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ def perform(id)
company.subscription.stripe_id,
{ items: [
{id: company.subscription.item_id, quantity: subscription_count }
]},
proration_date: Date.today.iso8601
]}
)
end
end
6 changes: 6 additions & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@

factory :company do
name { Faker::Company.name }

after(:build) do |company, _options|
next if company.memberships.any?

company.memberships << build(:membership, company: company)
end
end

factory :membership do
Expand Down
Loading

0 comments on commit 1550f7b

Please sign in to comment.