import * as pulumi from "@pulumi/pulumi" ; import * as cloud from "@pulumi/cloud" ; import { poll } from "./poll" ; let config = new pulumi . Config ( "salesforce" ) ; let salesforceEmail = config . require ( "email" ) ; let salesforcePassword = config . require ( "password" ) ; async function getAuthenticatedSalesforceConnection ( ) { let jsforce = require ( "jsforce" ) ; console . log ( `loaded jsforce` ) ; let conn = new jsforce . Connection ( ) ; let auth = await conn . login ( salesforceEmail , salesforcePassword ) ; console . log ( `authed with Salesforce: ${ JSON . stringify ( auth , null , "" ) }` ) ; return conn ; } ; export function query ( name , soql , watermarkDefault , watermarkField , watermarkSelection ) { let queryPoll = poll < Record > ( name , { minutes : 1 } , async ( watermark ) => { let conn = await getAuthenticatedSalesforceConnection ( ) ; if ( watermark === undefined ) { watermark = watermarkDefault ; } let queryText = soql ( watermark ) ; console . log ( `query text: ${ queryText }` ) ; let res = await conn . query ( queryText ) . run ( { autoFetch : true } ) ; console . log ( `data from Salesforce: ${ JSON . stringify ( res , null , "" ) }` ) ; watermark = res . records . reduce ( ( a , b ) => watermarkSelection ( a , b [ watermarkField ] ) , watermark ) ; return { nextToken : watermark , items : res . records , } ; } ) ; return queryPoll ; } export let queryAll = async ( soql ) => { let conn = await getAuthenticatedSalesforceConnection ( ) ; console . log ( `query text: ${ soql }` ) ; let res = await conn . query ( soql ) . run ( { autoFetch : true } ) ; console . log ( `data from Salesforce: ${ JSON . stringify ( res , null , "" ) }` ) ; if ( ! res . done ) { throw new Error ( `expected to fetch all results - got ${ res . records . length } of ${ < any > res . totalSize }` ) ; } return < any > res . records ; } ; export type Record = { [ property ] : any ; } ; interface QueryResult { done : boolean ; nextRecordsUrl ? : string ; totalSize : number ; records : Record [ ] ; } export function allObjectModifications ( name , object , fields ) { if ( fields . length === 0 ) { throw new Error ( "Expect at least one field name in the format `FieldA,FieldB`" ) ; } return query ( "contacts" , ( timestamp ) => `SELECT ${ fields },LastModifiedDate FROM ${ object } WHERE LastModifiedDate > ${ timestamp }` , "2000-01-01T00:00:00.000Z" , "LastModifiedDate" , ( a , b ) => a > b ? a : b ) ; } export let insert = async ( tableName , object ) => { let conn = await getAuthenticatedSalesforceConnection ( ) ; let record = await conn . sobject ( tableName ) . insert ( object ) ; console . log ( record ) ; } ;