From 9a9640773028ef9df2d2fbcae53e48ffa9922089 Mon Sep 17 00:00:00 2001 From: Pierre Brisorgueil Date: Fri, 1 May 2020 12:33:29 +0200 Subject: [PATCH] =?UTF-8?q?refactor(users,=20uploads):=20clean=20some=20re?= =?UTF-8?q?sponses=20=E2=99=BB=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../uploads/controllers/uploads.controller.js | 43 +++++++++---------- .../controllers/users.data.controller.js | 2 +- .../users/users.password.controller.js | 4 +- 3 files changed, 23 insertions(+), 26 deletions(-) diff --git a/modules/uploads/controllers/uploads.controller.js b/modules/uploads/controllers/uploads.controller.js index ee4f2cf01..f6753295c 100644 --- a/modules/uploads/controllers/uploads.controller.js +++ b/modules/uploads/controllers/uploads.controller.js @@ -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); diff --git a/modules/users/controllers/users.data.controller.js b/modules/users/controllers/users.data.controller.js index 6320a1b34..3cd5cd7a4 100644 --- a/modules/users/controllers/users.data.controller.js +++ b/modules/users/controllers/users.data.controller.js @@ -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); } diff --git a/modules/users/controllers/users/users.password.controller.js b/modules/users/controllers/users/users.password.controller.js index fe882bc38..7a9c4cfef 100644 --- a/modules/users/controllers/users/users.password.controller.js +++ b/modules/users/controllers/users/users.password.controller.js @@ -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 }); }; /** @@ -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 }); }; /**