'use strict';

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

var localsHelper = require('../../../../utils/localsHelper.js');
var utils = require('../../../../components/my-reports/utils.js');
var captureError = require('../../../../utils/captureError.js');

class RecentlyViewedReportsController {
    services;
    constructor(services) {
        this.services = services;
    }
    POST = async (req, res) => {
        const { dprUser } = localsHelper.default.getValues(res);
        const { id, reportId, tableId, executionId } = req.params;
        // Remove the report from recenly viewed list
        await this.services.recentlyViewedService.removeReport(dprUser.id, reportId, id, tableId);
        // And clean up the request data, if there is any
        if (executionId) {
            await this.services.requestedReportService.removeReport(executionId, dprUser.id);
        }
        // Update the locals
        res.locals['recentlyViewedReports'] = await this.services.recentlyViewedService.getAllReports(dprUser.id);
        res.locals['requestedReports'] = await this.services.requestedReportService.getAllReports(dprUser.id);
        try {
            // get the data for the requested list
            const maxRows = req.body?.maxRows !== undefined ? Number(req.body.maxRows) : undefined;
            const viewModel = await utils.initViewed(req, res, {
                ...(maxRows && { maxRows }),
            });
            return res.render('dpr/components/my-reports/my-reports-list/view.njk', { viewModel }, (err, html) => {
                if (err)
                    return res.sendStatus(500);
                return res.type('text/html').send(html);
            });
        }
        catch (error) {
            captureError.captureDprError(error, 'Failed to refresh list after removal');
            return res.sendStatus(500);
        }
    };
}

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