"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.PositionsHistoryRepository = void 0;
const const_1 = require("../../../../../schema-definitions/const");
const vehicle_positions_1 = require("../../../../../schema-definitions/vehicle-positions");
const PositionDto_1 = require("../../../../../schema-definitions/vehicle-positions/models/PositionDto");
const integration_engine_1 = require("@golemio/core/dist/integration-engine");
const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors");
const golemio_validator_1 = require("@golemio/core/dist/shared/golemio-validator");
const sequelize_1 = __importStar(require("@golemio/core/dist/shared/sequelize"));
class PositionsHistoryRepository extends integration_engine_1.PostgresModel {
    constructor() {
        super("VPPositionsHistoryRepository", {
            outputSequelizeAttributes: PositionDto_1.PositionDto.attributeModel,
            pgTableName: vehicle_positions_1.VehiclePositions.positions.pgTableNameHistory,
            pgSchema: const_1.PG_SCHEMA,
            savingType: "insertOnly",
            sequelizeAdditionalSettings: {
                indexes: [
                    {
                        fields: ["trips_id"],
                        name: "vehiclepositions_positions_trips_id",
                    },
                    {
                        fields: ["origin_time"],
                        name: "vehiclepositions_positions_origin_time",
                    },
                ],
            },
        }, new golemio_validator_1.JSONSchemaValidator(vehicle_positions_1.VehiclePositions.positions.name + "ModelValidator", PositionDto_1.PositionDto.jsonSchema));
        this.deleteNHoursOldData = async (hours) => {
            try {
                return await this.sequelizeModel.destroy({
                    where: {
                        updated_at: {
                            [sequelize_1.Op.lt]: sequelize_1.default.literal(`NOW() - INTERVAL '${hours} HOURS'`),
                        },
                    },
                });
            }
            catch (err) {
                throw new golemio_errors_1.GeneralError("Error while purging old data", this.constructor.name, err);
            }
        };
    }
}
exports.PositionsHistoryRepository = PositionsHistoryRepository;
//# sourceMappingURL=PositionsHistoryRepository.js.map