# This is an example Remi job that was auto-generated by Remi. require_relative 'all_jobs_shared' require 'remi/data_subject/salesforce' class SampleJob include AllJobsShared define_source :existing_contacts, Remi::DataSource::Salesforce, object: :Contact, credentials: params[:salesforce_credentials], api: :bulk, fields: { :Id => {}, :External_ID__c => {}, :IsActive => { type: :boolean }, :CreatedDate => { type: :date, in_format: '%Y-%m-%d %H:%M:%S' } }, query: <<-EOQ SELECT Id, External_ID__c FROM Contact EOQ define_source :sample_file, Remi::DataSource::CsvFile, extractor: Remi::Extractor::SftpFile.new( credentials: params[:sftp], remote_path: '/', pattern: /^SampleFile_(\d+)\.txt/, most_recent_only: true ), csv_options: { headers: true, col_sep: ",", encoding: "ISO-8859-1:UTF-8" }, fields: { :student_id => {}, :school_id => {}, :school_name => {}, :program => {}, :last_name => {}, :first_name => {}, :current_email => {}, :mailing_address_line_1 => {}, :mailing_address_line_2 => {}, :mailing_city => {}, :mailing_state => {}, :mailing_postal_code => {}, :birthdate => { type: :date, in_format: '%m/%d/%Y'}, :applied_date => { type: :date, in_format: '%m/%d/%Y'} } define_target :all_contacts, Remi::DataTarget::DataFrame define_target :contact_updates, Remi::DataTarget::Salesforce, credentials: params[:salesforce_credentials], object: :Contact, operation: :update, api: :bulk define_target :contact_creates, Remi::DataTarget::Salesforce, credentials: params[:salesforce_credentials], object: :Contact, operation: :create, api: :bulk define_param :program_name_lookup, RegexSieve.new({ /^BIO$/ => "Biology", /^Fake Biology$/ => nil, /(?:B|Microb)iology/ => "Biology", /^CHEM$/ => "Chemistry", /Chemistry/ => "Chemistry", /Physics/ => "Physics" }) define_transform :map_common_fields, sources: [:sample_file, :existing_contacts], targets: :all_contacts do # Exclude all source records with an invalid program name all_contacts.df = sample_file.df.dup Remi::SourceToTargetMap.apply(all_contacts.df) do map source(:program) .target(:Major__c) .transform(Remi::Transform::Lookup.new(params[:program_name_lookup])) end all_contacts.df = all_contacts.df.where(all_contacts.df[:Major__c].not_eq(nil)) student_id_to_sf_id = existing_contacts.df.map_rows { |row| [row[:External_ID__c], row[:Id]] }.to_h # Map fields that are common to both creates and updates Remi::SourceToTargetMap.apply(all_contacts.df) do # Prefixes source id record and then looks up existing salesforce Id prefixer = Remi::Transform::Prefix.new('SAMP') map source(:student_id) .target(:External_ID__c, :Id) .transform(->(row) { row[:External_ID__c] = prefixer.call(row[:student_id]) row[:Id] = student_id_to_sf_id[row[:External_ID__c]] }) end end define_transform :map_creates, sources: :all_contacts, targets: :contact_creates do work_contact_creates = all_contacts.df.where(all_contacts.df[:Id].eq(nil)) Remi::SourceToTargetMap.apply(work_contact_creates) do map source(:school_id) .target(:School_ID__c) map source(:school_name) .target(:School_Name__c) map source(:first_name) .target(:FirstName) .transform(Remi::Transform::IfBlank.new('Not Provided')) map source(:last_name) .target(:LastName) .transform(Remi::Transform::IfBlank.new('Not Provided')) map source(:mailing_city) .target(:MailingCity) map source(:mailing_state) .target(:MailingState) map source(:mailing_postal_code) .target(:MailingPostalCode) map source(:birthdate) .target(:Birthdate) .transform(Remi::Transform::FormatDate.new(in_format: sample_file.fields[:birthdate][:in_format])) map source(:applied_date) .target(:Applied_Date__c) .transform(Remi::Transform::IfBlank.new(Date.today.strftime(sample_file.fields[:applied_date][:in_format]))) .transform(Remi::Transform::FormatDate.new(in_format: sample_file.fields[:applied_date][:in_format])) map source(:mailing_address_line_1, :mailing_address_line_2) .target(:MailingStreet) .transform(->(row) { if row[:mailing_address_line_1].blank? '' else [row[:mailing_address_line_1], row[:mailing_address_line_2]].join(', ') end }) if_blank_unknown = Remi::Transform::IfBlank.new("Unknown") map source(:school_id, :school_name) .target(:School__c) .transform(->(row) { row[:school_id] = if_blank_unknown.call(row[:school_id]) row[:school_name] = if_blank_unknown.call(row[:school_name]) }) .transform(Remi::Transform::Concatenate.new('-')) map source(:current_email) .target(:Email) .transform(Remi::Transform::Replace.new(/,/, '.')) .transform(Remi::Transform::ValidateEmail.new) end contact_creates.df = work_contact_creates[ :External_ID__c, :School_ID__c, :School_Name__c, :School__c, :Major__c, :FirstName, :LastName, :Email, :MailingStreet, :MailingCity, :MailingState, :MailingPostalCode, :Birthdate, :Applied_Date__c ] end define_transform :map_updates, sources: :all_contacts, targets: :contact_updates do contact_updates.df = all_contacts.df[ :Id, :Major__c ].where(all_contacts.df[:Id].not_eq(nil)) end end