Skip to content

Commit

Permalink
#253 [Devops] Setup mongo and add telegram db Fail.
Browse files Browse the repository at this point in the history
  • Loading branch information
fdhhhdjd committed Apr 14, 2023
1 parent 65e82c7 commit 2280a25
Show file tree
Hide file tree
Showing 16 changed files with 289 additions and 139 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 @@ -242,8 +242,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(
Expand All @@ -269,8 +269,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(
Expand Down
4 changes: 4 additions & 0 deletions backend-manager-student/src/share/configs/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,4 +528,8 @@ module.exports = {
IDLE_TIMEOUT_MILLIS: 30000,
REAP_INTERVAL_MILLIS: 1000,
},
MONGO_SCHEMA: {
DOCUMENT_NAME: 'book_comment',
COLLECTION_NAME: 'book_comments',
},
};
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: 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
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/rating.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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
55 changes: 55 additions & 0 deletions backend-manager-student/src/share/schema/comment.schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//! LIBRARY
const { Schema, model } = require('mongoose');

//! CONFIGS
const CONSTANTS = require('../configs/constants');

/**
* @author Nguyễn Tiến Tài
* @created_at 13/04/2023
* @description Schema Comment Mongo
*/
const commentSchema = new Schema({
user_id: {
type: String,
required: CONSTANTS.DELETED_ENABLE,
},
book_id: {
type: String,
required: CONSTANTS.DELETED_ENABLE,
},
posted: {
type: Date,
required: CONSTANTS.DELETED_ENABLE,
},
content: {
type: String,
required: CONSTANTS.DELETED_ENABLE,
maxLength: 150,
},
parent_slug: {
type: String,
trim: CONSTANTS.DELETED_ENABLE,
},
full_slug: {
type: String,
trim: CONSTANTS.DELETED_ENABLE,
},
slug: {
type: String,
trim: CONSTANTS.DELETED_ENABLE,
},
isdeleted: {
type: Schema.Types.Boolean,
default: CONSTANTS.DELETED_DISABLE,
},
comment_replies_num: {
type: Number,
default: 0,
},

}, {
collection: CONSTANTS.MONGO_SCHEMA.COLLECTION_NAME,
timestamps: CONSTANTS.DELETED_ENABLE,
});
module.exports.COMMENT = model(CONSTANTS.MONGO.DOCUMENT_NAME, commentSchema);
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//! CONFIGS
const CONSTANTS = require('../../configs/constants');

//! SCHEMA
const { COMMENT } = require('../../schema/comment.schema');

//! MODEL
const user_model = require('../../models/user.model');

/**
* @author Nguyễn Tiến Tài
* @created_at 13/04/2023
* @description create Comment
* @function insert_comment
*/
const insert_comment = async (data) => await COMMENT.create(data);

/**
* @author Nguyễn Tiến Tài
* @created_at 13/04/2023
* @updated_at 14/04/2023
* @description List Comment
* @function list_comment
*/
const list_comment = async (match, search_data) => {
const comments = await COMMENT.find(match, search_data).sort({ full_slug: 1 }).lean();
const userPromises = comments.map(async (comment) => {
const user = await user_model.getStudentById(
{
user_id: String(comment.user_id),
isdeleted: CONSTANTS.DELETED_DISABLE,
},
{
name: 'name',
avatar_uri: 'avatar_uri',
},
);
return {
...comment,
full_name: user[0]?.name,
avatar_uri_user: user[0]?.avatar_uri,
};
});
// eslint-disable-next-line no-shadow
const list_comment = await Promise.all(userPromises);
return list_comment;
};

/**
* @author Nguyễn Tiến Tài
* @created_at 13/04/2023
* @description get paren slug
* @function get_paren_slug
*/
const get_paren_slug = async (data) => await COMMENT.findOne(data).lean();

module.exports = {
insert_comment,
list_comment,
get_paren_slug,
};
30 changes: 15 additions & 15 deletions backend-manager-student/src/share/utils/redis_pub_sub_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
Loading

0 comments on commit 2280a25

Please sign in to comment.