From f9eb9e739a36ce83dd20b3cb8082d13666b06c78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Ti=E1=BA=BFn=20T=C3=A0i?= <63393170+fdhhhdjd@users.noreply.github.com> Date: Sun, 23 Apr 2023 23:12:24 +0700 Subject: [PATCH] #275 [Backend] New Spec 23042023 --- backend-manager-student/src/admin_api/app.js | 21 +++ .../book.controllers/book.controller.js | 8 +- .../src/share/configs/message.js | 5 + .../src/share/middleware/handle_error.js | 24 ++-- .../src/share/models/author.model.js | 3 +- .../src/share/models/book.model.js | 3 +- .../src/share/models/phone.model.js | 3 +- .../src/share/models/rating.model.js | 3 +- .../src/share/models/user.model.js | 3 +- .../services/user_service/comment_service.js | 22 +++ .../src/share/utils/redis_pub_sub_helper.js | 30 ++-- backend-manager-student/src/user_api/app.js | 21 +++ .../comment.controllers/comment.controller.js | 4 +- .../comment.private.route.js | 128 +++++++++++++++++- .../comment.routes/comment.private.route.js | 8 ++ server-media-service/.eslintrc.js | 42 +++++- server-media-service/src/media-service/app.js | 24 +++- .../src/share/configs/message.js | 3 + .../src/share/services/remove_tmp.service.js | 15 +- .../src/share/services/upload.service.js | 45 ++++-- server-send-email-student/.eslintrc.js | 42 +++++- 21 files changed, 387 insertions(+), 70 deletions(-) diff --git a/backend-manager-student/src/admin_api/app.js b/backend-manager-student/src/admin_api/app.js index 5767021..adc0501 100644 --- a/backend-manager-student/src/admin_api/app.js +++ b/backend-manager-student/src/admin_api/app.js @@ -14,6 +14,8 @@ const session = require('express-session'); const CONFIGS = require('../share/configs/config'); const CONSTANTS = require('../share/configs/constants'); const OPTIONS = require('../share/configs/option'); +const MESSAGES = require('../share/configs/message'); +const { returnReasons } = require('../share/middleware/handle_error'); //! CACHE MEMORY const { REDIS_MASTER } = require('../share/db/init_multiple_redis'); @@ -84,4 +86,23 @@ app.use( //! ROUTE app.use(ADMIN_API); +//! INIT HANDLE ERROR +app.use((req, res, next) => { + const error = new Error(MESSAGES.GENERAL.NOTFOUND); + error.status = CONSTANTS.HTTP.STATUS_4XX_NOT_FOUND; + next(error); +}); + +/* eslint-disable no-unused-vars */ +app.use((error, req, res, next) => { + const statusCode = error.status || CONSTANTS.HTTP.STATUS_5XX_INTERNAL_SERVER_ERROR; + const message = error.message || MESSAGES.GENERAL.INTERNAL_SERVER; + return res.status(statusCode).json({ + status: statusCode, + stack: CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_DEV ? error.stack : MESSAGES.MEDIA.STRING_EMPTY, + message: returnReasons(CONSTANTS.HTTP.STATUS_4XX_NOT_FOUND), + element: message, + }); +}); + module.exports = app; diff --git a/backend-manager-student/src/admin_api/v1/controllers/book.controllers/book.controller.js b/backend-manager-student/src/admin_api/v1/controllers/book.controllers/book.controller.js index 9815bfc..6a0b3eb 100644 --- a/backend-manager-student/src/admin_api/v1/controllers/book.controllers/book.controller.js +++ b/backend-manager-student/src/admin_api/v1/controllers/book.controllers/book.controller.js @@ -112,8 +112,8 @@ const bookController = { result_insert_book_categories === undefined ? null : result_insert_book_categories - ? MESSAGES.GENERAL.SERVER_INSERT_FAIL - : MESSAGES.GENERAL.SERVER_CURD_SUCCESS, + ? MESSAGES.GENERAL.SERVER_INSERT_FAIL + : MESSAGES.GENERAL.SERVER_CURD_SUCCESS, }, }, }); @@ -269,8 +269,8 @@ const bookController = { result_insert_book_categories === undefined ? null : result_insert_book_categories - ? MESSAGES.GENERAL.SERVER_UPDATE_FAIL - : MESSAGES.GENERAL.SERVER_CURD_SUCCESS, + ? MESSAGES.GENERAL.SERVER_UPDATE_FAIL + : MESSAGES.GENERAL.SERVER_CURD_SUCCESS, }, }, }); diff --git a/backend-manager-student/src/share/configs/message.js b/backend-manager-student/src/share/configs/message.js index aba595d..6f03cc1 100644 --- a/backend-manager-student/src/share/configs/message.js +++ b/backend-manager-student/src/share/configs/message.js @@ -29,6 +29,9 @@ module.exports = { NOT_EXIT_ACCOUNT: 'Account admin not exit!', }, GENERAL: { + // ? EMPTY + STRING_EMPTY: '', + // ? INVALID INPUT HEADER,.. INVALID_INPUT: 'Invalid input!', INVALID_HEADER: 'Invalid Header!', @@ -42,6 +45,7 @@ module.exports = { // ? EXITS EXITS_EMAIL_PHONE: 'Email or Phone or Email or Mssv exits !', EXITS_NOT_BOOK: 'Book Not Found!', + EXITS_NOT_COMMENT: 'Comment Not Found!', EXITS_NOT_BORROW_BOOK: 'Borrow Book Not Found!', EXITS_DELETE_BOOK: 'Book already delete!', EXITS_DELETE_AUTHOR: 'Author already delete!', @@ -101,6 +105,7 @@ module.exports = { SERVER_INSERT_FAIL: 'Insert Fail!', SERVER_UPDATE_FAIL: 'Update Fail!', SERVER_CURD_SUCCESS: 'Success!', + INTERNAL_SERVER: 'Internal Server Error', // ? SUCCESS SUCCESS_CHANGE_PASSWORD: 'Change Password Success!', diff --git a/backend-manager-student/src/share/middleware/handle_error.js b/backend-manager-student/src/share/middleware/handle_error.js index d0e3cc4..a4edede 100644 --- a/backend-manager-student/src/share/middleware/handle_error.js +++ b/backend-manager-student/src/share/middleware/handle_error.js @@ -28,18 +28,18 @@ module.exports = { const constraint = error.constraint; let message; switch (constraint) { - case KEY_DUPLICATE.DUPLICATE_KEY_EMAIL: - message = MESSAGES.GENERAL.EXITS_EMAIL; - break; - case KEY_DUPLICATE.DUPLICATE_KEY_PHONE: - message = MESSAGES.GENERAL.EXITS_PHONE; - break; - case KEY_DUPLICATE.DUPLICATE_KEY_MSSV: - message = MESSAGES.GENERAL.EXITS_MSSV; - break; - default: - message = MESSAGES.GENERAL.ERROR_UNKNOWN; - break; + case KEY_DUPLICATE.DUPLICATE_KEY_EMAIL: + message = MESSAGES.GENERAL.EXITS_EMAIL; + break; + case KEY_DUPLICATE.DUPLICATE_KEY_PHONE: + message = MESSAGES.GENERAL.EXITS_PHONE; + break; + case KEY_DUPLICATE.DUPLICATE_KEY_MSSV: + message = MESSAGES.GENERAL.EXITS_MSSV; + break; + default: + message = MESSAGES.GENERAL.ERROR_UNKNOWN; + break; } return message; }, diff --git a/backend-manager-student/src/share/models/author.model.js b/backend-manager-student/src/share/models/author.model.js index 776a75c..6d8e652 100644 --- a/backend-manager-student/src/share/models/author.model.js +++ b/backend-manager-student/src/share/models/author.model.js @@ -10,7 +10,8 @@ module.exports = { createAuthor: (data) => new Promise((resolve, reject) => { try { - const result = knex('authors').insert(data).onConflict('author_id').merge().returning(['author_id']); + const result = knex('authors').insert(data).onConflict('author_id').merge() + .returning(['author_id']); resolve(result); } catch (error) { reject(error); diff --git a/backend-manager-student/src/share/models/book.model.js b/backend-manager-student/src/share/models/book.model.js index f8c7264..a720103 100644 --- a/backend-manager-student/src/share/models/book.model.js +++ b/backend-manager-student/src/share/models/book.model.js @@ -13,7 +13,8 @@ module.exports = { createBook: (data) => new Promise((resolve, reject) => { try { - const result = knex('books').insert(data).onConflict('book_id').merge().returning(['book_id']); + const result = knex('books').insert(data).onConflict('book_id').merge() + .returning(['book_id']); resolve(result); } catch (error) { reject(error); diff --git a/backend-manager-student/src/share/models/phone.model.js b/backend-manager-student/src/share/models/phone.model.js index c46c101..4fe56e1 100644 --- a/backend-manager-student/src/share/models/phone.model.js +++ b/backend-manager-student/src/share/models/phone.model.js @@ -10,7 +10,8 @@ module.exports = { createPhone: (data) => new Promise((resolve, reject) => { try { - const result = knex('phone').insert(data).onConflict('phone_id').merge().returning(['phone_id']); + const result = knex('phone').insert(data).onConflict('phone_id').merge() + .returning(['phone_id']); resolve(result); } catch (error) { reject(error); diff --git a/backend-manager-student/src/share/models/rating.model.js b/backend-manager-student/src/share/models/rating.model.js index b1e0fa1..dfcac46 100644 --- a/backend-manager-student/src/share/models/rating.model.js +++ b/backend-manager-student/src/share/models/rating.model.js @@ -10,7 +10,8 @@ module.exports = { createRatings: (data) => new Promise((resolve, reject) => { try { - const result = knex('book_rates').insert(data).onConflict('rate_id').merge().returning(['rate_id']); + const result = knex('book_rates').insert(data).onConflict('rate_id').merge() + .returning(['rate_id']); resolve(result); } catch (error) { reject(error); diff --git a/backend-manager-student/src/share/models/user.model.js b/backend-manager-student/src/share/models/user.model.js index f83ddfb..afff36b 100644 --- a/backend-manager-student/src/share/models/user.model.js +++ b/backend-manager-student/src/share/models/user.model.js @@ -119,7 +119,8 @@ module.exports = { createStudent: (data) => new Promise((resolve, reject) => { try { - const result_student = knex('user').insert(data).onConflict('user_id').merge().returning(['user_id']); + const result_student = knex('user').insert(data).onConflict('user_id').merge() + .returning(['user_id']); resolve(result_student); } catch (error) { reject(error); diff --git a/backend-manager-student/src/share/services/user_service/comment_service.js b/backend-manager-student/src/share/services/user_service/comment_service.js index d91ce01..5066791 100644 --- a/backend-manager-student/src/share/services/user_service/comment_service.js +++ b/backend-manager-student/src/share/services/user_service/comment_service.js @@ -46,6 +46,9 @@ const list_comment = async (match, search_data) => { return list_comment; }; +const list_comment_slug = async (match, search_data) => + await COMMENT.find(match, search_data).sort({ full_slug: 1 }).lean(); + /** * @author Nguyễn Tiến Tài * @created_at 13/04/2023 @@ -54,8 +57,27 @@ const list_comment = async (match, search_data) => { */ const get_paren_slug = async (data) => await COMMENT.findOne(data).lean(); +/** + * @author Nguyễn Tiến Tài + * @created_at 23/04/2023 + * @description Delete comment + * @function delete_comment + */ +const delete_comment = async (commentId) => await COMMENT.deleteOne(commentId); + +/** + * @author Nguyễn Tiến Tài + * @created_at 23/04/2023 + * @description get paren slug + * @function get_comment_id + */ +const get_comment_id = async (data) => await COMMENT.findOne(data).lean(); + module.exports = { insert_comment, list_comment, get_paren_slug, + delete_comment, + get_comment_id, + list_comment_slug, }; diff --git a/backend-manager-student/src/share/utils/redis_pub_sub_helper.js b/backend-manager-student/src/share/utils/redis_pub_sub_helper.js index c16693e..898fef3 100644 --- a/backend-manager-student/src/share/utils/redis_pub_sub_helper.js +++ b/backend-manager-student/src/share/utils/redis_pub_sub_helper.js @@ -74,21 +74,21 @@ const handleException = (err, name, port) => { }); let message_queue; switch (name) { - case CONSTANTS.NAME_SERVER.STUDENT: - message_queue = CONSTANTS.QUEUE.REDIS_SERVER_STUDENT; - break; - case CONSTANTS.NAME_SERVER.ADMIN: - message_queue = CONSTANTS.QUEUE.REDIS_SERVER_ADMIN; - break; - case CONSTANTS.NAME_SERVER.CRON: - message_queue = CONSTANTS.QUEUE.REDIS_SERVER_CRON; - break; - case CONSTANTS.NAME_SERVER.DB: - message_queue = CONSTANTS.QUEUE.REDIS_DB; - break; - default: - message_queue = CONSTANTS.QUEUE.REDIS_SERVER_CRON; - break; + case CONSTANTS.NAME_SERVER.STUDENT: + message_queue = CONSTANTS.QUEUE.REDIS_SERVER_STUDENT; + break; + case CONSTANTS.NAME_SERVER.ADMIN: + message_queue = CONSTANTS.QUEUE.REDIS_SERVER_ADMIN; + break; + case CONSTANTS.NAME_SERVER.CRON: + message_queue = CONSTANTS.QUEUE.REDIS_SERVER_CRON; + break; + case CONSTANTS.NAME_SERVER.DB: + message_queue = CONSTANTS.QUEUE.REDIS_DB; + break; + default: + message_queue = CONSTANTS.QUEUE.REDIS_SERVER_CRON; + break; } // Publish data queue Redis return queueMessageTelegram(message_queue, { diff --git a/backend-manager-student/src/user_api/app.js b/backend-manager-student/src/user_api/app.js index 45a68ff..198dc2a 100644 --- a/backend-manager-student/src/user_api/app.js +++ b/backend-manager-student/src/user_api/app.js @@ -14,6 +14,8 @@ const session = require('express-session'); const CONFIGS = require('../share/configs/config'); const CONSTANTS = require('../share/configs/constants'); const OPTIONS = require('../share/configs/option'); +const MESSAGES = require('../share/configs/message'); +const { returnReasons } = require('../share/middleware/handle_error'); //! CACHE MEMORY const { REDIS_MASTER } = require('../share/db/init_multiple_redis'); @@ -90,6 +92,25 @@ app.use(DEVICE_MIDDLEWARE); //! ROUTE app.use(USER_API); +//! INIT HANDLE ERROR +app.use((req, res, next) => { + const error = new Error(MESSAGES.GENERAL.NOTFOUND); + error.status = CONSTANTS.HTTP.STATUS_4XX_NOT_FOUND; + next(error); +}); + +/* eslint-disable no-unused-vars */ +app.use((error, req, res, next) => { + const statusCode = error.status || CONSTANTS.HTTP.STATUS_5XX_INTERNAL_SERVER_ERROR; + const message = error.message || MESSAGES.GENERAL.INTERNAL_SERVER; + return res.status(statusCode).json({ + status: statusCode, + stack: CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_DEV ? error.stack : MESSAGES.MEDIA.STRING_EMPTY, + message: returnReasons(CONSTANTS.HTTP.STATUS_4XX_NOT_FOUND), + element: message, + }); +}); + //! REDIS PUBSUB require('../share/db/redis_queue'); diff --git a/backend-manager-student/src/user_api/v1/controllers/comment.controllers/comment.controller.js b/backend-manager-student/src/user_api/v1/controllers/comment.controllers/comment.controller.js index 95c27c4..74409b0 100644 --- a/backend-manager-student/src/user_api/v1/controllers/comment.controllers/comment.controller.js +++ b/backend-manager-student/src/user_api/v1/controllers/comment.controllers/comment.controller.js @@ -62,7 +62,7 @@ const commentController = { } const search_data = { - _id: 0, + _id: 1, user_id: 1, book_id: 1, content: 1, @@ -70,6 +70,8 @@ const commentController = { full_slug: 1, parent_slug: 1, comment_replies_num: 1, + createdAt: 1, + updatedAt: 1, }; const comments = await list_comment(match, search_data); diff --git a/backend-manager-student/src/user_api/v1/controllers/comment.controllers/comment.private.route.js b/backend-manager-student/src/user_api/v1/controllers/comment.controllers/comment.private.route.js index bbd7ebb..c58ea3c 100644 --- a/backend-manager-student/src/user_api/v1/controllers/comment.controllers/comment.private.route.js +++ b/backend-manager-student/src/user_api/v1/controllers/comment.controllers/comment.private.route.js @@ -4,16 +4,26 @@ const MESSAGES = require('../../../../share/configs/message'); const HELPER = require('../../../../share/utils/helper'); const RANDOMS = require('../../../../share/utils/random'); +//! MODEL +const book_model = require('../../../../share/models/book.model'); + //! MIDDLEWARE const { returnReasons } = require('../../../../share/middleware/handle_error'); //! SERVICE -const { insert_comment, get_paren_slug } = require('../../../../share/services/user_service/comment_service'); +const { + insert_comment, + get_paren_slug, + delete_comment, + list_comment_slug, + get_comment_id, +} = require('../../../../share/services/user_service/comment_service'); const commentController = { /** * @author Nguyễn Tiến Tài * @created_at 09/04/2023 + * @updated_at 23/04/2023 * @description create Comment * @function createComment */ @@ -36,6 +46,23 @@ const commentController = { const { id } = req.auth_user; try { + // Check book exit in databse + const check_book_not_exit = await book_model.getBookById( + { book_id, isdeleted: CONSTANTS.DELETED_DISABLE }, + { + book_id: 'book_id', + }, + ); + if (!check_book_not_exit || !check_book_not_exit.length) { + return res.status(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST).json({ + status: CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST, + message: returnReasons(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST), + element: { + result: MESSAGES.GENERAL.EXITS_NOT_BOOK, + }, + }); + } + // Generate slug and timestamp let slug = RANDOMS.createID(); const posted = new Date(); @@ -99,5 +126,104 @@ const commentController = { }); } }, + + /** + * @author Nguyễn Tiến Tài + * @created_at 23/04/2023 + * @description delete Comment + * @function deleteComment + */ + deleteComment: async (req, res) => { + const { comment_id } = req.body.input.comment_input; + // Validate input + if (!comment_id) { + // Return bad request error if input is invalid + return res.status(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST).json({ + status: CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST, + message: returnReasons(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST), + element: { + result: MESSAGES.GENERAL.INVALID_INPUT, + }, + }); + } + // Get user Id from request + const { id } = req.auth_user; + try { + // Check comment exit in databse + const data_comment_id = await get_comment_id({ _id: comment_id, user_id: id }); + if (!data_comment_id) { + return res.status(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST).json({ + status: CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST, + message: returnReasons(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST), + element: { + result: MESSAGES.GENERAL.EXITS_NOT_COMMENT, + }, + }); + } + // Check book exit in databse + const check_book_not_exits = await book_model.getBookById( + { book_id: data_comment_id.book_id, isdeleted: CONSTANTS.DELETED_DISABLE }, + { + book_id: 'book_id', + }, + ); + if (!check_book_not_exits || !check_book_not_exits.length) { + return res.status(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST).json({ + status: CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST, + message: returnReasons(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST), + element: { + result: MESSAGES.GENERAL.EXITS_NOT_BOOK, + }, + }); + } + const match = { + book_id: data_comment_id.book_id, + full_slug: new RegExp(data_comment_id.slug, 'i'), + }; + + const search_data = { + _id: 1, + }; + const comments = await list_comment_slug(match, search_data); + + // Delete comment + let err; + let result; + if (!comments || !comments.length) { + [err, result] = await HELPER.handleRequest(delete_comment({ _id: comment_id, user_id: id })); + } else { + for (const list_comment_delete of comments) { + [err, result] = await HELPER.handleRequest( + delete_comment({ _id: list_comment_delete._id, user_id: id }), + ); + } + } + + if (result) { + return res.status(CONSTANTS.HTTP.STATUS_2XX_OK).json({ + status: CONSTANTS.HTTP.STATUS_2XX_OK, + message: returnReasons(CONSTANTS.HTTP.STATUS_2XX_OK), + }); + } + if (err) { + console.error(err); + + return res.status(CONSTANTS.HTTP.STATUS_5XX_INTERNAL_SERVER_ERROR).json({ + status: CONSTANTS.HTTP.STATUS_5XX_INTERNAL_SERVER_ERROR, + message: returnReasons(CONSTANTS.HTTP.STATUS_5XX_INTERNAL_SERVER_ERROR), + }); + } + } catch (error) { + console.error(error); + // Return service unavailable error if an error occurs + return res.status(CONSTANTS.HTTP.STATUS_5XX_SERVICE_UNAVAILABLE).json({ + status: CONSTANTS.HTTP.STATUS_5XX_SERVICE_UNAVAILABLE, + message: returnReasons(CONSTANTS.HTTP.STATUS_5XX_SERVICE_UNAVAILABLE), + element: { + result: MESSAGES.GENERAL.SERVER_OUT_OF_SERVICE, + }, + }); + } + }, }; module.exports = commentController; diff --git a/backend-manager-student/src/user_api/v1/routes/comment.routes/comment.private.route.js b/backend-manager-student/src/user_api/v1/routes/comment.routes/comment.private.route.js index 0555687..b69bbf3 100644 --- a/backend-manager-student/src/user_api/v1/routes/comment.routes/comment.private.route.js +++ b/backend-manager-student/src/user_api/v1/routes/comment.routes/comment.private.route.js @@ -12,4 +12,12 @@ const commentController = require('../../controllers/comment.controllers/comment */ router.post('/create', commentController.createComment); +/** + * @author Nguyễn Tiến Tài + * @created_at 23/04/2023 + * @description Route Delete Comment book + * @param {('POST')} [method='POST'] The request's method + */ +router.post('/delete', commentController.deleteComment); + module.exports = router; diff --git a/server-media-service/.eslintrc.js b/server-media-service/.eslintrc.js index 52f3ca6..733caf9 100644 --- a/server-media-service/.eslintrc.js +++ b/server-media-service/.eslintrc.js @@ -11,15 +11,12 @@ module.exports = { rules: { // 'arrow-parens': 'off', 'no-plusplus': ['error', { allowForLoopAfterthoughts: true }], - 'consistent-return': 'off', 'arrow-parens': ['error', 'always'], - 'max-len': ['error', { code: 120 }], - eqeqeq: 'error', 'function-paren-newline': 'off', indent: ['error', 4], camelcase: 'off', - 'linebreak-style': [2, 'unix'], + // 'linebreak-style': [2, 'unix'], 'no-useless-escape': 'off', 'no-console': [ 'error', @@ -41,8 +38,43 @@ module.exports = { allow: [], }, ], - 'operator-linebreak': [2, 'before', { overrides: { '?': 'after' } }], + 'operator-linebreak': [ + 'error', + 'after', + { + overrides: { + '?': 'before', + ':': 'before', + '&&': 'before', + '||': 'before', + }, + }, + ], 'import/prefer-default-export': 'off', 'import/no-unresolved': 'off', + 'linebreak-style': ['error', process.platform === 'win64' && 'win32' ? 'windows' : 'unix'], + // eslint-disable-next-line no-dupe-keys + 'no-return-await': 'off', + 'func-names': 'off', + 'no-ternary': 0, + 'no-unneeded-ternary': 0, + 'import/order': 'off', + 'no-restricted-globals': 'off', + 'prefer-const': 'off', + 'max-len': 'off', + 'no-restricted-syntax': 'off', + 'no-await-in-loop': 'off', + 'consistent-return': 'off', + 'no-template-curly-in-string': 'off', + 'prefer-destructuring': 'off', + radix: 'off', + 'no-else-return': 'off', + 'no-async-promise-executor': 'off', + 'no-promise-executor-return': 'off', + // eslint-disable-next-line no-dupe-keys + 'no-unused-expressions': 'off', + 'implicit-arrow-linebreak': 'off', + 'object-curly-newline': 'off', + 'no-nested-ternary': 'off', }, }; diff --git a/server-media-service/src/media-service/app.js b/server-media-service/src/media-service/app.js index c446e83..1acec8e 100644 --- a/server-media-service/src/media-service/app.js +++ b/server-media-service/src/media-service/app.js @@ -10,12 +10,14 @@ const swaggerJsDoc = require('swagger-jsdoc'); //! SHARE GENERAL const DEVICE_MIDDLEWARE = require('../share/middlewares/device.middleware'); +const { returnReasons } = require('../share/middlewares/handle_error'); //! SHARE const MEDIA_API = require('./v1/routes/index.route'); const CONSTANTS = require('../share/configs/constants'); const OPTIONS = require('../share/configs/option'); const CONFIGS = require('../share/configs/config'); +const MESSAGES = require('../share/configs/message'); //! USED LIBRARY const app = express(); @@ -61,6 +63,26 @@ app.use(DEVICE_MIDDLEWARE); //! ROUTE app.use(MEDIA_API); -//! REDIS PUBSUB +//! INIT HANDLE ERROR +app.use((req, res, next) => { + const error = new Error(MESSAGES.MEDIA.NOT_FOUND); + error.status = CONSTANTS.HTTP.STATUS_4XX_NOT_FOUND; + next(error); +}); + +/* eslint-disable no-unused-vars */ +app.use((error, req, res, next) => { + const statusCode = error.status || CONSTANTS.HTTP.STATUS_5XX_INTERNAL_SERVER_ERROR; + const message = error.message || MESSAGES.MEDIA.INTERNAL_SERVER; + return res.status(statusCode).json({ + status: statusCode, + stack: + CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_DEV + ? error.stack + : MESSAGES.MEDIA.STRING_EMPTY, + message: returnReasons(CONSTANTS.HTTP.STATUS_4XX_NOT_FOUND), + element: message, + }); +}); module.exports = app; diff --git a/server-media-service/src/share/configs/message.js b/server-media-service/src/share/configs/message.js index 5ab80dd..79a2725 100644 --- a/server-media-service/src/share/configs/message.js +++ b/server-media-service/src/share/configs/message.js @@ -19,5 +19,8 @@ module.exports = { NO_EXPIRED_TOKEN: 'Expired Token !', NO_SERVER_OUT_OF_SERVICE: 'Out Of Service!', NO_DEVICE_MISSING: 'Missing Device!', + NOT_FOUND: 'Not Found!', + INTERNAL_SERVER: 'Internal Server Error', + STRING_EMPTY: '', }, }; diff --git a/server-media-service/src/share/services/remove_tmp.service.js b/server-media-service/src/share/services/remove_tmp.service.js index b915f4c..14cdd87 100644 --- a/server-media-service/src/share/services/remove_tmp.service.js +++ b/server-media-service/src/share/services/remove_tmp.service.js @@ -1,14 +1,15 @@ +//! LIBRARY const fs = require('fs'); module.exports = { /** - * @author Nguyễn Tiến Tài - * @created_at 29/12/2022 - * @description delete image path - * @function handleRemoveTmp - * @param { path } - * @return {String} - */ + * @author Nguyễn Tiến Tài + * @created_at 29/12/2022 + * @description delete image path + * @function handleRemoveTmp + * @param { path } + * @return {String} + */ handleRemoveTmp: (path) => { fs.unlink(path, (err) => { if (err) throw err; diff --git a/server-media-service/src/share/services/upload.service.js b/server-media-service/src/share/services/upload.service.js index 4943b16..3957755 100644 --- a/server-media-service/src/share/services/upload.service.js +++ b/server-media-service/src/share/services/upload.service.js @@ -1,31 +1,46 @@ +//! DB const cloudinary_db = require('../db/cloudinary_db'); + +//! SERVICE const { handleRemoveTmp } = require('./remove_tmp.service'); -const CONSTANTS = require('../../share/configs/constants') + +//! CONFIGS +const CONSTANTS = require('../configs/constants'); + module.exports = { /** - * @author Nguyễn Tiến Tài - * @created_at 29/12/2022 - * @update_at 12/01/2023 - * @description Upload storage cloud - * @function handleUpload - * @param { path_image, name_image } - * @return {data} - */ + * @author Nguyễn Tiến Tài + * @created_at 29/12/2022 + * @update_at 12/01/2023 + * @description Upload storage cloud + * @function handleUpload + * @param { path_image, name_image } + * @return {data} + */ handleUpload: async (path_image, name_image, cloud_bucket) => { let data = null; await cloudinary_db.v2.uploader.upload( path_image, - cloud_bucket === CONSTANTS.MIME_DOCUMENT ? - { resource_type: CONSTANTS.OPTION_CLOUD, folder: CONSTANTS.OPTION_FOLDER_DOCUMENT, public_id: `${name_image}`, format: CONSTANTS.OPTION_CLOUD_DOCUMENT_FORMAT } - : { resource_type: CONSTANTS.OPTION_CLOUD, folder: CONSTANTS.OPTION_FOLDER_IMAGE, public_id: `${name_image}` }, + cloud_bucket === CONSTANTS.MIME_DOCUMENT + ? { + resource_type: CONSTANTS.OPTION_CLOUD, + folder: CONSTANTS.OPTION_FOLDER_DOCUMENT, + public_id: `${name_image}`, + format: CONSTANTS.OPTION_CLOUD_DOCUMENT_FORMAT, + } + : { + resource_type: CONSTANTS.OPTION_CLOUD, + folder: CONSTANTS.OPTION_FOLDER_IMAGE, + public_id: `${name_image}`, + }, async (err, result) => { if (err) { - data = err; + data = err; } else { - data = result + data = result; } handleRemoveTmp(path_image); - return data + return data; }, ); return data; diff --git a/server-send-email-student/.eslintrc.js b/server-send-email-student/.eslintrc.js index 11b86e1..733caf9 100644 --- a/server-send-email-student/.eslintrc.js +++ b/server-send-email-student/.eslintrc.js @@ -12,12 +12,11 @@ module.exports = { // 'arrow-parens': 'off', 'no-plusplus': ['error', { allowForLoopAfterthoughts: true }], 'arrow-parens': ['error', 'always'], - eqeqeq: 'error', 'function-paren-newline': 'off', indent: ['error', 4], camelcase: 'off', - 'linebreak-style': [2, 'unix'], + // 'linebreak-style': [2, 'unix'], 'no-useless-escape': 'off', 'no-console': [ 'error', @@ -39,8 +38,43 @@ module.exports = { allow: [], }, ], - 'operator-linebreak': [2, 'before', { overrides: { '?': 'after' } }], + 'operator-linebreak': [ + 'error', + 'after', + { + overrides: { + '?': 'before', + ':': 'before', + '&&': 'before', + '||': 'before', + }, + }, + ], 'import/prefer-default-export': 'off', - 'import/no-unresolved': 'off' + 'import/no-unresolved': 'off', + 'linebreak-style': ['error', process.platform === 'win64' && 'win32' ? 'windows' : 'unix'], + // eslint-disable-next-line no-dupe-keys + 'no-return-await': 'off', + 'func-names': 'off', + 'no-ternary': 0, + 'no-unneeded-ternary': 0, + 'import/order': 'off', + 'no-restricted-globals': 'off', + 'prefer-const': 'off', + 'max-len': 'off', + 'no-restricted-syntax': 'off', + 'no-await-in-loop': 'off', + 'consistent-return': 'off', + 'no-template-curly-in-string': 'off', + 'prefer-destructuring': 'off', + radix: 'off', + 'no-else-return': 'off', + 'no-async-promise-executor': 'off', + 'no-promise-executor-return': 'off', + // eslint-disable-next-line no-dupe-keys + 'no-unused-expressions': 'off', + 'implicit-arrow-linebreak': 'off', + 'object-curly-newline': 'off', + 'no-nested-ternary': 'off', }, };