const getTable = require("../middleware/genReportTerritories");

class generatorReportTerritoriesController {
    async getTable(req, res) {
        if (req.possibilities.can_add_territory) {
            let result = await getTable();

            if (result) {
                res.setHeader(
                    'Content-Disposition',
                    `attachment; filename*=UTF-8''${encodeURIComponent('Опрацювання_територій.xlsx')}`
                );
                res.setHeader(
                    'Content-Type',
                    'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
                );
                return res.status(200).send(result);
            } else {
                return res
                    .status(404)
                    .send({ message: 'Table creation error.' });
            }
        } else {
            return res
                .status(403)
                .send({ message: 'The user does not have enough rights.' });
        }

    }
}

module.exports = new generatorReportTerritoriesController();