Passage en revue des contrôleurs de nettoyage base de données + json utilisateurs appelés via cronjob

This commit is contained in:
Fabrice PENHOËT 2020-09-22 17:54:23 +02:00
parent 9d53778409
commit ffcfaef2de
2 changed files with 17 additions and 8 deletions

View File

@ -32,5 +32,6 @@ module.exports =
timeDifferenceMax:840, timeDifferenceMax:840,
// JSON dir // JSON dir
dirCacheUsers : "datas/users", dirCacheUsers : "datas/users",
dirCacheUsersAnswers : "datas/users/questionnaires/answers" dirCacheUsersAnswers : "datas/users/questionnaires/answers",
dirCacheUsersWithoutAnswers : "datas/users/questionnaires/without"
}; };

View File

@ -4,6 +4,7 @@ const slugify = require('slugify');
const { Op, QueryTypes } = require("sequelize");// pour certaines requêtes sql const { Op, QueryTypes } = require("sequelize");// pour certaines requêtes sql
const config = require("../config/main.js"); const config = require("../config/main.js");
const configUsers = require("../config/users.js");
const configTpl = require("../views/"+config.theme+"/config/"+config.availableLangs[0]+".js"); const configTpl = require("../views/"+config.theme+"/config/"+config.availableLangs[0]+".js");
const tool = require("../tools/main"); const tool = require("../tools/main");
@ -959,6 +960,7 @@ exports.getStats= async (req, res, next) =>
} }
// CRONS // CRONS
// Supprime les fichiers stockant les erreurs de connexion ayant expirés
exports.deleteLoginFail = async (req, res, next) => exports.deleteLoginFail = async (req, res, next) =>
{ {
try try
@ -974,6 +976,7 @@ exports.deleteLoginFail = async (req, res, next) =>
} }
} }
// Suppression des fichiers pour des utilisateurs n'existant plus
exports.deleteJsonFiles= async (req, res, next) => exports.deleteJsonFiles= async (req, res, next) =>
{ {
try try
@ -982,10 +985,14 @@ exports.deleteJsonFiles= async (req, res, next) =>
const users=await db["User"].findAll({ attributes: ["id"] }); const users=await db["User"].findAll({ attributes: ["id"] });
let saveFiles=[]; let saveFiles=[];
for(let i in users) for(let i in users)
{
saveFiles.push(users[i].id+".json"); saveFiles.push(users[i].id+".json");
saveFiles.push("stats"+users[i].id+".json");
}
const deleteFiles = await Promise.all([ const deleteFiles = await Promise.all([
toolFile.deleteFilesInDirectory(config.dirCacheUsers, saveFiles), toolFile.deleteFilesInDirectory(configUsers.dirCacheUsers, saveFiles),
toolFile.deleteFilesInDirectory(config.dirCacheUsersQuestionnaires, saveFiles) toolFile.deleteFilesInDirectory(configUsers.dirCacheUsersWithoutAnswers, saveFiles),
toolFile.deleteFilesInDirectory(configUsers.dirCacheUsersAnswers, saveFiles)
]); ]);
res.status(200).json(deleteFiles); res.status(200).json(deleteFiles);
next(); next();
@ -996,6 +1003,7 @@ exports.deleteJsonFiles= async (req, res, next) =>
} }
} }
// Suppression des comptes n'ayant pas été validé passé un certain délai
exports.deleteUnvalided= async (req, res, next) => exports.deleteUnvalided= async (req, res, next) =>
{ {
try try
@ -1008,7 +1016,7 @@ exports.deleteUnvalided= async (req, res, next) =>
{ {
const [results, metadata] = await db.sequelize.query("DELETE FROM `Users` WHERE UNIX_TIMESTAMP(createdAt) < "+timeExpiration+" AND `id` NOT IN (SELECT `UserId` FROM `Subscriptions`)"); const [results, metadata] = await db.sequelize.query("DELETE FROM `Users` WHERE UNIX_TIMESTAMP(createdAt) < "+timeExpiration+" AND `id` NOT IN (SELECT `UserId` FROM `Subscriptions`)");
const now=new Date(); const now=new Date();
for(i in userUnvalided) for(let i in userUnvalided)
await db["UserDeleted"].create({ createdAt: userUnvalided[i].createdAt, deletedAt: now, wasValided: false }); await db["UserDeleted"].create({ createdAt: userUnvalided[i].createdAt, deletedAt: now, wasValided: false });
res.message=metadata.affectedRows+txt.cronDeleteUnvalidedUsersMessage; res.message=metadata.affectedRows+txt.cronDeleteUnvalidedUsersMessage;
} }
@ -1021,20 +1029,20 @@ exports.deleteUnvalided= async (req, res, next) =>
} }
} }
// Suppression des comptes dont l'abonnement a expiré et ne s'étant pas connecté depuis à certains temps
exports.deleteInactiveAccounts= async(req, res, next) => exports.deleteInactiveAccounts= async(req, res, next) =>
{ {
try try
{ {
const db = require("../models/index"); const db = require("../models/index");
// compte inactif = abonnement expiré + non connecté depuis un certains nombres de jours const usersInactive= await db.sequelize.query("SELECT createdAt FROM `Users` WHERE status='user' AND (ADDDATE(`connectedAt`, "+config.inactiveAccountTimeToDeleteInDays+") < NOW() OR `connectedAt` IS NULL) AND `id` IN (SELECT `UserId` FROM `Subscriptions` WHERE ADDDATE(`createdAt`, `numberOfDays`)< NOW())", { type: QueryTypes.SELECT });
const usersInactive= await db.sequelize.query("SELECT createdAt FROM `Users` WHERE status='user' and ADDDATE(`connectedAt`, "+config.inactiveAccountTimeToDeleteInDays+") < NOW() AND `id` IN (SELECT `UserId` FROM `Subscriptions` WHERE ADDDATE(`createdAt`, `numberOfDays`)< NOW())", { type: QueryTypes.SELECT });
if(usersInactive.length!==0) if(usersInactive.length!==0)
{ {
const [results, metadata] = await db.sequelize.query("DELETE FROM `Users` WHERE ADDDATE(`connectedAt`, "+config.inactiveAccountTimeToDeleteInDays+") < NOW() AND `id` IN (SELECT `UserId` FROM `Subscriptions` WHERE ADDDATE(`createdAt`, `numberOfDays`)< NOW())"); const [results, metadata] = await db.sequelize.query("DELETE FROM `Users` WHERE (ADDDATE(`connectedAt`, "+config.inactiveAccountTimeToDeleteInDays+") < NOW() OR `connectedAt` IS NULL) AND status='user' AND `id` IN (SELECT `UserId` FROM `Subscriptions` WHERE ADDDATE(`createdAt`, `numberOfDays`)< NOW())");
if(metadata.affectedRows!==0) if(metadata.affectedRows!==0)
res.message=metadata.affectedRows+txt.deleteInactiveUsersMessage; res.message=metadata.affectedRows+txt.deleteInactiveUsersMessage;
const now=new Date(); const now=new Date();
for(i in usersInactive) for(let i in usersInactive)
await db["UserDeleted"].create({ createdAt: usersInactive[i].createdAt, deletedAt: now, wasValided: true }); await db["UserDeleted"].create({ createdAt: usersInactive[i].createdAt, deletedAt: now, wasValided: true });
} }
res.status(200).json(true); res.status(200).json(true);