import {Goods, GoodsAttributes, GoodsFavourites, sequelize} from "../../models";
const {Op} = sequelize;

module.exports = async (req, res, next) => {
    const favouriteGoodsArray = [];
    const favouriteGoods = await GoodsFavourites.findAll({
        where: {
            userId: req.body.id
        },
        include: {
          model: GoodsAttributes,
          attributes: ["imgUrlArray", 'valueAttribute', 'nameAttribute', 'price', 'id'],
          include: {
            model: Goods,
            attributes: ['id', 'goodsName', 'goodsDescription']
          }
        },
        attributes: ['goodsId']
    });

    favouriteGoods.map(elem => {
        const {id, goodsName, goodsDescription} = elem.GoodsAttribute.Good;
        favouriteGoodsArray.push({
            id: id,
            goodsName: goodsName,
            goodsDescription: goodsDescription,
            GoodsAttributes: [
                elem.GoodsAttribute
            ]
        });
    });

    req.body.favouriteGoodsArray = favouriteGoodsArray;
    next();
};