From b5f75ff848a13004f853666aa45ac0cef64408a9 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: Tue, 7 Mar 2023 18:16:19 +0700 Subject: [PATCH] #180 [Backend] Feature Borrwed Book --- .../admin.controller/admin.controller.js | 16 ++--- .../author.controller/author.controller.js | 8 ++- .../book.controller/book.controller.js | 6 +- .../middleware/access.student.middleware.js | 20 +++--- .../src/share/middleware/handle_error.js | 3 +- .../src/share/models/author.model.js | 35 +++++----- .../src/share/models/book.model.js | 35 +++++----- .../src/share/models/categories.model.js | 42 ++++++----- .../src/share/models/user.model.js | 42 ++++++----- .../src/share/models/user_device.model.js | 69 +++++++++---------- .../share/models/user_reset_password.model.js | 65 +++++++++-------- .../share/models/user_verification.model.js | 69 +++++++++---------- .../services/user_service/book_service.js | 18 +++++ .../src/share/utils/helper.js | 3 +- .../src/share/utils/limited_redis.js | 23 +++---- .../src/share/utils/redis_cluster_helper.js | 19 +++-- .../book.controllers/book.controller.js | 51 ++++++++++++++ .../user.controllers/user.controller.js | 8 +-- .../routes/books.routes/book.private.route.js | 8 +++ .../src/user_api/v1/routes/index.router.js | 9 ++- 20 files changed, 306 insertions(+), 243 deletions(-) create mode 100644 backend-manager-student/src/share/services/user_service/book_service.js diff --git a/backend-manager-student/src/admin_api/v1/controllers/admin.controller/admin.controller.js b/backend-manager-student/src/admin_api/v1/controllers/admin.controller/admin.controller.js index c7d56a3..f01cb2a 100644 --- a/backend-manager-student/src/admin_api/v1/controllers/admin.controller/admin.controller.js +++ b/backend-manager-student/src/admin_api/v1/controllers/admin.controller/admin.controller.js @@ -116,8 +116,8 @@ const adminController = { sameSite: CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT ? true : false, secure: CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT ? true : false, domain: - CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT - ? req.headers[CONSTANTS.HEADER_HEADER_FORWARDED_HOST]?.split(':')[0] + CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT ? + req.headers[CONSTANTS.HEADER_HEADER_FORWARDED_HOST]?.split(':')[0] : CONSTANTS.HEADER_DOMAIN, maxAge: CONSTANTS._1_MONTH, }); @@ -261,8 +261,8 @@ const adminController = { sameSite: CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT ? true : false, secure: CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT ? true : false, domain: - CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT - ? req.headers[CONSTANTS.HEADER_HEADER_FORWARDED_HOST]?.split(':')[0] + CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT ? + req.headers[CONSTANTS.HEADER_HEADER_FORWARDED_HOST]?.split(':')[0] : CONSTANTS.HEADER_DOMAIN, maxAge: CONSTANTS._1_MONTH, }); @@ -432,12 +432,12 @@ const adminController = { class: student.class, email: student.email, gender: - student.gender.toLowerCase() === CONSTANTS.GENDER_MALE_STRING - ? CONSTANTS.GENDER_MALE + student.gender.toLowerCase() === CONSTANTS.GENDER_MALE_STRING ? + CONSTANTS.GENDER_MALE : CONSTANTS.GENDER_FEMALE, avatar_uri: - student.gender.toLowerCase() === CONSTANTS.GENDER_MALE_STRING - ? CONSTANTS.GENDER_IMAGE_MALE + student.gender.toLowerCase() === CONSTANTS.GENDER_MALE_STRING ? + CONSTANTS.GENDER_IMAGE_MALE : CONSTANTS.GENDER_IMAGE_FEMALE, }); } diff --git a/backend-manager-student/src/admin_api/v1/controllers/author.controller/author.controller.js b/backend-manager-student/src/admin_api/v1/controllers/author.controller/author.controller.js index 063da15..990bdd6 100644 --- a/backend-manager-student/src/admin_api/v1/controllers/author.controller/author.controller.js +++ b/backend-manager-student/src/admin_api/v1/controllers/author.controller/author.controller.js @@ -17,7 +17,9 @@ const authorController = { * @return {Object:{Number,String} */ createAuthor: async (req, res) => { - const { name, avatar_uri, dob, gender } = req.body.input.author_input; + const { + name, avatar_uri, dob, gender, + } = req.body.input.author_input; // Check input if (!name || !avatar_uri || !dob || !gender) { @@ -72,7 +74,9 @@ const authorController = { * @return {Object:{Number,String} */ updateAuthor: async (req, res) => { - const { author_id, name, avatar_uri, dob, gender } = req.body.input.author_input; + const { + author_id, name, avatar_uri, dob, gender, + } = req.body.input.author_input; // Check input if (!name || !avatar_uri || !dob || !gender || !author_id) { diff --git a/backend-manager-student/src/admin_api/v1/controllers/book.controller/book.controller.js b/backend-manager-student/src/admin_api/v1/controllers/book.controller/book.controller.js index 91e639a..42459f2 100644 --- a/backend-manager-student/src/admin_api/v1/controllers/book.controller/book.controller.js +++ b/backend-manager-student/src/admin_api/v1/controllers/book.controller/book.controller.js @@ -19,8 +19,10 @@ const bookController = { * @return {Object:{Number,String} */ createBook: async (req, res) => { - const { name, author_id, image_uri, description, bookshelf, language, quantity, public_id_image } = - req.body.input.author_input; + const { + name, author_id, image_uri, description, bookshelf, language, quantity, public_id_image, + } + = req.body.input.author_input; // Check input if ( diff --git a/backend-manager-student/src/share/middleware/access.student.middleware.js b/backend-manager-student/src/share/middleware/access.student.middleware.js index d45421c..16b6872 100644 --- a/backend-manager-student/src/share/middleware/access.student.middleware.js +++ b/backend-manager-student/src/share/middleware/access.student.middleware.js @@ -64,28 +64,28 @@ const accessStudentMiddleware = async (req, res, next) => { // Convert pem const publicKey = PASSWORD.decodePemPubKey(data_device[0].public_key); - // Take info from token - let auth_user_decode = TOKENS.verifyAccessToken(accessToken, publicKey); - - // Check is Student - if (auth_user_decode?.role !== 0) { + // Check time Expired token + let check_access_token = HELPER.isAccessTokenValid(accessToken, publicKey); + if (!check_access_token) { return res.status(401).json({ status: 401, message: returnReasons('401'), element: { - result: 'You not is student!', + result: 'Expired Token', }, }); } - // Check time Expired token - let check_access_token = HELPER.isAccessTokenValid(accessToken, publicKey); - if (!check_access_token) { + // Take info from token + let auth_user_decode = TOKENS.verifyAccessToken(accessToken, publicKey); + + // Check is Student + if (auth_user_decode?.role !== 0) { return res.status(401).json({ status: 401, message: returnReasons('401'), element: { - result: 'Expired Token', + result: 'You not is student!', }, }); } diff --git a/backend-manager-student/src/share/middleware/handle_error.js b/backend-manager-student/src/share/middleware/handle_error.js index b2ff6c3..13e03e3 100644 --- a/backend-manager-student/src/share/middleware/handle_error.js +++ b/backend-manager-student/src/share/middleware/handle_error.js @@ -9,6 +9,5 @@ module.exports = { * @param { code } * @return { String } */ - returnReasons: (code) => - CONSTANTS.reasonPhraseCodeProNewMap().get(code) || CONSTANTS.reasonPhraseCodeProNewMap().get('default'), + returnReasons: (code) => CONSTANTS.reasonPhraseCodeProNewMap().get(code) || CONSTANTS.reasonPhraseCodeProNewMap().get('default'), }; diff --git a/backend-manager-student/src/share/models/author.model.js b/backend-manager-student/src/share/models/author.model.js index 776a75c..eae7424 100644 --- a/backend-manager-student/src/share/models/author.model.js +++ b/backend-manager-student/src/share/models/author.model.js @@ -7,15 +7,15 @@ module.exports = { * @created_at 03/01/2023 * @description create Author */ - createAuthor: (data) => - new Promise((resolve, reject) => { - try { - const result = knex('authors').insert(data).onConflict('author_id').merge().returning(['author_id']); - resolve(result); - } catch (error) { - reject(error); - } - }), + createAuthor: (data) => new Promise((resolve, reject) => { + try { + const result = knex('authors').insert(data).onConflict('author_id').merge() + .returning(['author_id']); + resolve(result); + } catch (error) { + reject(error); + } + }), /** * @author Nguyễn Tiến Tài @@ -31,15 +31,14 @@ module.exports = { * @created_at 03/01/2023 * @description Update Author */ - updateAuthor: async (data, student_query, return_data) => - new Promise((resolve, reject) => { - try { - const result = knex('authors').update(data).where(student_query).returning(return_data); - resolve(result); - } catch (error) { - reject(error); - } - }), + updateAuthor: async (data, student_query, return_data) => new Promise((resolve, reject) => { + try { + const result = knex('authors').update(data).where(student_query).returning(return_data); + resolve(result); + } catch (error) { + reject(error); + } + }), /** * @author Nguyễn Tiến Tài * @created_at 01/03/2023 diff --git a/backend-manager-student/src/share/models/book.model.js b/backend-manager-student/src/share/models/book.model.js index 776e383..a404835 100644 --- a/backend-manager-student/src/share/models/book.model.js +++ b/backend-manager-student/src/share/models/book.model.js @@ -7,15 +7,15 @@ module.exports = { * @created_at 03/01/2023 * @description create book */ - createBook: (data) => - new Promise((resolve, reject) => { - try { - const result = knex('books').insert(data).onConflict('book_id').merge().returning(['book_id']); - resolve(result); - } catch (error) { - reject(error); - } - }), + createBook: (data) => new Promise((resolve, reject) => { + try { + const result = knex('books').insert(data).onConflict('book_id').merge() + .returning(['book_id']); + resolve(result); + } catch (error) { + reject(error); + } + }), /** * @author Nguyễn Tiến Tài @@ -31,15 +31,14 @@ module.exports = { * @created_at 03/01/2023 * @description Update book */ - updateBook: async (data, student_query, return_data) => - new Promise((resolve, reject) => { - try { - const result = knex('books').update(data).where(student_query).returning(return_data); - resolve(result); - } catch (error) { - reject(error); - } - }), + updateBook: async (data, student_query, return_data) => new Promise((resolve, reject) => { + try { + const result = knex('books').update(data).where(student_query).returning(return_data); + resolve(result); + } catch (error) { + reject(error); + } + }), /** * @author Nguyễn Tiến Tài * @created_at 03/01/2023 diff --git a/backend-manager-student/src/share/models/categories.model.js b/backend-manager-student/src/share/models/categories.model.js index 8a1ceb8..29ce4ac 100644 --- a/backend-manager-student/src/share/models/categories.model.js +++ b/backend-manager-student/src/share/models/categories.model.js @@ -7,19 +7,18 @@ module.exports = { * @created_at 03/02/2023 * @description create Categories */ - createCategories: (data) => - new Promise((resolve, reject) => { - try { - const result = knex('categories') - .insert(data) - .onConflict('category_id') - .merge() - .returning(['category_id']); - resolve(result); - } catch (error) { - reject(error); - } - }), + createCategories: (data) => new Promise((resolve, reject) => { + try { + const result = knex('categories') + .insert(data) + .onConflict('category_id') + .merge() + .returning(['category_id']); + resolve(result); + } catch (error) { + reject(error); + } + }), /** * @author Nguyễn Tiến Tài @@ -35,15 +34,14 @@ module.exports = { * @created_at 03/02/2023 * @description Update Categories */ - updateCategories: async (data, student_query, return_data) => - new Promise((resolve, reject) => { - try { - const result = knex('categories').update(data).where(student_query).returning(return_data); - resolve(result); - } catch (error) { - reject(error); - } - }), + updateCategories: async (data, student_query, return_data) => new Promise((resolve, reject) => { + try { + const result = knex('categories').update(data).where(student_query).returning(return_data); + resolve(result); + } catch (error) { + reject(error); + } + }), /** * @author Nguyễn Tiến Tài * @created_at 03/02/2023 diff --git a/backend-manager-student/src/share/models/user.model.js b/backend-manager-student/src/share/models/user.model.js index 8217b5e..db27053 100644 --- a/backend-manager-student/src/share/models/user.model.js +++ b/backend-manager-student/src/share/models/user.model.js @@ -11,19 +11,18 @@ module.exports = { * @update_at 23/01/2022 * @description Add user */ - addUser: (data) => - new Promise((resolve, reject) => { - try { - const result = knex('user') - .insert(data) - .onConflict('user_id', 'email', 'phone_number', 'mssv') - .merge() - .returning(['user_id']); - resolve(result); - } catch (error) { - reject(error); - } - }), + addUser: (data) => new Promise((resolve, reject) => { + try { + const result = knex('user') + .insert(data) + .onConflict('user_id', 'email', 'phone_number', 'mssv') + .merge() + .returning(['user_id']); + resolve(result); + } catch (error) { + reject(error); + } + }), /** * @author Nguyễn Tiến Tài @@ -39,15 +38,14 @@ module.exports = { * @created_at 13/02/2023 * @description Update student */ - updateStudent: async (data, student_query, return_data) => - new Promise((resolve, reject) => { - try { - const result = knex('user').update(data).where(student_query).returning(return_data); - resolve(result); - } catch (error) { - reject(error); - } - }), + updateStudent: async (data, student_query, return_data) => new Promise((resolve, reject) => { + try { + const result = knex('user').update(data).where(student_query).returning(return_data); + resolve(result); + } catch (error) { + reject(error); + } + }), /** * @author Nguyễn Tiến Tài * @created_at 28/02/2023 diff --git a/backend-manager-student/src/share/models/user_device.model.js b/backend-manager-student/src/share/models/user_device.model.js index 1323935..8b304b0 100644 --- a/backend-manager-student/src/share/models/user_device.model.js +++ b/backend-manager-student/src/share/models/user_device.model.js @@ -10,52 +10,49 @@ module.exports = { * @created_at 03/02/2023 * @description insertDevice */ - insertDevice: (data) => - new Promise((resolve, reject) => { - try { - const result = knex('user_device') - .insert(data) - .onConflict('device_uuid') - .merge() - .returning(['device_uuid']); - resolve(result); - } catch (error) { - reject(error); - } - }), + insertDevice: (data) => new Promise((resolve, reject) => { + try { + const result = knex('user_device') + .insert(data) + .onConflict('device_uuid') + .merge() + .returning(['device_uuid']); + resolve(result); + } catch (error) { + reject(error); + } + }), /** * @author Nguyễn Tiến Tài * @created_at 03/02/2023 * @description insertDevice */ - checkUserByToken: (refresh_token, device_uuid) => - new Promise((resolve, reject) => { - try { - let user = knex('user_device') - .join('user', 'user.user_id', 'user_device.user_id') - .where('user.isdeleted', '=', CONSTANTS.DELETED_DISABLE) - .andWhere('user_device.refresh_token', refresh_token) - .andWhere('user_device.device_uuid', device_uuid) - .select('user.user_id', 'user.role', 'user.name', 'user.email', 'user.role'); - resolve(user); - } catch (error) { - reject(error); - } - }), + checkUserByToken: (refresh_token, device_uuid) => new Promise((resolve, reject) => { + try { + let user = knex('user_device') + .join('user', 'user.user_id', 'user_device.user_id') + .where('user.isdeleted', '=', CONSTANTS.DELETED_DISABLE) + .andWhere('user_device.refresh_token', refresh_token) + .andWhere('user_device.device_uuid', device_uuid) + .select('user.user_id', 'user.role', 'user.name', 'user.email', 'user.role'); + resolve(user); + } catch (error) { + reject(error); + } + }), /** * @author Nguyễn Tiến Tài * @created_at 05/02/2023 * @description updateDevice */ - updateDevice: (data, user) => - new Promise((resolve, reject) => { - try { - const result = knex('user_device').where('user_id', user).update(data).returning(['device_uuid']); - resolve(result); - } catch (error) { - reject(error); - } - }), + updateDevice: (data, user) => new Promise((resolve, reject) => { + try { + const result = knex('user_device').where('user_id', user).update(data).returning(['device_uuid']); + resolve(result); + } catch (error) { + reject(error); + } + }), /** * @author Nguyễn Tiến Tài * @created_at 15/02/2023 diff --git a/backend-manager-student/src/share/models/user_reset_password.model.js b/backend-manager-student/src/share/models/user_reset_password.model.js index 98f7647..5e80afb 100644 --- a/backend-manager-student/src/share/models/user_reset_password.model.js +++ b/backend-manager-student/src/share/models/user_reset_password.model.js @@ -10,19 +10,18 @@ module.exports = { * @created_at 23/02/2023 * @description Insert reset password student */ - insertResetPassword: (data) => - new Promise((resolve, reject) => { - try { - const result = knex('reset_password') - .insert(data) - .onConflict('id') - .merge() - .returning(['id', 'user_id']); - resolve(result); - } catch (error) { - reject(error); - } - }), + insertResetPassword: (data) => new Promise((resolve, reject) => { + try { + const result = knex('reset_password') + .insert(data) + .onConflict('id') + .merge() + .returning(['id', 'user_id']); + resolve(result); + } catch (error) { + reject(error); + } + }), /** * @author Nguyễn Tiến Tài * @created_at 24/02/2023 @@ -37,31 +36,29 @@ module.exports = { * @created_at 24/02/2023 * @description Update reset Password */ - updateResetPassword: async (data, student_query, return_data) => - new Promise((resolve, reject) => { - try { - const result = knex('reset_password').update(data).where(student_query).returning(return_data); - resolve(result); - } catch (error) { - reject(error); - } - }), + updateResetPassword: async (data, student_query, return_data) => new Promise((resolve, reject) => { + try { + const result = knex('reset_password').update(data).where(student_query).returning(return_data); + resolve(result); + } catch (error) { + reject(error); + } + }), /** * @author Nguyễn Tiến Tài * @created_at 28/02/2023 * @description List all reset password */ - getResetPasswordList: async () => - new Promise((resolve, reject) => { - knex('reset_password') - .where('isdeleted', CONSTANTS.DELETED_DISABLE) - .orderBy('created_at', 'asc') - .then((result) => { - resolve(result); - }) - .catch((err) => { - reject(err); - }); - }), + getResetPasswordList: async () => new Promise((resolve, reject) => { + knex('reset_password') + .where('isdeleted', CONSTANTS.DELETED_DISABLE) + .orderBy('created_at', 'asc') + .then((result) => { + resolve(result); + }) + .catch((err) => { + reject(err); + }); + }), }; diff --git a/backend-manager-student/src/share/models/user_verification.model.js b/backend-manager-student/src/share/models/user_verification.model.js index aadf783..e59edc4 100644 --- a/backend-manager-student/src/share/models/user_verification.model.js +++ b/backend-manager-student/src/share/models/user_verification.model.js @@ -10,19 +10,18 @@ module.exports = { * @created_at 25/02/2023 * @description Insert reset verification student */ - insertVerificationEmail: (data) => - new Promise((resolve, reject) => { - try { - const result = knex('verification') - .insert(data) - .onConflict('verify_id') - .merge() - .returning(['verify_id']); - resolve(result); - } catch (error) { - reject(error); - } - }), + insertVerificationEmail: (data) => new Promise((resolve, reject) => { + try { + const result = knex('verification') + .insert(data) + .onConflict('verify_id') + .merge() + .returning(['verify_id']); + resolve(result); + } catch (error) { + reject(error); + } + }), /** * @author Nguyễn Tiến Tài @@ -38,32 +37,30 @@ module.exports = { * @created_at 26/02/2023 * @description Update verification */ - updateVerification: async (data, student_query, return_data) => - new Promise((resolve, reject) => { - try { - const result = knex('verification').update(data).where(student_query).returning(return_data); - resolve(result); - } catch (error) { - reject(error); - } - }), + updateVerification: async (data, student_query, return_data) => new Promise((resolve, reject) => { + try { + const result = knex('verification').update(data).where(student_query).returning(return_data); + resolve(result); + } catch (error) { + reject(error); + } + }), /** * @author Nguyễn Tiến Tài * @created_at 26/02/2023 * @description Update verification */ - getVerificationList: async () => - new Promise((resolve, reject) => { - knex('verification') - .where('isdeleted', CONSTANTS.DELETED_DISABLE) - .where('verified', CONSTANTS.DELETED_DISABLE) - .where('check_login', CONSTANTS.DELETED_DISABLE) - .orderBy('created_at', 'asc') - .then((result) => { - resolve(result); - }) - .catch((err) => { - reject(err); - }); - }), + getVerificationList: async () => new Promise((resolve, reject) => { + knex('verification') + .where('isdeleted', CONSTANTS.DELETED_DISABLE) + .where('verified', CONSTANTS.DELETED_DISABLE) + .where('check_login', CONSTANTS.DELETED_DISABLE) + .orderBy('created_at', 'asc') + .then((result) => { + resolve(result); + }) + .catch((err) => { + reject(err); + }); + }), }; diff --git a/backend-manager-student/src/share/services/user_service/book_service.js b/backend-manager-student/src/share/services/user_service/book_service.js new file mode 100644 index 0000000..e1c3493 --- /dev/null +++ b/backend-manager-student/src/share/services/user_service/book_service.js @@ -0,0 +1,18 @@ +module.exports = { + /** + * @author Nguyễn Tiến Tài + * @created_at 07/03/2023 + * @description handle Quantity + * @function Check Quantity book + */ + handleCheckQuantityBook: async (book) => { + try { + if (book.quantity > 0) { + return false + } + return true + } catch (error) { + return true + } + }, +}; diff --git a/backend-manager-student/src/share/utils/helper.js b/backend-manager-student/src/share/utils/helper.js index 0dfa175..c70a261 100644 --- a/backend-manager-student/src/share/utils/helper.js +++ b/backend-manager-student/src/share/utils/helper.js @@ -159,8 +159,7 @@ module.exports = { * @description Mobile network * @returns {String} */ - returnMobileNetWork: (code) => - CONSTANTS.mobileCodeProNewMap().get(code) || CONSTANTS.mobileCodeProNewMap().get('default'), + returnMobileNetWork: (code) => CONSTANTS.mobileCodeProNewMap().get(code) || CONSTANTS.mobileCodeProNewMap().get('default'), /** * @author Nguyễn Tiến Tài * @created_at 20/01/2023 diff --git a/backend-manager-student/src/share/utils/limited_redis.js b/backend-manager-student/src/share/utils/limited_redis.js index f1d7978..8c2072f 100644 --- a/backend-manager-student/src/share/utils/limited_redis.js +++ b/backend-manager-student/src/share/utils/limited_redis.js @@ -39,18 +39,17 @@ const getRangeCache = (key, start, end) => REDIS_MASTER.lrange(key, start, end); * @description Set multi * @returns {Array} */ -const setBlackListCache = (key, user_id, accept_token, refresh_token, ttl) => - REDIS_MASTER.multi() - .lpush(key, accept_token, refresh_token) - .del(user_id) - .expire(key, ttl) - .exec((err, replies) => { - if (err) { - console.error(err); - } else { - console.info(replies); - } - }); +const setBlackListCache = (key, user_id, accept_token, refresh_token, ttl) => REDIS_MASTER.multi() + .lpush(key, accept_token, refresh_token) + .del(user_id) + .expire(key, ttl) + .exec((err, replies) => { + if (err) { + console.error(err); + } else { + console.info(replies); + } + }); /** * @author Nguyễn Tiến Tài * @param {key} diff --git a/backend-manager-student/src/share/utils/redis_cluster_helper.js b/backend-manager-student/src/share/utils/redis_cluster_helper.js index fbb19a3..4e278f2 100644 --- a/backend-manager-student/src/share/utils/redis_cluster_helper.js +++ b/backend-manager-student/src/share/utils/redis_cluster_helper.js @@ -7,17 +7,16 @@ const { REDIS_MASTER, REDIS_SLAVE } = require('../db/init_multiple_redis'); * @param {REDIS_MASTER OR REDIS_SLAVE,String} * @return {resolve or reject} */ -const getDataWithTimeout = async (redisClient, key) => - await new Promise((resolve, reject) => { - redisClient.get(key, (error, result) => { - if (error) { - reject(error); - } else { - resolve(result); - } - }); - setTimeout(() => reject(new Error('Timeout')), 3000); +const getDataWithTimeout = async (redisClient, key) => await new Promise((resolve, reject) => { + redisClient.get(key, (error, result) => { + if (error) { + reject(error); + } else { + resolve(result); + } }); + setTimeout(() => reject(new Error('Timeout')), 3000); +}); /** * @author Nguyễn Tiến Tài diff --git a/backend-manager-student/src/user_api/v1/controllers/book.controllers/book.controller.js b/backend-manager-student/src/user_api/v1/controllers/book.controllers/book.controller.js index 5f4f234..b7ddc32 100644 --- a/backend-manager-student/src/user_api/v1/controllers/book.controllers/book.controller.js +++ b/backend-manager-student/src/user_api/v1/controllers/book.controllers/book.controller.js @@ -9,6 +9,9 @@ const { returnReasons } = require('../../../../share/middleware/handle_error'); //! MODEL const book_model = require('../../../../share/models/book.model'); +//! SERVICE +const book_service = require('../../../../share/services/user_service/book_service'); + const bookController = { /** * @author Nguyễn Tiến Tài @@ -116,5 +119,53 @@ const bookController = { }); } }, + /** + * @author Nguyễn Tiến Tài + * @created_at 07/03/2022 + * @description Borrowed Book + * @function borrowBook + * @return {Object:{Number,String} + */ + borrowBook: async (req, res) => { + // Book input + const { book_id } = req.body.input.borrow_book_input; + + // Take user Id + const { id } = req.auth_user; + + if (!book_id || !id) { + return res.status(400).json({ + status: 400, + message: returnReasons('400'), + }); + } + + // Check data bool exits + const data_book = await book_model.getBookById({ book_id, isdeleted: CONSTANTS.DELETED_DISABLE }, { quantity: 'quantity' }); + + if (!data_book.length) { + return res.status(400).json({ + status: 400, + message: returnReasons('400'), + }); + } + + // Check quantity book + const check_quantity_book = await book_service.handleCheckQuantityBook(data_book[0]); + if (check_quantity_book) { + return res.status(400).json({ + status: 400, + message: returnReasons('400'), + element: { + result: 'Book Out Of Stock', + }, + }); + } + return res.status(200).json({ + status: 200, + message: returnReasons('200'), + }); + }, + }; module.exports = bookController; diff --git a/backend-manager-student/src/user_api/v1/controllers/user.controllers/user.controller.js b/backend-manager-student/src/user_api/v1/controllers/user.controllers/user.controller.js index 530a7a4..e8d6664 100644 --- a/backend-manager-student/src/user_api/v1/controllers/user.controllers/user.controller.js +++ b/backend-manager-student/src/user_api/v1/controllers/user.controllers/user.controller.js @@ -179,8 +179,8 @@ const userController = { sameSite: CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT ? true : false, secure: CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT ? true : false, domain: - CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT - ? req.headers[CONSTANTS.HEADER_HEADER_FORWARDED_HOST]?.split(':')[0] + CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT ? + req.headers[CONSTANTS.HEADER_HEADER_FORWARDED_HOST]?.split(':')[0] : CONSTANTS.HEADER_DOMAIN, maxAge: CONSTANTS._1_MONTH, }); @@ -404,8 +404,8 @@ const userController = { sameSite: CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT ? true : false, secure: CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT ? true : false, domain: - CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT - ? req.headers[CONSTANTS.HEADER_HEADER_FORWARDED_HOST]?.split(':')[0] + CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT ? + req.headers[CONSTANTS.HEADER_HEADER_FORWARDED_HOST]?.split(':')[0] : CONSTANTS.HEADER_DOMAIN, maxAge: CONSTANTS._1_MONTH, }); diff --git a/backend-manager-student/src/user_api/v1/routes/books.routes/book.private.route.js b/backend-manager-student/src/user_api/v1/routes/books.routes/book.private.route.js index 38714b1..7f543e0 100644 --- a/backend-manager-student/src/user_api/v1/routes/books.routes/book.private.route.js +++ b/backend-manager-student/src/user_api/v1/routes/books.routes/book.private.route.js @@ -20,4 +20,12 @@ router.get('/detail/:author_id', bookController.getDetailBook); */ router.get('/all', bookController.getAllBook); +/** + * @author Nguyễn Tiến Tài + * @created_at 07/03/2023 + * @description Route borrow book + * @param {('POST')} [method='POST'] The request's method + */ +router.post('/borrow', bookController.borrowBook); + module.exports = router; diff --git a/backend-manager-student/src/user_api/v1/routes/index.router.js b/backend-manager-student/src/user_api/v1/routes/index.router.js index 567f4fd..be4c8d9 100644 --- a/backend-manager-student/src/user_api/v1/routes/index.router.js +++ b/backend-manager-student/src/user_api/v1/routes/index.router.js @@ -25,11 +25,10 @@ router.use('/api/v1/user/private', rate_limit_middleware, accessStudentMiddlewar */ router.use('/api/v1/user', rate_limit_middleware, router_general); -router.get('/check-status', (req, res) => - res.status(200).json({ - status: 'success', - message: 'OK', - }), +router.get('/check-status', (req, res) => res.status(200).json({ + status: 'success', + message: 'OK', +}), ); module.exports = router;