From c6388cf942e2e9431238d961b4ea1e7bc27b19d0 Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Tue, 27 Dec 2022 15:06:46 +0100 Subject: [PATCH] Created the click action --- server/actions/click.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 server/actions/click.js diff --git a/server/actions/click.js b/server/actions/click.js new file mode 100644 index 0000000..ac6d18e --- /dev/null +++ b/server/actions/click.js @@ -0,0 +1,23 @@ +const {getLinkByAccess, clickLink} = require("../controller/link"); +const {getModule} = require("../controller/module"); +const app = require('express').Router(); + +const sendNotFound = (res) => res.status(404).render(process.cwd() + "/server/templates/404"); + +app.get("/", async (req, res) => { + res.locals.accessId = req.baseUrl.substring(1, req.baseUrl.length) || "home"; + const currentDomain = req.get('host')?.split(":")[0] || "localhost"; + + const link = await getLinkByAccess(res.locals.accessId, currentDomain); + if (!link) return sendNotFound(res); + + const module = getModule(link.type); + if (!module) return sendNotFound(res); + + await clickLink(link.accessId, link.domainName); + + res.locals.meta = JSON.parse(link.meta); + module.onClick(link.id, link.accessId, JSON.parse(link.meta), res); +}); + +module.exports = app; \ No newline at end of file