From aaa332add852dab6737894e9af7b4a3b624b4608 Mon Sep 17 00:00:00 2001 From: IJ Kim Date: Tue, 14 Apr 2015 15:36:49 -0400 Subject: [PATCH] added incommon on-boarding --- lib/sf.rb | 1 + lib/sf/account.rb | 40 +++++++++++++++++++++++++++++----------- lib/sf/base.rb | 37 ++++++++++++++++++++----------------- lib/sf/contact.rb | 12 +++++++++++- lib/sf/opportunity.rb | 41 +++++++++++++++++++++++++++++++++++++++++ lib/sf/version.rb | 2 +- 6 files changed, 103 insertions(+), 30 deletions(-) create mode 100644 lib/sf/opportunity.rb diff --git a/lib/sf.rb b/lib/sf.rb index 9c636a7..5cbf46a 100644 --- a/lib/sf.rb +++ b/lib/sf.rb @@ -18,6 +18,7 @@ require "sf/sponsorship" require "sf/room" require "sf/venue" +require "sf/opportunity" module Sf class << self def client diff --git a/lib/sf/account.rb b/lib/sf/account.rb index b5bddf9..693d972 100644 --- a/lib/sf/account.rb +++ b/lib/sf/account.rb @@ -1,7 +1,7 @@ class Sf::Account include Sf::Base - FIELDS = ['Id', 'Name', 'Member_Type__c', 'Member_Category__c', 'Member_Join_Date__c', 'Status__c', 'Website'] + FIELDS = ['Id', 'Name', 'Member_Type__c', 'Member_Category__c', 'Member_Join_Date__c', 'Status__c', 'Website', 'InCommon_Participant__c', 'iMIS_ID__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(', ') @@ -27,6 +27,10 @@ def respond_to_missing?(method_name, include_private = false) @account.respond_to?(method_name, include_private) || super end + def inc_execs + Sf::Contact.inc_execs_for(self.Id) + end + # Class Methods def self.find(id) new Sf.client.find("Account", id) @@ -41,33 +45,33 @@ def self.find_by(args = {}) 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") + 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") + 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") + 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' + "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") + 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"}) + i2 = find_by({Name: "Internet2", Status__c: "Active"}) find i2.Id end @@ -82,4 +86,18 @@ def self.inc_part?(account_id) def self.intl_partner?(account_id) intl_partners.map(&:Id).include?(account_id) end + + # It looks up for the past one year + def self.inc_parts_to_be + acct_ids = Sf::Opportunity.inc_participations(Date.today.prev_month(12)).map(&:AccountId) + 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 + + def self.inc_certs_to_be + acct_ids = Sf::Opportunity.cert_subscribers(Date.today.prev_month(12)).map(&:AccountId) + 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 diff --git a/lib/sf/base.rb b/lib/sf/base.rb index 1a3670a..684acaa 100644 --- a/lib/sf/base.rb +++ b/lib/sf/base.rb @@ -1,43 +1,46 @@ module Sf module Base def self.included(base) - base.extend(ClassMathods) + base.extend(ClassMathods) 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) - end + unless self.Id.blank? + params = {'Id' => self.Id} + args.map {|k,v| params[k] = v} + Sf.client.update!(self.sobject.attributes[:type], params) + end end end module ClassMathods def build_collection(models) - models.map { |model| new(model) } + models.map { |model| new(model) } end - def class_name - self.new.sobject[:sobject_type] + def class_name + obj = self.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(self.class_name, id) end def find_by_name(name) - sobject = where({Name: name}).first - find(sobject.Id) unless sobject.blank? + sobject = where({Name: name}).first + find(sobject.Id) unless sobject.blank? end def find_by(args = {}) - sobject = where(args).first - find(sobject.Id) unless sobject.blank? + sobject = where(args).first + find(sobject.Id) unless sobject.blank? 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}") - build_collection sobjects unless sobjects.blank? + where = args.map {|k,v| "#{k} = \'#{v}\'"}.join(" and ") + sobjects = Sf.client.query("select Id, Name from #{self.class_name} where #{where}") + build_collection sobjects unless sobjects.blank? end end diff --git a/lib/sf/contact.rb b/lib/sf/contact.rb index d2ffd76..8423ce6 100644 --- a/lib/sf/contact.rb +++ b/lib/sf/contact.rb @@ -7,7 +7,7 @@ class Sf::Contact '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'] + 'gs_executive__c', 'techex_executive__c', 'gender__c', 'Contact_ID__c'] FIELDS_SELECT_STR = FIELDS.join(', ') def initialize(contact=nil) @@ -114,6 +114,16 @@ def self.find_by(args = {}) where(args).first 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: "InCommon", Status__c: "Current" + } + where = inc_exec_filter.map {|k,v| "#{k} = \'#{v}\'"}.join(" and ") + build_contacts(from, where) + end + def self.internet2_staffs from = 'Contact_Affiliation__c' i2 = Sf::Account.internet2 diff --git a/lib/sf/opportunity.rb b/lib/sf/opportunity.rb new file mode 100644 index 0000000..d440226 --- /dev/null +++ b/lib/sf/opportunity.rb @@ -0,0 +1,41 @@ +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 + 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 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='NET+ - InCommon Participation' and IsWon = true and CloseDate >= #{since}") + 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='NET+ - Certificate Service' and IsWon = true and CloseDate >= #{since}") + build_collection sobjects unless sobjects.blank? + end +end diff --git a/lib/sf/version.rb b/lib/sf/version.rb index 57d8935..814760a 100644 --- a/lib/sf/version.rb +++ b/lib/sf/version.rb @@ -1,3 +1,3 @@ module Sf - VERSION = "0.1.19" + VERSION = "0.1.21" end