Skip to content

Commit

Permalink
#230 [Backend] CURD Student CMS
Browse files Browse the repository at this point in the history
  • Loading branch information
fdhhhdjd committed Apr 16, 2023
1 parent 10aab94 commit e48feac
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 64 deletions.
6 changes: 5 additions & 1 deletion backend-manager-student/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ module.exports = {
allow: [],
},
],
'operator-linebreak': ['error', 'after', { overrides: { '?': 'before', ':': 'before', '&&': 'before' } }],
'operator-linebreak': [
'error',
'after',
{ overrides: { '?': 'before', ':': 'before', '&&': 'before', '||': 'before' } },
],
'import/prefer-default-export': 'off',
'import/no-unresolved': 'off',
'linebreak-style': ['error', process.platform === 'win64' && 'win32' ? 'windows' : 'unix'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ paths:
gender:
type: number
example: 1
tole:
type: number
example: 1
required:
- name
- mssv
Expand All @@ -158,6 +161,7 @@ paths:
- dob
- class_room
- phone_number
- role
responses:
200:
description: Create success
Expand Down Expand Up @@ -254,6 +258,9 @@ paths:
gender:
type: number
example: 1
role:
type: number
example: 1
required:
- student_id
- name
Expand All @@ -263,6 +270,7 @@ paths:
- dob
- class_room
- phone_number
- role
responses:
200:
description: update success
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ const bookCategoriesController = {

// Check input
if (
!HELPER.validateBigInt(category_id) ||
!HELPER.validateBigInt(book_id) ||
!HELPER.validateBigInt(book_categories_id)
!HELPER.validateBigInt(category_id)
|| !HELPER.validateBigInt(book_id)
|| !HELPER.validateBigInt(book_categories_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 @@ -88,9 +88,9 @@ 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;
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 @@ -238,8 +238,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 @@ -265,8 +265,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
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ const StudentController = {
* @return {Object}
*/
createStudent: async (req, res) => {
const { name, mssv, phone_number, dob, class_room, email, gender } = req.body.input.create_student_input;
const { name, mssv, phone_number, dob, class_room, email, gender, role } = req.body.input.create_student_input;
// Check input
if (!name || !mssv || !phone_number || !dob || !class_room || !email || !gender) {
if (!name || !mssv || !phone_number || !dob || !class_room || !email || !gender || !role) {
return res.status(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST).json({
status: CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST,
message: returnReasons(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST),
Expand All @@ -36,6 +36,15 @@ const StudentController = {
},
});
}
if (Number(role) !== CONSTANTS.ROLE.ROLE_STUDENT && Number(role) !== CONSTANTS.ROLE.ROLE_MANAGER) {
return res.status(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST).json({
status: CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST,
message: returnReasons(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST),
element: {
result: MESSAGES.GENERAL.INVALID_ROLE,
},
});
}

const check_email = HELPER.validateEmail(email);

Expand Down Expand Up @@ -70,6 +79,7 @@ const StudentController = {
dob,
class: class_room,
email,
role,
gender,
avatar_uri:
gender === CONSTANTS.GENDER_MALE ? CONSTANTS.GENDER_IMAGE_MALE : CONSTANTS.GENDER_IMAGE_FEMALE,
Expand Down Expand Up @@ -113,7 +123,7 @@ const StudentController = {
*/
updateStudent: async (req, res) => {
// Input body
const { student_id, name, avatar_uri, public_id_avatar, address, dob, gender } =
const { student_id, name, avatar_uri, public_id_avatar, address, dob, gender, role } =
req.body.input.update_student_input;

// Check input
Expand All @@ -127,7 +137,7 @@ const StudentController = {
});
}
if (
[name, avatar_uri, public_id_avatar, address, dob, gender].some(
[name, avatar_uri, public_id_avatar, address, dob, gender, role].some(
(field) => field !== undefined && field.trim() === '',
)
) {
Expand All @@ -139,19 +149,37 @@ const StudentController = {
},
});
}

if (role) {
if (Number(role) !== CONSTANTS.ROLE.ROLE_STUDENT && Number(role) !== CONSTANTS.ROLE.ROLE_MANAGER) {
return res.status(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST).json({
status: CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST,
message: returnReasons(CONSTANTS.HTTP.STATUS_4XX_BAD_REQUEST),
element: {
result: MESSAGES.GENERAL.INVALID_ROLE,
},
});
}
}
const data_update = {
name,
avatar_uri,
public_id_avatar,
address,
dob,
gender,
role,
};
try {
// Check data book exits
const result_student = await student_model.getStudentById(
{ user_id: student_id, isdeleted: CONSTANTS.DELETED_DISABLE },
{ user_id: 'user_id' },
{
user_id: student_id,
isdeleted: CONSTANTS.DELETED_DISABLE,
},
{
user_id: 'user_id',
},
);

if (!result_student || !result_student.length) {
Expand Down Expand Up @@ -238,6 +266,7 @@ const StudentController = {
{
user_id: 'user_id',
email: 'email',
name: 'name',
phone_number: 'phone_number',
mssv: 'mssv',
},
Expand All @@ -259,6 +288,7 @@ const StudentController = {
student_model.updateStudent(
{
email: HELPER.getDeleteString(result_student_detail[0]?.email),
name: HELPER.getDeleteString(result_student_detail[0]?.name),
phone_number: HELPER.getDeleteString(result_student_detail[0]?.phone_number),
mssv: HELPER.getDeleteString(result_student_detail[0]?.mssv),
isdeleted: CONSTANTS.DELETED_ENABLE,
Expand Down
1 change: 1 addition & 0 deletions backend-manager-student/src/share/configs/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ module.exports = {
INVALID_MUTILP_FIELD: 'Please provide non-empty values for all fields',
INVALID_UNAUTHORIZED: 'Unauthorized',
INVALID_RATING: 'Invalid Rating',
INVALID_ROLE: 'Invalid create role manager or student',

// ? EXITS
EXITS_EMAIL_PHONE: 'Email or Phone or Email or Mssv exits !',
Expand Down
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
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ const BorrowBookController = {
'*',
);
const refund_book =
data_borrow_book.length > 0 &&
data_borrow_book[0].status !== CONSTANTS.STATUS_BORROW.DONE &&
data_borrow_book[0].status;
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
Loading

0 comments on commit e48feac

Please sign in to comment.