Skip to content

Commit

Permalink
added incommon on-boarding
Browse files Browse the repository at this point in the history
  • Loading branch information
ij committed Apr 14, 2015
1 parent 829e380 commit aaa332a
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 30 deletions.
1 change: 1 addition & 0 deletions lib/sf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
require "sf/sponsorship"
require "sf/room"
require "sf/venue"
require "sf/opportunity"
module Sf
class << self
def client
Expand Down
40 changes: 29 additions & 11 deletions lib/sf/account.rb
Original file line number Diff line number Diff line change
@@ -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(', ')
Expand All @@ -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)
Expand All @@ -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

Expand All @@ -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
37 changes: 20 additions & 17 deletions lib/sf/base.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 11 additions & 1 deletion lib/sf/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions lib/sf/opportunity.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/sf/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Sf
VERSION = "0.1.19"
VERSION = "0.1.21"
end

0 comments on commit aaa332a

Please sign in to comment.