'How can I remove a product as a seller and not have it appear in cart

Right now we are able to remove a product. When we remove a product we want the CART to reflect the changes. Meaning that if a user adds an item to CART and then the seller deletes the product for sale, the user will no longer see the product in cart. Our MongoDB database is set up in this way:

MongoDB Orders MongoDB Products

Here's our code so far:

//DELETE PRODUCT
async function deleteProductInDb(uri_, seller_info) {
  try {
    const client = await  MongoClient.connect(uri_, {
      useUnifiedTopology: true, serverApi: ServerApiVersion.v1
    });
    const db = client.db("boreal_db");
    var products_tb = db.collection("products");
    const response = await products_tb.deleteOne({"_id": new ObjectID(seller_info._id)},{
    })
    client.close();
    return response;
  } catch(error) {
    client.close();
    console.log(error);
  }
}
//DELETE PRODUCT
app.delete('/deleteProduct', function(req, res) {
  console.log(req.body);
  res.set({
    'Access-Control-Allow-Origin': '*'
  })
  deleteProductInDb(uri, req.body).then(response => {console.log(response); res.send(response)});
});
// Custom useContext for the Cart 
import React, { useReducer, useContext, createContext } from "react";

const CartStateContext = createContext();
const CartDispatchContext = createContext();

/*
 Manage the state cart (array).
 ADD: Add items to cart array
 REMOVE: Remove item from cart array using index
 Throws an error if different case.
*/

const reducer = (state, action) => {
 switch (action.type) {
   case "ADD":
     return [...state, action.item];
   case "REMOVE":
     const newArr = [...state];
     newArr.splice(action.index, 1);
     return newArr;
     case "REMOVEALL":
     return [];
   default:
     throw new Error(`unknown action ${action.type}`);
 }
};

/* Context could be imported everywhere in the application
 Implemented as an array (list), with each item assigned a key. 
*/
export const CartProvider = ({ children }) => {
 const [state, dispatch] = useReducer(reducer, []);

 return (
   <CartDispatchContext.Provider value={dispatch}>
     <CartStateContext.Provider value={state}>
       {children}
     </CartStateContext.Provider>
   </CartDispatchContext.Provider>
 );
};

export const useCart = () => useContext(CartStateContext);
export const useDispatchCart = () => useContext(CartDispatchContext);


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source