import { DataSource, LessThan } from 'typeorm';
import { JoinRequestEntity } from '../../../entities';
import { DeleteStaleJoinRequestsCommand } from '../../types';

export async function handleDeleteStaleJoinRequests(command: DeleteStaleJoinRequestsCommand, dataSource: DataSource): Promise<void> {
  const repo = dataSource.getRepository(JoinRequestEntity);
  const cutoff = Date.now() - command.payload.maxAgeMs;

  await repo.delete({ createdAt: LessThan(cutoff) });
}
