"use strict";
/**
 * Delta data access
 *
 *  Copyright (C) 2010-2019 R-T Specialty, LLC.
 *
 *  This file is part of the Liza Data Collection Framework.
 *
 *  liza is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Get deltas from the mongo document in order to process and publish them
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.MongoDeltaDao = void 0;
var ContextError_1 = require("../../error/ContextError");
var DaoError_1 = require("../../error/DaoError");
/** Manage deltas */
var MongoDeltaDao = /** @class */ (function () {
    /**
     * Initialize connection
     *
     * @param _collection - Mongo db collection
     */
    function MongoDeltaDao(_collection, _env) {
        this._collection = _collection;
        this._env = _env;
        /** The document fields to read */
        this.RESULT_FIELDS = {
            id: 1,
            programId: 1,
            agentName: 1,
            agentEntityId: 1,
            startDate: 1,
            lastUpdate: 1,
            quoteExpDate: 1,
            quoteSetId: 1,
            data: 1,
            ratedata: 1,
            rdelta: 1,
            topSavedStepId: 1,
            deltaPublishedTs: 1,
            meta: 1,
        };
    }
    /**
     * Get documents in need of processing
     *
     * @return documents in need of processing
     */
    MongoDeltaDao.prototype.getUnprocessedDocuments = function () {
        return this._collection
            .find({
            published: false,
            deltaError: { $ne: true },
            'ratedata.__rate_pending': { $in: [[0], null] },
            env: this._env,
        })
            .project(this.RESULT_FIELDS)
            .toArray()
            .catch(function (e) {
            throw new DaoError_1.DaoError('Error fetching unprocessed documents: ' + e);
        });
    };
    /**
     * Update the last published timestamp
     *
     * @param doc_id - Document to update
     * @param type   - Delta type
     * @param ts     - Timestamp to set
     */
    MongoDeltaDao.prototype.setPublishedTs = function (doc_id, type, ts) {
        var set_data = {};
        set_data['deltaPublishedTs.' + type] = ts;
        return this._collection
            .updateOne({ id: doc_id }, { $set: set_data }, { upsert: false })
            .catch(function (e) {
            throw ContextError_1.context(new DaoError_1.DaoError('Error updating published ts: ' + e), {
                doc_id: doc_id,
                quote_id: doc_id,
            });
        });
    };
    /**
     * Mark a given document as processed.
     *
     * First does a check to make sure that
     * the document does not have a newer update timestamp than the provided one
     *
     * @param doc_id         - The document to mark
     * @param last_update_ts - The last time this document was updated
     */
    MongoDeltaDao.prototype.markDocumentAsProcessed = function (doc_id, last_update_ts) {
        return this._collection
            .updateOne({ id: doc_id, lastUpdate: { $lte: last_update_ts } }, { $set: { published: true } }, { upsert: false })
            .catch(function (e) {
            throw ContextError_1.context(new DaoError_1.DaoError('Error marking document as processed: ' + e), {
                doc_id: doc_id,
                quote_id: doc_id,
                last_update_ts: last_update_ts,
            });
        });
    };
    /**
     * Flag the document as being in an error state
     *
     * @param doc_id - The document to flag
     *
     * @return any errors that occurred
     */
    MongoDeltaDao.prototype.setErrorFlag = function (doc_id) {
        return this._collection
            .updateOne({ id: doc_id }, { $set: { deltaError: true } }, { upsert: false })
            .catch(function (e) {
            throw ContextError_1.context(new DaoError_1.DaoError('Failed setting error flag: ' + e), {
                doc_id: doc_id,
                quote_id: doc_id,
            });
        });
    };
    /**
     * Get a count of documents in an error state
     *
     * @return a count of the documents in an error state
     */
    MongoDeltaDao.prototype.getErrorCount = function () {
        return this._collection.countDocuments({
            deltaError: true,
            env: this._env,
        });
    };
    /** The ratedata delta type */
    MongoDeltaDao.DELTA_RATEDATA = 'ratedata';
    /** The data delta type */
    MongoDeltaDao.DELTA_DATA = 'data';
    return MongoDeltaDao;
}());
exports.MongoDeltaDao = MongoDeltaDao;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiTW9uZ29EZWx0YURhby5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIk1vbmdvR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