import {GoodsAttributes, Goods, sequelize} from "../../models/index";
const {Op} = sequelize;
import InternalServerError from '../../utils/customErrors/internalServerError';



module.exports = async (req, res, next) => {

    const {goodsId, valueAttribute, imgUrlArray} = await GoodsAttributes.findOne({
        where: {
            id: req.params.id
        }
    });

    req.body.goodId = goodsId;
    req.body.decryptedData.pictureName = imgUrlArray;

    GoodsAttributes.destroy({
        where: {
            id: req.params.id
        }
    }).then(() => {

        Goods.findOne({
            where: {
                id: goodsId
            }
        }).then((selectedGoods) => {
            const {volumesArray} = selectedGoods;

            volumesArray.splice(
                volumesArray.indexOf(valueAttribute),
                1
            );

            selectedGoods.update({
                volumesArray: volumesArray
            }).then(() => next());
        });
    }).catch(e => {
       next(new InternalServerError());
    });
};