diff --git a/backend-manager-student/.eslintrc.js b/backend-manager-student/.eslintrc.js index 95bb51c..d4a750b 100644 --- a/backend-manager-student/.eslintrc.js +++ b/backend-manager-student/.eslintrc.js @@ -38,7 +38,11 @@ module.exports = { allow: [], }, ], - 'operator-linebreak': ['error', 'after', { overrides: { '?': 'before', ':': 'before', '&&': 'before' } }], + 'operator-linebreak': [ + 'error', + 'after', + { overrides: { '?': 'before', ':': 'before', '&&': 'before', '||': 'before' } }, + ], 'import/prefer-default-export': 'off', 'import/no-unresolved': 'off', 'linebreak-style': ['error', process.platform === 'win64' && 'win32' ? 'windows' : 'unix'], diff --git a/backend-manager-student/src/admin_api/v1/Swagger/admin_api/student.swagger.yaml b/backend-manager-student/src/admin_api/v1/Swagger/admin_api/student.swagger.yaml index 379374e..b94a702 100644 --- a/backend-manager-student/src/admin_api/v1/Swagger/admin_api/student.swagger.yaml +++ b/backend-manager-student/src/admin_api/v1/Swagger/admin_api/student.swagger.yaml @@ -150,6 +150,9 @@ paths: gender: type: number example: 1 + tole: + type: number + example: 1 required: - name - mssv @@ -158,6 +161,7 @@ paths: - dob - class_room - phone_number + - role responses: 200: description: Create success @@ -254,6 +258,9 @@ paths: gender: type: number example: 1 + role: + type: number + example: 1 required: - student_id - name @@ -263,6 +270,7 @@ paths: - dob - class_room - phone_number + - role responses: 200: description: update success diff --git a/backend-manager-student/src/admin_api/v1/controllers/book_categories.controllers/book_categories.controller.js b/backend-manager-student/src/admin_api/v1/controllers/book_categories.controllers/book_categories.controller.js index e5a228c..fc1ceb2 100644 --- a/backend-manager-student/src/admin_api/v1/controllers/book_categories.controllers/book_categories.controller.js +++ b/backend-manager-student/src/admin_api/v1/controllers/book_categories.controllers/book_categories.controller.js @@ -123,9 +123,9 @@ const bookCategoriesController = { // Check input if ( - !HELPER.validateBigInt(category_id) || - !HELPER.validateBigInt(book_id) || - !HELPER.validateBigInt(book_categories_id) + !HELPER.validateBigInt(category_id) + || !HELPER.validateBigInt(book_id) + || !HELPER.validateBigInt(book_categories_id) ) { return res.status(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST).json({ status: CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST, 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 7b51c71..fb90d24 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 @@ -88,9 +88,9 @@ const BorrowBookController = { ); // Condition refund book const check_refund_book = - data_borrow_book.length > 0 && - data_borrow_book[0].status !== CONSTANTS.STATUS_BORROW.DONE && - data_borrow_book[0].status === CONSTANTS.STATUS_BORROW.BORROWING; + data_borrow_book.length > 0 + && data_borrow_book[0].status !== CONSTANTS.STATUS_BORROW.DONE + && data_borrow_book[0].status === CONSTANTS.STATUS_BORROW.BORROWING; if (check_refund_book) { return res.status(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST).json({ status: CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST, @@ -238,8 +238,8 @@ const BorrowBookController = { let err; let result; if ( - Number(status) === CONSTANTS.STATUS_BORROW.BORROWING || - Number(status) === CONSTANTS.STATUS_BORROW.EXPIRED + Number(status) === CONSTANTS.STATUS_BORROW.BORROWING + || Number(status) === CONSTANTS.STATUS_BORROW.EXPIRED ) { // update book database [err, result] = await HELPER.handleRequest( @@ -265,8 +265,8 @@ const BorrowBookController = { }); } } else if ( - Number(status) === CONSTANTS.STATUS_BORROW.DONE || - Number(status) === CONSTANTS.STATUS_BORROW.LOST_BOOK_PROCESSED + Number(status) === CONSTANTS.STATUS_BORROW.DONE + || Number(status) === CONSTANTS.STATUS_BORROW.LOST_BOOK_PROCESSED ) { // Check data book exits const data_book = await book_model.getBookById( diff --git a/backend-manager-student/src/admin_api/v1/controllers/student.controllers/student.controller.js b/backend-manager-student/src/admin_api/v1/controllers/student.controllers/student.controller.js index 880a129..3195b02 100644 --- a/backend-manager-student/src/admin_api/v1/controllers/student.controllers/student.controller.js +++ b/backend-manager-student/src/admin_api/v1/controllers/student.controllers/student.controller.js @@ -25,9 +25,9 @@ const StudentController = { * @return {Object} */ createStudent: async (req, res) => { - const { name, mssv, phone_number, dob, class_room, email, gender } = req.body.input.create_student_input; + const { name, mssv, phone_number, dob, class_room, email, gender, role } = req.body.input.create_student_input; // Check input - if (!name || !mssv || !phone_number || !dob || !class_room || !email || !gender) { + if (!name || !mssv || !phone_number || !dob || !class_room || !email || !gender || !role) { return res.status(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST).json({ status: CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST, message: returnReasons(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST), @@ -36,6 +36,15 @@ const StudentController = { }, }); } + if (Number(role) !== CONSTANTS.ROLE.ROLE_STUDENT && Number(role) !== CONSTANTS.ROLE.ROLE_MANAGER) { + return res.status(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST).json({ + status: CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST, + message: returnReasons(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST), + element: { + result: MESSAGES.GENERAL.INVALID_ROLE, + }, + }); + } const check_email = HELPER.validateEmail(email); @@ -70,6 +79,7 @@ const StudentController = { dob, class: class_room, email, + role, gender, avatar_uri: gender === CONSTANTS.GENDER_MALE ? CONSTANTS.GENDER_IMAGE_MALE : CONSTANTS.GENDER_IMAGE_FEMALE, @@ -113,7 +123,7 @@ const StudentController = { */ updateStudent: async (req, res) => { // Input body - const { student_id, name, avatar_uri, public_id_avatar, address, dob, gender } = + const { student_id, name, avatar_uri, public_id_avatar, address, dob, gender, role } = req.body.input.update_student_input; // Check input @@ -127,7 +137,7 @@ const StudentController = { }); } if ( - [name, avatar_uri, public_id_avatar, address, dob, gender].some( + [name, avatar_uri, public_id_avatar, address, dob, gender, role].some( (field) => field !== undefined && field.trim() === '', ) ) { @@ -139,6 +149,18 @@ const StudentController = { }, }); } + + if (role) { + if (Number(role) !== CONSTANTS.ROLE.ROLE_STUDENT && Number(role) !== CONSTANTS.ROLE.ROLE_MANAGER) { + return res.status(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST).json({ + status: CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST, + message: returnReasons(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST), + element: { + result: MESSAGES.GENERAL.INVALID_ROLE, + }, + }); + } + } const data_update = { name, avatar_uri, @@ -146,12 +168,18 @@ const StudentController = { address, dob, gender, + role, }; try { // Check data book exits const result_student = await student_model.getStudentById( - { user_id: student_id, isdeleted: CONSTANTS.DELETED_DISABLE }, - { user_id: 'user_id' }, + { + user_id: student_id, + isdeleted: CONSTANTS.DELETED_DISABLE, + }, + { + user_id: 'user_id', + }, ); if (!result_student || !result_student.length) { @@ -238,6 +266,7 @@ const StudentController = { { user_id: 'user_id', email: 'email', + name: 'name', phone_number: 'phone_number', mssv: 'mssv', }, @@ -259,6 +288,7 @@ const StudentController = { student_model.updateStudent( { email: HELPER.getDeleteString(result_student_detail[0]?.email), + name: HELPER.getDeleteString(result_student_detail[0]?.name), phone_number: HELPER.getDeleteString(result_student_detail[0]?.phone_number), mssv: HELPER.getDeleteString(result_student_detail[0]?.mssv), isdeleted: CONSTANTS.DELETED_ENABLE, diff --git a/backend-manager-student/src/share/configs/message.js b/backend-manager-student/src/share/configs/message.js index 27fbc37..ee1f52b 100644 --- a/backend-manager-student/src/share/configs/message.js +++ b/backend-manager-student/src/share/configs/message.js @@ -37,6 +37,7 @@ module.exports = { INVALID_MUTILP_FIELD: 'Please provide non-empty values for all fields', INVALID_UNAUTHORIZED: 'Unauthorized', INVALID_RATING: 'Invalid Rating', + INVALID_ROLE: 'Invalid create role manager or student', // ? EXITS EXITS_EMAIL_PHONE: 'Email or Phone or Email or Mssv exits !', diff --git a/backend-manager-student/src/share/middleware/handle_error.js b/backend-manager-student/src/share/middleware/handle_error.js index d0e3cc4..a4edede 100644 --- a/backend-manager-student/src/share/middleware/handle_error.js +++ b/backend-manager-student/src/share/middleware/handle_error.js @@ -28,18 +28,18 @@ module.exports = { const constraint = error.constraint; let message; switch (constraint) { - case KEY_DUPLICATE.DUPLICATE_KEY_EMAIL: - message = MESSAGES.GENERAL.EXITS_EMAIL; - break; - case KEY_DUPLICATE.DUPLICATE_KEY_PHONE: - message = MESSAGES.GENERAL.EXITS_PHONE; - break; - case KEY_DUPLICATE.DUPLICATE_KEY_MSSV: - message = MESSAGES.GENERAL.EXITS_MSSV; - break; - default: - message = MESSAGES.GENERAL.ERROR_UNKNOWN; - break; + case KEY_DUPLICATE.DUPLICATE_KEY_EMAIL: + message = MESSAGES.GENERAL.EXITS_EMAIL; + break; + case KEY_DUPLICATE.DUPLICATE_KEY_PHONE: + message = MESSAGES.GENERAL.EXITS_PHONE; + break; + case KEY_DUPLICATE.DUPLICATE_KEY_MSSV: + message = MESSAGES.GENERAL.EXITS_MSSV; + break; + default: + message = MESSAGES.GENERAL.ERROR_UNKNOWN; + break; } return message; }, diff --git a/backend-manager-student/src/share/models/author.model.js b/backend-manager-student/src/share/models/author.model.js index 776a75c..6d8e652 100644 --- a/backend-manager-student/src/share/models/author.model.js +++ b/backend-manager-student/src/share/models/author.model.js @@ -10,7 +10,8 @@ module.exports = { createAuthor: (data) => new Promise((resolve, reject) => { try { - const result = knex('authors').insert(data).onConflict('author_id').merge().returning(['author_id']); + const result = knex('authors').insert(data).onConflict('author_id').merge() + .returning(['author_id']); resolve(result); } catch (error) { reject(error); diff --git a/backend-manager-student/src/share/models/book.model.js b/backend-manager-student/src/share/models/book.model.js index 8f81869..37784b0 100644 --- a/backend-manager-student/src/share/models/book.model.js +++ b/backend-manager-student/src/share/models/book.model.js @@ -13,7 +13,8 @@ module.exports = { createBook: (data) => new Promise((resolve, reject) => { try { - const result = knex('books').insert(data).onConflict('book_id').merge().returning(['book_id']); + const result = knex('books').insert(data).onConflict('book_id').merge() + .returning(['book_id']); resolve(result); } catch (error) { reject(error); diff --git a/backend-manager-student/src/share/models/phone.model.js b/backend-manager-student/src/share/models/phone.model.js index c46c101..4fe56e1 100644 --- a/backend-manager-student/src/share/models/phone.model.js +++ b/backend-manager-student/src/share/models/phone.model.js @@ -10,7 +10,8 @@ module.exports = { createPhone: (data) => new Promise((resolve, reject) => { try { - const result = knex('phone').insert(data).onConflict('phone_id').merge().returning(['phone_id']); + const result = knex('phone').insert(data).onConflict('phone_id').merge() + .returning(['phone_id']); resolve(result); } catch (error) { reject(error); diff --git a/backend-manager-student/src/share/models/rating.model.js b/backend-manager-student/src/share/models/rating.model.js index b1e0fa1..dfcac46 100644 --- a/backend-manager-student/src/share/models/rating.model.js +++ b/backend-manager-student/src/share/models/rating.model.js @@ -10,7 +10,8 @@ module.exports = { createRatings: (data) => new Promise((resolve, reject) => { try { - const result = knex('book_rates').insert(data).onConflict('rate_id').merge().returning(['rate_id']); + const result = knex('book_rates').insert(data).onConflict('rate_id').merge() + .returning(['rate_id']); resolve(result); } catch (error) { reject(error); diff --git a/backend-manager-student/src/share/models/user.model.js b/backend-manager-student/src/share/models/user.model.js index f83ddfb..afff36b 100644 --- a/backend-manager-student/src/share/models/user.model.js +++ b/backend-manager-student/src/share/models/user.model.js @@ -119,7 +119,8 @@ module.exports = { createStudent: (data) => new Promise((resolve, reject) => { try { - const result_student = knex('user').insert(data).onConflict('user_id').merge().returning(['user_id']); + const result_student = knex('user').insert(data).onConflict('user_id').merge() + .returning(['user_id']); resolve(result_student); } catch (error) { reject(error); diff --git a/backend-manager-student/src/share/utils/redis_pub_sub_helper.js b/backend-manager-student/src/share/utils/redis_pub_sub_helper.js index c16693e..898fef3 100644 --- a/backend-manager-student/src/share/utils/redis_pub_sub_helper.js +++ b/backend-manager-student/src/share/utils/redis_pub_sub_helper.js @@ -74,21 +74,21 @@ const handleException = (err, name, port) => { }); let message_queue; switch (name) { - case CONSTANTS.NAME_SERVER.STUDENT: - message_queue = CONSTANTS.QUEUE.REDIS_SERVER_STUDENT; - break; - case CONSTANTS.NAME_SERVER.ADMIN: - message_queue = CONSTANTS.QUEUE.REDIS_SERVER_ADMIN; - break; - case CONSTANTS.NAME_SERVER.CRON: - message_queue = CONSTANTS.QUEUE.REDIS_SERVER_CRON; - break; - case CONSTANTS.NAME_SERVER.DB: - message_queue = CONSTANTS.QUEUE.REDIS_DB; - break; - default: - message_queue = CONSTANTS.QUEUE.REDIS_SERVER_CRON; - break; + case CONSTANTS.NAME_SERVER.STUDENT: + message_queue = CONSTANTS.QUEUE.REDIS_SERVER_STUDENT; + break; + case CONSTANTS.NAME_SERVER.ADMIN: + message_queue = CONSTANTS.QUEUE.REDIS_SERVER_ADMIN; + break; + case CONSTANTS.NAME_SERVER.CRON: + message_queue = CONSTANTS.QUEUE.REDIS_SERVER_CRON; + break; + case CONSTANTS.NAME_SERVER.DB: + message_queue = CONSTANTS.QUEUE.REDIS_DB; + break; + default: + message_queue = CONSTANTS.QUEUE.REDIS_SERVER_CRON; + break; } // Publish data queue Redis return queueMessageTelegram(message_queue, { diff --git a/backend-manager-student/src/user_api/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 c324df7..e75dee3 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 @@ -89,9 +89,9 @@ const BorrowBookController = { '*', ); const refund_book = - data_borrow_book.length > 0 && - data_borrow_book[0].status !== CONSTANTS.STATUS_BORROW.DONE && - data_borrow_book[0].status; + data_borrow_book.length > 0 + && data_borrow_book[0].status !== CONSTANTS.STATUS_BORROW.DONE + && data_borrow_book[0].status; if (refund_book) { return res.status(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST).json({ status: CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST, 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 86872bd..fbaef42 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 @@ -276,16 +276,16 @@ const userController = { // Check input register if ( - !mssv - || !password - || !HELPER.isNumeric(mssv) - || !name - || !phone_number - || !email - || !dob - || !address - || !gender - || !class_room + !mssv || + !password || + !HELPER.isNumeric(mssv) || + !name || + !phone_number || + !email || + !dob || + !address || + !gender || + !class_room ) { return res.status(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST).json({ status: CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST, @@ -419,7 +419,10 @@ const userController = { // Check Token old const refetch_token_old = await user_device_model.getDeviceId( - { device_uuid: device.device_id }, + { + device_uuid: device.device_id, + isdeleted: CONSTANTS.DELETED_DISABLE, + }, { refresh_token: 'refresh_token', user_id: 'user_id', @@ -474,7 +477,15 @@ const userController = { isdeleted: CONSTANTS.DELETED_DISABLE, }; let users = await user_model.getStudentById(data_query, data_return); - + if (!users || !users.length) { + return res.status(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST).json({ + status: CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST, + message: returnReasons(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST), + element: { + result: MESSAGES.STUDENT.NOT_EXIT_ACCOUNT, + }, + }); + } // Assign from object refetch_token_old[0].name = users[0].name; refetch_token_old[0].email = users[0].email; @@ -506,6 +517,15 @@ const userController = { [err, result] = await HELPER.handleRequest( user_device_model.checkUserByToken(refresh_token_cookie, device.device_id), ); + if (!result || !result.length) { + return res.status(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST).json({ + status: CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST, + message: returnReasons(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST), + element: { + result: MESSAGES.STUDENT.NOT_EXIT_ACCOUNT, + }, + }); + } // Student exits if (result) { const refresh_token_exit = refresh_token_cookie;