'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var node = require('@sentry/node');
var utils = require('./utils.js');
var ErrorHandler = require('../../../../utils/ErrorHandler/ErrorHandler.js');
var getReportStatus = require('../../../../utils/ReportStatus/getReportStatus.js');
var localsHelper = require('../../../../utils/localsHelper.js');

class RequestStatusController {
    layoutPath;
    services;
    constructor(layoutPath, services) {
        this.layoutPath = layoutPath;
        this.services = services;
    }
    // Render status page
    GET = async (req, res, next) => {
        try {
            const pollingRenderData = await utils.default.renderPolling({
                req,
                res,
                services: this.services,
                next,
            });
            res.render(`dpr/routes/journeys/request-report/status/view`, {
                layoutPath: this.layoutPath,
                ...pollingRenderData,
            });
        }
        catch (error) {
            req.body ??= {};
            req.body.title = 'Failed to retrieve report status';
            req.body.errorDescription = 'We were unable to retrieve the report status:';
            req.body.error = new ErrorHandler.ErrorHandler(error).formatError();
            next(error);
        }
    };
    // Poll request status
    POST = async (req, res, _next) => {
        try {
            const { token, dprUser } = localsHelper.getValues(res);
            const { executionId, currentStatus } = req.body;
            const requestedReport = await this.services.requestedReportService.getReportByExecutionId(executionId, dprUser.id);
            if (!requestedReport) {
                return res.sendStatus(404);
            }
            const { resolution } = await getReportStatus.evaluateAndUpdateReportStatus({
                stored: requestedReport,
                services: this.services,
                res,
                token,
            });
            let status = currentStatus;
            if (resolution.type === 'UPDATE') {
                status = resolution.newStatus;
            }
            return res.send({ status });
        }
        catch (error) {
            node.captureException(error);
            return res.send({ status: 'FAILED' });
        }
    };
}

exports.RequestStatusController = RequestStatusController;
exports.default = RequestStatusController;
//# sourceMappingURL=controller.js.map
