Skip to content

Commit

Permalink
Merge pull request #68 from Refugee-Aid-Capstone/create_organization
Browse files Browse the repository at this point in the history
Create organization
  • Loading branch information
Dav1s-Ops authored Nov 27, 2023
2 parents 0a52e9a + a048dd7 commit f23b538
Show file tree
Hide file tree
Showing 6 changed files with 485 additions and 9 deletions.
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)
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

0 comments on commit f23b538

Please sign in to comment.