const SessionService = require("../../services/SessionService"); const app = require("express").Router(); const db = require("../../models"); const role = 1; // List termination configurations app.get( "/admin/termination-config", SessionService.verifySessionMiddleware(role, "admin"), async function (req, res, next) { try { const configs = await db.termination_config.findAll(); res.render("admin/Termination_Config", { configs, get_page_name: () => "Termination Configuration", _base_url: "/admin/termination-config", }); } catch (error) { console.log(error); res .status(500) .json({ success: false, message: "Failed to fetch termination configurations", }); } } ); // Get termination configuration by ID app.get( "/admin/termination-config/:id", SessionService.verifySessionMiddleware(role, "admin"), async function (req, res, next) { try { const { id } = req.params; const config = await db.termination_config.getByPK(id); if (!config) { return res .status(404) .json({ success: false, message: "Termination configuration not found", }); } return res.json({ success: true, payload: config }); } catch (error) { console.log(error); return res .status(500) .json({ success: false, message: "Failed to fetch termination configuration", }); } } ); // Create new termination configuration app.post( "/admin/termination-config", SessionService.verifySessionMiddleware(role, "admin"), async function (req, res, next) { try { const { message, title, counter } = req.body; const config = await db.termination_config.create({ message, title, counter: parseInt(counter), }); return res.json({ success: true, payload: config, message: "Termination configuration created successfully", }); } catch (error) { console.log(error); return res .status(500) .json({ success: false, message: "Failed to create termination configuration", }); } } ); // Update termination configuration app.put( "/admin/termination-config/:id", SessionService.verifySessionMiddleware(role, "admin"), async function (req, res, next) { try { const { id } = req.params; const { message, title, counter } = req.body; const config = await db.termination_config.edit( { message, title, counter: parseInt(counter), }, id ); return res.json({ success: true, payload: config, message: "Termination configuration updated successfully", }); } catch (error) { console.log(error); return res .status(500) .json({ success: false, message: "Failed to update termination configuration", }); } } ); // Delete termination configuration app.delete( "/admin/termination-config/:id", SessionService.verifySessionMiddleware(role, "admin"), async function (req, res, next) { try { const { id } = req.params; await db.termination_config.destroy(id); return res.json({ success: true, message: "Termination configuration deleted successfully", }); } catch (error) { console.log(error); return res .status(500) .json({ success: false, message: "Failed to delete termination configuration", }); } } ); // Get default termination configuration (for frontend) app.get("/api/termination-config", async function (req, res, next) { try { // Get the first configuration or create a default one let config = await db.termination_config.findOne(); if (!config) { // Create default configuration if none exists config = await db.termination_config.create({ message: "You have an allergy to one of the main ingredients in our system. Our current system will not suit you.", title: "Quiz will now be terminated", counter: 10, }); } return res.json({ success: true, payload: config }); } catch (error) { console.log(error); // Return default values if database fails return res.json({ success: true, payload: { message: "You have an allergy to one of the main ingredients in our system. Our current system will not suit you.", title: "Quiz will now be terminated", counter: 10, }, }); } }); module.exports = app;