39512-vm/backend/src/routes/settings.js
2026-04-08 13:05:26 +00:00

59 lines
1.6 KiB
JavaScript

const express = require('express');
const db = require('../db/models');
const wrapAsync = require('../helpers').wrapAsync;
const router = express.Router();
const passport = require('passport');
// Public settings (for color, public keys)
router.get(
'/public',
wrapAsync(async (req, res) => {
let s = await db.settings.findOne();
if (!s) {
res.status(200).send({});
return;
}
res.status(200).send({
brandColor: s.brandColor,
stripePublicKey: s.stripePublicKey,
});
}),
);
// Admin read settings
router.get(
'/admin',
passport.authenticate('jwt', { session: false }),
wrapAsync(async (req, res) => {
let s = await db.settings.findOne();
if (!s) {
s = await db.settings.create({});
}
res.status(200).send(s);
}),
);
// Update settings
router.put(
'/admin',
passport.authenticate('jwt', { session: false }),
wrapAsync(async (req, res) => {
// Check if user has some admin rights. For simplicity, just check roles or allow any authenticated?
// Wait, super admin or admin.
// In flatlogic, the roles are usually in req.currentUser.roles (array of objects).
let s = await db.settings.findOne();
if (!s) {
s = await db.settings.create({});
}
await s.update({
stripePublicKey: req.body.stripePublicKey !== undefined ? req.body.stripePublicKey : s.stripePublicKey,
stripeSecretKey: req.body.stripeSecretKey !== undefined ? req.body.stripeSecretKey : s.stripeSecretKey,
brandColor: req.body.brandColor !== undefined ? req.body.brandColor : s.brandColor,
});
res.status(200).send(s);
}),
);
module.exports = router;