import localsHelper from '../../../../utils/localsHelper.js';
import { initViewed } from '../../../../components/my-reports/utils.js';
import { captureDprError } from '../../../../utils/captureError.js';

class RecentlyViewedReportsController {
    services;
    constructor(services) {
        this.services = services;
    }
    POST = async (req, res) => {
        const { dprUser } = localsHelper.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 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) {
            captureDprError(error, 'Failed to refresh list after removal');
            return res.sendStatus(500);
        }
    };
}

export { RecentlyViewedReportsController, RecentlyViewedReportsController as default };
//# sourceMappingURL=controller.js.map
