Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reducing cognitive overload in src/controllers/helpers.js #27

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 27 additions & 22 deletions src/controllers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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 = {};
Expand Down