Skip to content

Commit

Permalink
hot fix telegram and borow book
Browse files Browse the repository at this point in the history
  • Loading branch information
fdhhhdjd committed Mar 30, 2023
1 parent 12127e3 commit f7698b2
Show file tree
Hide file tree
Showing 35 changed files with 244 additions and 165 deletions.
3 changes: 1 addition & 2 deletions backend-manager-student/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^4.6.0",
"ua-parser-js": "^1.0.33",
"crypto": "^1.0.1",
"node-telegram-bot-api": "^0.61.0"
"crypto": "^1.0.1"
},
"devDependencies": {
"jest": "^29.3.1"
Expand Down
4 changes: 2 additions & 2 deletions backend-manager-student/src/admin_api/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ app.use(
saveUninitialized: CONSTANTS.DELETED_ENABLE,
cookie: {
secure:
CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT
? CONSTANTS.DELETED_ENABLE
CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT ?
CONSTANTS.DELETED_ENABLE
: CONSTANTS.DELETED_DISABLE,
httpOnly: CONSTANTS.DELETED_ENABLE,
maxAge: CONSTANTS._1_HOURS_S,
Expand Down
14 changes: 9 additions & 5 deletions backend-manager-student/src/admin_api/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ const dotenv = require('dotenv');
//! APP
const app = require('./app');

//! REDIS PUBSUB
const REDIS_PUB_SUB = require('../share/utils/redis_pub_sub_helper');

//! SHARE
const CONSTANTS = require('../share/configs/constants');
const MESSAGES = require('../share/configs/message');
const { sendTelegram } = require('../share/utils/telegram');

//! USED LIBRARY
dotenv.config();
Expand All @@ -27,9 +29,11 @@ console.info(`Server is listening on port:http://localhost:${PORT}`);

const handleException = (err) => {
console.error('Unhandled Exception:', err);

const message = `Server Student and Admin ${PORT}:: ${err.name}: ${err.message}`;
sendTelegram(message);
const message = `Server Admin ${PORT}:: ${err.name}: ${err.message}`;
// Publish data queue Redis
return REDIS_PUB_SUB.queueMessageTelegram(CONSTANTS.QUEUE.REDIS_SERVER_ADMIN, {
message,
});
};

process.on(CONSTANTS.ERROR_REJECTION, handleException);
Expand All @@ -38,6 +42,6 @@ process.on(CONSTANTS.ERROR_EXCEPTION, handleException);

process.on(CONSTANTS.SIGINT, () => {
server.close(() => {
console.error('Server Student and Admin::: Off ');
console.error('Server Admin::: Off ');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,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 @@ -270,8 +270,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 @@ -442,12 +442,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 @@ -25,8 +25,8 @@ const bookController = {
* @return {Object:{Number,String}}
*/
createBook: async (req, res) => {
const { name, author_id, image_uri, description, page_number, bookshelf, language, quantity, public_id_image } =
req.body.input.book_input;
const { name, author_id, image_uri, description, page_number, bookshelf, language, quantity, public_id_image }
= req.body.input.book_input;

// Check input
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ const StudentController = {
*/
updateStudent: async (req, res) => {
// Input body
const { student_id, name, avatar_uri, public_id_avatar, address, dob, gender } =
req.body.input.update_student_input;
const { student_id, name, avatar_uri, public_id_avatar, address, dob, gender }
= req.body.input.update_student_input;

// Check input
if (!student_id || !HELPER.validateBigInt(student_id)) {
Expand Down
8 changes: 0 additions & 8 deletions backend-manager-student/src/share/configs/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,4 @@ module.exports = {
*/
IPA_API_RATE_LIMIT_DURATION: process.env.IPA_API_RATE_LIMIT_DURATION,
IPA_API_RATE_LIMIT: process.env.IPA_API_RATE_LIMIT,

/**
* @author Nguyễn Tiến Tài
* @created_at 29/03/2023
* @description CONNECT TELEGRAM
*/
KEY_TELEGRAM: process.env.KEY_TELEGRAM,
KEY_CHAT_ID: process.env.KEY_CHAT_ID,
};
10 changes: 10 additions & 0 deletions backend-manager-student/src/share/configs/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ module.exports = {
*/
// Student
KEY_USER_EXIT_U: 'student_api*',
KEY_USER_EXIT_A: 'admin_api*',
DELETE_KEY_CACHE_LRU: 'student_api_delete_key_lru',

/**
Expand All @@ -446,4 +447,13 @@ module.exports = {
ERROR_REJECTION: 'unhandledRejection',
ERROR_EXCEPTION: 'uncaughtException',
SIGINT: 'SIGINT',
/**
* @author Nguyễn Tiến Tài
* @created_at 30/03/2023
* @description KEY QUEUE
*/
QUEUE: {
REDIS_SERVER_ADMIN: 'admin_unErrorServer',
REDIS_SERVER_STUDENT: 'user_unErrorServer',
},
};
18 changes: 0 additions & 18 deletions backend-manager-student/src/share/db/bot_telegram.js

This file was deleted.

24 changes: 12 additions & 12 deletions backend-manager-student/src/share/middleware/handle_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: 2 additions & 1 deletion backend-manager-student/src/share/models/author.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion backend-manager-student/src/share/models/book.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ module.exports = {
if (borrow_book_id && !user_id) {
queryBuilder.where('borrowed_book_id', borrow_book_id);
}
if (!borrow_book_id && !user_id) {
if (!borrow_book_id && user_id) {
queryBuilder.where('user_id', user_id);
}
})
Expand Down
3 changes: 2 additions & 1 deletion backend-manager-student/src/share/models/phone.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion backend-manager-student/src/share/models/user.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions backend-manager-student/src/share/utils/redis_pub_sub_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,19 @@ const queueMessageUserApi = async (key, value) => {
console.info('Del success');
}
};
const queueMessageTelegram = async (key, value) => {
const key_convert = key;
const value_convert = JSON.stringify(value);
try {
// send email here
const publishResult = await REDIS_MASTER.publish(key_convert, value_convert);
console.info(`Published to ${publishResult} subscribers.`);
} finally {
console.info('Del success');
}
};
module.exports = {
sendEmailWithLock,
queueMessageUserApi,
queueMessageTelegram,
};
13 changes: 0 additions & 13 deletions backend-manager-student/src/share/utils/telegram.js

This file was deleted.

34 changes: 32 additions & 2 deletions backend-manager-student/src/user_api/server.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,47 @@
//! LIBRARY
const dotenv = require('dotenv');

//! APP
const app = require('./app');

//! REDIS PUBSUB
const REDIS_PUB_SUB = require('../share/utils/redis_pub_sub_helper');

//! SHARE
const CONSTANTS = require('../share/configs/constants');
const MESSAGES = require('../share/configs/message');

//! USED LIBRARY
dotenv.config();

app.get('/', (req, res) => {
const health_check = {
uptime: process.uptime(),
message: 'Server User Api',
message: MESSAGES.STUDENT.SERVER,
timestamp: Date.now(),
};
return res.send(health_check);
});

const PORT = process.env.PORT_USER_API || 5001;
app.listen(PORT);
const server = app.listen(PORT);
console.info(`Server is listening on port:http://localhost:${PORT}`);

const handleException = (err) => {
console.error('Unhandled Exception:', err);
const message = `Server Student ${PORT}:: ${err.name}: ${err.message}`;
// Publish data queue Redis
REDIS_PUB_SUB.queueMessageTelegram(CONSTANTS.QUEUE.REDIS_SERVER_STUDENT, {
message,
});
};

process.on(CONSTANTS.ERROR_REJECTION, handleException);

process.on(CONSTANTS.ERROR_EXCEPTION, handleException);

process.on(CONSTANTS.SIGINT, () => {
server.close(() => {
console.error('Server Student::: Off ');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,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 = MESSAGES.GENERAL.ALREADY_BOOK_BORROW;
break;
case CONSTANTS.STATUS_BORROW.BORROWING:
result_borrow = MESSAGES.GENERAL.PLEASE_REFUND_BOOK;
break;
default:
result_borrow = MESSAGES.GENERAL.BORROW_FAIL;
break;
case CONSTANTS.STATUS_BORROW.PENDING:
result_borrow = MESSAGES.GENERAL.ALREADY_BOOK_BORROW;
break;
case CONSTANTS.STATUS_BORROW.BORROWING:
result_borrow = MESSAGES.GENERAL.PLEASE_REFUND_BOOK;
break;
default:
result_borrow = MESSAGES.GENERAL.BORROW_FAIL;
break;
}
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 @@ -184,8 +184,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,
});
Expand Down Expand Up @@ -422,8 +422,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,
});
Expand Down
3 changes: 1 addition & 2 deletions server-media-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
"pg": "^8.8.0",
"ioredis": "^5.2.4",
"knex": "^2.3.0",
"sharp": "^0.31.3",
"node-telegram-bot-api": "^0.61.0"
"sharp": "^0.31.3"
}
}
Loading

0 comments on commit f7698b2

Please sign in to comment.