@@ -610,39 +610,57 @@ async function fetchOdooSuppliers(
610610) : Promise < { success : boolean ; suppliers : OdooSupplier [ ] ; error ?: string } > {
611611 const normalizedUrl = normalizeOdooUrl ( url )
612612 try {
613+ console . log ( '[Odoo] Authenticating to:' , normalizedUrl )
614+
613615 // Authenticate first
614616 const uid = await odooXmlRpc ( normalizedUrl , 'common' , 'authenticate' , [
615617 database , username , apiKey , { }
616618 ] )
617619
620+ console . log ( '[Odoo] Auth result uid:' , uid )
621+
618622 if ( ! uid || uid === false ) {
619623 return { success : false , suppliers : [ ] , error : 'Authentication failed' }
620624 }
621625
622626 // Search for suppliers (partners with supplier_rank > 0)
627+ console . log ( '[Odoo] Searching for suppliers...' )
623628 const supplierIds = await odooXmlRpc ( normalizedUrl , 'object' , 'execute_kw' , [
624629 database , uid , apiKey ,
625630 'res.partner' , 'search' ,
626631 [ [ [ 'supplier_rank' , '>' , 0 ] ] ] ,
627632 { limit : 5000 } // Reasonable limit
628- ] ) as number [ ]
633+ ] )
629634
630- if ( ! supplierIds || supplierIds . length === 0 ) {
635+ console . log ( '[Odoo] Supplier IDs result:' , typeof supplierIds , Array . isArray ( supplierIds ) ? supplierIds . length : supplierIds )
636+
637+ // Ensure supplierIds is an array
638+ const ids = Array . isArray ( supplierIds ) ? supplierIds : [ ]
639+
640+ if ( ids . length === 0 ) {
641+ console . log ( '[Odoo] No suppliers found' )
631642 return { success : true , suppliers : [ ] }
632643 }
633644
634645 // Read supplier details
635- const suppliers = await odooXmlRpc ( normalizedUrl , 'object' , 'execute_kw' , [
646+ console . log ( '[Odoo] Reading' , ids . length , 'supplier details...' )
647+ const suppliersResult = await odooXmlRpc ( normalizedUrl , 'object' , 'execute_kw' , [
636648 database , uid , apiKey ,
637649 'res.partner' , 'read' ,
638- [ supplierIds , [
650+ [ ids , [
639651 'id' , 'name' , 'ref' , 'email' , 'phone' , 'mobile' , 'website' ,
640652 'street' , 'street2' , 'city' , 'zip' , 'state_id' , 'country_id' , 'active'
641653 ] ]
642- ] ) as OdooSupplier [ ]
654+ ] )
655+
656+ console . log ( '[Odoo] Suppliers result type:' , typeof suppliersResult , Array . isArray ( suppliersResult ) ? suppliersResult . length : 'not array' )
657+
658+ // Ensure result is an array
659+ const suppliers = Array . isArray ( suppliersResult ) ? suppliersResult as OdooSupplier [ ] : [ ]
643660
644661 return { success : true , suppliers }
645662 } catch ( err ) {
663+ console . error ( '[Odoo] Error:' , err )
646664 return { success : false , suppliers : [ ] , error : String ( err ) }
647665 }
648666}
@@ -3483,10 +3501,25 @@ export async function buildServer(): Promise<FastifyInstance> {
34833501 return reply . code ( 400 ) . send ( { error : 'Sync failed' , message : odooSuppliers . error } )
34843502 }
34853503
3504+ // Safety check - ensure suppliers is an array
3505+ const suppliers = Array . isArray ( odooSuppliers . suppliers ) ? odooSuppliers . suppliers : [ ]
3506+ console . log ( `[Odoo Sync] Found ${ suppliers . length } suppliers from Odoo` )
3507+
3508+ if ( suppliers . length === 0 ) {
3509+ return {
3510+ success : true ,
3511+ created : 0 ,
3512+ updated : 0 ,
3513+ skipped : 0 ,
3514+ errors : 0 ,
3515+ message : 'No suppliers found in Odoo with supplier_rank > 0'
3516+ }
3517+ }
3518+
34863519 // Process suppliers
34873520 let created = 0 , updated = 0 , skipped = 0 , errors = 0
34883521
3489- for ( const odooSupplier of odooSuppliers . suppliers ) {
3522+ for ( const odooSupplier of suppliers ) {
34903523 try {
34913524 // Check if supplier already exists by erp_id
34923525 const { data : existing } = await request . supabase !
0 commit comments