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

async function SaveOrder(req) {
  try {
    await sequelize.authenticate();
    let { custom_id } = req.body;

    let order = await Order.update({ order_status: 'APPROVED' }, { where: { custom_id } });

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

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

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

export default csrf(handler);
