Skip to content

Commit

Permalink
Merge pull request #747 from PierreBrisorgueil/refactorSomeResponses
Browse files Browse the repository at this point in the history
refactor(users, uploads): clean some responses ♻️
  • Loading branch information
PierreBrisorgueil authored May 1, 2020
2 parents 3487f6f + 9a96407 commit fed473e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
43 changes: 20 additions & 23 deletions modules/uploads/controllers/uploads.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,29 +107,26 @@ exports.uploadByImageName = async (req, res, next, uploadImageName) => {
// Name
const imageName = uploadImageName.split('.');
const opts = imageName[0].split('-');
if (imageName.length !== 2) responses.error(res, 404, 'Not Found', 'Wrong name schema')();
else if (opts.length > 3) responses.error(res, 404, 'Not Found', 'Too much params')();
else {
// data work
const upload = await UploadsService.get(`${opts[0]}.${imageName[1]}`);
if (!upload) responses.error(res, 404, 'Not Found', 'No Upload with that name has been found')();
else {
// options
const sharp = _.get(config, `uploads.${upload.metadata.kind}.sharp`);
if (opts[1] && (!sharp || !sharp.sizes)) responses.error(res, 422, 'Unprocessable Entity', 'Size param not available')();
else if (opts[1] && (!/^\d+$/.test(opts[1]) || !sharp.sizes.includes(opts[1]))) responses.error(res, 422, 'Unprocessable Entity', 'Wrong size param')();
else if (opts[2] && (!sharp || !sharp.operations)) responses.error(res, 422, 'Unprocessable Entity', 'Operations param not available')();
else if (opts[2] && !sharp.operations.includes(opts[2])) responses.error(res, 422, 'Unprocessable Entity', 'Operation param not available')();
else {
// return
req.upload = upload;
req.isOwner = upload.metadata.user; // used if we proteck road by isOwner policy
req.sharpSize = parseInt(opts[1], 0) || null;
req.sharpOption = opts[2] || null;
next();
}
}
}
if (imageName.length !== 2) return responses.error(res, 404, 'Not Found', 'Wrong name schema')();
if (opts.length > 3) return responses.error(res, 404, 'Not Found', 'Too much params')();

// data work
const upload = await UploadsService.get(`${opts[0]}.${imageName[1]}`);
if (!upload) return responses.error(res, 404, 'Not Found', 'No Upload with that name has been found')();

// options
const sharp = _.get(config, `uploads.${upload.metadata.kind}.sharp`);
if (opts[1] && (!sharp || !sharp.sizes)) return responses.error(res, 422, 'Unprocessable Entity', 'Size param not available')();
if (opts[1] && (!/^\d+$/.test(opts[1]) || !sharp.sizes.includes(opts[1]))) return responses.error(res, 422, 'Unprocessable Entity', 'Wrong size param')();
if (opts[2] && (!sharp || !sharp.operations)) return responses.error(res, 422, 'Unprocessable Entity', 'Operations param not available')();
if (opts[2] && !sharp.operations.includes(opts[2])) return responses.error(res, 422, 'Unprocessable Entity', 'Operation param not available')();

// return
req.upload = upload;
req.isOwner = upload.metadata.user; // used if we proteck road by isOwner policy
req.sharpSize = parseInt(opts[1], 0) || null;
req.sharpOption = opts[2] || null;
next();
} catch (err) {
console.log('err', err);
next(err);
Expand Down
2 changes: 1 addition & 1 deletion modules/users/controllers/users.data.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ exports.getMail = async (req, res) => {
});

if (!mail.accepted) return responses.error(res, 400, 'Bad Request', 'Failure sending email')();
responses.success(res, 'An email has been sent to the user email with data')();
responses.success(res, 'An email has been sent to the user email with data')({ status: true });
} catch (err) {
responses.error(res, 422, 'Unprocessable Entity', errors.getMessage(err))(err);
}
Expand Down
4 changes: 2 additions & 2 deletions modules/users/controllers/users/users.password.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ exports.forgot = async (req, res) => {
},
});
if (!mail.accepted) return responses.error(res, 400, 'Bad Request', 'Failure sending email')();
responses.success(res, 'An email has been sent to the provided email with further instructions')();
responses.success(res, 'An email has been sent to the provided email with further instructions')({ status: true });
};

/**
Expand Down Expand Up @@ -121,7 +121,7 @@ exports.reset = async (req, res) => {
},
});
if (!mail.accepted) return responses.error(res, 400, 'Bad Request', 'Failure sending email')();
responses.success(res, 'An email has been sent to the provided email with further instructions')();
responses.success(res, 'An email has been sent to the provided email with further instructions')({ status: true });
};

/**
Expand Down

0 comments on commit fed473e

Please sign in to comment.