{"size": 1686, "ext": "js", "max_stars_repo_path": "routes/sub/update.js", "max_stars_repo_name": "Sampaguitas/pdb_server", "max_stars_repo_stars_event_min_datetime": "2019-05-20T03:52:33.000Z", "max_stars_repo_stars_event_max_datetime": "2019-05-20T03:52:33.000Z", "max_issues_repo_path": "routes/sub/update.js", "max_issues_repo_name": "Sampaguitas/pdb_server", "max_issues_repo_issues_event_min_datetime": "2020-03-30T07:10:05.000Z", "max_issues_repo_issues_event_max_datetime": "2020-11-17T06:58:40.000Z", "max_forks_repo_path": "routes/sub/update.js", "max_forks_repo_name": "Sampaguitas/pdb_server", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 4, "max_forks_count": null, "avg_line_length": 33.0588235294, "max_line_length": 102, "alphanum_fraction": 0.456109134}
const express = require('express');
const router = express.Router();
const Sub = require('../../models/Sub');
const _ = require('lodash');

router.put('/', (req, res) => {
    var data = {};
    const id = decodeURI(req.query.id);
    
    Object.keys(req.body).forEach(function (k) {
        data[k] = decodeURI(req.body[k]);
    });

    if (data.inspRelDate || data.relQty) {
        Sub.findById(id, function (err, sub) {
            if (!sub) {
                return res.status(400).json({ message: 'Could not update Sub information.' });
            } else {
                // if (data.inspRelDate && sub.rfiQty && !sub.relQty) {
                //     data.relQty = sub.rfiQty;
                // } else 
                if (!!data.relQty && !sub.inspRelDate) {
                    data.inspRelDate = new Date();
                }
                Sub.findByIdAndUpdate(id, { $set: data }, { new: true }, function (err, sub) {
                    if (!sub) {
                        return res.status(400).json({ message: 'Could not update Sub information.' });
                    }
                    else {
                        return res.status(200).json({ message: 'Sub has been updated.' });
                    }
                });
            }
        });
    } else {
        Sub.findByIdAndUpdate(id, { $set: data }, { new: true }, function (err, sub) {
            if (!sub) {
                return res.status(400).json({ message: 'Could not update Sub information.' });
            }
            else {
                return res.status(200).json({ message: 'Sub has been updated.' });
            }
        });
    }

    
    
});

module.exports = router;
