Check to update or create new record

Im having trouble getting expected results on this backend script

Im getting 100 latest updated Records from Salesforce Opportunity object

then im querying our Wix Collection to see how many of these records already exists, we create a list to see if any of the stage fields are different to update record keeping the long bio description from Wix and the Photo Gallery intact and updating the rest from Salesforce.

Then the records that are not found in collection insert new record to Wix collection.

Its not updating the fields correctly or creating from the new records

const limit = 100 ;

export async function mainControl ( ) {
try {
const queryResults = await postUpDogs2 ();
const existingDogsMap = await getExistingDogsMap ();

    **const**  dogsToUpdate  = []; 
    **const**  dogsToInsert  = []; 

    **for**  ( **const**  queryResult  **of**  queryResults ) { 
        **const**  existingDog  =  existingDogsMap [ queryResult . _id ]; 
        **const**  dogData  =  createDogData ( queryResult ); 
        console . log ( 'existingDog' ) 
        console . log ( existingDog ) 
        **if**  ( existingDog ) { 
            **const**  dogUpdatedFields  =  getDogUpdatedFields ( existingDog ,  queryResult ); 

            **if**  ( Object . keys ( dogUpdatedFields ). length  >  0 ) { 
                dogsToUpdate . push ({ 
                    _id :  existingDog . _id , 
                    ... dogData , 
                    ... dogUpdatedFields 
                }); 
                console . log ( 'dogsToUpdate' ) 
                console . log ( dogsToUpdate ) 
            } 
        }  **else**  { 
            dogsToInsert . push ( dogData ); 
        } 
    } 

    **await**  insertRecords ( dogsToUpdate ,  dogsToInsert ); 

}  **catch**  ( error ) { 
    console . error ( 'An error occurred:' ,  error ); 
} 

}

export async function getExistingDogsMap ( ) {
try {
const existingDogs = await getExistingDogsArray ();
console . log ( ‘existingDogs’ )
console . log ( existingDogs )
const existingDogsMap = {};

    **for**  ( **const**  dog  **of**  existingDogs ) { 
        existingDogsMap [ dog . Id ] =  dog ; 
        console . log ( 'dog' ) 
        console . log ( dog ) 
    } 

    **return**  existingDogsMap ; 
}  **catch**  ( error ) { 
    **throw new**  Error ( `Failed to get existing dogs.  ${ error }`); 
} 

}

export async function postUpDogs2 ( ) {
try {
const fields = [
“Id” ,
“Name” ,
“Description” ,
“Website_Preview_Bio__c” ,
“Website_Intake_Photo__c” ,
“DOB__c” ,
“Life_Stage__c” ,
“Gender__c” ,
“Size__c” ,
“Breed__c” ,
“StageName” ,
“Color__c” ,
“Photoset_ID__c” ,
“Arrived__c” ,
“Website_Stage__c”
];

    **const**  queryResults  =  **await**  getAllOpportunityRecords2 ( fields ); 
    **return**  queryResults ; 
}  **catch**  ( error ) { 
    **throw new**  Error ( `Failed to fetch opportunity records.  ${ error }`); 
} 

}

export async function getAllOpportunityRecords2 ( fields ) {
try {
const query = SELECT ${ fields . join ( ',' )} FROM Opportunity ORDER BY LastModifiedDate DESC LIMIT ${ limit };
const res = await queryRecords ( query );
return res . records ;
} catch ( error ) {
throw new Error ( Failed to query opportunity records. ${ error });
}
}

export async function getExistingDogsArray ( ) {
try {
const res = await wixData . query ( “Dogs” ). limit ( limit ). descending ( ‘_updatedDate’ ). find ();
return res . items ;
} catch ( error ) {
throw new Error ( Failed to get existing dogs. ${ error });
}
}

export function getDogUpdatedFields ( existingDog , queryResult ) {
const dogUpdatedFields = {};

**if**  ( existingDog . stageName  !==  queryResult . StageName ) { 
    dogUpdatedFields . stageName  =  queryResult . StageName ; 
} 

// if (existingDog.updated <= queryResult.LastModifiedDate) { 
//     dogUpdatedFields.updated = queryResult.LastModifiedDate; 
// } 

// if (existingDog.photo !== queryResult.Website_Intake_Photo__c) { 
//     dogUpdatedFields.photo = queryResult.Website_Intake_Photo__c; 
// } 

// if (existingDog.description !== queryResult.Description) { 
//     dogUpdatedFields.description = queryResult.Description; 
// } 

// if (existingDog.lifeStage !== queryResult.Life_Stage__c) { 
//     dogUpdatedFields.lifeStage = queryResult.Life_Stage__c; 
// } 

// if (existingDog.websiteStage !== queryResult.Website_Stage__c) { 
//     dogUpdatedFields.websiteStage = queryResult.Website_Stage__c; 
// } 
// dogUpdatedFields.mediaGallery 
// dogUpdatedFields.longBioDescription 

**return**  dogUpdatedFields ; 

}

function createDogData ( queryResult ) {
return {
_id : queryResult . Id ,
salesforceId : queryResult . Id ,
name : queryResult . Name ,
description : queryResult . Description ,
dob : queryResult . DOB__c ,
lifeStage : queryResult . Life_Stage__c ,
gender : queryResult . Gender__c ,
size : queryResult . Size__c ,
breed : queryResult . Breed__c ,
stageName : queryResult . StageName ,
photo : queryResult . Website_Intake_Photo__c ,
arrived : queryResult . Arrived__c ,
websiteStage : queryResult . Website_Stage__c ,
websitePreviewBio : queryResult . Website_Preview_Bio__c ,
websiteProfile : queryResult . Website_Profile__c
};
}

export async function insertRecords ( dogsToUpdate , dogsToInsert ) {
try {
if ( dogsToUpdate . length > 0 ) {
console . log ( ‘Updating dog records!’ );
const updateResults = await wixData . bulkUpdate ( “Dogs” , dogsToUpdate );
console . log ( Records updated: ${ updateResults . updated });
}
if ( dogsToInsert . length > 0 ) {
console . log ( ‘Inserting dog records!’ );
const insertResults = await wixData . bulkInsert ( “Dogs” , dogsToInsert );
console . log ( Records inserted: ${ insertResults . inserted });
}
} catch ( error ) {
throw new Error ( Failed to insert or update records. ${ error });
}
}