{"size": 2089, "ext": "js", "max_stars_repo_path": "src/controllers/ward.controller.js", "max_stars_repo_name": "alentar/rpms-server", "max_stars_repo_stars_event_min_datetime": "2018-03-26T09:19:18.000Z", "max_stars_repo_stars_event_max_datetime": "2018-05-28T14:20:17.000Z", "max_issues_repo_path": "src/controllers/ward.controller.js", "max_issues_repo_name": "alentar/rpms-server", "max_issues_repo_issues_event_min_datetime": "2018-03-03T11:19:50.000Z", "max_issues_repo_issues_event_max_datetime": "2018-07-18T12:12:14.000Z", "max_forks_repo_path": "src/controllers/ward.controller.js", "max_forks_repo_name": "alentar/rpms-server", "max_forks_repo_forks_event_min_datetime": "2018-03-03T02:31:31.000Z", "max_forks_repo_forks_event_max_datetime": "2018-03-23T13:04:13.000Z"}
{"max_stars_count": 2, "max_issues_count": 32, "max_forks_count": 3, "avg_line_length": 28.2297297297, "max_line_length": 125, "alphanum_fraction": 0.6706558162}
'use strict'

const Ward = require('../models/ward.model')
const httpStatus = require('http-status')
const APIError = require('../utils/APIError')
const ObjectId = require('mongoose').Types.ObjectId
const _ = require('lodash')

exports.create = async (req, res, next) => {
  try {
    const ward = new Ward(_.omit(req.body, ['beds']))

    const savedWard = await ward.save()
    res.status(httpStatus.CREATED)
    res.send({
      ward: savedWard
    })
  } catch (err) {
    return next(Ward.checkDuplicateWardError(err))
  }
}

exports.delete = async (req, res, next) => {
  try {
    if (!ObjectId.isValid(req.params.wardId)) throw new APIError('Invalid resource identifier', httpStatus.NOT_FOUND)

    const ward = await Ward.findByIdAndRemove(req.params.wardId)

    if (!ward) throw new APIError('Requested resource not found', httpStatus.NOT_FOUND)

    return res.json({success: true})
  } catch (err) {
    next(err)
  }
}

exports.update = async (req, res, next) => {
  try {
    if (!ObjectId.isValid(req.params.wardId)) throw new APIError('Invalid resource identifier', httpStatus.NOT_FOUND)

    const wardObject = _.omit(req.body, ['beds'])
    const ward = await Ward.findByIdAndUpdate(req.params.wardId, wardObject, { new: true })

    if (!ward) throw new APIError('Requested resource not found', httpStatus.NOT_FOUND)

    return res.json({ ward })
  } catch (error) {
    return next(Ward.checkDuplicateWardError(error))
  }
}

exports.view = async (req, res, next) => {
  try {
    if (!ObjectId.isValid(req.params.wardId)) throw new APIError('Invalid resource identifier', httpStatus.NOT_FOUND)

    const ward = await Ward.findById(req.params.wardId).populate('beds.patient', ['name', 'age', 'admittedAt', 'sex', 'bht'])

    if (!ward) throw new APIError('Requested resource not found', httpStatus.NOT_FOUND)

    return res.json({ ward })
  } catch (error) {
    return next(error)
  }
}

exports.index = async (req, res, next) => {
  try {
    const result = await Ward.list(req.query)
    return res.json(result)
  } catch (error) {
    return next(error)
  }
}
