import { getActiveJourneyValue } from '../../../utils/sessionHelper.js';
import localsHelper from '../../../utils/localsHelper.js';
import DownloadUtils from './utils.js';

class DownloadReportController {
    layoutPath;
    services;
    constructor(layoutPath, services) {
        this.layoutPath = layoutPath;
        this.services = services;
    }
    POST = async (req, res) => {
        const { dprUser } = localsHelper.getValues(res);
        const { reportId, id, loadType, tableId } = req.body;
        const currentReportSearch = getActiveJourneyValue(req, { id, reportId, tableId }, 'currentReportSearch');
        const currentReportPathname = getActiveJourneyValue(req, { id, reportId, tableId }, 'currentReportPathname');
        let redirect = currentReportPathname && currentReportPathname.includes('/download-disabled')
            ? currentReportPathname
            : `${currentReportPathname}/download-disabled`;
        redirect = currentReportSearch ? `${redirect}${currentReportSearch}` : redirect;
        const canDownloadReport = await this.services.downloadPermissionService.downloadEnabledForReport(dprUser.id, reportId, id);
        if (canDownloadReport) {
            await DownloadUtils.downloadReport({ req, res, services: this.services, redirect, loadType });
        }
        else {
            res.redirect(redirect);
        }
    };
}

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