Temp user updates and several bug fixes

This commit is contained in:
2025-08-03 16:08:11 -06:00
parent 2a53008e04
commit 2ebbb2dc44
19 changed files with 843 additions and 35 deletions

View File

@@ -0,0 +1,88 @@
const nocodbService = require('./nocodb');
const logger = require('../utils/logger');
const config = require('../config');
class AccountExpirationService {
constructor() {
this.checkInterval = null;
}
// Start the expiration check service
start() {
// Run check every hour
this.checkInterval = setInterval(() => {
this.checkExpiredAccounts();
}, 60 * 60 * 1000); // 1 hour
// Also run immediately on start
this.checkExpiredAccounts();
logger.info('Account expiration service started');
}
// Stop the service
stop() {
if (this.checkInterval) {
clearInterval(this.checkInterval);
this.checkInterval = null;
}
}
// Check and handle expired accounts
async checkExpiredAccounts() {
try {
// Get all users
const users = await nocodbService.getLoginRecords();
const now = new Date();
const expiredUsers = [];
for (const user of users) {
if (user.ExpiresAt && new Date(user.ExpiresAt) < now) {
expiredUsers.push(user);
}
}
// Delete expired accounts
for (const user of expiredUsers) {
await this.deleteExpiredAccount(user);
}
if (expiredUsers.length > 0) {
logger.info(`Deleted ${expiredUsers.length} expired temp accounts`);
}
} catch (error) {
logger.error('Error checking expired accounts:', error);
}
}
// Delete a single expired account
async deleteExpiredAccount(user) {
try {
await nocodbService.deleteRecord(
config.nocodb.loginSheetId,
user.Id
);
logger.info(`Deleted expired temp account: ${user.Email}`);
// Optional: Send notification email before deletion
// await emailService.sendAccountExpirationNotice(user.Email);
} catch (error) {
logger.error(`Failed to delete expired account ${user.Email}:`, error);
}
}
// Calculate expiration date based on admin settings
static calculateExpirationDate(expireDays) {
if (!expireDays || expireDays <= 0) return null;
const expirationDate = new Date();
expirationDate.setDate(expirationDate.getDate() + expireDays);
return expirationDate;
}
}
module.exports = new AccountExpirationService();