Class: Jbr::Property

Inherits:
Resource show all
Defined in:
lib/jbr/property.rb

Overview

Where the work happens: one address on a client's file.

Constant Summary collapse

CREATE =

The mutation that adds a property to a client already on file.

<<~GRAPHQL
  mutation propertyCreateMutation($clientId: EncodedId!, $input: PropertyCreateInput!) {
    propertyCreate(clientId: $clientId, input: $input) {
      properties { id }
      userErrors { message }
    }
  }
GRAPHQL
FIELDS =

What Jobber calls each address field, against what a caller passes.

{ street1: :street, city: :city, province: :state, postalCode: :zip }
COORDINATES =

The two Jobber tucks inside the address and a property carries beside the street.

%i[latitude longitude]
SELECTION =

The address as Jobber answers it, asked for wherever a property is read.

"#{FIELDS.keys.join ' '} coordinates { #{COORDINATES.join ' '} }"
MATCHED =

The fields a match is made on. City and state are written but never matched: Jobber holds whatever was typed, so "NC" and "North Carolina" -- or "Winston Salem" and "Winston-Salem" -- would read as two homes. The ZIP already places the home.

%i[street1 postalCode]

Instance Attribute Summary

Attributes inherited from Resource

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#initialize

Constructor Details

This class inherits a constructor from Jbr::Resource

Class Method Details

.address_from(fields = {}) ⇒ Hash

The address as Jobber takes it, from the fields a caller passes.

Parameters:

  • fields (Hash) (defaults to: {})

    any of :street, :city, :state and :zip.

Returns:

  • (Hash)

    the address, without the fields the caller left out.



31
32
33
# File 'lib/jbr/property.rb', line 31

def self.address_from(fields = {})
  FIELDS.to_h { |jobber, ours| [ jobber, fields[ours] ] }.compact
end

.fields_from(address) ⇒ Hash

The fields a caller reads, from the address as Jobber holds it.

Parameters:

  • address (Hash, nil)

    the address Jobber answered, if it answered one.

Returns:

  • (Hash)

    any of :street, :city, :state, :zip, :latitude and :longitude, without the ones Jobber left out.



39
40
41
42
43
44
# File 'lib/jbr/property.rb', line 39

def self.fields_from(address)
  address ||= {}
  coordinates = address['coordinates'] || {}
  FIELDS.to_h { |jobber, ours| [ ours, address[jobber.to_s] ] }.
    merge(COORDINATES.to_h { |ours| [ ours, coordinates[ours.to_s] ] }).compact
end

Instance Method Details

#find_or_create_for(client_id:, address:, existing: []) ⇒ String?

Reach the property at an address, adding one when none of the client's matches.

Parameters:

  • client_id (String)

    the client the property belongs to.

  • address (Hash)

    the fields the work happens at.

  • existing (Array<Hash>) (defaults to: [])

    the properties already on the client's file.

Returns:

  • (String, nil)

    the property ID.



51
52
53
54
55
56
57
58
59
60
# File 'lib/jbr/property.rb', line 51

def find_or_create_for(client_id:, address:, existing: [])
  wanted = self.class.address_from address
  match = existing.find { |property| same_address? wanted, property['address'] }
  return match['id'] if match

  output = @oauth.query CREATE, variables: {
    clientId: client_id, input: { properties: [ { address: wanted } ] },
  }
  (output&.dig('propertyCreate', 'properties')&.first || {})['id']
end