habits.andr33v.ru/server/tasks/cleanup.ts

28 lines
812 B
TypeScript

import prisma from '../utils/prisma';
/**
* Deletes anonymous users that were created more than 24 hours ago.
* This is designed to be run as a scheduled task to prevent accumulation
* of stale data from users who start the onboarding but never register.
*/
export async function cleanupAnonymousUsers() {
const twentyFourHoursAgo = new Date(Date.now() - 24 * 60 * 60 * 1000);
try {
const result = await prisma.user.deleteMany({
where: {
isAnonymous: true,
createdAt: {
lt: twentyFourHoursAgo, // lt = less than
},
},
});
console.log(`[CleanupTask] Successfully deleted ${result.count} anonymous users.`);
return result;
} catch (error) {
console.error('[CleanupTask] Error deleting anonymous users:', error);
throw error;
}
}