How can I limit cart quantity for upselling in my online store?

Hi there, we want to have upselling in the way that by adding a specific product, you will get a recommendation to add the second specific product but the quantity of the second item can’t exceed the first one. We have written the below however, it seems that the “else if” condition doesn’t work and we don’t get the alert and the customer is still able to add more of the second item (although can’t go to the checkout page).

//Function to add upsell product
async function addUpsell() {
    var isApplicable = false;
    var alreadyAdded = false;
    var item11Line = "";
    var item2Line = "";
  
      function findItem2Line(item){
        return item.id == (second item id)
      }
      
      function findItem1Line(item){
        return item.id == (first item id)
      }
  
    await fetch('/cart.js', {
        method: 'GET'
    }).then(res => res.json()).then(res => {
        console.log(res);
        if (res.total_price >= 9000 || res.items.find(item => item.id == (first item id ) && !res.items.find(item => item.id == (second item id) )) {
            isApplicable = true;
        } 
      
        else {
            isApplicable = false;
            alreadyAdded = true;
        }
    })
    
    
      
    if (isApplicable && !alreadyAdded) {
        
        var addData = {
            'id': (second item id),
          'quantity':1
        };

        fetch('/cart/add.js', {
            body: JSON.stringify(addData),
            credentials: 'same-origin',
            headers: {
                'Content-Type': 'application/json',
                'X-Requested-With': 'xmlhttprequest' 
            },
            method: 'POST'
        }).then(function(response) {
            return response.json();
        }).then(function(json) {
            ajaxCart.load();
        })

    } else if (isApplicable && alreadyAdded && data.items[data.items.findIndex(findItem2Line)].quantity == data.items[data.items.findIndex(findItem1Line)].quantity) {        
         alert('Discounted item already present in cart.')
      
    }
}

I talked to Shopify support and they gave me the link to this community as they couldn’t help.

How can I disable the add option as soon as the number of my items become equal?
Thanks.