From c74b81f42d184e7666621660e45899c642f18166 Mon Sep 17 00:00:00 2001 From: Aisha Al Attiyah Date: Sat, 14 Sep 2024 14:24:47 +0300 Subject: [PATCH] fix-nesting-issue --- src/controllers/helpers.js | 49 +++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/src/controllers/helpers.js b/src/controllers/helpers.js index c17e701b79..0a10d499af 100644 --- a/src/controllers/helpers.js +++ b/src/controllers/helpers.js @@ -20,6 +20,32 @@ const helpers = module.exports; const relative_path = nconf.get('relative_path'); const url = nconf.get('url'); +const handleSuccessfulResponse = (statusCode, res, payload) => { + console.log('Aisha'); + if (res.req.loggedIn) { + res.set('cache-control', 'private'); + } + + let code = 'ok'; + let message = 'OK'; + switch (statusCode) { + case 202: + code = 'accepted'; + message = 'Accepted'; + break; + + case 204: + code = 'no-content'; + message = 'No Content'; + break; + } + + res.status(statusCode).json({ + status: { code, message }, + response: payload || {}, + }); +}; + helpers.noScriptErrors = async function (req, res, error, httpStatus) { if (req.body.noscript !== 'true') { if (typeof error === 'string') { @@ -454,28 +480,7 @@ helpers.formatApiResponse = async (statusCode, res, payload) => { } if (String(statusCode).startsWith('2')) { - if (res.req.loggedIn) { - res.set('cache-control', 'private'); - } - - let code = 'ok'; - let message = 'OK'; - switch (statusCode) { - case 202: - code = 'accepted'; - message = 'Accepted'; - break; - - case 204: - code = 'no-content'; - message = 'No Content'; - break; - } - - res.status(statusCode).json({ - status: { code, message }, - response: payload || {}, - }); + handleSuccessfulResponse(statusCode, res, payload); } else if (payload instanceof Error || typeof payload === 'string') { const message = payload instanceof Error ? payload.message : payload; const response = {};