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,
// JSON dir
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 config = require("../config/main.js");
const configUsers = require("../config/users.js");
const configTpl = require("../views/"+config.theme+"/config/"+config.availableLangs[0]+".js");
const tool = require("../tools/main");
@ -959,6 +960,7 @@ exports.getStats= async (req, res, next) =>
}
// CRONS
// Supprime les fichiers stockant les erreurs de connexion ayant expirés
exports.deleteLoginFail = async (req, res, next) =>
{
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) =>
{
try
@ -982,10 +985,14 @@ exports.deleteJsonFiles= async (req, res, next) =>
const users=await db["User"].findAll({ attributes: ["id"] });
let saveFiles=[];
for(let i in users)
{
saveFiles.push(users[i].id+".json");
saveFiles.push("stats"+users[i].id+".json");
}
const deleteFiles = await Promise.all([
toolFile.deleteFilesInDirectory(config.dirCacheUsers, saveFiles),
toolFile.deleteFilesInDirectory(config.dirCacheUsersQuestionnaires, saveFiles)
toolFile.deleteFilesInDirectory(configUsers.dirCacheUsers, saveFiles),
toolFile.deleteFilesInDirectory(configUsers.dirCacheUsersWithoutAnswers, saveFiles),
toolFile.deleteFilesInDirectory(configUsers.dirCacheUsersAnswers, saveFiles)
]);
res.status(200).json(deleteFiles);
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) =>
{
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 now=new Date();
for(i in userUnvalided)
for(let i in userUnvalided)
await db["UserDeleted"].create({ createdAt: userUnvalided[i].createdAt, deletedAt: now, wasValided: false });
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) =>
{
try
{
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() 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() OR `connectedAt` IS NULL) AND `id` IN (SELECT `UserId` FROM `Subscriptions` WHERE ADDDATE(`createdAt`, `numberOfDays`)< NOW())", { type: QueryTypes.SELECT });
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)
res.message=metadata.affectedRows+txt.deleteInactiveUsersMessage;
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 });
}
res.status(200).json(true);