"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FetchAndProcessEventsTask = void 0;
const models_1 = require("../../models");
const RopidVYMIEventsTransformation_1 = require("../../RopidVYMIEventsTransformation");
const shared_1 = require("../../../shared");
const integration_engine_1 = require("@golemio/core/dist/integration-engine");
const helpers_1 = require("@golemio/core/dist/integration-engine/helpers");
const EventFetchOutputValidationSchema_1 = require("./schema/EventFetchOutputValidationSchema");
class FetchAndProcessEventsTask extends integration_engine_1.AbstractTask {
    constructor(queueprefix) {
        super(queueprefix);
        this.queueName = "fetchAndProcessEvents";
        this.queueTtl = 1 * 60 * 1000; // 1 minute;
        this.schema = EventFetchOutputValidationSchema_1.EventFetchOutputValidationSchema;
        this.execute = async (content) => {
            const dbLastModified = await this.modelVYMIMeta.getLastModified(models_1.DatasetEnum.EVENTS);
            const version = dbLastModified.version + 1;
            await this.modelVYMIMeta.save({
                dataset: models_1.DatasetEnum.EVENTS,
                key: shared_1.MetaDatasetInfoKeyEnum.LAST_MODIFIED,
                type: shared_1.MetaTypeEnum.DATASET_INFO,
                value: new Date().toISOString(),
                version,
            });
            try {
                const { events, routes, stops } = await this.prepareAndTransformEvents(content.data, version);
                await Promise.all([
                    this.prepareAndSaveEvents("events", this.modelVYMIEvents, events, version),
                    this.prepareAndSaveEvents("eventsRoutes", this.modelVYMIEventsRoutes, routes, version),
                    this.prepareAndSaveEvents("eventsStops", this.modelVYMIEventsStops, stops, version),
                ]);
                await this.modelVYMIMeta.checkSavedRows(models_1.DatasetEnum.EVENTS, version);
                await this.modelVYMIMeta.replaceTmpTables(models_1.DatasetEnum.EVENTS, version);
                // Save current digest
                await this.modelVYMIMeta.save({
                    dataset: models_1.DatasetEnum.EVENTS,
                    key: shared_1.MetaDatasetInfoKeyEnum.DIGEST,
                    type: shared_1.MetaTypeEnum.DATASET_INFO,
                    value: content.digest,
                    version,
                });
            }
            catch (err) {
                helpers_1.log.error(err);
                await this.modelVYMIMeta.rollbackFailedSaving(models_1.DatasetEnum.EVENTS, version);
            }
        };
        this.prepareAndTransformEvents = async (data, version) => {
            const { events, routes, stops } = await this.transformation.transform(data);
            const baseMetaInputData = {
                dataset: models_1.DatasetEnum.EVENTS,
                type: shared_1.MetaTypeEnum.TABLE_TOTAL_COUNT,
                version,
            };
            await Promise.all([
                this.modelVYMIMeta.save({
                    key: "events",
                    value: events.length,
                    ...baseMetaInputData,
                }),
                this.modelVYMIMeta.save({
                    key: "eventsRoutes",
                    value: routes.length,
                    ...baseMetaInputData,
                }),
                this.modelVYMIMeta.save({
                    key: "eventsStops",
                    value: stops.length,
                    ...baseMetaInputData,
                }),
            ]);
            return {
                events,
                routes,
                stops,
            };
        };
        this.prepareAndSaveEvents = async (key, model, transformedData, version) => {
            await model.truncate(true);
            await model.save(transformedData, true);
            await this.modelVYMIMeta.save({
                dataset: models_1.DatasetEnum.EVENTS,
                key,
                type: shared_1.MetaTypeEnum.STATE,
                value: shared_1.MetaStateEnum.SAVED,
                version,
            });
        };
        this.modelVYMIMeta = new models_1.RopidVYMIMetadataModel();
        this.transformation = new RopidVYMIEventsTransformation_1.RopidVYMIEventsTransformation();
        this.modelVYMIEvents = new models_1.RopidVYMIEventsModel(true);
        this.modelVYMIEventsRoutes = new models_1.RopidVYMIEventsRoutesModel(true);
        this.modelVYMIEventsStops = new models_1.RopidVYMIEventsStopsModel(true);
    }
}
exports.FetchAndProcessEventsTask = FetchAndProcessEventsTask;
//# sourceMappingURL=FetchAndProcessEventsTask.js.map