'use strict'; const express = require('express'); const multer = require('multer'); const path = require('path'); const os = require('os'); const fs = require('fs'); 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); } }); router.get('/logo', authenticateToken, async (req, res, next) => { try { const branding = await svc.getBranding(models, req.user.organizationId); if (!branding.logoPath) return next(new AppError('NOT_FOUND', 'No custom logo set.', 404)); // Find logo file in uploads dir const uploadsDir = process.env.UPLOADS_DIR || path.join(__dirname, '..', '..', 'uploads'); const files = fs.readdirSync(uploadsDir).filter(f => f.startsWith(`logo_${req.user.organizationId}_`)); if (!files.length) return next(new AppError('NOT_FOUND', 'Logo file not found.', 404)); const logoFile = files.sort().pop(); // latest res.sendFile(path.join(uploadsDir, logoFile)); } catch (err) { next(err); } }); module.exports = router;