From 80bea65c2f84c0a7fc49418655e0e85d83a5184b Mon Sep 17 00:00:00 2001 From: ClaraCrazy Date: Tue, 6 Jan 2026 13:51:17 +0100 Subject: [PATCH] [Chore]: Update API code location and fix routes --- .../web/api}/middleware/authenticator.js | 0 .../web/api}/middleware/error-handler.js | 0 .../web/api}/middleware/rate-limiter.js | 0 .../web/api}/middleware/response-formatter.js | 12 ++++++++++++ {api => infrastructure/web/api}/router.js | 2 +- .../web/api}/routes/account.api.md | 0 {api => infrastructure/web/api}/routes/account.js | 4 ++-- {api => infrastructure/web/api}/routes/auth.api.md | 0 {api => infrastructure/web/api}/routes/auth.js | 0 {api => infrastructure/web/api}/routes/config.api.md | 0 {api => infrastructure/web/api}/routes/config.js | 3 +-- {api => infrastructure/web/api}/routes/inbox.api.md | 0 {api => infrastructure/web/api}/routes/inbox.js | 0 {api => infrastructure/web/api}/routes/locks.api.md | 0 {api => infrastructure/web/api}/routes/locks.js | 3 ++- {api => infrastructure/web/api}/routes/mail.api.md | 0 {api => infrastructure/web/api}/routes/mail.js | 0 {api => infrastructure/web/api}/routes/stats.api.md | 0 {api => infrastructure/web/api}/routes/stats.js | 2 +- infrastructure/web/web.js | 4 ++-- 20 files changed, 21 insertions(+), 9 deletions(-) rename {api => infrastructure/web/api}/middleware/authenticator.js (100%) rename {api => infrastructure/web/api}/middleware/error-handler.js (100%) rename {api => infrastructure/web/api}/middleware/rate-limiter.js (100%) rename {api => infrastructure/web/api}/middleware/response-formatter.js (82%) rename {api => infrastructure/web/api}/router.js (98%) rename {api => infrastructure/web/api}/routes/account.api.md (100%) rename {api => infrastructure/web/api}/routes/account.js (98%) rename {api => infrastructure/web/api}/routes/auth.api.md (100%) rename {api => infrastructure/web/api}/routes/auth.js (100%) rename {api => infrastructure/web/api}/routes/config.api.md (100%) rename {api => infrastructure/web/api}/routes/config.js (94%) rename {api => infrastructure/web/api}/routes/inbox.api.md (100%) rename {api => infrastructure/web/api}/routes/inbox.js (100%) rename {api => infrastructure/web/api}/routes/locks.api.md (100%) rename {api => infrastructure/web/api}/routes/locks.js (96%) rename {api => infrastructure/web/api}/routes/mail.api.md (100%) rename {api => infrastructure/web/api}/routes/mail.js (100%) rename {api => infrastructure/web/api}/routes/stats.api.md (100%) rename {api => infrastructure/web/api}/routes/stats.js (97%) diff --git a/api/middleware/authenticator.js b/infrastructure/web/api/middleware/authenticator.js similarity index 100% rename from api/middleware/authenticator.js rename to infrastructure/web/api/middleware/authenticator.js diff --git a/api/middleware/error-handler.js b/infrastructure/web/api/middleware/error-handler.js similarity index 100% rename from api/middleware/error-handler.js rename to infrastructure/web/api/middleware/error-handler.js diff --git a/api/middleware/rate-limiter.js b/infrastructure/web/api/middleware/rate-limiter.js similarity index 100% rename from api/middleware/rate-limiter.js rename to infrastructure/web/api/middleware/rate-limiter.js diff --git a/api/middleware/response-formatter.js b/infrastructure/web/api/middleware/response-formatter.js similarity index 82% rename from api/middleware/response-formatter.js rename to infrastructure/web/api/middleware/response-formatter.js index e855eae..761bcaf 100644 --- a/api/middleware/response-formatter.js +++ b/infrastructure/web/api/middleware/response-formatter.js @@ -7,9 +7,19 @@ function responseFormatter(req, res, next) { * @param {*} data - Data to return * @param {number} statusCode - HTTP status code (default: 200) */ + // Determine mode: 'normal', 'debug', or 'ux-debug' + let mode = 'normal'; + const config = req.app && req.app.get ? req.app.get('config') : null; + if (config && config.uxDebugMode) { + mode = 'ux-debug'; + } else if (process.env.DEBUG && process.env.DEBUG.includes('48hr-email')) { + mode = 'debug'; + } + res.apiSuccess = function(data = null, statusCode = 200, templateContext = null) { const response = { success: true, + mode: mode, data: data }; if (templateContext) response.templateContext = templateContext; @@ -25,6 +35,7 @@ function responseFormatter(req, res, next) { res.apiError = function(message, code = 'ERROR', statusCode = 400, templateContext = null) { const response = { success: false, + mode: mode, error: message, code: code }; @@ -44,6 +55,7 @@ function responseFormatter(req, res, next) { } const response = { success: true, + mode: mode, data: items, count: items.length, total: total !== null ? total : items.length diff --git a/api/router.js b/infrastructure/web/api/router.js similarity index 98% rename from api/router.js rename to infrastructure/web/api/router.js index 814372c..7b469c5 100644 --- a/api/router.js +++ b/infrastructure/web/api/router.js @@ -55,4 +55,4 @@ function createApiRouter(dependencies) { return router } -module.exports = createApiRouter +module.exports = createApiRouter \ No newline at end of file diff --git a/api/routes/account.api.md b/infrastructure/web/api/routes/account.api.md similarity index 100% rename from api/routes/account.api.md rename to infrastructure/web/api/routes/account.api.md diff --git a/api/routes/account.js b/infrastructure/web/api/routes/account.js similarity index 98% rename from api/routes/account.js rename to infrastructure/web/api/routes/account.js index d904c06..663c5b1 100644 --- a/api/routes/account.js +++ b/infrastructure/web/api/routes/account.js @@ -45,8 +45,8 @@ function createAccountRouter(dependencies) { try { const userId = req.user.id - // Get user stats - const stats = userRepository.getUserStats(userId) + // Get user stats (pass config.user for mock repo compatibility) + const stats = userRepository.getUserStats(userId, config.user) // Get verified emails const verifiedEmails = userRepository.getForwardEmails(userId) diff --git a/api/routes/auth.api.md b/infrastructure/web/api/routes/auth.api.md similarity index 100% rename from api/routes/auth.api.md rename to infrastructure/web/api/routes/auth.api.md diff --git a/api/routes/auth.js b/infrastructure/web/api/routes/auth.js similarity index 100% rename from api/routes/auth.js rename to infrastructure/web/api/routes/auth.js diff --git a/api/routes/config.api.md b/infrastructure/web/api/routes/config.api.md similarity index 100% rename from api/routes/config.api.md rename to infrastructure/web/api/routes/config.api.md diff --git a/api/routes/config.js b/infrastructure/web/api/routes/config.js similarity index 94% rename from api/routes/config.js rename to infrastructure/web/api/routes/config.js index 3f9a1fb..9d61fb7 100644 --- a/api/routes/config.js +++ b/infrastructure/web/api/routes/config.js @@ -59,8 +59,7 @@ function createConfigRouter(dependencies) { res.apiSuccess({ authentication: config.user.authEnabled, forwarding: config.smtp.enabled, - statistics: config.http.statisticsEnabled, - inboxLocking: config.user.authEnabled + statistics: config.http.features.statistics }) }) diff --git a/api/routes/inbox.api.md b/infrastructure/web/api/routes/inbox.api.md similarity index 100% rename from api/routes/inbox.api.md rename to infrastructure/web/api/routes/inbox.api.md diff --git a/api/routes/inbox.js b/infrastructure/web/api/routes/inbox.js similarity index 100% rename from api/routes/inbox.js rename to infrastructure/web/api/routes/inbox.js diff --git a/api/routes/locks.api.md b/infrastructure/web/api/routes/locks.api.md similarity index 100% rename from api/routes/locks.api.md rename to infrastructure/web/api/routes/locks.api.md diff --git a/api/routes/locks.js b/infrastructure/web/api/routes/locks.js similarity index 96% rename from api/routes/locks.js rename to infrastructure/web/api/routes/locks.js index eea682a..82cd141 100644 --- a/api/routes/locks.js +++ b/infrastructure/web/api/routes/locks.js @@ -13,9 +13,10 @@ const createAuthenticator = require('../middleware/authenticator') function createLocksRouter(dependencies) { const { inboxLock, userRepository, apiTokenRepository, config } = dependencies + // Inbox locking is always enabled if authentication is enabled if (!inboxLock || !config.user.authEnabled) { router.all('*', (req, res) => { - res.apiError('Inbox locking is disabled', 'FEATURE_DISABLED', 503) + res.apiError('Authentication is required for inbox locking', 'AUTH_REQUIRED', 401) }) return router } diff --git a/api/routes/mail.api.md b/infrastructure/web/api/routes/mail.api.md similarity index 100% rename from api/routes/mail.api.md rename to infrastructure/web/api/routes/mail.api.md diff --git a/api/routes/mail.js b/infrastructure/web/api/routes/mail.js similarity index 100% rename from api/routes/mail.js rename to infrastructure/web/api/routes/mail.js diff --git a/api/routes/stats.api.md b/infrastructure/web/api/routes/stats.api.md similarity index 100% rename from api/routes/stats.api.md rename to infrastructure/web/api/routes/stats.api.md diff --git a/api/routes/stats.js b/infrastructure/web/api/routes/stats.js similarity index 97% rename from api/routes/stats.js rename to infrastructure/web/api/routes/stats.js index 9e55817..05691e4 100644 --- a/api/routes/stats.js +++ b/infrastructure/web/api/routes/stats.js @@ -11,7 +11,7 @@ function createStatsRouter(dependencies) { // Ensure router is declared before any usage const { statisticsStore, mailProcessingService, imapService, config } = dependencies - if (!config.http.statisticsEnabled) { + if (!config.http.features.statistics) { router.all('*', (req, res) => { res.apiError('Statistics are disabled', 'FEATURE_DISABLED', 503) }) diff --git a/infrastructure/web/web.js b/infrastructure/web/web.js index 767784a..7785676 100644 --- a/infrastructure/web/web.js +++ b/infrastructure/web/web.js @@ -11,7 +11,7 @@ const helmet = require('helmet') const socketio = require('socket.io') const config = require('../../application/config') -const createApiRouter = require('../../api/router') +const createApiRouter = require('./api/router') const inboxRouter = require('./routes/inbox') const loginRouter = require('./routes/login') const errorRouter = require('./routes/error') @@ -231,4 +231,4 @@ server.on('listening', () => { server.emit('ready') }) -module.exports = { app, io, server } +module.exports = { app, io, server } \ No newline at end of file