
const whiskers = require('whiskers');
const Security = require('../src/security.js');
const { AccessModel, ACCESS_TYPE, ACCESS_TO } = require('../model/access.js');

module.exports = { register: app => {
    app.router.get("/", (req, res) => {
        let context = app.routerUtils.commonRenderInfos();
        context.page_title += " - Dashboard";
        context.sources = [];
        res.end(whiskers.render(require('../templates/index.js'), context));
    });
    app.router.get("/linkLogin", async (req, res) => {
        app.routerUtils.onApiRequest(req, res);
        if (!req.body?.link)
            return app.routerUtils.onBadRequest(res);
        const access = await app.databaseHelper.findOne(AccessModel, { type: ACCESS_TYPE.link, typeData: req.body.link });
        if (access) {
            Security.addLinkToSession(req, access.id, access.typeData, access.linkLabel);
            if (access.accessTo == ACCESS_TO.admin)
                Security.setAdmin(req, true);
        }
        app.routerUtils.redirect(res, "/");
    });
}};

