import { csrf } from 'src/lib/csrf';
import { Product, sequelize } from './model';

async function destroyProduct(req) {
  try {
    await sequelize.authenticate();
    let { pid } = req.query;

    let product = await Product.findOne({ where: { pid } });
    await product.destroy();

    return { status: 'OK' };
  } catch (error) {
    console.error(error);
  }
}

async function handler(req, res) {
  try {
    let result = await destroyProduct(req);

    return res.status(200).send(result);
  } catch (err) {
    console.log(err);
    res.status(200).send({ status: 'error', message: 'helloworld error' });
  }
}

export default csrf(handler);
