103 lines
3.6 KiB
JavaScript
103 lines
3.6 KiB
JavaScript
'use strict';
|
|
|
|
const express = require('express');
|
|
const multer = require('multer');
|
|
const os = require('os');
|
|
const { models } = require('../../core/db');
|
|
const { authenticateToken, authenticateRole } = require('../../core/auth');
|
|
const { ok, noContent } = require('../../core/respond');
|
|
const { AppError } = require('../../core/errors');
|
|
const { updateOrg: updateOrgSchema, updateBranding: updateBrandingSchema } = require('../../core/schemas/org');
|
|
const svc = require('./service');
|
|
|
|
const router = express.Router();
|
|
router.prefix = 'org';
|
|
|
|
const OWNERS = ['Owner', 'SystemAdmin'];
|
|
|
|
const upload = multer({
|
|
dest: process.env.UPLOAD_TEMP_DIR || os.tmpdir(),
|
|
limits: { fileSize: 5 * 1024 * 1024 }, // 5 MB for logos
|
|
});
|
|
|
|
function validate(schema) {
|
|
return (req, res, next) => {
|
|
const result = schema.safeParse(req.body);
|
|
if (!result.success) {
|
|
const e = result.error.errors[0];
|
|
return next(new AppError('VALIDATION_FAILED', e.message, 400, e.path[0]));
|
|
}
|
|
req.body = result.data;
|
|
next();
|
|
};
|
|
}
|
|
|
|
router.get('/', authenticateToken, async (req, res, next) => {
|
|
try {
|
|
const org = await svc.getOrg(models, req.user.organizationId);
|
|
ok(res, org);
|
|
} catch (err) { next(err); }
|
|
});
|
|
|
|
router.patch('/', authenticateToken, authenticateRole(OWNERS), validate(updateOrgSchema), async (req, res, next) => {
|
|
try {
|
|
const org = await svc.updateOrg(models, req, req.user.organizationId, req.body);
|
|
ok(res, org);
|
|
} catch (err) { next(err); }
|
|
});
|
|
|
|
router.get('/branding/public', async (req, res, next) => {
|
|
try {
|
|
const org = await models.Organization.findOne({ order: [['id', 'ASC']] });
|
|
if (!org) return ok(res, {});
|
|
const branding = await svc.getBranding(models, org.id);
|
|
ok(res, branding);
|
|
} catch (err) { next(err); }
|
|
});
|
|
|
|
router.get('/branding', authenticateToken, async (req, res, next) => {
|
|
try {
|
|
if (!req.user.organizationId) return ok(res, {});
|
|
const branding = await svc.getBranding(models, req.user.organizationId);
|
|
ok(res, branding);
|
|
} catch (err) { next(err); }
|
|
});
|
|
|
|
router.patch('/branding', authenticateToken, authenticateRole(OWNERS), validate(updateBrandingSchema), async (req, res, next) => {
|
|
try {
|
|
const branding = await svc.updateBranding(models, req, req.user.organizationId, req.body);
|
|
ok(res, branding);
|
|
} catch (err) { next(err); }
|
|
});
|
|
|
|
router.post('/logo', authenticateToken, authenticateRole(OWNERS), upload.single('logo'), async (req, res, next) => {
|
|
try {
|
|
if (!req.file) return next(new AppError('VALIDATION_FAILED', 'No logo file provided.', 400));
|
|
const branding = await svc.uploadLogo(models, req, req.user.organizationId, req.file);
|
|
ok(res, { logoPath: branding.logoPath });
|
|
} catch (err) { next(err); }
|
|
});
|
|
|
|
// Public: logos are rendered by <img src> (login page, banner), which cannot send
|
|
// an Authorization header. Org id comes from the path; legacy /logo falls back to
|
|
// the first org, matching GET /branding/public.
|
|
router.get('/logo/:orgId?', async (req, res, next) => {
|
|
try {
|
|
let orgId = parseInt(req.params.orgId, 10);
|
|
if (isNaN(orgId)) {
|
|
const org = await models.Organization.findOne({ order: [['id', 'ASC']] });
|
|
if (!org) return next(new AppError('NOT_FOUND', 'No custom logo set.', 404));
|
|
orgId = org.id;
|
|
}
|
|
|
|
const filePath = svc.findLogoFile(orgId);
|
|
if (!filePath) return next(new AppError('NOT_FOUND', 'No custom logo set.', 404));
|
|
|
|
// Path is versioned (?v=), so the file at a given URL never changes
|
|
res.set('Cache-Control', 'public, max-age=31536000, immutable');
|
|
res.sendFile(filePath);
|
|
} catch (err) { next(err); }
|
|
});
|
|
|
|
module.exports = router;
|