import {Orders, GoodsAttributes, Goods, sequelize} from "../../models";
import {orderStatus} from "../../utils/constans/orders";

const {Op} = sequelize;

module.exports = async (req, res, next) => {
    const userOrdersArray = [];
    const orders = await Orders.findAll({
        where: {
            userId: req.body.id
        },
        include: {
            model: GoodsAttributes,
            attributes: ["imgUrlArray", 'valueAttribute', 'nameAttribute', 'price', 'id'],
            include: {
                model: Goods,
                attributes: ['id', 'goodsName', 'goodsDescription']
            }
        },
        attributes: ['goodsId', 'orderStatus', 'count'],
        order: [['orderStatus', 'asc']]
    });

    orders.map(elem => {
        const {id, goodsName, goodsDescription} = elem.GoodsAttribute.Good;
        const {orderStatus, count} = elem;
        userOrdersArray.push({
            id: id,
            goodsName: goodsName,
            goodsDescription: goodsDescription,
            orderStatus: orderStatus,
            count: count,
            GoodsAttributes: [
                elem.GoodsAttribute
            ]
        });
    });
   req.body.userOrdersArray = userOrdersArray;
   next();
};