Skip to content

Commit

Permalink
#180 hotfix book tai dev 20230314
Browse files Browse the repository at this point in the history
  • Loading branch information
fdhhhdjd committed Mar 14, 2023
1 parent 130c51e commit 5135a2e
Show file tree
Hide file tree
Showing 20 changed files with 336 additions and 317 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down Expand Up @@ -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,
});
Expand Down Expand Up @@ -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,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion backend-manager-student/src/share/middleware/handle_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
};
35 changes: 18 additions & 17 deletions backend-manager-student/src/share/models/author.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
35 changes: 18 additions & 17 deletions backend-manager-student/src/share/models/book.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
134 changes: 69 additions & 65 deletions backend-manager-student/src/share/models/book_borrowed.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 5135a2e

Please sign in to comment.