diff --git a/README.md b/README.md index f7e521c..81326eb 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Add the sf gem to your application's Gemfile: For example, ``` -gem 'sf', :git => 'git+ssh://github.internet2.edu/Internet2-TSG/sf.git', :tag => 'v0.1.34' +gem 'sf', :git => 'git+ssh://github.internet2.edu/Internet2-TSG/sf.git', :tag => 'v0.1.65' ``` And then execute: diff --git a/lib/sf/contact.rb b/lib/sf/contact.rb index 9287a76..0ebb477 100644 --- a/lib/sf/contact.rb +++ b/lib/sf/contact.rb @@ -1,206 +1,204 @@ -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', - 'gs_executive__c', 'techex_executive__c', 'gender__c', 'Contact_ID__c'] - FIELDS_SELECT_STR = FIELDS.join(', ') - - def initialize(contact=nil) - @contact = contact.nil? ? Restforce::SObject.new(sobject_type: 'Contact') : contact - @diffs = {} - end +# frozen_string_literal: true + +module Sf + class Contact + include Sf::Base + attr_accessor :diffs, :person + + FIELDS = %w[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 + gs_executive__c techex_executive__c gender__c Contact_ID__c].freeze + 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 + Sf::Account.find self.AccountId.to_s + end - def account_name - account.Name - end + def account_name + account.Name + end - def method_missing(method_name, *args, &block) - @contact.send(method_name, *args, &block) - 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 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 update(args = {}) + params = { 'Id' => self.Id } + args.map { |k, v| params[k] = v } + Sf.client.update!('Contact', params) + end - def update_biography(biography) - Sf.client.update!("Contact", {'Id' => self.Id, 'Biography__c' => biography}) - end + def update_biography(biography) + Sf.client.update!('Contact', { 'Id' => self.Id, 'Biography__c' => biography }) + end - def set_person_by_email(email) - self.person = Sf::Contact.find_by(Email: email) - end + def set_person_by_email(email) + self.person = Sf::Contact.find_by(Email: email) + end - def get_diffs - self.Id.nil? ? flag_all_attrbutes_as_diff : flag_attributes_if_different - end + def get_diffs + self.Id.nil? ? flag_all_attrbutes_as_diff : flag_attributes_if_different + end - def attrs_from_person - sf_attrs = {} - sf_person_mapping.each do |p, sf| - compare_attr(sf_attrs, p, sf) + def attrs_from_person + sf_attrs = {} + sf_person_mapping.each do |p, sf| + compare_attr(sf_attrs, p, sf) + end + sf_attrs end - sf_attrs - end - def create_from_person! - begin - Sf.client.create!("Contact", self.attrs_from_person) + def create_from_person! + Sf.client.create!('Contact', attrs_from_person) rescue Exception => e - self.person.errors[:base] << e.to_s + person.errors[:base] << e.to_s nil end - end - def update_from_person! - begin - update(self.attrs_from_person) if self.Id.present? + def update_from_person! + update(attrs_from_person) if self.Id.present? rescue Exception => e - self.person.errors[:base] << e.to_s + person.errors[:base] << e.to_s nil end - end - def sf_person_mapping - { - 'prefix' => 'Salutation', - 'first_name' => 'FirstName', - 'last_name' => 'LastName', - 'informal_name' => 'Informal_Name__c', - 'gender' => 'gender__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', - 'biography' => 'Biography__c', - 'sf_id' => 'Id', - 'sf_account_id' => 'AccountId' - } - end + def sf_person_mapping + { + 'prefix' => 'Salutation', + 'first_name' => 'FirstName', + 'last_name' => 'LastName', + 'informal_name' => 'Informal_Name__c', + 'gender' => 'gender__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', + 'biography' => 'Biography__c', + 'sf_id' => 'Id', + 'sf_account_id' => 'AccountId' + } + end - def compare_attr(sf_attrs, person_attr, sf_attr) - if person.respond_to?(person_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 + def compare_attr(sf_attrs, person_attr, sf_attr) + if person.respond_to?(person_attr) + person_value = person.send(person_attr) + sf_value = send(sf_attr) + sf_attrs[sf_attr] = person_value if person_value.present? && !person_value.eql?(sf_value) end 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(id) + new Sf.client.find('Contact', id) + end - def self.find_by(args = {}) - where(args).first - 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.inc_execs_for(account_id='') - from = 'Contact_Affiliation__c' - inc_exec_filter = { - Account__c: account_id, Role__c: "Executive Contact", - Program__c: "Trust and Identity", Status__c: "Current" - } - where = inc_exec_filter.map {|k,v| "#{k} = \'#{v}\'"}.join(" and ") - build_contacts(from, where) - 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.inc_execs_for(account_id = '') + from = 'Contact_Affiliation__c' + inc_exec_filter = { + Account__c: account_id, Role__c: 'Executive Contact', + Program__c: 'Trust and Identity', Status__c: 'Current' + } + where = inc_exec_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.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 + 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 ((Actual_End_Date__c = null and Term_End_Date__c >= #{date}) or Actual_End_Date__c >= #{date}) and Contact__c <> null" - build_contacts(from, where) - end + 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.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 + 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 + 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.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 + 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 + private - def self.build_contacts(from, where) - build_collection Sf.client.query( - "SELECT #{Sf::Contact::FIELDS_SELECT_STR} + 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 - def flag_all_attrbutes_as_diff - sf_person_mapping.merge(sf_person_mapping) do |p, sf| - true if person.respond_to?(p) - end.compact - end + def flag_all_attrbutes_as_diff + sf_person_mapping.merge(sf_person_mapping) do |p, _sf| + true if person.respond_to?(p) + end.compact + end - def flag_attributes_if_different - sf_person_mapping.merge(sf_person_mapping) do |p, sf| - true if person.respond_to?(p) and !self.send(sf).eql?(person.send(p)) - end.compact + def flag_attributes_if_different + sf_person_mapping.merge(sf_person_mapping) do |p, sf| + true if person.respond_to?(p) && !send(sf).eql?(person.send(p)) + end.compact + end end - end