import { DataSource } from 'typeorm';
import { JoinRequestEntity } from '../../../entities';
import { GetPendingRequestsForServerQuery } from '../../types';
import { rowToJoinRequest } from '../../mappers';

export async function handleGetPendingRequestsForServer(query: GetPendingRequestsForServerQuery, dataSource: DataSource) {
  const repo = dataSource.getRepository(JoinRequestEntity);
  const rows = await repo.find({ where: { serverId: query.payload.serverId, status: 'pending' } });

  return rows.map(rowToJoinRequest);
}
