Skip to content

Commit

Permalink
#247 [Backend] Library Feedback 01/04/2023
Browse files Browse the repository at this point in the history
  • Loading branch information
fdhhhdjd committed Apr 6, 2023
1 parent 6082f1a commit 8d80486
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -629,11 +629,11 @@ const adminController = {

// Check Input is empty
if (
(name !== undefined && name.trim() === '') ||
(avatar_uri !== undefined && avatar_uri.trim() === '') ||
(public_id_avatar !== undefined && public_id_avatar.trim() === '') ||
(address !== undefined && address.trim() === '') ||
(dob !== undefined && dob.trim() === '')
(name !== undefined && name.trim() === '')
|| (avatar_uri !== undefined && avatar_uri.trim() === '')
|| (public_id_avatar !== undefined && public_id_avatar.trim() === '')
|| (address !== undefined && address.trim() === '')
|| (dob !== undefined && dob.trim() === '')
) {
return res.status(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST).json({
status: CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ const bookController = {

// Check input
if (
!name ||
!author_id ||
!image_uri ||
!description ||
!bookshelf ||
!language ||
!quantity ||
!public_id_image ||
!page_number
!name
|| !author_id
|| !image_uri
|| !description
|| !bookshelf
|| !language
|| !quantity
|| !public_id_image
|| !page_number
) {
return res.status(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST).json({
status: CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST,
Expand Down Expand Up @@ -132,15 +132,15 @@ const bookController = {

// Check Input is empty
if (
(name !== undefined && name.trim() === '') ||
(author_id !== undefined && author_id.trim() === '') ||
(image_uri !== undefined && image_uri.trim() === '') ||
(description !== undefined && description.trim() === '') ||
(bookshelf !== undefined && bookshelf.trim() === '') ||
(language !== undefined && language.trim() === '') ||
(quantity !== undefined && quantity.trim() === '') ||
(public_id_image !== undefined && public_id_image.trim() === '') ||
(page_number !== undefined && page_number.trim() === '')
(name !== undefined && name.trim() === '')
|| (author_id !== undefined && author_id.trim() === '')
|| (image_uri !== undefined && image_uri.trim() === '')
|| (description !== undefined && description.trim() === '')
|| (bookshelf !== undefined && bookshelf.trim() === '')
|| (language !== undefined && language.trim() === '')
|| (quantity !== undefined && quantity.trim() === '')
|| (public_id_image !== undefined && public_id_image.trim() === '')
|| (page_number !== undefined && page_number.trim() === '')
) {
return res.status(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST).json({
status: CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ const BorrowBookController = {
{ book_id: 'book_id', status: 'status', user_id: 'user_id', quantity: 'quantity' },
);
// Check book lost processing
const checkLostProcessing = (arr) => !arr.some((obj) => obj.status === CONSTANTS.STATUS_BORROW.LOST_BOOK_PROCESSING);
const checkLostProcessing = (arr) =>
!arr.some((obj) => obj.status === CONSTANTS.STATUS_BORROW.LOST_BOOK_PROCESSING);
const totalQuantity = check_borrow_book.reduce((acc, cur) => {
if (cur.user_id === user_id) {
return acc + cur.quantity;
Expand Down Expand Up @@ -85,7 +86,10 @@ 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;
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;
if (check_refund_book) {
return res.status(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST).json({
status: CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST,
Expand Down Expand Up @@ -123,10 +127,14 @@ const BorrowBookController = {

// create book_borrowed database
const data_insert = {
borrowed_book_id: data_borrow_book.length > 0 ? data_borrow_book[0].borrowed_book_id : RANDOMS.createID(),
borrowed_book_id:
data_borrow_book.length > 0 ? data_borrow_book[0].borrowed_book_id : RANDOMS.createID(),
book_id,
user_id,
quantity: data_borrow_book.length > 0 ? Number(data_borrow_book[0].quantity) + Number(quantity) : Number(quantity),
quantity:
data_borrow_book.length > 0
? Number(data_borrow_book[0].quantity) + Number(quantity)
: Number(quantity),
status: CONSTANTS.STATUS_BORROW.PENDING,
};

Expand Down Expand Up @@ -232,7 +240,10 @@ const BorrowBookController = {
}
let err;
let result;
if (Number(status) === CONSTANTS.STATUS_BORROW.BORROWING || Number(status) === CONSTANTS.STATUS_BORROW.EXPIRED) {
if (
Number(status) === CONSTANTS.STATUS_BORROW.BORROWING
|| Number(status) === CONSTANTS.STATUS_BORROW.EXPIRED
) {
// update book database
[err, result] = await HELPER.handleRequest(
borrow_book_model.updateBorrowBook(
Expand All @@ -256,7 +267,10 @@ const BorrowBookController = {
message: returnReasons(CONSTANTS.HTTP.STATUS_5XX_INTERNAL_SERVER_ERROR),
});
}
} else if (Number(status) === CONSTANTS.STATUS_BORROW.DONE || Number(status) === CONSTANTS.STATUS_BORROW.LOST_BOOK_PROCESSED) {
} else if (
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(
{ book_id, isdeleted: CONSTANTS.DELETED_DISABLE },
Expand Down
28 changes: 14 additions & 14 deletions backend-manager-student/src/share/middleware/handle_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ module.exports = {
* @return { String }
*/
returnReasons: (code) =>
CONSTANTS.reasonPhraseCodeProNewMap().get(code.toString()) ||
CONSTANTS.reasonPhraseCodeProNewMap().get(CONSTANTS.HTTP.NO_STATUS_DEFAULT),
CONSTANTS.reasonPhraseCodeProNewMap().get(code.toString())
|| CONSTANTS.reasonPhraseCodeProNewMap().get(CONSTANTS.HTTP.NO_STATUS_DEFAULT),

/**
* @author Nguyễn Tiến Tài
Expand All @@ -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;
},
Expand Down
3 changes: 1 addition & 2 deletions backend-manager-student/src/share/models/author.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ 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);
Expand Down
3 changes: 1 addition & 2 deletions backend-manager-student/src/share/models/book.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ 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);
Expand Down
3 changes: 1 addition & 2 deletions backend-manager-student/src/share/models/phone.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ 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);
Expand Down
3 changes: 1 addition & 2 deletions backend-manager-student/src/share/models/rating.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ 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);
Expand Down
3 changes: 1 addition & 2 deletions backend-manager-student/src/share/models/user.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ const BorrowBookController = {
{ book_id: 'book_id', status: 'status', user_id: 'user_id', quantity: 'quantity' },
);
// Check book lost processing
const checkLostProcessing = (arr) => !arr.some((obj) => obj.status === CONSTANTS.STATUS_BORROW.LOST_BOOK_PROCESSING);
const checkLostProcessing = (arr) =>
!arr.some((obj) => obj.status === CONSTANTS.STATUS_BORROW.LOST_BOOK_PROCESSING);
const totalQuantity = check_borrow_book.reduce((acc, cur) => {
if (cur.user_id === id) {
return acc + cur.quantity;
Expand Down Expand Up @@ -86,7 +87,10 @@ const BorrowBookController = {
{ book_id, isdeleted: CONSTANTS.DELETED_DISABLE, user_id: id },
'*',
);
const refund_book = data_borrow_book.length > 0 && data_borrow_book[0].status !== CONSTANTS.STATUS_BORROW.DONE && data_borrow_book[0].status;
const refund_book =
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,
Expand Down Expand Up @@ -124,10 +128,14 @@ const BorrowBookController = {

// create book_borrowed database
const data_insert = {
borrowed_book_id: data_borrow_book.length > 0 ? data_borrow_book[0].borrowed_book_id : RANDOMS.createID(),
borrowed_book_id:
data_borrow_book.length > 0 ? data_borrow_book[0].borrowed_book_id : RANDOMS.createID(),
book_id,
user_id: id,
quantity: data_borrow_book.length > 0 ? Number(data_borrow_book[0].quantity) + Number(quantity) : Number(quantity),
quantity:
data_borrow_book.length > 0
? Number(data_borrow_book[0].quantity) + Number(quantity)
: Number(quantity),
status: CONSTANTS.STATUS_BORROW.PENDING,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ const ratingController = {
const { id } = req.auth_user;
// Check input
if (
!HELPER.validateBigInt(book_id) ||
!HELPER.validateBigInt(rating) ||
!HELPER.validateBigInt(id) ||
!HELPER.validateBigInt(borrowed_book_id)
!HELPER.validateBigInt(book_id)
|| !HELPER.validateBigInt(rating)
|| !HELPER.validateBigInt(id)
|| !HELPER.validateBigInt(borrowed_book_id)
) {
return res.status(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST).json({
status: CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 8d80486

Please sign in to comment.