Skip to content

Commit

Permalink
#180 hotfix book tai dev 20230315
Browse files Browse the repository at this point in the history
  • Loading branch information
fdhhhdjd committed Mar 14, 2023
1 parent 5135a2e commit cc1bc43
Show file tree
Hide file tree
Showing 29 changed files with 447 additions and 342 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,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) {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,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 (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,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) {
Expand Down
9 changes: 9 additions & 0 deletions backend-manager-student/src/share/configs/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,4 +357,13 @@ module.exports = {
*/
YES: 1,
NO: 0,
/**
* @author Nguyễn Tiến Tài
* @created_at 15/03/2023
* @description Key Redis General
* @param { String }
*/
// Student
KEY_USER_EXIT_U: 'user_api*',
DELETE_KEY_CACHE_LRU: 'user_api_delete_key_lru',
};
4 changes: 4 additions & 0 deletions backend-manager-student/src/share/db/init_multiple_redis.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//! LIBRARY
const IOREDIS = require('ioredis');

//! SHARE
const CONFIGS = require('../configs/config');

/**
* @author Nguyễn Tiến Tài
* @created_at 22/01/2023
* @updated_at 15/03/2023
* @description Connect Cache Redis Master and Slave
*/
const ConnectionRedis = (REDIS) => {
Expand Down
3 changes: 1 addition & 2 deletions backend-manager-student/src/share/middleware/handle_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
};
35 changes: 17 additions & 18 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,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
Expand Down
35 changes: 17 additions & 18 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,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
Expand Down
134 changes: 65 additions & 69 deletions backend-manager-student/src/share/models/book_borrowed.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit cc1bc43

Please sign in to comment.