Skip to content

Commit

Permalink
Merge pull request #160 from fdhhhdjd/developer
Browse files Browse the repository at this point in the history
#159 [Cront - Backend] Run Cron API Student
  • Loading branch information
fdhhhdjd authored Feb 28, 2023
2 parents d9316d3 + 5079f4e commit d6a9ad2
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 24 deletions.
20 changes: 15 additions & 5 deletions backend-manager-student/src/cron_job/cron.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
const cron = require('node-cron');
const cronCtrl = require('./v1/controllers/cron.controller');
//! SHARE
const CONSTANTS = require('../share/configs/constants');

//! LIBRARY
const cron = require('node-cron');

//! MODEL
const cron_verification_student = require('./v1/cron_student/controllers/cron_verification.controller');
const cron_reset_password_student = require('./v1/cron_student/controllers/cron_reset_password.controller');

/**
* @author Nguyễn Tiến Tài
* @created_at 16/12/2022
* @description Run all Cron
* @updated_at 28/02/2022
* @description Run Cron delete verification
*/

cron.schedule(CONSTANTS._5_SECONDS_CRON, () => {
cronCtrl.cron_demo();
// Delete verification
cron_verification_student.cron_deleted_verification_student();

// Delete reset password
cron_reset_password_student.cron_deleted_reset_password_student();
});
console.info('Server cron running !!!');

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//! Share
const HELPER = require('../../../../share/utils/helper');
const CONSTANTS = require('../../../../share/configs/constants');

//! Model
const user_reset_password_model = require('../../../../share/models/user_reset_password.model');

module.exports = {
/**
* @author Nguyễn Tiến Tài
* @created_at 28/02/2022
* @description cron_deleted_reset_password_student
* @function cron_deleted_reset_password_student
*/
async cron_deleted_reset_password_student() {
try {
// Get all table reset_password
const resetPasswordList = await user_reset_password_model.getResetPasswordList();

// Verification exit data
if (resetPasswordList.length > 0) {
// Repeat every data
for (const resetPassword of resetPasswordList) {
// Check data is expired
const isExpired = HELPER.isExpired(resetPassword.reset_password_expire);

// If expired delete
if (isExpired !== CONSTANTS.DELETED_DISABLE) {
await user_reset_password_model.updateResetPassword(
{ isdeleted: CONSTANTS.DELETED_ENABLE },
{ id: resetPassword.id },
{ id: 'id' },
);
}
}

console.info('CRON DELETE RESET PASSWORD DONE !');
} else {
console.error('NO LIST RESET PASSWORD!!!');
}
} catch (error) {
console.error('UPDATE FAIL!', error);
}
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//! Share
const HELPER = require('../../../../share/utils/helper');
const CONSTANTS = require('../../../../share/configs/constants');

//! Model
const user_verification_model = require('../../../../share/models/user_verification.model');

module.exports = {
/**
* @author Nguyễn Tiến Tài
* @created_at 16/12/2022
* @updated_at 28/02/2022
* @description cron_deleted_verification_student
* @function cron_deleted_verification_student
*/
async cron_deleted_verification_student() {
try {
// Get all table verification
const verificationList = await user_verification_model.getVerificationList();

// Verification exit data
if (verificationList.length > 0) {
// Repeat every data
for (const verification of verificationList) {
// Check data is expired
const isExpired = HELPER.isExpired(verification.linkEmailExpire);

// If expired delete
if (isExpired !== CONSTANTS.DELETED_DISABLE) {
await user_verification_model.updateVerification(
{ isdeleted: CONSTANTS.DELETED_ENABLE },
{ verify_id: verification.verify_id },
{ verify_id: 'verify_id' },
);
}
}

console.info('CRON DELETE VERIFICATION DONE !');
} else {
console.error('NO LIST VERIFICATION!!!');
}
} catch (error) {
console.error('UPDATE FAIL!', error);
}
},
};
1 change: 1 addition & 0 deletions backend-manager-student/src/share/configs/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
* @param { "Name":"value" => Example: Male: 1 }
*/
_30_SECONDS: 30 * 1000,
_10_SECONDS: 10 * 1000,
_1_MINUTES: 60 * 1000,
_5_MINUTES: 5 * 60 * 1000,
_10_MINUTES: 10 * 60 * 1000,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//! Share
const CONSTANTS = require('../configs/constants');

//! Database
const knex = require('../db/postgresql');

module.exports = {
Expand Down Expand Up @@ -42,4 +46,22 @@ module.exports = {
reject(error);
}
}),

/**
* @author Nguyễn Tiến Tài
* @created_at 28/02/2023
* @description List all reset password
*/
getResetPasswordList: async () =>
new Promise((resolve, reject) => {
knex('reset_password')
.where('isdeleted', CONSTANTS.DELETED_DISABLE)
.orderBy('created_at', 'asc')
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
}),
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//! Share
const CONSTANTS = require('../configs/constants');

//! Database
const knex = require('../db/postgresql');

module.exports = {
Expand Down Expand Up @@ -43,4 +47,23 @@ module.exports = {
reject(error);
}
}),
/**
* @author Nguyễn Tiến Tài
* @created_at 26/02/2023
* @description Update verification
*/
getVerificationList: async () =>
new Promise((resolve, reject) => {
knex('verification')
.where('isdeleted', CONSTANTS.DELETED_DISABLE)
.where('verified', CONSTANTS.DELETED_DISABLE)
.where('check_login', CONSTANTS.DELETED_DISABLE)
.orderBy('created_at', 'asc')
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
}),
};
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ module.exports = {
const data_insert = {
verify_id: ran_dom_unique_string,
user_id: id,
link_email_expire: Date.now() + CONSTANTS._10_MINUTES,
link_email_expire: Date.now() + CONSTANTS._10_SECONDS,
// link_email_expire: Date.now() + CONSTANTS._10_MINUTES,
};

// Insert database
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Share
const HELPER = require('../../../share/utils/helper');
const CONSTANTS = require('../../../share/configs/constants');
const PASSWORD = require('../../../share/utils/password');
Expand Down

0 comments on commit d6a9ad2

Please sign in to comment.