'use strict';

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

var localsHelper = require('../../../../../utils/localsHelper.js');
var utils = require('./utils.js');
var utils$1 = require('../../utils.js');
var ErrorHandler = require('../../../../../utils/ErrorHandler/ErrorHandler.js');

class ViewAyncReportController {
    layoutPath;
    services;
    constructor(layoutPath, services) {
        this.layoutPath = layoutPath;
        this.services = services;
    }
    GET = async (req, res, next) => {
        const { type, tableId } = req.params;
        try {
            const params = { req, res, services: this.services, next };
            const renderData = await utils.default.renderReport(params);
            res.render(`dpr/routes/journeys/view-report/report`, {
                layoutPath: this.layoutPath,
                ...renderData,
            });
        }
        catch (error) {
            const dprError = new ErrorHandler.ErrorHandler(error).formatError();
            let refreshLink;
            const { recentlyViewedService } = this.services;
            if (dprError.status === 'EXPIRED' && recentlyViewedService) {
                const { dprUser } = localsHelper.default.getValues(res);
                refreshLink = await recentlyViewedService.asyncSetToExpiredByTableId(tableId, dprUser.id);
            }
            req.body ??= {};
            req.body.title = `Failed to retrieve ${type}`;
            req.body.error = dprError;
            if (refreshLink) {
                req.body.refreshLink = refreshLink;
            }
            next(error);
        }
    };
    applyFilters = async (req, res, _next) => {
        await utils$1.default.applyReportInteractiveQuery(req, res, this.services, 'filters');
    };
    applyColumns = async (req, res, _next) => {
        await utils$1.default.applyReportInteractiveQuery(req, res, this.services, 'columns');
    };
}

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