1- import { log } from ' console' ;
1+ import { log } from " console" ;
22
3- import config from ' @/config' ;
4- import createIntuitService , { IntuitService } from ' @/services/intuit/service' ;
5- import SalesforceService from ' @/services/salesforce' ;
6- import createLogger from ' @/utils/logger' ;
3+ import config from " @/config" ;
4+ import createIntuitService , { IntuitService } from " @/services/intuit/service" ;
5+ import SalesforceService from " @/services/salesforce" ;
6+ import createLogger from " @/utils/logger" ;
77import {
8- Account , ItemRef , Product , QuickbooksCreateCustomerInput , QuickbooksCreateEstimateInput , QuickbooksCustomer ,
9- QuickbooksEstimate , QuickbooksEstimateResponse , QuickbooksFindCustomersInput ,
10- SalesforceClosedWonResource
11- } from '@/utils/types' ;
12- import { isProduction } from '@/utils/utils' ;
8+ Account ,
9+ ItemRef ,
10+ Product ,
11+ QuickbooksCreateCustomerInput ,
12+ QuickbooksCreateEstimateInput ,
13+ QuickbooksCustomer ,
14+ QuickbooksEstimate ,
15+ QuickbooksEstimateResponse ,
16+ QuickbooksFindCustomersInput ,
17+ SalesforceClosedWonResource ,
18+ } from "@/utils/types" ;
19+ import { isProduction } from "@/utils/utils" ;
1320
1421const logger = createLogger ( "Intuit Processor" ) ;
1522
@@ -131,7 +138,7 @@ const processCustomerHierarchy = async (
131138 const customers = [ ] ;
132139
133140 for ( const resource of resources ) {
134- const { account, parent } = resource ;
141+ const { account, parent, contact } = resource ;
135142 const accountProducerId = isProduction ? account ?. AVSFQB__Quickbooks_Id__c : account . QBO_Account_ID_Staging__c ;
136143
137144 if ( parent ?. Id ) {
@@ -178,6 +185,9 @@ const processCustomerHierarchy = async (
178185 const customer = await processCustomer ( service , accountProducerId , account . Id , {
179186 DisplayName : account . Name ,
180187 CompanyName : account . Name ,
188+ PrimaryEmailAddr : {
189+ Address : contact . Email ,
190+ } ,
181191 BillAddr : {
182192 City : account . BillingCity ,
183193 Line1 : account . BillingStreet ,
@@ -199,82 +209,78 @@ const processCustomerHierarchy = async (
199209 return customers ;
200210} ;
201211
202- const processItemRef = async (
203- service : IntuitService ,
204- product : Product
205- ) : Promise < ItemRef > => {
212+ const processItemRef = async ( service : IntuitService , product : Product ) : Promise < ItemRef > => {
206213 let itemRef : ItemRef | null = null ;
207214
208215 logger . debug ( `Finding item with Quickbooks ID: ${ product [ "AVSFQB__Quickbooks_Id__c" ] } ` ) ;
209216
210- const id = [ ] ;
211- product [ "AVSFQB__Quickbooks_Id__c" ] &&
212- id . push ( { field : "Id" , operator : "=" , value : product [ "AVSFQB__Quickbooks_Id__c" ] } ) ;
213-
214- id . length > 0 &&
215- ( await service . items
216- . find ( [ { field : "Id" , operator : "=" , value : product [ "AVSFQB__Quickbooks_Id__c" ] } ] )
217- . then ( ( items ) => {
218- if ( items ?. QueryResponse ?. Item ?. length >= 1 ) {
219- logger . info ( `Items found via Quickbooks ID in salesforce: ${ JSON . stringify ( items , null , 2 ) } ` ) ;
220- itemRef = {
221- name : items . QueryResponse . Item [ 0 ] . Name ,
222- value : items . QueryResponse . Item [ 0 ] . Id ,
223- } ;
224- }
225- } )
226- . catch ( ( err ) => {
227- logger . error ( { message : "Error finding item by Quickbooks ID" , err } ) ;
228- } ) ) ;
229-
230- if ( ! id ) logger . warn ( `No Quickbooks ID found for product: ${ product . Name } ` ) ;
231-
232- if ( ! itemRef ) {
233- logger . warn ( `Item not found with ID, finding by Sku: ${ product . Name } ` ) ;
234- logger . debug ( `Finding item by SKU: ${ product . ProductCode } ` ) ;
235-
236- await service . items
237- . find ( [ { field : "Sku" , operator : "LIKE" , value : product . ProductCode } ] )
238- . then ( ( items ) => {
239- if ( items ?. QueryResponse ?. Item ?. length >= 1 ) {
240- logger . info ( `Items found via Sku: ${ JSON . stringify ( items , null , 2 ) } ` ) ;
241- itemRef = {
242- name : items . QueryResponse . Item [ 0 ] . Name ,
243- value : items . QueryResponse . Item [ 0 ] . Id ,
244- } ;
245- }
246- } )
247- . catch ( ( err ) => {
248- logger . error ( { message : "Error finding item by SKU" , err } ) ;
249- } ) ;
250- }
217+ const id = [ ] ;
218+ product [ "AVSFQB__Quickbooks_Id__c" ] &&
219+ id . push ( { field : "Id" , operator : "=" , value : product [ "AVSFQB__Quickbooks_Id__c" ] } ) ;
220+
221+ id . length > 0 &&
222+ ( await service . items
223+ . find ( [ { field : "Id" , operator : "=" , value : product [ "AVSFQB__Quickbooks_Id__c" ] } ] )
224+ . then ( ( items ) => {
225+ if ( items ?. QueryResponse ?. Item ?. length >= 1 ) {
226+ logger . info ( `Items found via Quickbooks ID in salesforce: ${ JSON . stringify ( items , null , 2 ) } ` ) ;
227+ itemRef = {
228+ name : items . QueryResponse . Item [ 0 ] . Name ,
229+ value : items . QueryResponse . Item [ 0 ] . Id ,
230+ } ;
231+ }
232+ } )
233+ . catch ( ( err ) => {
234+ logger . error ( { message : "Error finding item by Quickbooks ID" , err } ) ;
235+ } ) ) ;
236+
237+ if ( ! id ) logger . warn ( `No Quickbooks ID found for product: ${ product . Name } ` ) ;
238+
239+ if ( ! itemRef ) {
240+ logger . warn ( `Item not found with ID, finding by Sku: ${ product . Name } ` ) ;
241+ logger . debug ( `Finding item by SKU: ${ product . ProductCode } ` ) ;
242+
243+ await service . items
244+ . find ( [ { field : "Sku" , operator : "LIKE" , value : product . ProductCode } ] )
245+ . then ( ( items ) => {
246+ if ( items ?. QueryResponse ?. Item ?. length >= 1 ) {
247+ logger . info ( `Items found via Sku: ${ JSON . stringify ( items , null , 2 ) } ` ) ;
248+ itemRef = {
249+ name : items . QueryResponse . Item [ 0 ] . Name ,
250+ value : items . QueryResponse . Item [ 0 ] . Id ,
251+ } ;
252+ }
253+ } )
254+ . catch ( ( err ) => {
255+ logger . error ( { message : "Error finding item by SKU" , err } ) ;
256+ } ) ;
257+ }
251258
252- if ( ! itemRef ) {
253- logger . warn ( `Item not found with Quickbooks ID: ${ product [ "AVSFQB__Quickbooks_Id__c" ] } ` ) ;
254- logger . debug ( `Finding item with Name: ${ product . Name } ` ) ;
255- await service . items
256- . find ( [ { field : "Name" , operator : "LIKE" , value : product . Name } ] )
257- . then ( ( items ) => {
258- if ( items ?. QueryResponse ?. Item ?. length >= 1 ) {
259- logger . info ( `Items found via Name: ${ JSON . stringify ( items , null , 2 ) } ` ) ;
260- itemRef = {
261- name : items . QueryResponse . Item [ 0 ] . Name ,
262- value : items . QueryResponse . Item [ 0 ] . Id ,
263- } ;
264- }
265- } )
266- . catch ( ( err ) => {
267- logger . error ( { message : "Error finding item by Name" , err } ) ;
268- } ) ;
269- }
259+ if ( ! itemRef ) {
260+ logger . warn ( `Item not found with Quickbooks ID: ${ product [ "AVSFQB__Quickbooks_Id__c" ] } ` ) ;
261+ logger . debug ( `Finding item with Name: ${ product . Name } ` ) ;
262+ await service . items
263+ . find ( [ { field : "Name" , operator : "LIKE" , value : product . Name } ] )
264+ . then ( ( items ) => {
265+ if ( items ?. QueryResponse ?. Item ?. length >= 1 ) {
266+ logger . info ( `Items found via Name: ${ JSON . stringify ( items , null , 2 ) } ` ) ;
267+ itemRef = {
268+ name : items . QueryResponse . Item [ 0 ] . Name ,
269+ value : items . QueryResponse . Item [ 0 ] . Id ,
270+ } ;
271+ }
272+ } )
273+ . catch ( ( err ) => {
274+ logger . error ( { message : "Error finding item by Name" , err } ) ;
275+ } ) ;
276+ }
270277
271- if ( ! itemRef ) {
272- logger . warn ( `Item not found with SKU: ${ product . ProductCode } ` ) ;
273- }
278+ if ( ! itemRef ) {
279+ logger . warn ( `Item not found with SKU: ${ product . ProductCode } ` ) ;
280+ }
274281
275282 return itemRef ;
276- }
277-
283+ } ;
278284
279285const processEstimate = async (
280286 service : IntuitService ,
@@ -304,12 +310,14 @@ const processEstimate = async (
304310
305311 // check if the product exists in the products array, if not use the default item ref
306312 const isProductAvailable = ! ! products [ i ] ;
307- const itemRef = isProductAvailable ? await processItemRef ( service , products [ i ] ) . catch ( ( err ) => {
308- logger . error ( { message : "Error finding item" , err } ) ;
309- return null ;
310- } ) : null ;
313+ const itemRef = isProductAvailable
314+ ? await processItemRef ( service , products [ i ] ) . catch ( ( err ) => {
315+ logger . error ( { message : "Error finding item" , err } ) ;
316+ return null ;
317+ } )
318+ : null ;
311319
312- if ( ! opportunityLineItem . ServiceDate ) {
320+ if ( ! opportunityLineItem . ServiceDate ) {
313321 logger . warn ( `ServiceDate not found for opportunity line item: ${ opportunityLineItem . Id } ` ) ;
314322 }
315323
@@ -406,7 +414,9 @@ const createIntuitProcessor = async () => {
406414 const { opportunityId, Id } = estimate ;
407415 const result = await salesforce . mutation . updateOpportunity ( {
408416 Id : opportunityId ,
409- AVSFQB__QB_ERROR__C : estimate . productError ? `Error! Please double check Products in Quickbooks Estimate: txnId=` + estimate . Id : "Estimate Created by Engineering" ,
417+ AVSFQB__QB_ERROR__C : estimate . productError
418+ ? `Error! Please double check Products in Quickbooks Estimate: txnId=` + estimate . Id
419+ : "Estimate Created by Engineering" ,
410420 ...( ! isProduction && { QBO_Oppty_ID_Staging__c : estimate . Id } ) ,
411421 //* Only mutate this field in production
412422 ...( isProduction && { AVFSQB__Quickbooks_Id__c : estimate . Id } ) ,
0 commit comments