Skip to content

Commit

Permalink
added roster
Browse files Browse the repository at this point in the history
  • Loading branch information
ij committed Jul 30, 2014
1 parent f602ef2 commit a2f9788
Show file tree
Hide file tree
Showing 8 changed files with 598 additions and 2 deletions.
27 changes: 25 additions & 2 deletions lib/sf.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
require "restforce"
require "sf/version"

require "sf/base"
require "sf/account"
require "sf/contact"
require "sf/contact_affiliation"
require "sf/reg_event"
require "sf/reg_registration"
require "sf/reg_item"
module Sf
# Your code goes here...
class << self
def client
if @client.nil?
if File.exist?("#{::Rails.root.to_s}/config/restforce.yml")
sfdc_auth_config = YAML.load_file("#{::Rails.root.to_s}/config/restforce.yml")
@client = Restforce.new :host => sfdc_auth_config['host'],
:username => sfdc_auth_config['username'],
:password => sfdc_auth_config['password'],
:security_token => sfdc_auth_config['security_token'],
:client_id => sfdc_auth_config['client_id'],
:client_secret => sfdc_auth_config['client_secret']
end
else
@client
end
end
end
end
85 changes: 85 additions & 0 deletions lib/sf/account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
class Sf::Account
include Sf::Base

FIELDS = ['Id', 'Name', 'Member_Type__c', 'Member_Category__c', 'Member_Join_Date__c', 'Status__c', 'Website']
FIELDS_SELECT_STR = FIELDS.join(', ')
MEMBER_TYPES = ['R&E Network Member', 'University Member', 'Industry Member', 'Affiliate Member']
MEMBER_TYPES_STR = MEMBER_TYPES.map{|v| "\'#{v}\'"}.join(', ')

def initialize(account=nil)
@account = account
end

def update(args={})
params = {Id: self.Id}
args.map {|k,v| params[k] = v}
end

def contacts
Sf::Contact.where({AccountId: self.Id, Status__c: 'Active'})
end

def method_missing(method_name, *args, &block)
@account.send(method_name, *args, &block)
end

def respond_to_missing?(method_name, include_private = false)
@account.respond_to?(method_name, include_private) || super
end

# Class Methods
def self.find(id)
new Sf.client.find("Account", id)
end

def self.find_by(args = {})
where = args.map {|k,v| "#{k} = \'#{v}\'"}.join(" and ")
sobject = Sf.client.query("select #{FIELDS_SELECT_STR} from Account where #{where}").first
if sobject.present?
new sobject
end
end

def self.intl_partners
build_collection Sf.client.query("select #{FIELDS_SELECT_STR} from Account where Member_Category__c = 'International Partner' order by Name")
end

def self.members
build_collection Sf.client.query("select #{FIELDS_SELECT_STR} from Account where Member_Type__c IN (#{MEMBER_TYPES_STR}) order by Name")
end

def self.all
build_collection Sf.client.query("select #{FIELDS_SELECT_STR} from Account order by Name")
end

def self.community_orgs
build_collection Sf.client.query(
"select #{FIELDS_SELECT_STR} from Account
where Name='Internet2' or InCommon_Participant__c = true
or Member_Type__c IN (#{MEMBER_TYPES_STR})
or Member_Category__c = 'International Partner'
order by Name"
)
end

def self.incommon_participants
build_collection Sf.client.query("select #{FIELDS_SELECT_STR} from Account where InCommon_Participant__c = true order by Name")
end

def self.internet2
i2 = find_by({Name: "Internet2", Status__c: "Active"})
find i2.Id
end

def self.member?(account_id)
members.map(&:Id).include?(account_id)
end

def self.inc_part?(account_id)
incommon_participants.map(&:Id).include?(account_id)
end

def self.intl_partner?(account_id)
intl_partners.map(&:Id).include?(account_id)
end
end
15 changes: 15 additions & 0 deletions lib/sf/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Sf
module Base
def self.included(base)
base.extend(ClassMathods)
end

module ClassMathods
def build_collection(models)
models.map { |model| new(model) }
end
end

end

end
173 changes: 173 additions & 0 deletions lib/sf/contact.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
class Sf::Contact
include Sf::Base
attr_accessor :diffs, :person

FIELDS = ['Id', 'AccountId', 'Salutation', 'FirstName', 'LastName',
'Title', 'Email', 'HasOptedOutOfEmail', 'Phone', 'MailingStreet',
'MailingCity', 'MailingState', 'MailingPostalCode', 'MailingCountry',
'Status__c', 'Functional_Title__c', 'Informal_Name__c',
'Meeting_Reg_Pre_Population_Opt_Out__c', 'Meeting_Reg_Hold__c']
FIELDS_SELECT_STR = FIELDS.join(', ')

def initialize(contact=nil)
@contact = contact.nil? ? Restforce::SObject.new(sobject_type: 'Contact') : contact
@diffs = {}
end

def account
Sf::Account.find "#{self.AccountId}"
end

def account_name
account.Name
end

def method_missing(method_name, *args, &block)
@contact.send(method_name, *args, &block)
end

def respond_to_missing?(method_name, include_private = false)
@contact.respond_to?(method_name, include_private) || super
end

def update(args={})
params = {'Id' => self.Id}
args.map {|k,v| params[k] = v}
Sf.client.update("Contact", params)
end

def set_person_by_email(email)
self.person = Sf::Contact.find_by(Email: email)
end

def get_diffs
unless self.Id.nil?
sf_person_mapping.each do |p, sf|
diffs[p.to_sym] = true unless self.send(sf).eql? person.send(p)
end
else
sf_person_mapping.each do |p, sf|
diffs[p.to_sym] = true
end
end
diffs
end

def attrs_from_person
sf_attrs = {}
sf_person_mapping.each do |p, sf|
compare_attr(sf_attrs, p, sf)
end
sf_attrs
end

def create_from_person!
Sf.client.create("Contact", self.attrs_from_person)
end

def update_from_person!
update(self.attrs_from_person) if self.Id.present?
end

def sf_person_mapping
{
'prefix' => 'Salutation',
'first_name' => 'FirstName',
'last_name' => 'LastName',
'informal_name' => 'Informal_Name__c',
'title' => 'Title',
'functional_title' => 'Functional_Title__c',
'email' => 'Email',
'phone_number' => 'Phone',
'website' => 'Website',
'street_address' => 'MailingStreet',
'city' => 'MailingCity',
'state' => 'MailingState',
'zip' => 'MailingPostalCode',
'country' => 'MailingCountry',
'exclude_directory' => 'Meeting_Reg_Pre_Population_Opt_Out__c',
'sf_id' => 'Id',
'sf_account_id' => 'AccountId'
}
end

def compare_attr(sf_attrs, person_attr, sf_attr)
person_value = person.send(person_attr)
sf_value = self.send(sf_attr)
if person_value.present? and !person_value.eql?(sf_value)
sf_attrs[sf_attr] = person_value
end
end
def self.find(id)
new Sf.client.find("Contact", id)
end

def self.where(args = {})
where = args.map {|k,v| "#{k} = \'#{v}\'"}.join(" and ")
sobjects = Sf.client.query("select #{FIELDS_SELECT_STR} from Contact where #{where}")
build_collection sobjects unless sobjects.blank?
end

def self.find_by(args = {})
where(args).first
end

def self.internet2_staffs
from = 'Contact_Affiliation__c'
i2 = Sf::Account.internet2
staff_filter = {
Account__c: i2.Id, Role__c: "Staff",
Program__c: "Internet2 Administration", Service__c: "Human Resources",
Status__c: "Current"
}
where = staff_filter.map {|k,v| "#{k} = \'#{v}\'"}.join(" and ")
build_contacts(from, where)
end

def self.internet2_staff?(contact_id)
Sf::Contact.internet2_staffs.map(&:Id).include?(contact_id)
end

def self.board_of_trustees_as_of(date=nil)
from = 'Seat__c'
date ||= Date.today
date = date.strftime('%Y-%m-%d')
where = "Committee__r.Name = 'BOT (Board of Trustees)' and
Start_Date__c <= #{date} and Term_End_Date__c >= #{date} and
Contact__c <> null"
build_contacts(from, where)
end

def self.board_member_as_of?(contact_id, date=nil)
Sf::Contact.board_of_trustees_as_of(date).map(&:Id).include?(contact_id)
end

def self.advisory_group_members
from = 'Seat__c'
where = "Committee__r.Category__c = 'Advisory Group' and
Term_End_Date__c = null and Actual_End_Date__c = null
and Contact__c <> null"
build_contacts(from, where)
end

def self.advisory_group_member?(contact_id)
Sf::Contact.advisory_group_members.map(&:Id).include?(contact_id)
end

def self.active_functional_title_pick_list
func_titles = Sf.client.picklist_values("Contact", "Functional_Title__c").map{|f| f if f.active}
func_titles.compact.sort{|a,b| a.label <=> b.label}
func_titles
end

private

def self.build_contacts(from, where)
build_collection Sf.client.query(
"SELECT #{Sf::Contact::FIELDS_SELECT_STR}
FROM Contact
WHERE Id
IN (SELECT Contact__c FROM #{from} where #{where})"
)
end
end
38 changes: 38 additions & 0 deletions lib/sf/contact_affiliation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Sf::ContactAffiliation
include Sf::Base
API_NAME = 'Contact_Affiliation__c'
#FIELDS = [
# 'Account__c', 'Contact__c', 'Contact_Email__c', 'Contact_Name__c',
# 'End_Date__c', 'Primary__c', 'Program__c', 'Role__c', 'Service__c',
# 'Status__c', 'Title__c'
#]
FIELDS = [
'Account__c', 'Contact__c', 'Contact_Email__c', 'Contact_Name__c',
'Program__c', 'Role__c', 'Service__c',
'Status__c', 'Title__c'
]
FIELDS_SELECT_STR = FIELDS.join(', ')

def initialize(contact_affiliation = nil)
@contact_affiliation = contact_affiliation
end

def account
new Sf::Account.find("Account", Account__c)
end

def method_missing(method_name, *args, &block)
@contact_affiliation.send(method_name, *args, &block)
end

def respond_to_missing?(method_name, include_private = false)
@contact_affiliation.respond_to?(method_name, include_private) || super
end

def self.where(args = {})
where = args.map {|k,v| "#{k} = \'#{v}\'"}.join(" and ")
sobjects = Sf.client.query("select #{FIELDS_SELECT_STR} from #{API_NAME} where #{where}")
build_collection sobjects unless sobjects.blank?
end

end
Loading

0 comments on commit a2f9788

Please sign in to comment.