"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "PrintCompletionService", {
    enumerable: true,
    get: function() {
        return PrintCompletionService;
    }
});
const _models = require("../../models");
const _printcompletionservicevalidation = require("../validators/print-completion-service.validation");
const _validators = require("../../handlers/validators");
const _octoprintwebsocketconstants = require("../octoprint/constants/octoprint-websocket.constants");
const _printcompletionshared = require("./print-completion.shared");
class PrintCompletionService {
    logger;
    constructor({ loggerFactory }){
        this.logger = loggerFactory(PrintCompletionService.name);
    }
    toDto(entity) {
        return {
            id: entity.id,
            completionLog: entity.completionLog,
            context: entity.context,
            fileName: entity.fileName,
            createdAt: entity.createdAt,
            status: entity.status,
            printerId: entity.printerId.toString()
        };
    }
    async create(input) {
        const { printerId, fileName, completionLog, status, context } = await (0, _validators.validateInput)(input, _printcompletionservicevalidation.createPrintCompletionRules);
        return _models.PrintCompletion.create({
            printerId,
            fileName,
            completionLog,
            status,
            createdAt: Date.now(),
            context
        });
    }
    async list() {
        return _models.PrintCompletion.find({});
    }
    async findPrintCompletion(correlationId) {
        return _models.PrintCompletion.find({
            "context.correlationId": correlationId
        });
    }
    async updateContext(correlationId, context) {
        if (!correlationId?.length) {
            this.logger.warn("Ignoring undefined correlationId, cant update print completion context");
            return;
        }
        const completionEntry = await _models.PrintCompletion.findOne({
            "context.correlationId": correlationId,
            status: _octoprintwebsocketconstants.EVENT_TYPES.PrintStarted
        });
        if (!completionEntry) {
            this.logger.warn(`Print with correlationId ${correlationId} could not be updated with new context as it was not found`);
            return;
        }
        completionEntry.context = context;
        await completionEntry.save();
    }
    async loadPrintContexts() {
        const contexts = await _models.PrintCompletion.aggregate([
            {
                $sort: {
                    printerId: 1,
                    createdAt: -1
                }
            },
            {
                $group: {
                    _id: "$printerId",
                    createdAt: {
                        $first: "$createdAt"
                    },
                    context: {
                        $first: "$context"
                    },
                    status: {
                        $first: "$status"
                    },
                    fileName: {
                        $first: "$fileName"
                    }
                }
            },
            {
                $match: {
                    status: {
                        $nin: [
                            _octoprintwebsocketconstants.EVENT_TYPES.PrintDone,
                            _octoprintwebsocketconstants.EVENT_TYPES.PrintFailed
                        ]
                    }
                }
            }
        ]);
        return Object.fromEntries(contexts.map((c)=>{
            c.printerId = c._id;
            delete c._id;
            return [
                c.printerId,
                c
            ];
        }));
    }
    async listGroupByPrinterStatus() {
        const printCompletionsAggr = await _models.PrintCompletion.aggregate([
            {
                $group: {
                    _id: "$printerId",
                    printEvents: {
                        $push: {
                            printerId: "$printerId",
                            context: "$context",
                            completionLog: "$completionLog",
                            fileName: "$fileName",
                            status: "$status",
                            createdAt: "$createdAt"
                        }
                    }
                }
            }
        ]);
        return (0, _printcompletionshared.processCompletions)(printCompletionsAggr);
    }
}

//# sourceMappingURL=print-completion.service.js.map