From b11f0152b8889c23460b781d5c0925797a5f86e5 Mon Sep 17 00:00:00 2001 From: Aisha Al Attiyah Date: Tue, 3 Sep 2024 08:45:35 +0300 Subject: [PATCH] reduce cognitive complexity --- dump.rdb | Bin 0 -> 7470 bytes src/controllers/helpers.js | 48 ++++++++++++++++++++----------------- 2 files changed, 26 insertions(+), 22 deletions(-) create mode 100644 dump.rdb diff --git a/dump.rdb b/dump.rdb new file mode 100644 index 0000000000000000000000000000000000000000..aafe5342cc5d0b8dd45497767e37db4aba557030 GIT binary patch literal 7470 zcmc&(dvqJ+mH)mOJuE+E*_P!;;?c{NWy!CQ03y(lEjvzdf{C4EyM<&%(r0O6X-3S9 z{9suSC5UBFl?3OJ(6i&mVJ*|=~g|g>td%BeE;jv6uNhCRZwhBLU zfy2(7QY0Q?nhAHaTY=5MotVAo%xm$UfQVQbrN4dN#ou+IlT*}io^BL}Aog)`1m!B0 zyTCpLtgPwaS$_ltivb)9A}JtpF_{zi`~)1sq56?n)Gow%IUmB^VJ;X%yq40br4dO+ zQA!8yr%zJ*H8~I-z$nsPMe78P--zyrBT1G@0US>nbGi{bhzbCl(QDBl;$=?@-QN!Y z$Ok*+kr+~s%3%~mZVAb`JK|hW1>k*Lj2vxgy|%F#|Fz`ql+m!Pp5}!h^84M{yJJX{ z1fDG$O$R8+Y&4d8@#A!q7){*{gdYR(2i9LbBZC|on$6A``b}?DP1T%S99tk4IhkCf zk0tZewrmER*$g;w2Jl5YlX(W)Jn-MofFGF;eswGje$%4ho3h{&SHQ<`C)OIzylJF0WZW2aMwjfMLvlG54H3i%3Hjkp_&&m1{m9-^)P4L)|D{ zt)8XhGBJ%v^P-3>LdWk@6H7(Ja&<%kVOBmvzvk7{XyzbscmX5|Gf1TGUzSC{nnl2h z5x^H?oge+XndrC9Cq$V)9lqn6hJTpawZ!m?TdyAe+{AjaN+4OSxNZrsPedUO3jkJ1 zcZM*<7k_`b_Ga{Ti#(SL@vIydvHZR1EvqS;gYA(8ur0~5?D8Q?7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_a zAF-TF6ulC)-WRx_>lTDqdl&@r9Bbo;i1{D|7P4g{)z`v9+dgzYf znoIvqHpvu1k7Vz(pH^|AJ^_;k` { + 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 +479,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 = {};