Untitled

mail@pastecode.io avatar
unknown
javascript
3 years ago
4.4 kB
3
Indexable
Never
const exchangeCard = async (req, res) => {
  const { token_user1 } = req.body;
  const cards_user1 = req.body.cardID_user1;
  const { token_user2 } = req.body;
  const cards_user2 = req.body.cardID_user2;
  try {
    let user1 = {};
    jwt.verify(token_user1, JWT_SECRET, (err, tokenInfo) => {
      user1 = tokenInfo;
    });
    let user2 = {};
    jwt.verify(token_user2, JWT_SECRET, (err, tokenInfo) => {
      user2 = tokenInfo;
    });
    const user1_id = user1.id;
    const user2_id = user2.id;

    const findUser1 = await Inventory.findOne({ user_id: user1_id }).lean();
    const findUser2 = await Inventory.findOne({ user_id: user2_id }).lean();

    const user1_cards = findUser1.inventory.filter((card) => {
      if (cards_user1.includes(card.Card.card_id)) {
        return card;
      }
    });

    const user2_cards = findUser2.inventory.filter((card) => {
      if (cards_user2.includes(card.Card.card_id)) {
        return card;
      }
    });
    // console.log("1", user1_cards, "username1 : ", findUser1.username);
    // console.log("2", user2_cards, "username2 : ", findUser2.username);
    // console.log("*****************************8");
    // console.log(findUser1.inventory);
    // console.log("*****************************8");

    if (user1_cards.length <= 0 || user2_cards.length <= 0) {
      return res
        .status(200)
        .json({ status: 200, message: "Look like you don't own that card" });
    }
    // console.log(findUser2);

    user1_cards.forEach((card) => {
      const filteredCards = findUser2.inventory.filter((userCard) => {
        return userCard.Card.card_id === card.Card.card_id;
      });
      console.log(filteredCards.length);
      if (filteredCards.length > 0) {
        console.log("error");
      } else {
        // console.log("*****************************8");
        // console.log(card);
        // console.log("*****************************8");
        Inventory.updateOne(
          { user_id: findUser2.user_id },
          { $push: { inventory: card } },
          { upsert: true }
        ).catch((err) => {
          console.log(err);
        });
      }
    });
    user2_cards.forEach((card) => {
      const filteredCards = findUser1.inventory.filter((userCard) => {
        return userCard.Card.card_id === card.Card.card_id;
      });
      console.log(filteredCards.length);
      if (filteredCards.length > 0) {
        console.log("error");
      } else {
        // console.log("*****************************8");
        // console.log(card);
        // console.log("*****************************8");
        Inventory.updateOne(
          { user_id: findUser1.user_id },
          { $push: { inventory: card } },
          { upsert: true }
        ).catch((err) => {
          console.log(err);
        });
      }
    });
    // console.log(findUser1.inventory);
    cards_user1.forEach((card) => {
      let cardExchanged = "";
      const filteredCards = findUser1.inventory.filter((userCard) => {
        // console.log(userCard.Card.card_id);
        cardExchanged = userCard;
        console.log(cardExchanged);
        return userCard.Card.card_id === card;
      });
      console.log(cardExchanged);
      console.log(filteredCards.length);
      if (filteredCards.length > 0) {
        Inventory.updateOne(
          { user_id: findUser1.user_id },
          { $pull: { inventory: cardExchanged.Card } },
          { upsert: true }
        ).catch((err) => {
          console.log(err);
        });
      } else {
        console.log("Couldn't remove");
      }
    });
    cards_user2.forEach((card) => {
      let cardExchanged = "";
      const filteredCards = findUser2.inventory.filter((userCard) => {
        cardExchanged = userCard;
        return userCard.Card.card_id === card;
      });
      console.log(filteredCards.length);
      if (filteredCards.length > 0) {
        Inventory.updateOne(
          { user_id: findUser1.user_id },
          { $pull: { inventory: cardExchanged.Card } },
          { upsert: true }
        ).catch((err) => {
          console.log(err);
        });
      } else {
        console.log("Couldn't remove");
      }
    });
    res.status(200).json({
      status: 200,
    });
  } catch (err) {
    console.log(err);
    res.sendStatus(403);
  }
};