From 130c51ec71bab4bd018aa9c1093f4e60baf070d8 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, 14 Mar 2023 17:58:42 +0700 Subject: [PATCH 1/2] #180 hotfix book tai dev 20230314 --- .../admin.controllers/admin.controller.js | 16 +-- .../author.controllers/author.controller.js | 8 +- .../book.controllers/book.controller.js | 39 ++--- .../borrow_book.controller.js | 13 +- .../src/share/configs/constants.js | 8 ++ .../src/share/middleware/handle_error.js | 3 +- .../src/share/models/author.model.js | 35 +++-- .../src/share/models/book.model.js | 35 +++-- .../src/share/models/book_borrowed.model.js | 134 +++++++++--------- .../src/share/models/categories.model.js | 42 +++--- .../src/share/models/phone.model.js | 35 +++-- .../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 +++++---- .../share/patterns/LRU_Strategy.patterns.js | 19 ++- .../services/admin_service/book_service.js | 73 ++++++++++ .../src/share/utils/helper.js | 3 +- .../src/share/utils/limited_redis.js | 23 ++- .../src/share/utils/random.js | 16 +++ .../src/share/utils/redis_cluster_helper.js | 19 ++- .../book.controllers/book.controller.js | 4 +- .../borrow_book.controller.js | 24 ++-- .../user.controllers/user.controller.js | 12 +- .../src/user_api/v1/routes/index.router.js | 9 +- 25 files changed, 461 insertions(+), 354 deletions(-) create mode 100644 backend-manager-student/src/share/services/admin_service/book_service.js diff --git a/backend-manager-student/src/admin_api/v1/controllers/admin.controllers/admin.controller.js b/backend-manager-student/src/admin_api/v1/controllers/admin.controllers/admin.controller.js index c7d56a3..f01cb2a 100644 --- a/backend-manager-student/src/admin_api/v1/controllers/admin.controllers/admin.controller.js +++ b/backend-manager-student/src/admin_api/v1/controllers/admin.controllers/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.controllers/author.controller.js b/backend-manager-student/src/admin_api/v1/controllers/author.controllers/author.controller.js index fab4069..e301e16 100644 --- a/backend-manager-student/src/admin_api/v1/controllers/author.controllers/author.controller.js +++ b/backend-manager-student/src/admin_api/v1/controllers/author.controllers/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 (!author_id) { 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 0a59e39..d3ce725 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 @@ -2,6 +2,7 @@ const HELPER = require('../../../../share/utils/helper'); const RANDOMS = require('../../../../share/utils/random'); const CONSTANTS = require('../../../../share/configs/constants'); +const MEMORY_CACHE = require('../../../../share/utils/limited_redis'); //! MIDDLEWARE const { globalCache } = require('../../../../share/patterns/LRU_Strategy.patterns'); @@ -10,6 +11,9 @@ const { returnReasons } = require('../../../../share/middleware/handle_error'); //! MODEL const book_model = require('../../../../share/models/book.model'); +//! SERVICE +const book_admin_service = require('../../../../share/services/admin_service/book_service'); + const bookController = { /** * @author Nguyễn Tiến Tài @@ -19,8 +23,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 ( @@ -60,8 +66,8 @@ const bookController = { let result; [err, result] = await HELPER.handleRequest(book_model.createBook(data_insert)); if (result) { - // Delete data cache lru argothim - globalCache.delCache(CONSTANTS.KEY_REDIS.ALL_BOOK); + // Del key Redis + MEMORY_CACHE.delKeyCache(CONSTANTS.KEY_REDIS.ALL_BOOK); return res.status(200).json({ status: 200, @@ -157,8 +163,8 @@ const bookController = { book_id, }); - // Delete data cache lru argothim - globalCache.delMultiCache(CONSTANTS.KEY_REDIS.ALL_BOOK, key_cache_book_detail); + // Delete Cache + book_admin_service.handleDeleteCache(key_cache_book_detail, CONSTANTS.KEY_REDIS.ALL_BOOK); return res.status(200).json({ status: 200, @@ -236,8 +242,8 @@ const bookController = { book_id, }); - // Delete data cache lru argothim - globalCache.delMultiCache(CONSTANTS.KEY_REDIS.ALL_BOOK, key_cache_book_detail); + // Delete Cache + book_admin_service.handleDeleteCache(key_cache_book_detail, CONSTANTS.KEY_REDIS.ALL_BOOK); return res.status(200).json({ status: 200, @@ -288,7 +294,7 @@ const bookController = { // detail book database const cache_lru_book = globalCache.getCache(key_cache_book_detail); - if (cache_lru_book !== -1) { + if (cache_lru_book !== CONSTANTS.NO) { return res.status(200).json({ status: 200, message: returnReasons('200'), @@ -304,7 +310,7 @@ const bookController = { ); if (result_book_detail) { // Add data cache lru argothim - globalCache.putCache(key_cache_book_detail, result_book_detail[0]); + book_admin_service.handleSetCacheLRU(key_cache_book_detail, result_book_detail[0]); return res.status(200).json({ status: 200, @@ -333,22 +339,23 @@ const bookController = { */ getAllBook: async (req, res) => { try { - // detail book database - const cache_lru_book = globalCache.getCache(CONSTANTS.KEY_REDIS.ALL_BOOK); - if (cache_lru_book !== -1) { + // Detail book database + const cache_redis_book = await MEMORY_CACHE.getCache(CONSTANTS.KEY_REDIS.ALL_BOOK); + if (cache_redis_book) { return res.status(200).json({ status: 200, message: returnReasons('200'), element: { - result: cache_lru_book, + result: JSON.parse(cache_redis_book), }, }); } // Take data db const result_book_detail = await book_model.getAllBook({ isdeleted: CONSTANTS.DELETED_DISABLE }, '*'); if (result_book_detail) { - // Add data cache lru argothim - globalCache.putCache(CONSTANTS.KEY_REDIS.ALL_BOOK, result_book_detail); + const redisTTLWithRandom = RANDOMS.getRedisTTLWithRandom(CONSTANTS._1_MONTH); + // Add data redis + book_admin_service.handleSetCacheRedis(CONSTANTS.KEY_REDIS.ALL_BOOK, result_book_detail, redisTTLWithRandom); return res.status(200).json({ status: 200, diff --git a/backend-manager-student/src/admin_api/v1/controllers/borrow_book.controllers/borrow_book.controller.js b/backend-manager-student/src/admin_api/v1/controllers/borrow_book.controllers/borrow_book.controller.js index 676b668..10b9597 100644 --- a/backend-manager-student/src/admin_api/v1/controllers/borrow_book.controllers/borrow_book.controller.js +++ b/backend-manager-student/src/admin_api/v1/controllers/borrow_book.controllers/borrow_book.controller.js @@ -3,13 +3,15 @@ const HELPER = require('../../../../share/utils/helper'); const CONSTANTS = require('../../../../share/configs/constants'); //! MIDDLEWARE -const { globalCache } = require('../../../../share/patterns/LRU_Strategy.patterns'); const { returnReasons } = require('../../../../share/middleware/handle_error'); //! MODEL const borrow_book_model = require('../../../../share/models/book_borrowed.model'); const book_model = require('../../../../share/models/book.model'); +//! SERVICE +const book_admin_service = require('../../../../share/services/admin_service/book_service'); + const BorrowBookController = { /** * @author Nguyễn Tiến Tài @@ -19,7 +21,9 @@ const BorrowBookController = { * @return {Object:{Number,String} */ updateBorrowBook: async (req, res) => { - const { book_id, user_id, start_date, due_date, status } = req.body.input.borrow_book_input; + const { + book_id, user_id, start_date, due_date, status, + } = req.body.input.borrow_book_input; // Check input if (!book_id || !user_id || !start_date || !due_date || !status) { @@ -109,8 +113,9 @@ const BorrowBookController = { const key_cache_book_detail = HELPER.getURIFromTemplate(CONSTANTS.KEY_REDIS.DETAIL_BOOK, { book_id, }); - // Delete data cache lru argothim - globalCache.delMultiCache(CONSTANTS.KEY_REDIS.ALL_BOOK, key_cache_book_detail); + + // Delete Cache + book_admin_service.handleDeleteCache(key_cache_book_detail, CONSTANTS.KEY_REDIS.ALL_BOOK); return res.status(200).json({ status: 200, diff --git a/backend-manager-student/src/share/configs/constants.js b/backend-manager-student/src/share/configs/constants.js index e3978f6..d4457a2 100644 --- a/backend-manager-student/src/share/configs/constants.js +++ b/backend-manager-student/src/share/configs/constants.js @@ -349,4 +349,12 @@ module.exports = { ALL_BOOK: 'book_all', DETAIL_BOOK: 'detail_book_${book_id}', }, + /** + * @author Nguyễn Tiến Tài + * @created_at 14/03/2023 + * @description True and False + * @param { String } + */ + YES: 1, + NO: 0, }; 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/book_borrowed.model.js b/backend-manager-student/src/share/models/book_borrowed.model.js index ceb06e7..6c54ad7 100644 --- a/backend-manager-student/src/share/models/book_borrowed.model.js +++ b/backend-manager-student/src/share/models/book_borrowed.model.js @@ -10,19 +10,18 @@ module.exports = { * @created_at 07/03/2023 * @description create BorrowBook */ - createBorrowBook: (data) => - new Promise((resolve, reject) => { - try { - const result = knex('borrowed_book') - .insert(data) - .onConflict('borrowed_book_id') - .merge() - .returning(['borrowed_book_id']); - resolve(result); - } catch (error) { - reject(error); - } - }), + createBorrowBook: (data) => new Promise((resolve, reject) => { + try { + const result = knex('borrowed_book') + .insert(data) + .onConflict('borrowed_book_id') + .merge() + .returning(['borrowed_book_id']); + resolve(result); + } catch (error) { + reject(error); + } + }), /** * @author Nguyễn Tiến Tài @@ -38,15 +37,14 @@ module.exports = { * @created_at 07/03/2023 * @description Update BorrowBook */ - updateBorrowBook: async (data, student_query, return_data) => - new Promise((resolve, reject) => { - try { - const result = knex('borrowed_book').update(data).where(student_query).returning(return_data); - resolve(result); - } catch (error) { - reject(error); - } - }), + updateBorrowBook: async (data, student_query, return_data) => new Promise((resolve, reject) => { + try { + const result = knex('borrowed_book').update(data).where(student_query).returning(return_data); + resolve(result); + } catch (error) { + reject(error); + } + }), /** * @author Nguyễn Tiến Tài * @created_at 07/03/2023 @@ -79,62 +77,60 @@ module.exports = { * @created_at 07/03/2023 * @description Transaction Update Borrow Book */ - transactionUpdateBorrowBook: async (data_update_book, data_update_borrow) => - new Promise(async (resolve, reject) => { - // start transaction - const trx = await knex.transaction(); - try { - // Query 1: createBorrowBook - const borrowBookId = await trx('borrowed_book') - .update({ status: data_update_borrow.status }) - .where({ book_id: data_update_borrow.book_id, user_id: data_update_borrow.user_id }) - .returning(['borrowed_book_id']); + transactionUpdateBorrowBook: async (data_update_book, data_update_borrow) => new Promise(async (resolve, reject) => { + // start transaction + const trx = await knex.transaction(); + try { + // Query 1: createBorrowBook + const borrowBookId = await trx('borrowed_book') + .update({ status: data_update_borrow.status }) + .where({ book_id: data_update_borrow.book_id, user_id: data_update_borrow.user_id }) + .returning(['borrowed_book_id']); - // Query 2: updateBorrowBook - const updatedData = await trx('books') - .update({ quantity: data_update_book.quantity }) - .where({ book_id: data_update_book.book_id }) - .returning(['book_id']); + // Query 2: updateBorrowBook + const updatedData = await trx('books') + .update({ quantity: data_update_book.quantity }) + .where({ book_id: data_update_book.book_id }) + .returning(['book_id']); - // Commit transaction - await trx.commit(); - return resolve(borrowBookId, updatedData); - } catch (error) { - trx.rollback(); - reject(error); - } - }), + // Commit transaction + await trx.commit(); + return resolve(borrowBookId, updatedData); + } catch (error) { + trx.rollback(); + reject(error); + } + }), /** * @author Nguyễn Tiến Tài * @created_at 08/03/2023 * @description Transaction Borrow Book */ - transactionBorrowBook: async (data_insert, data_update) => - new Promise(async (resolve, reject) => { - // start transaction - const trx = await knex.transaction(); - try { - // Query 1: createBorrowBook - const borrowBookId = await trx('borrowed_book') - .insert(data_insert) - .onConflict('borrowed_book_id') - .merge() - .returning(['borrowed_book_id']); + transactionBorrowBook: async (data_insert, data_update) => new Promise(async (resolve, reject) => { + // start transaction + const trx = await knex.transaction(); + try { + // Query 1: createBorrowBook + const borrowBookId = await trx('borrowed_book') + .insert(data_insert) + .onConflict('borrowed_book_id') + .merge() + .returning(['borrowed_book_id']); - // Query 2: updateBorrowBook - const updatedData = await trx('books') - .update(data_update) - .where({ book_id: data_insert.book_id }) - .returning(['book_id']); + // Query 2: updateBorrowBook + const updatedData = await trx('books') + .update(data_update) + .where({ book_id: data_insert.book_id }) + .returning(['book_id']); - // Commit transaction - await trx.commit(); - return resolve(borrowBookId, updatedData); - } catch (error) { - trx.rollback(); - reject(error); - } - }), + // Commit transaction + await trx.commit(); + return resolve(borrowBookId, updatedData); + } catch (error) { + trx.rollback(); + reject(error); + } + }), /** * @author Nguyễn Tiến Tài * @created_at 09/03/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/phone.model.js b/backend-manager-student/src/share/models/phone.model.js index c46c101..786d3e2 100644 --- a/backend-manager-student/src/share/models/phone.model.js +++ b/backend-manager-student/src/share/models/phone.model.js @@ -7,15 +7,15 @@ module.exports = { * @created_at 09/03/2023 * @description create Phone */ - createPhone: (data) => - new Promise((resolve, reject) => { - try { - const result = knex('phone').insert(data).onConflict('phone_id').merge().returning(['phone_id']); - resolve(result); - } catch (error) { - reject(error); - } - }), + createPhone: (data) => new Promise((resolve, reject) => { + try { + const result = knex('phone').insert(data).onConflict('phone_id').merge() + .returning(['phone_id']); + resolve(result); + } catch (error) { + reject(error); + } + }), /** * @Phone Nguyễn Tiến Tài @@ -31,13 +31,12 @@ module.exports = { * @created_at 09/03/2023 * @description Update Phone */ - updatePhone: async (data, student_query, return_data) => - new Promise((resolve, reject) => { - try { - const result = knex('phone').update(data).where(student_query).returning(return_data); - resolve(result); - } catch (error) { - reject(error); - } - }), + updatePhone: async (data, student_query, return_data) => new Promise((resolve, reject) => { + try { + const result = knex('phone').update(data).where(student_query).returning(return_data); + 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 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/patterns/LRU_Strategy.patterns.js b/backend-manager-student/src/share/patterns/LRU_Strategy.patterns.js index e98234c..9b18716 100644 --- a/backend-manager-student/src/share/patterns/LRU_Strategy.patterns.js +++ b/backend-manager-student/src/share/patterns/LRU_Strategy.patterns.js @@ -1,8 +1,13 @@ +//! LIBRARY const LRU = require('lru-cache'); + +//! SHARE const CONFIGS = require('../configs/config'); +const CONSTANTS = require('../configs/constants'); /** * @author Nguyễn Tiến Tài * @created_at 22/12/2022 + * @updaed_at 14/03/2023 * @description File Algorithm Cache LRU */ class LRUCache { @@ -26,7 +31,7 @@ class LRUCache { const val = this._cache.get(key); return val; } - return -1; + return CONSTANTS.NO; } catch (error) { throw new Error(`Error getting key ${key} from cache: ${error}`); } @@ -41,6 +46,18 @@ class LRUCache { } } + exitKeyCache(key) { + try { + const hasKey = this._cache.has(key); + if (hasKey) { + return CONSTANTS.YES; + } + return CONSTANTS.NO; + } catch (error) { + throw new Error(`Error check key ${key} from cache: ${error}`); + } + } + delMultiCache(key_1, key_2) { try { this._cache.delete(key_1) && this._cache.delete(key_2); diff --git a/backend-manager-student/src/share/services/admin_service/book_service.js b/backend-manager-student/src/share/services/admin_service/book_service.js new file mode 100644 index 0000000..6832f59 --- /dev/null +++ b/backend-manager-student/src/share/services/admin_service/book_service.js @@ -0,0 +1,73 @@ +//! SHARE +const MEMORY_CACHE = require('../../utils/limited_redis'); +const CONSTANTS = require('../../configs/constants'); + +//! MIDDLEWARE +const { globalCache } = require('../../patterns/LRU_Strategy.patterns'); + +module.exports = { + /** + * @author Nguyễn Tiến Tài + * @created_at 14/03/2023 + * @description Delete Cache + * @function handleDeleteCache + */ + handleDeleteCache: async (key_cache_lru, key_cache_redis) => { + try { + // Check Key lru argothim + const check_key_lru_book_detail = globalCache.exitKeyCache(key_cache_lru); + if (check_key_lru_book_detail === CONSTANTS.YES) { + // Delete data cache lru argothim + globalCache.delCache(key_cache_lru); + } + + // Check key Redis + const check_key_redis_book_all = await MEMORY_CACHE.existsKeyCache(key_cache_redis); + if (check_key_redis_book_all === CONSTANTS.YES) { + // Delete key Redis + MEMORY_CACHE.delKeyCache(key_cache_redis); + } + return false; + } catch (error) { + return true; + } + }, + /** + * @author Nguyễn Tiến Tài + * @created_at 14/03/2023 + * @description Save Cache Redis + * @function handleSetCacheRedis + */ + handleSetCacheRedis: async (key_cache_redis, value, ttl) => { + try { + // Check key Redis + const check_key_redis_book_all = await MEMORY_CACHE.existsKeyCache(key_cache_redis); + if (check_key_redis_book_all === CONSTANTS.NO) { + // Set data Redis + MEMORY_CACHE.setCacheEx(key_cache_redis, JSON.stringify(value), ttl); + } + return false; + } catch (error) { + return true; + } + }, + /** + * @author Nguyễn Tiến Tài + * @created_at 14/03/2023 + * @description Save Cache LRU + * @function handleSetCacheRedis + */ + handleSetCacheLRU: async (key_cache_lru, value) => { + try { + // Check Key lru argothim + const check_key_lru_book_detail = globalCache.exitKeyCache(key_cache_lru); + if (check_key_lru_book_detail === CONSTANTS.NO) { + // Add data cache lru argothim + globalCache.putCache(key_cache_lru, value); + } + return false; + } 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/random.js b/backend-manager-student/src/share/utils/random.js index ef817f4..93a3b9f 100644 --- a/backend-manager-student/src/share/utils/random.js +++ b/backend-manager-student/src/share/utils/random.js @@ -61,4 +61,20 @@ module.exports = { generatorOtp() { return OtpGenerator.generate(CONFIGS.RANDOM_NUMBER_OTP, OPTION_OTP); }, + /** + * @author Nguyễn Tiến Tài + * @param {null} + * @created_at 14/03/2023 + * @description Random time ttl redis + * @returns {Number} + */ + getRedisTTLWithRandom(initialTTL) { + // Số random được thêm vào (tính bằng mili giây) + const randomTime = Math.floor(Math.random() * CONSTANTS._1_HOURS_S_REDIS); // Tối đa 1 giờ + + // Tổng thời gian TTL + const ttl = initialTTL + randomTime; + + return ttl; + }, }; 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 89184a4..def02cd 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 @@ -35,7 +35,7 @@ const bookController = { // detail book database const cache_lru_book = globalCache.getCache(key_cache_book_detail); - if (cache_lru_book !== -1) { + if (cache_lru_book !== CONSTANTS.NO) { return res.status(200).json({ status: 200, message: returnReasons('200'), @@ -83,7 +83,7 @@ const bookController = { try { // detail book database const cache_lru_book = globalCache.getCache(CONSTANTS.KEY_REDIS.ALL_BOOK); - if (cache_lru_book !== -1) { + if (cache_lru_book !== CONSTANTS.NO) { return res.status(200).json({ status: 200, message: returnReasons('200'), diff --git a/backend-manager-student/src/user_api/v1/controllers/borrow_book.controllers/borrow_book.controller.js b/backend-manager-student/src/user_api/v1/controllers/borrow_book.controllers/borrow_book.controller.js index 0d37d24..9779768 100644 --- a/backend-manager-student/src/user_api/v1/controllers/borrow_book.controllers/borrow_book.controller.js +++ b/backend-manager-student/src/user_api/v1/controllers/borrow_book.controllers/borrow_book.controller.js @@ -4,7 +4,6 @@ const CONSTANTS = require('../../../../share/configs/constants'); const RANDOMS = require('../../../../share/utils/random'); //! MIDDLEWARE -const { globalCache } = require('../../../../share/patterns/LRU_Strategy.patterns'); const { returnReasons } = require('../../../../share/middleware/handle_error'); //! MODEL @@ -13,6 +12,7 @@ const borrowed_book_model = require('../../../../share/models/book_borrowed.mode //! SERVICE const book_service = require('../../../../share/services/user_service/book_service'); +const book_admin_service = require('../../../../share/services/admin_service/book_service'); const BorrowBookController = { /** @@ -58,15 +58,15 @@ const BorrowBookController = { if (data_borrow_book.length > 0 && data_borrow_book[0].status !== CONSTANTS.STATUS_BORROW.DONE) { let result_borrow; switch (data_borrow_book[0].status) { - case CONSTANTS.STATUS_BORROW.PENDING: - result_borrow = 'Book already borrow !!'; - break; - case CONSTANTS.STATUS_BORROW.BORROWING: - result_borrow = 'Please return the book !!'; - break; - default: - result_borrow = 'Fail'; - break; + case CONSTANTS.STATUS_BORROW.PENDING: + result_borrow = 'Book already borrow !!'; + break; + case CONSTANTS.STATUS_BORROW.BORROWING: + result_borrow = 'Please return the book !!'; + break; + default: + result_borrow = 'Fail'; + break; } return res.status(400).json({ status: 400, @@ -128,8 +128,8 @@ const BorrowBookController = { book_id, }); - // Delete data cache lru argothim - globalCache.delMultiCache(CONSTANTS.KEY_REDIS.ALL_BOOK, key_cache_book_detail); + // Delete Cache + book_admin_service.handleDeleteCache(key_cache_book_detail, CONSTANTS.KEY_REDIS.ALL_BOOK); return res.status(200).json({ status: 200, 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 1c6f993..6e78c7a 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 @@ -180,8 +180,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, }); @@ -420,8 +420,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, }); @@ -1477,7 +1477,9 @@ const userController = { const { id } = req.auth_user; // Input body - const { name, avatar_uri, public_id_avatar, address, dob, gender } = req.body.input.user_update_profile_input; + const { + name, avatar_uri, public_id_avatar, address, dob, gender, + } = req.body.input.user_update_profile_input; // Check user_id if (!id) { 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; From 5135a2edc7570208a18339d50e74af09fcd05349 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, 14 Mar 2023 18:00:13 +0700 Subject: [PATCH 2/2] #180 hotfix book tai dev 20230314 --- .../admin.controllers/admin.controller.js | 16 +-- .../author.controllers/author.controller.js | 8 +- .../book.controllers/book.controller.js | 12 +- .../borrow_book.controller.js | 4 +- .../src/share/middleware/handle_error.js | 3 +- .../src/share/models/author.model.js | 35 ++--- .../src/share/models/book.model.js | 35 ++--- .../src/share/models/book_borrowed.model.js | 134 +++++++++--------- .../src/share/models/categories.model.js | 42 +++--- .../src/share/models/phone.model.js | 35 ++--- .../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 ++++----- .../src/share/utils/helper.js | 3 +- .../src/share/utils/limited_redis.js | 23 +-- .../src/share/utils/redis_cluster_helper.js | 19 +-- .../borrow_book.controller.js | 18 +-- .../user.controllers/user.controller.js | 12 +- .../src/user_api/v1/routes/index.router.js | 9 +- 20 files changed, 336 insertions(+), 317 deletions(-) diff --git a/backend-manager-student/src/admin_api/v1/controllers/admin.controllers/admin.controller.js b/backend-manager-student/src/admin_api/v1/controllers/admin.controllers/admin.controller.js index f01cb2a..c7d56a3 100644 --- a/backend-manager-student/src/admin_api/v1/controllers/admin.controllers/admin.controller.js +++ b/backend-manager-student/src/admin_api/v1/controllers/admin.controllers/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.controllers/author.controller.js b/backend-manager-student/src/admin_api/v1/controllers/author.controllers/author.controller.js index e301e16..fab4069 100644 --- a/backend-manager-student/src/admin_api/v1/controllers/author.controllers/author.controller.js +++ b/backend-manager-student/src/admin_api/v1/controllers/author.controllers/author.controller.js @@ -17,9 +17,7 @@ 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) { @@ -74,9 +72,7 @@ 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 (!author_id) { 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 d3ce725..e8779a3 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 @@ -23,10 +23,8 @@ 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 ( @@ -355,7 +353,11 @@ const bookController = { if (result_book_detail) { const redisTTLWithRandom = RANDOMS.getRedisTTLWithRandom(CONSTANTS._1_MONTH); // Add data redis - book_admin_service.handleSetCacheRedis(CONSTANTS.KEY_REDIS.ALL_BOOK, result_book_detail, redisTTLWithRandom); + book_admin_service.handleSetCacheRedis( + CONSTANTS.KEY_REDIS.ALL_BOOK, + result_book_detail, + redisTTLWithRandom, + ); return res.status(200).json({ status: 200, diff --git a/backend-manager-student/src/admin_api/v1/controllers/borrow_book.controllers/borrow_book.controller.js b/backend-manager-student/src/admin_api/v1/controllers/borrow_book.controllers/borrow_book.controller.js index 10b9597..b200150 100644 --- a/backend-manager-student/src/admin_api/v1/controllers/borrow_book.controllers/borrow_book.controller.js +++ b/backend-manager-student/src/admin_api/v1/controllers/borrow_book.controllers/borrow_book.controller.js @@ -21,9 +21,7 @@ const BorrowBookController = { * @return {Object:{Number,String} */ updateBorrowBook: async (req, res) => { - const { - book_id, user_id, start_date, due_date, status, - } = req.body.input.borrow_book_input; + const { book_id, user_id, start_date, due_date, status } = req.body.input.borrow_book_input; // Check input if (!book_id || !user_id || !start_date || !due_date || !status) { diff --git a/backend-manager-student/src/share/middleware/handle_error.js b/backend-manager-student/src/share/middleware/handle_error.js index 13e03e3..b2ff6c3 100644 --- a/backend-manager-student/src/share/middleware/handle_error.js +++ b/backend-manager-student/src/share/middleware/handle_error.js @@ -9,5 +9,6 @@ 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 eae7424..776a75c 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,14 +31,15 @@ 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 a404835..776e383 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,14 +31,15 @@ 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/book_borrowed.model.js b/backend-manager-student/src/share/models/book_borrowed.model.js index 6c54ad7..ceb06e7 100644 --- a/backend-manager-student/src/share/models/book_borrowed.model.js +++ b/backend-manager-student/src/share/models/book_borrowed.model.js @@ -10,18 +10,19 @@ module.exports = { * @created_at 07/03/2023 * @description create BorrowBook */ - createBorrowBook: (data) => new Promise((resolve, reject) => { - try { - const result = knex('borrowed_book') - .insert(data) - .onConflict('borrowed_book_id') - .merge() - .returning(['borrowed_book_id']); - resolve(result); - } catch (error) { - reject(error); - } - }), + createBorrowBook: (data) => + new Promise((resolve, reject) => { + try { + const result = knex('borrowed_book') + .insert(data) + .onConflict('borrowed_book_id') + .merge() + .returning(['borrowed_book_id']); + resolve(result); + } catch (error) { + reject(error); + } + }), /** * @author Nguyễn Tiến Tài @@ -37,14 +38,15 @@ module.exports = { * @created_at 07/03/2023 * @description Update BorrowBook */ - updateBorrowBook: async (data, student_query, return_data) => new Promise((resolve, reject) => { - try { - const result = knex('borrowed_book').update(data).where(student_query).returning(return_data); - resolve(result); - } catch (error) { - reject(error); - } - }), + updateBorrowBook: async (data, student_query, return_data) => + new Promise((resolve, reject) => { + try { + const result = knex('borrowed_book').update(data).where(student_query).returning(return_data); + resolve(result); + } catch (error) { + reject(error); + } + }), /** * @author Nguyễn Tiến Tài * @created_at 07/03/2023 @@ -77,60 +79,62 @@ module.exports = { * @created_at 07/03/2023 * @description Transaction Update Borrow Book */ - transactionUpdateBorrowBook: async (data_update_book, data_update_borrow) => new Promise(async (resolve, reject) => { - // start transaction - const trx = await knex.transaction(); - try { - // Query 1: createBorrowBook - const borrowBookId = await trx('borrowed_book') - .update({ status: data_update_borrow.status }) - .where({ book_id: data_update_borrow.book_id, user_id: data_update_borrow.user_id }) - .returning(['borrowed_book_id']); + transactionUpdateBorrowBook: async (data_update_book, data_update_borrow) => + new Promise(async (resolve, reject) => { + // start transaction + const trx = await knex.transaction(); + try { + // Query 1: createBorrowBook + const borrowBookId = await trx('borrowed_book') + .update({ status: data_update_borrow.status }) + .where({ book_id: data_update_borrow.book_id, user_id: data_update_borrow.user_id }) + .returning(['borrowed_book_id']); - // Query 2: updateBorrowBook - const updatedData = await trx('books') - .update({ quantity: data_update_book.quantity }) - .where({ book_id: data_update_book.book_id }) - .returning(['book_id']); + // Query 2: updateBorrowBook + const updatedData = await trx('books') + .update({ quantity: data_update_book.quantity }) + .where({ book_id: data_update_book.book_id }) + .returning(['book_id']); - // Commit transaction - await trx.commit(); - return resolve(borrowBookId, updatedData); - } catch (error) { - trx.rollback(); - reject(error); - } - }), + // Commit transaction + await trx.commit(); + return resolve(borrowBookId, updatedData); + } catch (error) { + trx.rollback(); + reject(error); + } + }), /** * @author Nguyễn Tiến Tài * @created_at 08/03/2023 * @description Transaction Borrow Book */ - transactionBorrowBook: async (data_insert, data_update) => new Promise(async (resolve, reject) => { - // start transaction - const trx = await knex.transaction(); - try { - // Query 1: createBorrowBook - const borrowBookId = await trx('borrowed_book') - .insert(data_insert) - .onConflict('borrowed_book_id') - .merge() - .returning(['borrowed_book_id']); + transactionBorrowBook: async (data_insert, data_update) => + new Promise(async (resolve, reject) => { + // start transaction + const trx = await knex.transaction(); + try { + // Query 1: createBorrowBook + const borrowBookId = await trx('borrowed_book') + .insert(data_insert) + .onConflict('borrowed_book_id') + .merge() + .returning(['borrowed_book_id']); - // Query 2: updateBorrowBook - const updatedData = await trx('books') - .update(data_update) - .where({ book_id: data_insert.book_id }) - .returning(['book_id']); + // Query 2: updateBorrowBook + const updatedData = await trx('books') + .update(data_update) + .where({ book_id: data_insert.book_id }) + .returning(['book_id']); - // Commit transaction - await trx.commit(); - return resolve(borrowBookId, updatedData); - } catch (error) { - trx.rollback(); - reject(error); - } - }), + // Commit transaction + await trx.commit(); + return resolve(borrowBookId, updatedData); + } catch (error) { + trx.rollback(); + reject(error); + } + }), /** * @author Nguyễn Tiến Tài * @created_at 09/03/2023 diff --git a/backend-manager-student/src/share/models/categories.model.js b/backend-manager-student/src/share/models/categories.model.js index 29ce4ac..8a1ceb8 100644 --- a/backend-manager-student/src/share/models/categories.model.js +++ b/backend-manager-student/src/share/models/categories.model.js @@ -7,18 +7,19 @@ 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 @@ -34,14 +35,15 @@ 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/phone.model.js b/backend-manager-student/src/share/models/phone.model.js index 786d3e2..c46c101 100644 --- a/backend-manager-student/src/share/models/phone.model.js +++ b/backend-manager-student/src/share/models/phone.model.js @@ -7,15 +7,15 @@ module.exports = { * @created_at 09/03/2023 * @description create Phone */ - createPhone: (data) => new Promise((resolve, reject) => { - try { - const result = knex('phone').insert(data).onConflict('phone_id').merge() - .returning(['phone_id']); - resolve(result); - } catch (error) { - reject(error); - } - }), + createPhone: (data) => + new Promise((resolve, reject) => { + try { + const result = knex('phone').insert(data).onConflict('phone_id').merge().returning(['phone_id']); + resolve(result); + } catch (error) { + reject(error); + } + }), /** * @Phone Nguyễn Tiến Tài @@ -31,12 +31,13 @@ module.exports = { * @created_at 09/03/2023 * @description Update Phone */ - updatePhone: async (data, student_query, return_data) => new Promise((resolve, reject) => { - try { - const result = knex('phone').update(data).where(student_query).returning(return_data); - resolve(result); - } catch (error) { - reject(error); - } - }), + updatePhone: async (data, student_query, return_data) => + new Promise((resolve, reject) => { + try { + const result = knex('phone').update(data).where(student_query).returning(return_data); + 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 db27053..8217b5e 100644 --- a/backend-manager-student/src/share/models/user.model.js +++ b/backend-manager-student/src/share/models/user.model.js @@ -11,18 +11,19 @@ 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 @@ -38,14 +39,15 @@ 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 8b304b0..1323935 100644 --- a/backend-manager-student/src/share/models/user_device.model.js +++ b/backend-manager-student/src/share/models/user_device.model.js @@ -10,49 +10,52 @@ 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 5e80afb..98f7647 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,18 +10,19 @@ 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 @@ -36,29 +37,31 @@ 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 e59edc4..aadf783 100644 --- a/backend-manager-student/src/share/models/user_verification.model.js +++ b/backend-manager-student/src/share/models/user_verification.model.js @@ -10,18 +10,19 @@ 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 @@ -37,30 +38,32 @@ 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/utils/helper.js b/backend-manager-student/src/share/utils/helper.js index c70a261..0dfa175 100644 --- a/backend-manager-student/src/share/utils/helper.js +++ b/backend-manager-student/src/share/utils/helper.js @@ -159,7 +159,8 @@ 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 8c2072f..f1d7978 100644 --- a/backend-manager-student/src/share/utils/limited_redis.js +++ b/backend-manager-student/src/share/utils/limited_redis.js @@ -39,17 +39,18 @@ 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 4e278f2..fbb19a3 100644 --- a/backend-manager-student/src/share/utils/redis_cluster_helper.js +++ b/backend-manager-student/src/share/utils/redis_cluster_helper.js @@ -7,16 +7,17 @@ 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); - } +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); }); - setTimeout(() => reject(new Error('Timeout')), 3000); -}); /** * @author Nguyễn Tiến Tài diff --git a/backend-manager-student/src/user_api/v1/controllers/borrow_book.controllers/borrow_book.controller.js b/backend-manager-student/src/user_api/v1/controllers/borrow_book.controllers/borrow_book.controller.js index 9779768..fd02ff5 100644 --- a/backend-manager-student/src/user_api/v1/controllers/borrow_book.controllers/borrow_book.controller.js +++ b/backend-manager-student/src/user_api/v1/controllers/borrow_book.controllers/borrow_book.controller.js @@ -58,15 +58,15 @@ const BorrowBookController = { if (data_borrow_book.length > 0 && data_borrow_book[0].status !== CONSTANTS.STATUS_BORROW.DONE) { let result_borrow; switch (data_borrow_book[0].status) { - case CONSTANTS.STATUS_BORROW.PENDING: - result_borrow = 'Book already borrow !!'; - break; - case CONSTANTS.STATUS_BORROW.BORROWING: - result_borrow = 'Please return the book !!'; - break; - default: - result_borrow = 'Fail'; - break; + case CONSTANTS.STATUS_BORROW.PENDING: + result_borrow = 'Book already borrow !!'; + break; + case CONSTANTS.STATUS_BORROW.BORROWING: + result_borrow = 'Please return the book !!'; + break; + default: + result_borrow = 'Fail'; + break; } return res.status(400).json({ status: 400, 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 6e78c7a..1c6f993 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 @@ -180,8 +180,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, }); @@ -420,8 +420,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, }); @@ -1477,9 +1477,7 @@ const userController = { const { id } = req.auth_user; // Input body - const { - name, avatar_uri, public_id_avatar, address, dob, gender, - } = req.body.input.user_update_profile_input; + const { name, avatar_uri, public_id_avatar, address, dob, gender } = req.body.input.user_update_profile_input; // Check user_id if (!id) { 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 be4c8d9..567f4fd 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,10 +25,11 @@ 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;