{"size": 2730, "ext": "js", "max_stars_repo_path": "src/app/controllers/WorkerController.js", "max_stars_repo_name": "MaisDennis/gerenteDash", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/app/controllers/WorkerController.js", "max_issues_repo_name": "MaisDennis/gerenteDash", "max_issues_repo_issues_event_min_datetime": "2020-10-12T23:35:13.000Z", "max_issues_repo_issues_event_max_datetime": "2020-10-12T23:35:13.000Z", "max_forks_repo_path": "src/app/controllers/WorkerController.js", "max_forks_repo_name": "MaisDennis/gerenteDash", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 22.9411764706, "max_line_length": 80, "alphanum_fraction": 0.4721611722}
import * as Yup from 'yup';
import { Op } from 'sequelize';
// -----------------------------------------------------------------------------
import Worker from '../models/Worker';
import File from '../models/File';
// -----------------------------------------------------------------------------
class WorkerController {
  async store(req, res) {
    const schema = Yup.object().shape({
      name: Yup.string().required(),
      dept: Yup.string(),
      phonenumber: Yup.string()
        .required()
        .min(11),
      worker_password: Yup.string()
        .required()
        .min(1),
      gender: Yup.string(),
    });
    if (!(await schema.isValid(req.body))) {
      return res.status(400).json({ error: 'Create failed' });
    }

    const workerExists = await Worker.findOne({
      where: { name: req.body.name },
    });
    if (workerExists) {
      return res
        .status(400)
        .json({ error: 'Create failed: Worker already exists.' });
    }

    const {
      name,
      dept,
      phonenumber,
      worker_password,
      gender,
      user_id,
    } = req.body;

    const lastFourDigits = phonenumber.slice(-4);

    const worker = await Worker.create({
      name,
      dept,
      phonenumber,
      phonenumber_lastfourdigits: lastFourDigits,
      worker_password,
      gender,
      user_id,
    });
    return res.json(worker);
  }

  // ---------------------------------------------------------------------------
  async update(req, res) {
    const { id } = req.params;
    const {
      name,
      dept,
      phonenumber,
      worker_password,
      gender,
      canceled_at,
    } = req.body;
    // const canceled_at = new Date();

    const lastFourDigits = phonenumber.slice(-4);

    let worker = await Worker.findByPk(id);

    worker = await worker.update({
      name,
      dept,
      phonenumber,
      phonenumber_lastfourdigits: lastFourDigits,
      worker_password,
      gender,
      canceled_at,
    });

    return res.json(worker);
  }

  // ---------------------------------------------------------------------------
  async index(req, res) {
    const { nameFilter, userID } = req.query;
    const workers = await Worker.findAll({
      attributes: [
        'id',
        'name',
        'dept',
        'phonenumber',
        'worker_password',
        'gender',
        'avatar_id',
        'user_id',
      ],
      where: {
        name: {
          [Op.like]: `%${nameFilter}%`,
        },
        user_id: userID,
      },
      include: [
        {
          model: File,
          as: 'avatar',
          attributes: ['name', 'path', 'url'],
        },
      ],
    });
    return res.json(workers);
  }
}

export default new WorkerController();
