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

async function check_in(req) {
  try {
    await sequelize.authenticate();
    let { count, pid, description } = req.body;

    for (let i = 0; i < count; i++) {
      await Item.create({ pid, description });
    }
    return { status: 'OK', count, pid };
  } catch (error) {
    console.error(error);
  }
}

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

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

export default csrf(handler);
