# This is an example Remi job that was auto-generated by Remi. require_relative 'all_jobs_shared' class SampleJob include AllJobsShared using Remi::Refinements::Daru define_source :existing_contacts, Remi::DataSource::Salesforce, object: :Contact, credentials: params[:salesforce_credentials], api: :bulk, fields: { :Id => {}, :External_ID__c => {} }, 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_file: /^SampleFile_(\d+)\.txt/, remote_folder: '/', 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, format: '%m/%d/%Y'}, :applied_date => { type: :date, 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][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 map source(:student_id) .target(:External_ID__c, :Id) .transform(Remi::Transform[:prefix]['SAMP']) .transform(->(v) { [v, Remi::Transform[:lookup][student_id_to_sf_id].call(v)] }) 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].('Not Provided')) map source(:last_name) .target(:LastName) .transform(Remi::Transform[:ifblank].('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[:format_date][from_fmt: sample_file.fields[:birthdate][:format]]) map source(:applied_date) .target(:Applied_Date__c) .transform(Remi::Transform[:ifblank].(Date.today.strftime(sample_file.fields[:applied_date][:format]))) .transform(Remi::Transform[:format_date].(from_fmt: sample_file.fields[:applied_date][:format])) map source(:mailing_address_line_1, :mailing_address_line_2) .target(:MailingStreet) .transform(->(line_1, line_2) { Remi::Transform[:ifblank].(nil).call(line_1).nil? ? [] : [line_1, line_2] }) .transform(Remi::Transform[:concatenate].(', ')) map source(:school_id, :school_name) .target(:School__c) .transform(->(id, name) {[ Remi::Transform[:ifblank]["Unknown"].call(id), Remi::Transform[:ifblank]["Unknown"].call(name) ]}) .transform(Remi::Transform[:concatenate].('-')) map source(:current_email) .target(:Email) .transform(Remi::Transform[:replace].(/,/, '.')) .transform(Remi::Transform[:validate_email].call) 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