diff --git a/lib/sf/account.rb b/lib/sf/account.rb index 1270b2d..5e1cef0 100644 --- a/lib/sf/account.rb +++ b/lib/sf/account.rb @@ -1,123 +1,125 @@ -class Sf::Account - include Sf::Base +# frozen_string_literal: true - FIELDS = ['Id', 'Name', 'Member_Type__c', 'Member_Category__c', 'MOU_Participant__c', 'Member_Join_Date__c', 'Status__c', 'Website', 'InCommon_Participant__c', 'InCommon_Participation__c', 'iMIS_ID__c', 'Membership_Status__c'] - 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(', ') +module Sf + class Account + include Sf::Base - def initialize(account=nil) - @account = account - end + FIELDS = %w[Id Name Member_Type__c Member_Category__c MOU_Participant__c Member_Join_Date__c + Status__c Website InCommon_Participant__c InCommon_Participation__c iMIS_ID__c Membership_Status__c].freeze + FIELDS_SELECT_STR = FIELDS.join(', ') + MEMBER_TYPES = ['R&E Network Member', 'University Member', 'Industry Member', 'Affiliate Member'].freeze + MEMBER_TYPES_STR = MEMBER_TYPES.map { |v| "\'#{v}\'" }.join(', ') - def update(args={}) - params = {Id: self.Id} - args.map {|k,v| params[k] = v} - end + def initialize(account = nil) + @account = account + end - def contacts - Sf::Contact.where({AccountId: self.Id, Status__c: 'Active'}) - end + def update(args = {}) + params = { Id: self.Id } + args.map { |k, v| params[k] = v } + end - def method_missing(method_name, *args, &block) - @account.send(method_name, *args, &block) - end + def contacts + Sf::Contact.where({ AccountId: self.Id, Status__c: 'Active' }) + end - def respond_to_missing?(method_name, include_private = false) - @account.respond_to?(method_name, include_private) || super - end + def method_missing(method_name, *args, &block) + @account.send(method_name, *args, &block) + end - def inc_execs - Sf::Contact.inc_execs_for(self.Id) - end + def respond_to_missing?(method_name, include_private = false) + @account.respond_to?(method_name, include_private) || super + end - def inc_part_since_year - if self.InCommon_Participant__c - start_date = start_dates_by_subscription["InCommon Participation"] - start_date.split("-")[0] if start_date + def inc_execs + Sf::Contact.inc_execs_for(self.Id) end - end - # Class Methods - def self.find(id) - new Sf.client.find("Account", id) - end + def inc_part_since_year + if self.InCommon_Participant__c + start_date = start_dates_by_subscription['InCommon Participation'] + start_date.split('-')[0] if start_date + end + 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 + # Class Methods + def self.find(id) + new Sf.client.find('Account', id) end - end - def self.intl_partners - build_collection Sf.client.query("select #{FIELDS_SELECT_STR} from Account where MOU_Participant__c = true order by Name") - 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 + new sobject if sobject.present? + end - def self.members - build_collection Sf.client.query("select #{FIELDS_SELECT_STR} from Account where Membership_Status__c = true order by Name") - end + def self.intl_partners + build_collection Sf.client.query("select #{FIELDS_SELECT_STR} from Account where MOU_Participant__c = true order by Name") + end - def self.all - build_collection Sf.client.query("select #{FIELDS_SELECT_STR} from Account order by Name") - end + def self.members + build_collection Sf.client.query("select #{FIELDS_SELECT_STR} from Account where Membership_Status__c = true 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 + def self.community_orgs + build_collection Sf.client.query( + "select #{FIELDS_SELECT_STR} from Account where Name='Internet2' or InCommon_Participant__c = true or Membership_Status__c = true or MOU_Participant__c = true order by Name" - ) - end + ) + 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.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.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.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.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 + def self.intl_partner?(account_id) + intl_partners.map(&:Id).include?(account_id) + end - # Accounts with a closed won opportunity but not yet the "InCommon Control" check box is checked. - def self.inc_parts_to_be - acct_ids = Sf::Opportunity.inc_participations(Date.today.prev_month(2)).map(&:AccountId) - if acct_ids.empty? - [] - else - accts_str = acct_ids.map{|v| "\'#{v}\'"}.join(', ') - build_collection Sf.client.query("select #{FIELDS_SELECT_STR} from Account where InCommon_Participant__c != true and Id in (#{accts_str})") + # Accounts with a closed won opportunity but not yet the "InCommon Control" check box is checked. + def self.inc_parts_to_be + acct_ids = Sf::Opportunity.inc_participations(Date.today.prev_month(2)).map(&:AccountId) + if acct_ids.empty? + [] + else + accts_str = acct_ids.map { |v| "\'#{v}\'" }.join(', ') + build_collection Sf.client.query("select #{FIELDS_SELECT_STR} from Account where InCommon_Participant__c != true and Id in (#{accts_str})") + end end - end - def self.inc_certs_to_be - acct_ids = Sf::Opportunity.cert_subscribers(Date.today.prev_month(2)).map(&:AccountId) - if acct_ids.empty? - [] - else - accts_str = acct_ids.map{|v| "\'#{v}\'"}.join(', ') - build_collection Sf.client.query("select #{FIELDS_SELECT_STR} from Account where InCommon_Participant__c = true and Id in (#{accts_str})") + def self.inc_certs_to_be + acct_ids = Sf::Opportunity.cert_subscribers(Date.today.prev_month(2)).map(&:AccountId) + if acct_ids.empty? + [] + else + accts_str = acct_ids.map { |v| "\'#{v}\'" }.join(', ') + build_collection Sf.client.query("select #{FIELDS_SELECT_STR} from Account where InCommon_Participant__c = true and Id in (#{accts_str})") + end end - end - def self.active_inc_parts(service_name) - today = Date.today.strftime('%Y-%m-%d') - sobjects = Sf.client.query("select + def self.active_inc_parts(service_name) + today = Date.today.strftime('%Y-%m-%d') + sobjects = Sf.client.query("select Supporting_Institution__r.Id, Supporting_Institution__r.Name, Supporting_Institution__r.InCommon_Website_Name__c, @@ -127,11 +129,11 @@ def self.active_inc_parts(service_name) and (Service_End_Date__c = null or Service_End_Date__c > #{today}) and (Service_Start_Date__c = null or Service_Start_Date__c < #{today}) and Do_Not_Publish__c <> true") - build_collection sobjects.map(&:Supporting_Institution__r) unless sobjects.blank? - end + build_collection sobjects.map(&:Supporting_Institution__r) unless sobjects.blank? + end - def self.cert_service_subscribers - sobjects = Sf.client.query("select + def self.cert_service_subscribers + sobjects = Sf.client.query("select Supporting_Institution__r.Id, Supporting_Institution__r.Name, Supporting_Institution__r.InCommon_Website_Name__c, @@ -141,21 +143,21 @@ def self.cert_service_subscribers and Service_End_Date__c = null and (Type_of_Support__c = 'Subscriber' or Type_of_Support__c = 'subscriber_parent') and Do_Not_Publish__c <> true") - build_collection sobjects.map(&:Supporting_Institution__r) unless sobjects.blank? - end + build_collection sobjects.map(&:Supporting_Institution__r) unless sobjects.blank? + end - private + private - def subscriptions - # sorting by Service_Start_Date__c in case of multiple subscriptions of the same service - Sf.client.query("select Service__r.Id, Service__r.Name, Service_Start_Date__c, Service_End_Date__c from Service_Institution__c where Supporting_Institution__c = \'#{self.Id}\' ORDER BY Service_Start_Date__c") - end + def subscriptions + # sorting by Service_Start_Date__c in case of multiple subscriptions of the same service + Sf.client.query("select Service__r.Id, Service__r.Name, Service_Start_Date__c, Service_End_Date__c from Service_Institution__c where Supporting_Institution__c = \'#{self.Id}\' ORDER BY Service_Start_Date__c") + end - # The latest subscription is included only in case of the multiple subscriptions of the same service. - def start_dates_by_subscription - start_dates = {} - subscriptions.each {|s| start_dates[s.Service__r.Name] = s.Service_Start_Date__c} - start_dates + # The latest subscription is included only in case of the multiple subscriptions of the same service. + def start_dates_by_subscription + start_dates = {} + subscriptions.each { |s| start_dates[s.Service__r.Name] = s.Service_Start_Date__c } + start_dates + end end - end diff --git a/lib/sf/base.rb b/lib/sf/base.rb index 4f5cec5..a7ef6dc 100644 --- a/lib/sf/base.rb +++ b/lib/sf/base.rb @@ -1,12 +1,14 @@ +# frozen_string_literal: true + module Sf module Base def self.included(base) base.extend(ClassMathods) - def update(args={}) + def update(args = {}) unless self.Id.blank? - params = {'Id' => self.Id} - args.map {|k,v| params[k] = v} - Sf.client.update!(self.sobject.attributes[:type], params) + params = { 'Id' => self.Id } + args.map { |k, v| params[k] = v } + Sf.client.update!(sobject.attributes[:type], params) end end end @@ -17,18 +19,18 @@ def build_collection(models) end def class_name - obj = self.new - #TODO: migrate it to use obj.sobject, refer Opportunity initialize + obj = new + # TODO: migrate it to use obj.sobject, refer Opportunity initialize cname = obj.sobject[:sobject_type] cname ||= obj.sobject end def find(id) - new Sf.client.find(self.class_name, id) + new Sf.client.find(class_name, id) end def find_by_name(name) - sobject = where({Name: name}).first + sobject = where({ Name: name }).first find(sobject.Id) unless sobject.blank? end @@ -38,13 +40,10 @@ def find_by(args = {}) end def where(args = {}) - where = args.map {|k,v| "#{k} = \'#{v}\'"}.join(" and ") - sobjects = Sf.client.query("select Id, Name from #{self.class_name} where #{where}") + where = args.map { |k, v| "#{k} = \'#{v}\'" }.join(' and ') + sobjects = Sf.client.query("select Id, Name from #{class_name} where #{where}") build_collection sobjects unless sobjects.blank? end - end - end - end diff --git a/lib/sf/contact_affiliation.rb b/lib/sf/contact_affiliation.rb index 86ef16f..219a859 100644 --- a/lib/sf/contact_affiliation.rb +++ b/lib/sf/contact_affiliation.rb @@ -1,38 +1,41 @@ -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(', ') +# frozen_string_literal: true - def initialize(contact_affiliation = nil) - @contact_affiliation = contact_affiliation - end +module Sf + class 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 = %w[ + Account__c Contact__c Contact_Email__c Contact_Name__c + Program__c Role__c Service__c + Status__c Title__c + ].freeze + FIELDS_SELECT_STR = FIELDS.join(', ') - def account - new Sf::Account.find("Account", Account__c) - end + def initialize(contact_affiliation = nil) + @contact_affiliation = contact_affiliation + end - def method_missing(method_name, *args, &block) - @contact_affiliation.send(method_name, *args, &block) - end + def account + new Sf::Account.find('Account', Account__c) + end - def respond_to_missing?(method_name, include_private = false) - @contact_affiliation.respond_to?(method_name, include_private) || super - 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? + 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 - end diff --git a/lib/sf/opportunity.rb b/lib/sf/opportunity.rb index 7111e3c..56f9a70 100644 --- a/lib/sf/opportunity.rb +++ b/lib/sf/opportunity.rb @@ -1,53 +1,58 @@ -class Sf::Opportunity - include Sf::Base - attr_reader :sobject - - FIELDS = [ - 'Id', 'Name', 'AccountId', 'CloseDate', 'IsClosed', 'IsWon', 'StageName', - 'Amount', 'Type', 'Bill_to_Contact__c', 'Priority__c', 'Private__c', - 'Program__c', 'Division__c', 'Sold_to_Contact__c', 'Term__c' - ] - FIELDS_SELECT_STR = FIELDS.join(', ') - - def initialize(sobject=nil) - if sobject.nil? - mash_obj = Restforce::Mash.build({type: 'Opportunity'}, Sf.client) - @sobject = Restforce::SObject.new({attributes: mash_obj}, Sf.client) - else - @sobject = sobject +# frozen_string_literal: true + +module Sf + class Opportunity + include Sf::Base + attr_reader :sobject + + FIELDS = %w[ + Id Name AccountId CloseDate IsClosed IsWon StageName + Amount Type Bill_to_Contact__c Priority__c Private__c + Program__c Division__c Sold_to_Contact__c Term__c + ].freeze + FIELDS_SELECT_STR = FIELDS.join(', ') + + def initialize(sobject = nil) + if sobject.nil? + mash_obj = Restforce::Mash.build({ type: 'Opportunity' }, Sf.client) + @sobject = Restforce::SObject.new({ attributes: mash_obj }, Sf.client) + else + @sobject = sobject + end end - end - def method_missing(method_name, *args, &block) - @sobject.send(method_name, *args, &block) - end - def respond_to_missing?(method_name, include_private = false) - @sobject.respond_to?(method_name, include_private) || super - end + def method_missing(method_name, *args, &block) + @sobject.send(method_name, *args, &block) + end - def self.inc_participations(since_date=nil) - since_date ||=Date.today - since = since_date.to_date.to_s - sobjects = Sf.client.query("select #{FIELDS_SELECT_STR} from Opportunity where Name like '%InCommon Participation%' and IsWon = true and CloseDate >= #{since}") - build_collection sobjects unless sobjects.blank? - end + def respond_to_missing?(method_name, include_private = false) + @sobject.respond_to?(method_name, include_private) || super + end - def self.cert_subscribers(since_date=nil) - since_date ||=Date.today - since = since_date.to_date.to_s - sobjects = Sf.client.query("select #{FIELDS_SELECT_STR} from Opportunity where Name like '%Certificate Service%' and (not Type like '%Existing Business%') and IsWon = true and CloseDate >= #{since}") - build_collection sobjects unless sobjects.blank? - end + def self.inc_participations(since_date = nil) + since_date ||= Date.today + since = since_date.to_date.to_s + sobjects = Sf.client.query("select #{FIELDS_SELECT_STR} from Opportunity where Name like '%InCommon Participation%' and IsWon = true and CloseDate >= #{since}") + build_collection sobjects unless sobjects.blank? + end - def self.eduroam_opportunities - stages = "('Closed Won', 'Negotiation/Review', 'Transferred Internally', 'Proposal/Price Quote', 'Prospecting')" - sobjects = Sf.client.query("select #{FIELDS_SELECT_STR}, Account.Name from Opportunity where Name like '%Trust and Identity - eduroam%' and StageName in #{stages}") - build_collection sobjects unless sobjects.blank? - end + def self.cert_subscribers(since_date = nil) + since_date ||= Date.today + since = since_date.to_date.to_s + sobjects = Sf.client.query("select #{FIELDS_SELECT_STR} from Opportunity where Name like '%Certificate Service%' and (not Type like '%Existing Business%') and IsWon = true and CloseDate >= #{since}") + build_collection sobjects unless sobjects.blank? + end + + def self.eduroam_opportunities + stages = "('Closed Won', 'Negotiation/Review', 'Transferred Internally', 'Proposal/Price Quote', 'Prospecting')" + sobjects = Sf.client.query("select #{FIELDS_SELECT_STR}, Account.Name from Opportunity where Name like '%Trust and Identity - eduroam%' and StageName in #{stages}") + build_collection sobjects unless sobjects.blank? + end - # stages - # "Prospecting", "Closed Won", "Proposal/Price Quote", "Closed Lost", - # "Negotiation/Review", "INACTIVE", "Value Proposition", - # "Transferred Internally", "Referred", "Id. Decision Makers", - # "Needs Analysis", "Qualification", "Perception Analysis" + # stages + # "Prospecting", "Closed Won", "Proposal/Price Quote", "Closed Lost", + # "Negotiation/Review", "INACTIVE", "Value Proposition", + # "Transferred Internally", "Referred", "Id. Decision Makers", + # "Needs Analysis", "Qualification", "Perception Analysis" + end end diff --git a/lib/sf/presenter.rb b/lib/sf/presenter.rb index 7e45baf..17f3b56 100644 --- a/lib/sf/presenter.rb +++ b/lib/sf/presenter.rb @@ -1,23 +1,26 @@ -class Sf::Presenter - include Sf::Base - attr_reader :sobject +# frozen_string_literal: true - def initialize(sobject=nil) - @sobject = sobject.nil? ? Restforce::SObject.new(sobject_type: 'EventPresenter__c') : sobject - end +module Sf + class Presenter + include Sf::Base + attr_reader :sobject - def method_missing(method_name, *args, &block) - @sobject.send(method_name, *args, &block) - end + def initialize(sobject = nil) + @sobject = sobject.nil? ? Restforce::SObject.new(sobject_type: 'EventPresenter__c') : sobject + end - def respond_to_missing?(method_name, include_private = false) - @sobject.respond_to?(method_name, include_private) || super - end + def method_missing(method_name, *args, &block) + @sobject.send(method_name, *args, &block) + end - def where(args = {}) - where = args.map {|k,v| "#{k} = \'#{v}\'"}.join(" and ") - sobjects = Sf.client.query("select Id, Name, cms_presenter_id__c from #{self.class_name} where #{where}") - build_collection sobjects unless sobjects.blank? - end + def respond_to_missing?(method_name, include_private = false) + @sobject.respond_to?(method_name, include_private) || super + end + def where(args = {}) + where = args.map { |k, v| "#{k} = \'#{v}\'" }.join(' and ') + sobjects = Sf.client.query("select Id, Name, cms_presenter_id__c from #{class_name} where #{where}") + build_collection sobjects unless sobjects.blank? + end + end end diff --git a/lib/sf/reg_meeting.rb b/lib/sf/reg_meeting.rb index 60a6f6e..3093304 100644 --- a/lib/sf/reg_meeting.rb +++ b/lib/sf/reg_meeting.rb @@ -1,45 +1,47 @@ -class Sf::RegMeeting - include Sf::Base +# frozen_string_literal: true - def initialize(reg_meeting=nil) - @reg_meeting = reg_meeting.nil? ? Restforce::SObject.new(sobject_type: 'RegMeeting__c') : reg_meeting - end +module Sf + class RegMeeting + include Sf::Base - def method_missing(method_name, *args, &block) - @reg_meeting.send(method_name, *args, &block) - end + def initialize(reg_meeting = nil) + @reg_meeting = reg_meeting.nil? ? Restforce::SObject.new(sobject_type: 'RegMeeting__c') : reg_meeting + end - def respond_to_missing?(method_name, include_private = false) - @reg_meeting.respond_to?(method_name, include_private) || super - end + def method_missing(method_name, *args, &block) + @reg_meeting.send(method_name, *args, &block) + end - def update(attrs = {}) - attrs['Id'] = self.Id - Sf.client.update!("RegMeeting__c", attrs) - end + def respond_to_missing?(method_name, include_private = false) + @reg_meeting.respond_to?(method_name, include_private) || super + end - def self.find(id) - new Sf.client.find("RegMeeting__c", id) - end + def update(attrs = {}) + attrs['Id'] = self.Id + Sf.client.update!('RegMeeting__c', attrs) + end - def self.find_by_code(code) - found_meeting = where({code__c: code}).first - new Sf.client.find("RegMeeting__c", found_meeting.Id) unless found_meeting.nil? - end + def self.find(id) + new Sf.client.find('RegMeeting__c', id) + end - def self.find_all_by_event_id(event_id) - found_meetings = where({reg_event_id__c: event_id}) - meetings = found_meetings.map do |meeting| - Sf.client.find("RegMeeting__c", meeting.Id) unless meeting.nil? + def self.find_by_code(code) + found_meeting = where({ code__c: code }).first + new Sf.client.find('RegMeeting__c', found_meeting.Id) unless found_meeting.nil? end - build_collection meetings - end - def self.where(args = {}) - where = args.map {|k,v| "#{k} = \'#{v}\'"}.join(" and ") - sobjects = Sf.client.query("select Id, Name from RegMeeting__c where #{where}") - build_collection sobjects unless sobjects.blank? - end + def self.find_all_by_event_id(event_id) + found_meetings = where({ reg_event_id__c: event_id }) + meetings = found_meetings.map do |meeting| + Sf.client.find('RegMeeting__c', meeting.Id) unless meeting.nil? + end + build_collection meetings + end + def self.where(args = {}) + where = args.map { |k, v| "#{k} = \'#{v}\'" }.join(' and ') + sobjects = Sf.client.query("select Id, Name from RegMeeting__c where #{where}") + build_collection sobjects unless sobjects.blank? + end + end end - diff --git a/lib/sf/reg_option.rb b/lib/sf/reg_option.rb index 526ba61..911143e 100644 --- a/lib/sf/reg_option.rb +++ b/lib/sf/reg_option.rb @@ -1,54 +1,56 @@ -class Sf::RegOption - include Sf::Base +# frozen_string_literal: true - def initialize(reg_option=nil) - @reg_option = reg_option.nil? ? Restforce::SObject.new(sobject_type: 'RegOption__c') : reg_option - end +module Sf + class RegOption + include Sf::Base - def method_missing(method_name, *args, &block) - @reg_option.send(method_name, *args, &block) - end + def initialize(reg_option = nil) + @reg_option = reg_option.nil? ? Restforce::SObject.new(sobject_type: 'RegOption__c') : reg_option + end - def respond_to_missing?(method_name, include_private = false) - @reg_option.respond_to?(method_name, include_private) || super - end + def method_missing(method_name, *args, &block) + @reg_option.send(method_name, *args, &block) + end - def update(attrs = {}) - attrs['Id'] = self.Id - Sf.client.update("RegOption__c", attrs) - end + def respond_to_missing?(method_name, include_private = false) + @reg_option.respond_to?(method_name, include_private) || super + end - def self.find(id) - new Sf.client.find("RegOption__c", id) - end + def update(attrs = {}) + attrs['Id'] = self.Id + Sf.client.update('RegOption__c', attrs) + end - def self.find_by_code(code) - found_option = where({code__c: code}).first - new Sf.client.find("RegOption__c", found_option.Id) unless found_option.nil? - end + def self.find(id) + new Sf.client.find('RegOption__c', id) + end - def self.find_all_by_meeting_id(meeting_id) - found_meeting_options = Sf::RegMeetingOptionAssociation.find_all_by_meeting_id(meeting_id) - found_option_ids = found_meeting_options.map(&:reg_option_id__c) - options = found_option_ids.compact.map do |option_id| - Sf.client.find("RegOption__c", option_id) + def self.find_by_code(code) + found_option = where({ code__c: code }).first + new Sf.client.find('RegOption__c', found_option.Id) unless found_option.nil? end - build_collection options - end - def self.find_all_by_event_id(event_id) - found_option = where({reg_event_id__c: event_id}) - option = found_option.map do |option| - Sf.client.find("RegOption__c", option.Id) unless option.nil? + def self.find_all_by_meeting_id(meeting_id) + found_meeting_options = Sf::RegMeetingOptionAssociation.find_all_by_meeting_id(meeting_id) + found_option_ids = found_meeting_options.map(&:reg_option_id__c) + options = found_option_ids.compact.map do |option_id| + Sf.client.find('RegOption__c', option_id) + end + build_collection options end - build_collection option - end - def self.where(args = {}) - where = args.map {|k,v| "#{k} = \'#{v}\'"}.join(" and ") - sobjects = Sf.client.query("select Id, Name from RegOption__c where #{where}") - build_collection sobjects unless sobjects.blank? - end + def self.find_all_by_event_id(event_id) + found_option = where({ reg_event_id__c: event_id }) + option = found_option.map do |option| + Sf.client.find('RegOption__c', option.Id) unless option.nil? + end + build_collection option + end + def self.where(args = {}) + where = args.map { |k, v| "#{k} = \'#{v}\'" }.join(' and ') + sobjects = Sf.client.query("select Id, Name from RegOption__c where #{where}") + build_collection sobjects unless sobjects.blank? + end + end end - diff --git a/lib/sf/reg_rate.rb b/lib/sf/reg_rate.rb index 1a17e38..3258e17 100644 --- a/lib/sf/reg_rate.rb +++ b/lib/sf/reg_rate.rb @@ -1,45 +1,47 @@ -class Sf::RegRate - include Sf::Base +# frozen_string_literal: true - def initialize(reg_rate=nil) - @reg_rate = reg_rate.nil? ? Restforce::SObject.new(sobject_type: 'RegRate__c') : reg_rate - end +module Sf + class RegRate + include Sf::Base - def method_missing(method_name, *args, &block) - @reg_rate.send(method_name, *args, &block) - end + def initialize(reg_rate = nil) + @reg_rate = reg_rate.nil? ? Restforce::SObject.new(sobject_type: 'RegRate__c') : reg_rate + end - def respond_to_missing?(method_name, include_private = false) - @reg_rate.respond_to?(method_name, include_private) || super - end + def method_missing(method_name, *args, &block) + @reg_rate.send(method_name, *args, &block) + end - def update(attrs = {}) - attrs['Id'] = self.Id - Sf.client.update!("RegRate__c", attrs) - end + def respond_to_missing?(method_name, include_private = false) + @reg_rate.respond_to?(method_name, include_private) || super + end - def self.find(id) - new Sf.client.find("RegRate__c", id) - end + def update(attrs = {}) + attrs['Id'] = self.Id + Sf.client.update!('RegRate__c', attrs) + end - def self.find_by_code(code) - found_rate = where({code__c: code}).first - new Sf.client.find("RegRate__c", found_rate.Id) unless found_rate.nil? - end + def self.find(id) + new Sf.client.find('RegRate__c', id) + end - def self.find_all_by_meeting_id(meeting_id) - found_rates = where({reg_meeting_id__c: meeting_id}) - rates = found_rates.map do |rate| - Sf.client.find("RegRate__c", rate.Id) unless rate.nil? + def self.find_by_code(code) + found_rate = where({ code__c: code }).first + new Sf.client.find('RegRate__c', found_rate.Id) unless found_rate.nil? end - build_collection rates - end - def self.where(args = {}) - where = args.map {|k,v| "#{k} = \'#{v}\'"}.join(" and ") - sobjects = Sf.client.query("select Id, Name from RegRate__c where #{where}") - build_collection sobjects unless sobjects.blank? - end + def self.find_all_by_meeting_id(meeting_id) + found_rates = where({ reg_meeting_id__c: meeting_id }) + rates = found_rates.map do |rate| + Sf.client.find('RegRate__c', rate.Id) unless rate.nil? + end + build_collection rates + end + def self.where(args = {}) + where = args.map { |k, v| "#{k} = \'#{v}\'" }.join(' and ') + sobjects = Sf.client.query("select Id, Name from RegRate__c where #{where}") + build_collection sobjects unless sobjects.blank? + end + end end - diff --git a/lib/sf/reg_registration.rb b/lib/sf/reg_registration.rb index 7312f9f..e65c399 100644 --- a/lib/sf/reg_registration.rb +++ b/lib/sf/reg_registration.rb @@ -1,225 +1,226 @@ -require "csv" -class Sf::RegRegistration - include Sf::Base - attr_accessor :registration, :zip, :speaker, :sponsor - - FIELDS = ['account_id', 'cc_email', 'city', 'contact_id', 'country', - 'created_at', 'designation', 'email', 'event_code', - 'exclude_directory', 'first_name', 'full_name', 'functional_title', - 'informal_name', 'last_name', 'processed_by', 'processed_at', - 'mobile_number', 'user_entered_organization_name', - 'derived_organization_name', 'phone_number', 'prefix', - 'reg_classes', 'reg_event_id', 'reg_registration_id', - 'rne_private', 'roster_private', 'sponsor_private', - 'co_host_private', 'state', 'status', 'street_address', - 'submitted_at', 'title', 'updated_at', 'version', 'website', 'zip', - 'fax_number', 'check_in_at', 'receipt_id' ] - - FIELDS_SELECT_STR = FIELDS.map{|fd| "#{fd}__c"}.join(', ') - CKIN_REG_ITEMS_NO_DISP = %w(rwelcome paoi QFIRST) - CKIN_REG_RATES_NO_DISP = %w(MK20 MOURATE MRATE MINCOM MEARLY GENERAL CRATE CMOU MFEDDISC NMEM MDAY) - - def initialize(reg_registration=nil) - @reg_registration = reg_registration.nil? ? Restforce::SObject.new(sobject_type: 'RegRegistration__c') : reg_registration - end +# frozen_string_literal: true + +require 'csv' +module Sf + class RegRegistration + include Sf::Base + attr_accessor :registration, :zip, :speaker, :sponsor + + FIELDS = %w[account_id cc_email city contact_id country + created_at designation email event_code + exclude_directory first_name full_name functional_title + informal_name last_name processed_by processed_at + mobile_number user_entered_organization_name + derived_organization_name phone_number prefix + reg_classes reg_event_id reg_registration_id + rne_private roster_private sponsor_private + co_host_private state status street_address + submitted_at title updated_at version website zip + fax_number check_in_at receipt_id].freeze + + FIELDS_SELECT_STR = FIELDS.map { |fd| "#{fd}__c" }.join(', ') + CKIN_REG_ITEMS_NO_DISP = %w[rwelcome paoi QFIRST].freeze + CKIN_REG_RATES_NO_DISP = %w[MK20 MOURATE MRATE MINCOM MEARLY GENERAL CRATE CMOU MFEDDISC NMEM MDAY].freeze + + def initialize(reg_registration = nil) + @reg_registration = reg_registration.nil? ? Restforce::SObject.new(sobject_type: 'RegRegistration__c') : reg_registration + end - def method_missing(method_name, *args, &block) - @reg_registration.send(method_name, *args, &block) - end + def method_missing(method_name, *args, &block) + @reg_registration.send(method_name, *args, &block) + end - def respond_to_missing?(method_name, include_private = false) - @reg_registration.respond_to?(method_name, include_private) || super - end + def respond_to_missing?(method_name, include_private = false) + @reg_registration.respond_to?(method_name, include_private) || super + end - def create_from_registration - attrs = {} - self.registration_mapping.each do |reg_attr, sf_reg_attr| - reg_value = self.registration.send(reg_attr) - reg_value = reg_value.iso8601 if reg_value.class.eql?(ActiveSupport::TimeWithZone) - attrs[sf_reg_attr] = reg_value unless reg_value.nil? - end - attrs['event_code__c'] = self.registration.event.code - attrs['reg_event_id__c'] = self.registration.event.sf_event_id - attrs['processed_at__c'] = Time.now.iso8601 - if self.registration.payment.present? and payment = self.registration.payment - attrs['payment_method__c'] = payment.payment_method - attrs['credit_card_type__c'] = payment.card_type - attrs['charged_amount__c'] = payment.charge_amount - attrs['paid_amount__c'] = payment.paid_amount - attrs['credit_card_number__c'] = payment.card_last4 - attrs['payment_reference_id__c'] = payment.pnref - attrs['bank_authorization_code__c'] = payment.authcode - end - if sf_reg_id = Sf.client.create!("RegRegistration__c", attrs) - self.registration.reg_items.each do |reg_item| - #TODO: rescue and rollback - if reg_item.sf_reg_item_id = Sf::RegItem.create_for(reg_item, sf_reg_id) - reg_item.save + def create_from_registration + attrs = {} + registration_mapping.each do |reg_attr, sf_reg_attr| + reg_value = registration.send(reg_attr) + reg_value = reg_value.iso8601 if reg_value.instance_of?(ActiveSupport::TimeWithZone) + attrs[sf_reg_attr] = reg_value unless reg_value.nil? + end + attrs['event_code__c'] = registration.event.code + attrs['reg_event_id__c'] = registration.event.sf_event_id + attrs['processed_at__c'] = Time.now.iso8601 + if registration.payment.present? && (payment = registration.payment) + attrs['payment_method__c'] = payment.payment_method + attrs['credit_card_type__c'] = payment.card_type + attrs['charged_amount__c'] = payment.charge_amount + attrs['paid_amount__c'] = payment.paid_amount + attrs['credit_card_number__c'] = payment.card_last4 + attrs['payment_reference_id__c'] = payment.pnref + attrs['bank_authorization_code__c'] = payment.authcode + end + if sf_reg_id = Sf.client.create!('RegRegistration__c', attrs) + registration.reg_items.each do |reg_item| + # TODO: rescue and rollback + reg_item.save if reg_item.sf_reg_item_id = Sf::RegItem.create_for(reg_item, sf_reg_id) end end + sf_reg_id end - sf_reg_id - end - def update(attrs = {}) - attrs['Id'] = self.Id - Sf.client.update("RegRegistration__c", attrs) - end + def update(attrs = {}) + attrs['Id'] = self.Id + Sf.client.update('RegRegistration__c', attrs) + end - def registration_mapping - { - 'sf_account_id' => 'account_id__c', - 'cc_email' => 'cc_email__c', - 'city' => 'city__c', - 'sf_id' => 'contact_id__c', - 'country' => 'country__c', - 'created_at' => 'created_at__c', - 'designation' => 'designation__c', - 'email' => 'email__c', - 'first_name' => 'first_name__c', - 'full_name' => 'full_name__c', - 'functional_title' => 'functional_title__c', - 'informal_name' => 'informal_name__c', - 'last_name' => 'last_name__c', - 'mobile_number' => 'mobile_number__c', - 'organization_name' => 'user_entered_organization_name__c', - 'phone_number' => 'phone_number__c', - 'fax_number' => 'fax_number__c', - 'prefix' => 'prefix__c', - 'reg_classes' => 'reg_classes__c', - 'id' => 'reg_registration_id__c', - 'rne_private' => 'rne_private__c', - 'roster_private' => 'roster_private__c', - 'co_host_private' => 'co_host_private__c', - 'sponsor_private' => 'sponsor_private__c', - 'state' => 'state__c', - 'status' => 'status__c', - 'street_address' => 'street_address__c', - 'submitted_at' => 'submitted_at__c', - 'title' => 'title__c', - 'updated_at' => 'updated_at__c', - 'version' => 'version__c', - 'website' => 'website__c', - 'zip' => 'zip__c', - 'total' => 'charged_amount__c', - 'processed_by' => 'processed_by__c', - 'processed_at' => 'processed_at__c', - 'receipt_id' => 'receipt_id__c', - 'reference_id' => 'reference_id__c', - 'balance' => 'balance__c', - 'from_ip_address' => 'ip_address__c', - 'sf_reg_event_id' => 'reg_event_id__c' - } - end + def registration_mapping + { + 'sf_account_id' => 'account_id__c', + 'cc_email' => 'cc_email__c', + 'city' => 'city__c', + 'sf_id' => 'contact_id__c', + 'country' => 'country__c', + 'created_at' => 'created_at__c', + 'designation' => 'designation__c', + 'email' => 'email__c', + 'first_name' => 'first_name__c', + 'full_name' => 'full_name__c', + 'functional_title' => 'functional_title__c', + 'informal_name' => 'informal_name__c', + 'last_name' => 'last_name__c', + 'mobile_number' => 'mobile_number__c', + 'organization_name' => 'user_entered_organization_name__c', + 'phone_number' => 'phone_number__c', + 'fax_number' => 'fax_number__c', + 'prefix' => 'prefix__c', + 'reg_classes' => 'reg_classes__c', + 'id' => 'reg_registration_id__c', + 'rne_private' => 'rne_private__c', + 'roster_private' => 'roster_private__c', + 'co_host_private' => 'co_host_private__c', + 'sponsor_private' => 'sponsor_private__c', + 'state' => 'state__c', + 'status' => 'status__c', + 'street_address' => 'street_address__c', + 'submitted_at' => 'submitted_at__c', + 'title' => 'title__c', + 'updated_at' => 'updated_at__c', + 'version' => 'version__c', + 'website' => 'website__c', + 'zip' => 'zip__c', + 'total' => 'charged_amount__c', + 'processed_by' => 'processed_by__c', + 'processed_at' => 'processed_at__c', + 'receipt_id' => 'receipt_id__c', + 'reference_id' => 'reference_id__c', + 'balance' => 'balance__c', + 'from_ip_address' => 'ip_address__c', + 'sf_reg_event_id' => 'reg_event_id__c' + } + end - def self.all - build_collection Sf.client.query( - "SELECT #{Sf::RegRegistration::FIELDS_SELECT_STR} + def self.all + build_collection Sf.client.query( + "SELECT #{Sf::RegRegistration::FIELDS_SELECT_STR} FROM RegRegistration__c" - ) - end + ) + end - def self.find(id) - new Sf.client.find("RegRegistration__c", id) - end + def self.find(id) + new Sf.client.find('RegRegistration__c', id) + end - def self.find_all_by_event_code(code) - where_stmt = self.where(event_code__c: code) - self.query(where_stmt, order_stmt) - end + def self.find_all_by_event_code(code) + where_stmt = where(event_code__c: code) + query(where_stmt, order_stmt) + end - def self.all_processed(code) - where_stmt = self.where({event_code__c: code, status__c: "processed"}) - order_stmt = self.order_by(last_name__c: "ASC") - self.query(where_stmt, order_stmt) - end + def self.all_processed(code) + where_stmt = where({ event_code__c: code, status__c: 'processed' }) + order_stmt = order_by(last_name__c: 'ASC') + query(where_stmt, order_stmt) + end - def self.all_for_check_in(code) - where_stmt = "event_code__c = \'#{code}\' and status__c = \'processed\'" - order_stmt = self.order_by(last_name__c: "ASC") - self.query_with_reg_item_codes(where_stmt, order_stmt) - end + def self.all_for_check_in(code) + where_stmt = "event_code__c = \'#{code}\' and status__c = \'processed\'" + order_stmt = order_by(last_name__c: 'ASC') + query_with_reg_item_codes(where_stmt, order_stmt) + end - def self.all_checked_in(code) - where_stmt = "event_code__c = \'#{code}\' and check_in_at__c != null" - order_stmt = self.order_by(last_name__c: "ASC") - self.query_with_reg_item_codes(where_stmt, order_stmt) - end + def self.all_checked_in(code) + where_stmt = "event_code__c = \'#{code}\' and check_in_at__c != null" + order_stmt = order_by(last_name__c: 'ASC') + query_with_reg_item_codes(where_stmt, order_stmt) + end - def self.not_checked_in(code) - where_stmt = "event_code__c = \'#{code}\' and status__c = \'processed\' and check_in_at__c = null" - order_stmt = self.order_by(last_name__c: "ASC") - self.query_with_reg_item_codes(where_stmt, order_stmt) - end + def self.not_checked_in(code) + where_stmt = "event_code__c = \'#{code}\' and status__c = \'processed\' and check_in_at__c = null" + order_stmt = order_by(last_name__c: 'ASC') + query_with_reg_item_codes(where_stmt, order_stmt) + end - def self.not_checked_in_last_name_between(code, a, z) - next_letter = z.downcase + "zzzzzzzzzz" - where_stmt = "event_code__c = \'#{code}\' and status__c = \'processed\' and check_in_at__c = null and last_name__c >= \'#{a.upcase}\' and last_name__c < \'#{next_letter}\'" - order_stmt = self.order_by(last_name__c: "ASC") - self.query_with_reg_item_codes(where_stmt, order_stmt) - end + def self.not_checked_in_last_name_between(code, a, z) + next_letter = "#{z.downcase}zzzzzzzzzz" + where_stmt = "event_code__c = \'#{code}\' and status__c = \'processed\' and check_in_at__c = null and last_name__c >= \'#{a.upcase}\' and last_name__c < \'#{next_letter}\'" + order_stmt = order_by(last_name__c: 'ASC') + query_with_reg_item_codes(where_stmt, order_stmt) + end - def self.where(args = {}) - args.map {|k,v| "#{k} = \'#{v}\'"}.join(" and ") - end + def self.where(args = {}) + args.map { |k, v| "#{k} = \'#{v}\'" }.join(' and ') + end - # order_by(last_name: "ASC") - def self.order_by(args = {}) - orders = args.map {|k,v| "#{k} #{v}"}.join(" ") - "ORDER BY #{orders}" - end + # order_by(last_name: "ASC") + def self.order_by(args = {}) + orders = args.map { |k, v| "#{k} #{v}" }.join(' ') + "ORDER BY #{orders}" + end - def self.query_with_reg_item_codes(where_stmt, order_stmt) - reg_item_select = "select code__c, reg_rate_code__c from reg_items__r" - sobjects = Sf.client.query("select Id, #{FIELDS_SELECT_STR}, (#{reg_item_select}) from RegRegistration__c where #{where_stmt} #{order_stmt}") - build_collection sobjects unless sobjects.blank? - end + def self.query_with_reg_item_codes(where_stmt, order_stmt) + reg_item_select = 'select code__c, reg_rate_code__c from reg_items__r' + sobjects = Sf.client.query("select Id, #{FIELDS_SELECT_STR}, (#{reg_item_select}) from RegRegistration__c where #{where_stmt} #{order_stmt}") + build_collection sobjects unless sobjects.blank? + end - def self.query(where_stmt, order_stmt) - sobjects = Sf.client.query("select Id, #{FIELDS_SELECT_STR} from RegRegistration__c where #{where_stmt} #{order_stmt}") - build_collection sobjects unless sobjects.blank? - end + def self.query(where_stmt, order_stmt) + sobjects = Sf.client.query("select Id, #{FIELDS_SELECT_STR} from RegRegistration__c where #{where_stmt} #{order_stmt}") + build_collection sobjects unless sobjects.blank? + end - def self.attendees_for(event_code, reg_item_code) - return if event_code.nil? or reg_item_code.nil? - rr = 'reg_registration_id__r' - selects = "#{rr}.Id, #{rr}.email__c, #{rr}.last_name__c, #{rr}.first_name__c, #{rr}.account_id__r.Name, #{rr}.derived_organization_name__c, #{rr}.title__c, #{rr}.roster_private__c, #{rr}.LastModifiedDate, #{rr}.CreatedDate, #{rr}.status__c" - where = "code__c = \'#{reg_item_code}\' and #{rr}.event_code__c = \'#{event_code}\'" - sobjects = Sf.client.query("select #{selects} from RegItem__c where #{where}") - build_collection sobjects.map(&:reg_registration_id__r) unless sobjects.blank? - end + def self.attendees_for(event_code, reg_item_code) + return if event_code.nil? || reg_item_code.nil? - def self.roster_for(event_code, reg_item_code) - all_attendees = self.attendees_for(event_code, reg_item_code) - all_attendees.map!{|a| a unless a.roster_private__c} unless all_attendees.nil? - all_attendees.compact unless all_attendees.nil? - end + rr = 'reg_registration_id__r' + selects = "#{rr}.Id, #{rr}.email__c, #{rr}.last_name__c, #{rr}.first_name__c, #{rr}.account_id__r.Name, #{rr}.derived_organization_name__c, #{rr}.title__c, #{rr}.roster_private__c, #{rr}.LastModifiedDate, #{rr}.CreatedDate, #{rr}.status__c" + where = "code__c = \'#{reg_item_code}\' and #{rr}.event_code__c = \'#{event_code}\'" + sobjects = Sf.client.query("select #{selects} from RegItem__c where #{where}") + build_collection sobjects.map(&:reg_registration_id__r) unless sobjects.blank? + end - def self.attendees_count(event_code, reg_item_code) - self.attendees_for(event_code, reg_item_code).size - end + def self.roster_for(event_code, reg_item_code) + all_attendees = attendees_for(event_code, reg_item_code) + all_attendees&.map! { |a| a unless a.roster_private__c } + all_attendees&.compact + end - def self.attendees_for_to_csv(event_code, reg_item_code) - self.to_csv self.attendees_for(event_code, reg_item_code) - end + def self.attendees_count(event_code, reg_item_code) + attendees_for(event_code, reg_item_code).size + end + + def self.attendees_for_to_csv(event_code, reg_item_code) + to_csv attendees_for(event_code, reg_item_code) + end - def self.to_csv(list) - CSV.generate do |csv| - list.each do |a| - csv << [a.email__c, a.last_name__c, a.first_name__c] + def self.to_csv(list) + CSV.generate do |csv| + list.each do |a| + csv << [a.email__c, a.last_name__c, a.first_name__c] + end end end - end - def self.with_speakers_and_sponsors(event_code, attendees) - speaker_ids = Sf::Session.presenter_ids_by_event_code(event_code) - sponsor_ids = Sf::Sponsorship.by_event_code(event_code).map(&:sponsor__c) - attendees.map do |a| - a.speaker = true if speaker_ids.include?(a.contact_id__c) - a.sponsor = true if sponsor_ids.include?(a.account_id__c) - a + def self.with_speakers_and_sponsors(event_code, attendees) + speaker_ids = Sf::Session.presenter_ids_by_event_code(event_code) + sponsor_ids = Sf::Sponsorship.by_event_code(event_code).map(&:sponsor__c) + attendees.map do |a| + a.speaker = true if speaker_ids.include?(a.contact_id__c) + a.sponsor = true if sponsor_ids.include?(a.account_id__c) + a + end end end - end - diff --git a/lib/sf/service_institution.rb b/lib/sf/service_institution.rb index 8795cfc..b133199 100644 --- a/lib/sf/service_institution.rb +++ b/lib/sf/service_institution.rb @@ -1,39 +1,41 @@ -class Sf::ServiceInstitution - include Sf::Base - API_NAME = 'Service_Institution__c' - FIELDS = [ - 'Id', 'Name', 'Account_ID__c', 'Current_Phase__c', 'Do_Not_Publish__c', - 'Provider__c', 'Service__c', 'Service_End_Date__c', 'Service_Provider__c', - 'Service_Start_Date__c', 'Subscription__c', 'Supporting_Institution__c', - 'Tier_of_Support__c' - ] - FIELDS_SELECT_STR = FIELDS.join(', ') - - def initialize(service_institution = nil) - @service_institution = service_institution - end - - def account - new Sf::Account.find("Account", Supporting_Institution__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 +# frozen_string_literal: true + +module Sf + class ServiceInstitution + include Sf::Base + API_NAME = 'Service_Institution__c' + FIELDS = %w[ + Id Name Account_ID__c Current_Phase__c Do_Not_Publish__c + Provider__c Service__c Service_End_Date__c Service_Provider__c + Service_Start_Date__c Subscription__c Supporting_Institution__c + Tier_of_Support__c + ].freeze + FIELDS_SELECT_STR = FIELDS.join(', ') + + def initialize(service_institution = nil) + @service_institution = service_institution + end + + def account + new Sf::Account.find('Account', Supporting_Institution__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.find(id) + new Sf.client.find('ServiceInstitution__c', id) + 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 - - def self.find(id) - new Sf.client.find("ServiceInstitution__c", id) - 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 - diff --git a/lib/sf/session.rb b/lib/sf/session.rb index 8fc291c..d2a1060 100644 --- a/lib/sf/session.rb +++ b/lib/sf/session.rb @@ -1,131 +1,132 @@ -class Sf::Session - include Sf::Base - attr_reader :sobject - attr_accessor :proposal +# frozen_string_literal: true - def initialize(sobject=nil) - @sobject = sobject.nil? ? Restforce::SObject.new(sobject_type: 'EventSession__c') : sobject - end +module Sf + class Session + include Sf::Base + attr_reader :sobject + attr_accessor :proposal - def method_missing(method_name, *args, &block) - @sobject.send(method_name, *args, &block) - end + def initialize(sobject = nil) + @sobject = sobject.nil? ? Restforce::SObject.new(sobject_type: 'EventSession__c') : sobject + end - def respond_to_missing?(method_name, include_private = false) - @sobject.respond_to?(method_name, include_private) || super - end + def method_missing(method_name, *args, &block) + @sobject.send(method_name, *args, &block) + end - def create_from_proposal - Sf.client.create!('EventSession__c', proposal_attrs) - end + def respond_to_missing?(method_name, include_private = false) + @sobject.respond_to?(method_name, include_private) || super + end - def proposal_attrs - sf_event = Sf::RegEvent.find_by_code(self.proposal.meeting_config.event_code) - attrs = {} - attrs['event__c'] = sf_event.Id - #attrs['Name'] = self.proposal.session_title[0..79] - attrs['title__c'] = self.proposal.session_title - attrs['abstract__c'] = self.proposal.abstract - attrs['cms_proposal_id__c'] = self.proposal.id - attrs['submitter_email__c'] = self.proposal.submitter_email - attrs['submitter_phone__c'] = self.proposal.submitter_phone - attrs['cms_proposal_id__c'] = self.proposal.id - if session_type_id = self.proposal.session_type_id - sf_session_type = Sf::SessionType.find_by(reg_event__c: sf_event.Id, cms_session_type_id__c: session_type_id) - attrs['session_type__c'] = sf_session_type.Id unless sf_session_type.blank? - end - if primary_track_id = self.proposal.primary_track_id - sf_primary_track = Sf::Track.find_by(reg_event_id__c: sf_event.Id, cms_track_id__c: primary_track_id) - attrs['primary_track_id__c'] = sf_primary_track.Id unless sf_primary_track.blank? - end - if secondary_track_id = self.proposal.secondary_track_id - sf_secondary_track = Sf::Track.find_by(reg_event_id__c: sf_event.Id, cms_track_id__c: secondary_track_id) - attrs['secondary_track_id__c'] = sf_secondary_track.Id unless sf_secondary_track.blank? - end - attrs['timestamp__c'] = Time.now.iso8601 - meeting = self.proposal.meeting_config.meeting - if meeting.present? - attrs['cms_meeting_id__c'] = meeting.id - end - attrs - end + def create_from_proposal + Sf.client.create!('EventSession__c', proposal_attrs) + end - def timestamp - self.timestamp__c.try(:to_datetime) - end + def proposal_attrs + sf_event = Sf::RegEvent.find_by_code(proposal.meeting_config.event_code) + attrs = {} + attrs['event__c'] = sf_event.Id + # attrs['Name'] = self.proposal.session_title[0..79] + attrs['title__c'] = proposal.session_title + attrs['abstract__c'] = proposal.abstract + attrs['cms_proposal_id__c'] = proposal.id + attrs['submitter_email__c'] = proposal.submitter_email + attrs['submitter_phone__c'] = proposal.submitter_phone + attrs['cms_proposal_id__c'] = proposal.id + if session_type_id = proposal.session_type_id + sf_session_type = Sf::SessionType.find_by(reg_event__c: sf_event.Id, cms_session_type_id__c: session_type_id) + attrs['session_type__c'] = sf_session_type.Id unless sf_session_type.blank? + end + if primary_track_id = proposal.primary_track_id + sf_primary_track = Sf::Track.find_by(reg_event_id__c: sf_event.Id, cms_track_id__c: primary_track_id) + attrs['primary_track_id__c'] = sf_primary_track.Id unless sf_primary_track.blank? + end + if secondary_track_id = proposal.secondary_track_id + sf_secondary_track = Sf::Track.find_by(reg_event_id__c: sf_event.Id, cms_track_id__c: secondary_track_id) + attrs['secondary_track_id__c'] = sf_secondary_track.Id unless sf_secondary_track.blank? + end + attrs['timestamp__c'] = Time.now.iso8601 + meeting = proposal.meeting_config.meeting + attrs['cms_meeting_id__c'] = meeting.id if meeting.present? + attrs + end - def ready_to_publish? - required_attrs = %w(title__c start_time__c date__c end_time__c) - ready = true - required_attrs.each do |attr| - if self.send(attr).blank? - ready = false - break + def timestamp + timestamp__c.try(:to_datetime) + end + + def ready_to_publish? + required_attrs = %w[title__c start_time__c date__c end_time__c] + ready = true + required_attrs.each do |attr| + if send(attr).blank? + ready = false + break + end end + ready end - ready - end - def <=>(b) - if self.start_time__c.present? and b.start_time__c.present? - self.start_time__c <=> b.start_time__c - else - 0 + def <=>(other) + if start_time__c.present? && other.start_time__c.present? + start_time__c <=> other.start_time__c + else + 0 + end end - end - def self.by_event_code(code) - event = Sf::RegEvent.find_by(code__c: code) - self.where(event__c: event.Id) unless event.blank? - end + def self.by_event_code(code) + event = Sf::RegEvent.find_by(code__c: code) + where(event__c: event.Id) unless event.blank? + end - def self.presenters_by_event_code(code) - event = Sf::RegEvent.find_by(code__c: code) - sobjects = Sf.client.query("select Id, cms_session_id__c, (select Id, contact_id__c from Event_Presenters__r) + def self.presenters_by_event_code(code) + event = Sf::RegEvent.find_by(code__c: code) + sobjects = Sf.client.query("select Id, cms_session_id__c, (select Id, contact_id__c from Event_Presenters__r) from EventSession__c where event__c = \'#{event.Id}\'") - build_collection sobjects unless sobjects.blank? - end + build_collection sobjects unless sobjects.blank? + end - def self.track_presenters_by_event_code(code) - event = Sf::RegEvent.find_by(code__c: code) - sobjects = Sf.client.query("select Id, cms_session_id__c, session_type__r.Name, (select Id, contact_id__c from Event_Presenters__r) + def self.track_presenters_by_event_code(code) + event = Sf::RegEvent.find_by(code__c: code) + sobjects = Sf.client.query("select Id, cms_session_id__c, session_type__r.Name, (select Id, contact_id__c from Event_Presenters__r) from EventSession__c where session_type__r.Name = 'Track Session' and event__c = \'#{event.Id}\'") - build_collection sobjects unless sobjects.blank? - end + build_collection sobjects unless sobjects.blank? + end - def self.presenter_ids_by_event_code(code) - self.track_presenters_by_event_code(code).map do |s| - s.Event_Presenters__r.map(&:contact_id__c) unless s.Event_Presenters__r.blank? - end.flatten.uniq.compact - end + def self.presenter_ids_by_event_code(code) + track_presenters_by_event_code(code).map do |s| + s.Event_Presenters__r.map(&:contact_id__c) unless s.Event_Presenters__r.blank? + end.flatten.uniq.compact + end - def self.unpublished_by_event_code(code) - event = Sf::RegEvent.find_by(code__c: code) - self.where(event__c: event.Id, cms_session_id__c: '') unless event.blank? - end + def self.unpublished_by_event_code(code) + event = Sf::RegEvent.find_by(code__c: code) + where(event__c: event.Id, cms_session_id__c: '') unless event.blank? + end - def self.updated_auto_publish_sessions(code) - event = Sf::RegEvent.find_by(code__c: code) - rubyuser = Sf.client.options[:username].split('@')[0] - where_stmt = "event__c = \'#{event.Id}\' and auto_publish__c = true and LastModifiedBy.Name != \'#{rubyuser}\'" - self.query(where_stmt) - end + def self.updated_auto_publish_sessions(code) + event = Sf::RegEvent.find_by(code__c: code) + rubyuser = Sf.client.options[:username].split('@')[0] + where_stmt = "event__c = \'#{event.Id}\' and auto_publish__c = true and LastModifiedBy.Name != \'#{rubyuser}\'" + query(where_stmt) + end - def self.query(where_stmt) - sobjects = Sf.client.query("select Id, Name, title__c, primary_track_id__r.Name, + def self.query(where_stmt) + sobjects = Sf.client.query("select Id, Name, title__c, primary_track_id__r.Name, session_type__r.Name, start_time__c, room_id__r.Name, cms_session_id__c, timestamp__c, date__c, end_time__c, auto_publish__c, LastModifiedBy.Name from EventSession__c where #{where_stmt}") - build_collection sobjects unless sobjects.blank? - end + build_collection sobjects unless sobjects.blank? + end - def self.where(args = {}) - where = args.map {|k,v| "#{k} = \'#{v}\'"}.join(" and ") - sobjects = Sf.client.query("select Id, Name, title__c, primary_track_id__r.Name, + def self.where(args = {}) + where = args.map { |k, v| "#{k} = \'#{v}\'" }.join(' and ') + sobjects = Sf.client.query("select Id, Name, title__c, primary_track_id__r.Name, session_type__r.Name, start_time__c, room_id__r.Name, cms_session_id__c, timestamp__c, date__c, end_time__c, auto_publish__c from EventSession__c where #{where}") - build_collection sobjects unless sobjects.blank? + build_collection sobjects unless sobjects.blank? + end end end - diff --git a/lib/sf/sponsorship.rb b/lib/sf/sponsorship.rb index 6478b4b..bac56d2 100644 --- a/lib/sf/sponsorship.rb +++ b/lib/sf/sponsorship.rb @@ -1,52 +1,55 @@ -class Sf::Sponsorship - include Sf::Base - attr_reader :sobject - attr_accessor :sponsor - - def initialize(sobject=nil) - @sobject = sobject.nil? ? Restforce::SObject.new(sobject_type: 'EventSponsorship__c') : sobject - end - - def method_missing(method_name, *args, &block) - @sobject.send(method_name, *args, &block) - end - - def respond_to_missing?(method_name, include_private = false) - @sobject.respond_to?(method_name, include_private) || super - end - - def attrs_from_sponsor - attrs = {} - attrs["cms_sponsor_id__c"] = self.sponsor.id - attrs["cms_sponsor_name__c"] = self.sponsor.sponsor_name - attrs["event__c"] = self.sponsor.sf_event_id - attrs["logo_url__c"] = self.sponsor.image - attrs["sponsor__c"] = self.sponsor.sf_id unless self.sponsor.sf_id.blank? - attrs["sponsorship_type__c"] = self.sponsor.sponsorship_type.try(:humanize) - attrs - end - - def create_from_sponsor - Sf.client.create!("EventSponsorship__c", self.attrs_from_sponsor) if self.sponsor.present? - end - - def update_from_sponsor - attrs = self.attrs_from_sponsor - attrs["Id"] = self.Id - Sf.client.update!("EventSponsorship__c", attrs) if self.sponsor.present? - end - - def self.by_event_code(code) - event = Sf::RegEvent.find_by(code__c: code) - self.where(event__c: event.Id) unless event.blank? - end - def self.where(args = {}) - where = args.map {|k,v| "#{k} = \'#{v}\'"}.join(" and ") - sobjects = Sf.client.query("select Id, event__c, contact__c, sponsor__c, logo_url__c, +# frozen_string_literal: true + +module Sf + class Sponsorship + include Sf::Base + attr_reader :sobject + attr_accessor :sponsor + + def initialize(sobject = nil) + @sobject = sobject.nil? ? Restforce::SObject.new(sobject_type: 'EventSponsorship__c') : sobject + end + + def method_missing(method_name, *args, &block) + @sobject.send(method_name, *args, &block) + end + + def respond_to_missing?(method_name, include_private = false) + @sobject.respond_to?(method_name, include_private) || super + end + + def attrs_from_sponsor + attrs = {} + attrs['cms_sponsor_id__c'] = sponsor.id + attrs['cms_sponsor_name__c'] = sponsor.sponsor_name + attrs['event__c'] = sponsor.sf_event_id + attrs['logo_url__c'] = sponsor.image + attrs['sponsor__c'] = sponsor.sf_id unless sponsor.sf_id.blank? + attrs['sponsorship_type__c'] = sponsor.sponsorship_type.try(:humanize) + attrs + end + + def create_from_sponsor + Sf.client.create!('EventSponsorship__c', attrs_from_sponsor) if sponsor.present? + end + + def update_from_sponsor + attrs = attrs_from_sponsor + attrs['Id'] = self.Id + Sf.client.update!('EventSponsorship__c', attrs) if sponsor.present? + end + + def self.by_event_code(code) + event = Sf::RegEvent.find_by(code__c: code) + where(event__c: event.Id) unless event.blank? + end + + def self.where(args = {}) + where = args.map { |k, v| "#{k} = \'#{v}\'" }.join(' and ') + sobjects = Sf.client.query("select Id, event__c, contact__c, sponsor__c, logo_url__c, cms_sponsor_id__c, cms_sponsor_name__c, sponsorship_type__c from EventSponsorship__c where #{where}") - build_collection sobjects unless sobjects.blank? + build_collection sobjects unless sobjects.blank? + end end end - -