Skip to content

Commit

Permalink
#220 Performance code and clearn code
Browse files Browse the repository at this point in the history
  • Loading branch information
fdhhhdjd committed Mar 23, 2023
1 parent 7657f64 commit f7513e3
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 111 deletions.
16 changes: 10 additions & 6 deletions server-send-email-student/src/send_email_user/app.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
//! Library
//! LIBRARY
const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
const bodyParser = require('body-parser');
const compression = require('compression');

//! used library
//! SHARE
const CONFIGS = require('../share/configs/config');
const CONSTANTS = require('../share/configs/constants');

//! USED LIBRARY
const app = express();
if (process.env.NODE_ENV === 'PRODUCTION') {
if (CONFIGS.NODE_ENV === CONSTANTS.ENVIRONMENT_PRODUCT) {
app.enable('trust proxy');
}
app.use(helmet());
Expand All @@ -21,8 +25,8 @@ app.use(
);
app.use(
compression({
level: 6,
threshold: 100 * 1000,
level: CONSTANTS.LEVEL,
threshold: CONSTANTS.THRESHOLD,
filter: (req, res) => {
if (req.headers['x-no-compression']) {
return false;
Expand All @@ -33,6 +37,6 @@ app.use(
);

//! Redis PubSub users
require('./v1/redis_sub_queue')
require('./v1/redis_sub_queue');

module.exports = app;
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const { returnReasons } = require('../../../../../share/middlewares/middleware');

const emailSenderUsers = {
sendEmailUser: async (message) => {
let message_queue = message
return console.log('OKKKKKKKK', message_queue)
}
}
const message_queue = message;
return console.log('OKKKKKKKK', message_queue);
},
};
module.exports = emailSenderUsers;
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
//! LIBARY
const retry = require('async-retry');
const path = require('path')
const path = require('path');

//! SERVICE
const send_email = require('../../../../../share/configs/email_template');

//! SHARE
const CONSTANTS = require('../../../../../share/configs/constants')
const CONFIGS = require('../../../../../share/configs/config')
const CONSTANTS = require('../../../../../share/configs/constants');
const CONFIGS = require('../../../../../share/configs/config');
const TEMPLATE_EMAIL = require('../../../../../share/configs/template_email');
const HELPERS = require('../../../../../share/utils/helpers');

const emailSenderUsers = {
/**
Expand All @@ -18,13 +20,13 @@ const emailSenderUsers = {
* @return { Object }
*/
sendEmailUserWarningHacker: async (message_sub) => {
const type = CONSTANTS.TYPE_STUDENT
const message = message_sub.data_renew_token
const type = CONSTANTS.TYPE_STUDENT;
const message = message_sub.data_renew_token;
const option = {
from: CONFIGS.SMTP_MAIL,
to: message.email,
subject: `Verify Your Email`,
template: 'warning_account_by_hacker',
subject: TEMPLATE_EMAIL.STUDENT_WARNING_HACKER.SUBJECT,
template: TEMPLATE_EMAIL.STUDENT_WARNING_HACKER.TEMPLATE,
attachments: [
{
filename: 'logo.png',
Expand All @@ -35,32 +37,33 @@ const emailSenderUsers = {
context: {
data: message,
},
}
};
try {
await send_email(option, type)
console.info('Send_Email Success::::::', message.email)
await send_email(option, type);
console.info('Send_Email Success::::::', message.email);
} catch (error) {
console.error('Lỗi khi gửi email:', error.message);
await retry(send_email.bind(null, option), { retries: CONSTANTS.NUMBER_RETRY_EMAIL });
}
}
/**
}, /**
* @author Nguyễn Tiến Tài
* @created_at 23/02/2023
* @description Send Email reset Password
* @function sendEmailUserLinkResetPassword
* @return { Object }
*/,
*/
sendEmailUserLinkResetPassword: async (message_sub) => {
const type = CONSTANTS.TYPE_STUDENT
const message = message_sub.data_reset
message.support_email = CONFIGS.SMTP_MAIL
console.log(message_sub, '----')
const type = CONSTANTS.TYPE_STUDENT;
const message = message_sub.data_reset;
message.support_email = CONFIGS.SMTP_MAIL;
const name_subject = HELPERS.getURIFromTemplate(TEMPLATE_EMAIL.STUDENT_RESET_PASSWORD.SUBJECT, {
name: message.name,
});
const option = {
from: CONFIGS.SMTP_MAIL,
to: message.email,
subject: `Link Reset Password ${message.name}`,
template: 'link_reset_password_student',
subject: name_subject,
template: TEMPLATE_EMAIL.STUDENT_RESET_PASSWORD.TEMPLATE,
attachments: [
{
filename: 'logo.png',
Expand All @@ -71,10 +74,10 @@ const emailSenderUsers = {
context: {
data: message,
},
}
};
try {
await send_email(option, type)
console.info('Send_Email Success::::::', message)
await send_email(option, type);
console.info('Send_Email Success::::::', message);
} catch (error) {
console.error('Lỗi khi gửi email:', error.message);
await retry(send_email.bind(null, option), { retries: CONSTANTS.NUMBER_RETRY_EMAIL });
Expand All @@ -83,19 +86,22 @@ const emailSenderUsers = {
/**
* @author Nguyễn Tiến Tài
* @created_at 27/02/2023
* @description Send Email verification
* @description Send Email verification
* @function sendEmailUserLinkVerification
* @return { Object }
*/
sendEmailUserLinkVerification: async (message_sub) => {
const type = CONSTANTS.TYPE_STUDENT
const message = message_sub
message.support_email = CONFIGS.SMTP_MAIL
const type = CONSTANTS.TYPE_STUDENT;
const message = message_sub;
message.support_email = CONFIGS.SMTP_MAIL;
const name_subject = HELPERS.getURIFromTemplate(TEMPLATE_EMAIL.STUDENT_VERIFICATION_EMAIL.SUBJECT, {
name: message.name,
});
const option = {
from: CONFIGS.SMTP_MAIL,
to: message.email,
subject: `Verification Email ${message.name}`,
template: 'link_verification_email_student',
subject: name_subject,
template: TEMPLATE_EMAIL.STUDENT_RESET_PASSWORD.TEMPLATE,
attachments: [
{
filename: 'logo.png',
Expand All @@ -106,14 +112,14 @@ const emailSenderUsers = {
context: {
data: message,
},
}
};
try {
await send_email(option, type)
console.info('Send_Email Success::::::', message)
await send_email(option, type);
console.info('Send_Email Success::::::', message);
} catch (error) {
console.error('Lỗi khi gửi email:', error.message);
await retry(send_email.bind(null, option), { retries: CONSTANTS.NUMBER_RETRY_EMAIL });
}
}
}
},
};
module.exports = emailSenderUsers;
7 changes: 7 additions & 0 deletions server-send-email-student/src/share/configs/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@ module.exports = {
REDIS_HOST: process.env.REDIS_HOST,
REDIS_USER: process.env.REDIS_USER,
REDIS_PASSWORD: process.env.REDIS_PASSWORD,

/**
* @author Nguyễn Tiến Tài
* @created_at 23/03/2023
* @description ENVIRONMENT
*/
NODE_ENV: process.env.NODE_ENV,
};
77 changes: 13 additions & 64 deletions server-send-email-student/src/share/configs/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,70 +94,6 @@ module.exports = {
return result;
},

/**
* @author Nguyễn Tiến Tài
* @created_at 23/03/2023
* @description All status code
*/
HTTP: {
// 1xx
STATUS_CONTINUE: 100,
STATUS_SWITCHING_PROTOCOLS: 101,
STATUS_PROCESSING: 102,
STATUS_EARLY_HINTS: 103,
// 2xx
STATUS_OK: 200,
STATUS_CREATED: 201,
STATUS_ACCEPTED: 202,
STATUS_NON_AUTHORITATIVE_INFORMATION: 203,
STATUS_NO_CONTENT: 204,
STATUS_RESET_CONTENT: 205,
STATUS_PARTIAL_CONTENT: 206,
STATUS_MULTI_STATUS: 207,
STATUS_ALREADY_REPORTED: 208,
STATUS_IM_USED: 226,
// 3xx
STATUS_MULTIPLE_CHOICES: 300,
STATUS_MOVED_PERMANENTLY: 301,
STATUS_FOUND: 302,
STATUS_SEE_OTHER: 303,
STATUS_NOT_MODIFIED: 304,
STATUS_USE_PROXY: 305,
STATUS_TEMPORARY_REDIRECT: 307,
STATUS_PERMANENT_REDIRECT: 308,
// 4xx
STATUS_BAD_REQUEST: 400,
STATUS_UNAUTHORIZED: 401,
STATUS_PAYMENT_REQUIRED: 402,
STATUS_FORBIDDEN: 403,
STATUS_NOT_FOUND: 404,
STATUS_METHOD_NOT_ALLOWED: 405,
STATUS_NOT_ACCEPTABLE: 406,
STATUS_PROXY_AUTHENTICATION_REQUIRED: 407,
STATUS_REQUEST_TIMEOUT: 408,
STATUS_CONFLICT: 409,
STATUS_GONE: 410,
STATUS_LENGTH_REQUIRED: 411,
STATUS_PRECONDITION_FAILED: 412,
STATUS_PAYLOAD_TOO_LARGE: 413,
STATUS_TOO_MANY_REQUESTS: 429,
STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE: 431,
STATUS_UNAVAILABLE_FOR_LEGAL_REASONS: 451,
// 5xx
STATUS_INTERNAL_SERVER_ERROR: 500,
STATUS_NOT_IMPLEMENTED: 501,
STATUS_BAD_GATEWAY: 502,
STATUS_SERVICE_UNAVAILABLE: 503,
STATUS_GATEWAY_TIMEOUT: 504,
STATUS_HTTP_VERSION_NOT_SUPPORTED: 505,
STATUS_VARIANT_ALSO_NEGOTIATES: 506,
STATUS_INSUFFICIENT_STORAGE: 507,
STATUS_LOOP_DETECTED: 508,
STATUS_BANDWIDTH_LIMIT_EXCEEDED: 509,
STATUS_NOT_EXTENDED: 510,
STATUS_NETWORK_AUTHENTICATION_REQUIRED: 511,
},

/**
* @author Nguyễn Tiến Tài
* @created_at 19/02/2023
Expand Down Expand Up @@ -190,5 +126,18 @@ module.exports = {
STUDENT_FOLDER: 'user_views',
ADMIN_FOLDER: 'admin_views',
PATH_FOLDER_U_A: './src/share/views/${folder_u_a}',
/**
* @author Nguyễn Tiến Tài
* @created_at 23/03/2023
* @description environment
*/
ENVIRONMENT_DEV: 'DEVELOPER',
/**
* @author Nguyễn Tiến Tài
* @created_at 23/03/2023
* @description environment
*/
THRESHOLD: 100 * 1000,
LEVEL: 6,

};
19 changes: 19 additions & 0 deletions server-send-email-student/src/share/configs/template_email.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @author Nguyễn Tiến Tài
* @created_at 23/02/2022
* @description Message template email
*/
module.exports = {
STUDENT_WARNING_HACKER: {
SUBJECT: 'Verify Your Email',
TEMPLATE: 'Warning_account_by_hacker',
},
STUDENT_RESET_PASSWORD: {
SUBJECT: 'Verify Your ELink Reset Password ${name}',
TEMPLATE: 'link_reset_password_student',
},
STUDENT_VERIFICATION_EMAIL: {
SUBJECT: 'Verification Email ${name}',
TEMPLATE: 'link_verification_email_student',
},
};
6 changes: 4 additions & 2 deletions server-send-email-student/src/share/utils/helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const REGEX = require('../configs/regex')
//! SHARE
const REGEX = require('../configs/regex');

module.exports = {
/**
* @author Nguyễn Tiến Tài
Expand All @@ -10,4 +12,4 @@ module.exports = {
getURIFromTemplate(template, data) {
return template.replace(REGEX.REGEX_IS_STRING_PARAM, (_, key) => data[key]);
},
}
};

0 comments on commit f7513e3

Please sign in to comment.