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

Create organization #68

Merged
merged 13 commits into from
Nov 27, 2023
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
37 changes: 37 additions & 0 deletions app/graphql/mutations/register_organization.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

module Mutations
class RegisterOrganization < BaseMutation
null true

type Types::OrganizationType

argument :name, String, required: true
argument :contact_phone, String, required: false
argument :contact_email, String, required: false
argument :street_address, String, required: false
argument :website, String, required: false
argument :city, String, required: true
argument :state, String, required: true
argument :zip, String, required: true
argument :share_address, Boolean, required: false
argument :share_phone, Boolean, required: false
argument :share_email, Boolean, required: false


def resolve(**args)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love how DRY **args makes this 🥳

location = Geocoder.search(address(args))
args[:latitude] = location.first.latitude
args[:longitude] = location.first.longitude
begin
Organization.create!(args)
rescue ActiveRecord::RecordInvalid => error
raise GraphQL::ExecutionError, "#{error.message}"
end
end

def address(args)
[args[:street_address], args[:city], args[:state], args[:zip]].compact.join(', ')
end
end
end
1 change: 1 addition & 0 deletions app/graphql/types/mutation_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
module Types
class MutationType < Types::BaseObject
field :update_aid_request, mutation: Mutations::UpdateAidRequest
field :register_organization, mutation: Mutations::RegisterOrganization
end
end
20 changes: 16 additions & 4 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ class Organization < ApplicationRecord
has_many :aid_requests

validates :name,
:contact_phone,
:contact_email,
:street_address,
:website,
:city,
:state,
:zip,
Expand All @@ -17,4 +13,20 @@ class Organization < ApplicationRecord
:share_phone,
:share_email,
inclusion: [true,false]

validate :contact_info_given

validate :contact_info_shared

def contact_info_given
unless (contact_phone && !contact_phone.empty?) || (contact_email && !contact_email.empty?) || (street_address && !street_address&.empty?)
errors.add(:please_include, "either a phone number, email address, and/or street address")
end
end

def contact_info_shared
unless (street_address && share_address) || (contact_phone && share_phone) || (contact_email && share_email)
errors.add(:please_share, "at least one form of contact so volunteers may get in touch with you.")
end
end
end
Loading