All files / mylog-api/controllers limitations.controller.js

21.05% Statements 12/57
7.69% Branches 2/26
9.09% Functions 1/11
21.05% Lines 12/57

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 1582x 2x 2x 2x                                                               2x                                                                     2x                                   2x                                         2x                                 2x 4x     2x                         2x                            
const Limitation = require("../models/limitation.model.js");
const { body, validationResult } = require('express-validator')
const luxon = require("luxon");
exports.create = (req, res) => {
    // Validate request
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
        console.log(errors.array());
        return res.status(422).json({ errors: errors.array() });
    }
    var utcVersion = luxon.DateTime.fromSQL(req.body.Version).toISO({ includeOffset: false, suppressMilliseconds: true });
    // Create a Simulator
    const limitation = {
        PilotID: req.body.PilotID,
        DurationType: req.body.DurationType,
        Duration: req.body.Duration,
        TimeType: req.body.TimeType,
        ValueType: req.body.ValueType,
        LimitType: req.body.LimitType,
        Value: req.body.Value,
        Version: utcVersion,
        Status: req.body.Status,
        Name: req.body.Name
    };
 
    Limitation.create(limitation, (err, data) => {
        if (err)
            res.status(500).send({
                message:
                    err.message || "Some error occurred while creating the limitation."
            });
        else res.send(data);
    });
};
 
exports.update = (req, res) => {
    console.log(req.body);
    // Validate request
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
        console.log(errors.array());
        return res.status(422).json({ errors: errors.array() });
    }
    var utcVersion = luxon.DateTime.fromSQL(req.body.Version).toISO({ includeOffset: false, suppressMilliseconds: true });
    // Create a Simulator
    console.log(req.body);
    const limitation = {
        ID: req.body.ID,
        PilotID: req.body.PilotID,
        DurationType: req.body.DurationType,
        Duration: req.body.Duration,
        TimeType: req.body.TimeType,
        ValueType: req.body.ValueType,
        LimitType: req.body.LimitType,
        Value: req.body.Value,
        Version: utcVersion,
        Status: req.body.Status,
        Name: req.body.Name
    };
 
    Limitation.update(limitation, (err, data) => {
        if (err)
            res.status(500).send({
                message:
                    err.message || "Some error occurred while updating the limitation."
            });
        else res.send(data);
    });
};
 
exports.getAllByPilotID = (req, res) => {
    var pilotID = req.query.PilotID;
    Limitation.get(pilotID, (err, data) => {
        if (err) {
            if (err.kind === "not_found") {
                res.status(404).send({
                    message: `Not found Limitations`
                });
            } else {
                res.status(500).send({
                    message: "Error retrieving Limitations"
                });
            }
        }
        else res.send(data);
    });
};
 
exports.deleteLimitationByPilotID = (req, res) => {
    var pilotID = req.body.PilotID;
    var id = req.body.ID;
    var version = req.body.Version;
    Limitation.delete(pilotID, id, version, (err, data) => {
        if (err) {
            if (err.kind === "not_found") {
                res.status(404).send({
                    message: `Not found Limitations`
                });
            } else {
                res.status(500).send({
                    message: "Error deleting Limitations"
                });
            }
        }
        else res.send(data);
    });
};
 
 
exports.sync = (req, res) => {
    var version = req.query.version;
    var utcVersion = luxon.DateTime.fromSQL(version).toISO({ includeOffset: false, suppressMilliseconds: true });
 
    var pilotID = req.query.pilotID;
 
    Limitation.sync(pilotID, utcVersion, (err, data) => {
        if (err)
            res.status(500).send({
                message:
                    err.message || "Some error occurred while syncing limitations."
            });
        else res.send(data);
    });
 
}
 
exports.validate = (method) => {
    switch (method) {
 
        case 'create': {
            return [
                body('Duration', "Duration is empty").exists().isInt(),
                body('PilotID', "pilotID is invalid").exists().isInt(),
                body('DurationType', "DurationType is invalid").exists().isInt(),
                body('TimeType', "timeType is empty").exists().isInt(),
                body('ValueType', "ValueType is empty").exists().isInt(),
                body('LimitType', "LimitType is empty").exists().isInt(),
                body('Value', "Value is empty").exists(),
                body('Name', "name is empty").exists(),
                body('Status', "status is empty").exists(),
            ]
        }
        case 'update': {
            return [
                body('ID', "ID is empty").exists().isInt(),
                body('Duration', "Duration is empty").exists().isInt(),
                body('PilotID', "pilotID is invalid").exists().isInt(),
                body('DurationType', "DurationType is invalid").exists().isInt(),
                body('TimeType', "timeType is empty").exists().isInt(),
                body('ValueType', "ValueType is empty").exists().isInt(),
                body('LimitType', "LimitType is empty").exists().isInt(),
                body('Value', "Value is empty").exists(),
                body('Name', "name is empty").exists(),
                body('Status', "status is empty").exists(),
            ]
        }
    }
};