"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.CommonRunsMessagesRepository = void 0;
const const_1 = require("../../../../../schema-definitions/const");
const models_1 = require("../../../../../schema-definitions/vehicle-positions/models");
const models_2 = require("@golemio/core/dist/integration-engine/models");
const golemio_validator_1 = require("@golemio/core/dist/shared/golemio-validator");
const sequelize_1 = __importStar(require("@golemio/core/dist/shared/sequelize"));
class CommonRunsMessagesRepository extends models_2.PostgresModel {
    constructor() {
        super("CommonRunsMessagesRepository", {
            pgTableName: models_1.CommonRunsMessagesModel.TABLE_NAME,
            pgSchema: const_1.PG_SCHEMA,
            outputSequelizeAttributes: models_1.CommonRunsMessagesModel.attributeModel,
            savingType: "insertOnly",
            sequelizeAdditionalSettings: {
                indexes: [
                    {
                        fields: ["id"],
                        name: "vehiclepositions_runs_messages_pkey",
                    },
                ],
            },
        }, new golemio_validator_1.JSONSchemaValidator("CommonRunsMessagesRepositoryValidator", models_1.CommonRunsMessagesModel.arrayJsonSchema));
        this.getLastMessage = async (runId) => {
            const result = await this.find({
                where: {
                    runs_id: runId,
                    msg_timestamp: {
                        [sequelize_1.Op.gt]: sequelize_1.default.literal("NOW() - INTERVAL '1 HOURS'"),
                    },
                },
                order: [["msg_timestamp", "DESC"]],
                limit: 1,
                raw: true,
            });
            return result && result.length === 1 ? result[0] : undefined;
        };
    }
}
exports.CommonRunsMessagesRepository = CommonRunsMessagesRepository;
//# sourceMappingURL=CommonRunsMessagesRepository.js.map