All files / mylog-api/models logEntry.model.js

6.25% Statements 4/64
0% Branches 0/14
0% Functions 0/15
6.25% Lines 4/64

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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 2002x 2x 2x                                                                                                                                                                                                                                                                                                                                                                                                         2x
const sql = require("./db.js");
const moment = require("moment")
const luxon = require("luxon");
class LogEntry {
    constructor(logEntry) {
        this.PilotID = logEntry.PilotID,
            this.Registration = logEntry.Registration,
            this.BlockOffTime = logEntry.BlockOffTime,
            this.DepartureAirportID = logEntry.DepartureAirportID,
            this.DestinationAirportID = logEntry.DestinationAirportID,
            this.BlockOnTime = logEntry.BlockOnTime,
            this.TotalTime = logEntry.TotalTime,
            this.SP_SE_Time = logEntry.SP_SE_Time,
            this.SP_ME_Time = logEntry.SP_ME_Time,
            this.MultiPilotTime = logEntry.MultiPilotTime,
            this.PIC = logEntry.PIC,
            this.DayLanding = logEntry.DayLanding,
            this.NightLanding = logEntry.NightLanding,
            this.IFRTime = logEntry.IFRTime,
            this.NightTime = logEntry.NightTime,
            this.PICTime = logEntry.PICTime,
            this.COPilotTime = logEntry.COPilotTime,
            this.DualTime = logEntry.DualTime,
            this.SimTime = logEntry.SimTime,
            this.InstructorTime = logEntry.InstructorTime,
            this.Remarks = logEntry.Remarks,
            this.FlightCrewList = logEntry.FlightCrewList
    }
 
    static create(newLogEntry, result) {
 
        newLogEntry.Status = 5;
        sql.query("INSERT INTO LogEntries SET ?", newLogEntry, (err, res) => {
            if (err) {
                console.log("error: ", err);
                result(err, null);
                return;
            }
 
 
            console.log("crated logEntry: ", { ID: res.insertId, ...newLogEntry });
            const apiResponse = {
                ops: "LogEntryAdd",
                status: "success",
                message: "Log Entry created",
                payload: { ID: res.insertId, ...newLogEntry },
                affectedRows: 1
            };
            result(null, apiResponse);
        });
    }
 
    static update(logEntry, result) {
 
        logEntry.Status = 5
        sql.query("UPDATE LogEntries SET ? where ID = ?", [logEntry, logEntry.ID], (err, res) => {
            if (err) {
                console.log("error: ", err);
                result(err, null);
                return;
            }
 
 
            console.log("updated logEntry: ", { ID: logEntry.ID, ...logEntry });
            const apiResponse = {
                ops: "LogEntryUpdate",
                status: "success",
                message: "Log Entry updated",
                payload: { ID: logEntry.ID, ...logEntry },
                affectedRows: 1
            };
            result(null, apiResponse);
        });
    }
 
    static getRecentsByPilotID(pilotID, result) {
        var query = `SELECT L.*,
        CAST(JSON_UNQUOTE(JSON_OBJECT('ID', Dep.ID, 'Name', Dep.Name, 'IATA_CODE', Dep.IATA_CODE, 'ICAO_CODE', Dep.ICAO_CODE, 'Coordinates', Dep.Coordinates, 'Elevation', Dep.Elevation,'City', Dep.City, 'Country', Dep.Country)) as JSON) as DepartureAirport,
        CAST(JSON_UNQUOTE(JSON_OBJECT('ID', Dest.ID, 'Name', Dest.Name, 'IATA_CODE', Dest.IATA_CODE, 'ICAO_CODE', Dest.ICAO_CODE, 'Coordinates', Dest.Coordinates, 'Elevation', Dest.Elevation, 'City', Dest.City, 'Country', Dest.Country)) as JSON) as 'DestinationAirport'
        
 FROM LogEntries L 
INNER JOIN Airports Dep ON L.DepartureAirportID = Dep.ID
INNER JOIN Airports Dest ON L.DestinationAirportID = Dest.ID
WHERE L.PilotID = ? ORDER BY L.AddDate DESC LIMIT 10`
 
        sql.query(query, [pilotID], (err, res) => {
            if (err) {
                console.log("error: ", err);
                result(err, null);
                return;
            }
 
            const apiResponse = {
                ops: "getRecents",
                status: "success",
                message: "Recent Log Entries",
                payload: res,
                affectedRows: res.length
            };
            result(null, apiResponse);
        });
    }
 
    static getTotalHourByPilotID(pilotID, result) {
        var query = `SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( TotalTime ) ) )  as TotalTime FROM LogEntries WHERE PilotID = ?`;
        sql.query(query, [pilotID], (err, res) => {
            if (err) {
                console.log("error: ", err);
                result(err, null);
                return;
            }
 
            const apiResponse = {
                ops: "getTotalHourByPilotID",
                status: "success",
                message: "Total hours by pilotID",
                payload: res[0]["TotalTime"],
                affectedRows: res.length
            };
            result(null, apiResponse);
        });
 
    }
 
    static getLast12MonthTotalHours(pilotID, result) {
        var query = `SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( TotalTime ) ) )  as TotalTime, SEC_TO_TIME( SUM( TIME_TO_SEC( SimTime ) ) )  as SimTime FROM LogEntries
        WHERE PilotID = 1 and FlightDate> now() - INTERVAL 12 month
        GROUP BY MONTH(FlightDate);`;
        sql.query(query, [pilotID], (err, res) => {
            if (err) {
                console.log("error: ", err);
                result(err, null);
                return;
            }
 
            const apiResponse = {
                ops: "getLast12MonthTotalHours",
                status: "success",
                message: "Last 12 hours sum by pilotID",
                payload: res,
                affectedRows: res.length
            };
 
            result(null, apiResponse);
        });
 
    }
 
    static monthlyTotalHours(pilotID, year, result) {
        var query = "SELECT MONTH(FlightDate),SUM(TotalTime) FROM `LogEntries` where PilotID = ? and YEAR(FlightDate) = ? GROUP BY MONTH(FlightDate) ORDER BY MONTH(FlightDate);";
        sql.query(query, [pilotID, year], (err, res) => {
            if (err) {
                console.log("error: ", err);
                result(err, null);
                return;
            }
 
            const apiResponse = {
                ops: "monthlyTotalHours",
                status: "success",
                message: "Monthly total hours by pilotID: " + pilotID + " and year: " + year,
                payload: res,
                affectedRows: res.length
            };
            // console.log("apiResponse:", apiResponse);
            result(null, apiResponse);
        });
    }
 
    static sync(pilotID, version, result) {
        var query = `SELECT L.*,
        CAST(JSON_UNQUOTE(JSON_OBJECT('ID', Dep.ID, 'Name', Dep.Name, 'IATA_CODE', Dep.IATA_CODE, 'ICAO_CODE', Dep.ICAO_CODE, 'Coordinates', Dep.Coordinates, 'Elevation', Dep.Elevation,'City', Dep.City, 'Country', Dep.Country)) as JSON) as DepartureAirport,
        CAST(JSON_UNQUOTE(JSON_OBJECT('ID', Dest.ID, 'Name', Dest.Name, 'IATA_CODE', Dest.IATA_CODE, 'ICAO_CODE', Dest.ICAO_CODE, 'Coordinates', Dest.Coordinates, 'Elevation', Dest.Elevation, 'City', Dest.City, 'Country', Dest.Country)) as JSON) as 'DestinationAirport',
        IF(Sim.ID IS NOT NULL, CAST(JSON_UNQUOTE(JSON_OBJECT('ID', Sim.ID, 'Registration', Sim.Registration, 'Name', Sim.Name, 'Manufacturer', Sim.Manufacturer, 'Model',Sim.Model, 'AircraftType',Sim.AircraftType, 'PilotID', Sim.PilotID, 'SimDeviceType',Sim.SimDeviceType, 'Version', Sim.Version, 'Status', Sim.Status)) as JSON), JSON_OBJECT()) as Simulator
        FROM LogEntries L 
        INNER JOIN Airports Dep ON L.DepartureAirportID = Dep.ID
        INNER JOIN Airports Dest ON L.DestinationAirportID = Dest.ID
        LEFT OUTER JOIN Simulators Sim ON L.Registration = Sim.Registration
        WHERE L.PilotID = ? and L.Version > ? ORDER BY L.AddDate DESC`;
        console.log(query);
        sql.query(query, [pilotID, version], (err, res) => {
            if (err) {
                console.log("error: ", err);
                result(err, null);
                return;
            }
 
            const apiResponse = {
                ops: "sync",
                status: "success",
                message: "Sync Log Entries",
                payload: res,
                affectedRows: res.length
            };
            // console.log("apiResponse:", apiResponse);
            result(null, apiResponse);
        });
    }
}
module.exports = LogEntry;