import { DataSource } from 'typeorm';
import { AuthUserEntity } from '../../../entities';
import { GetUserByUsernameQuery } from '../../types';
import { rowToAuthUser } from '../../mappers';

export async function handleGetUserByUsername(query: GetUserByUsernameQuery, dataSource: DataSource) {
  const repo = dataSource.getRepository(AuthUserEntity);
  const row = await repo.findOne({ where: { username: query.payload.username } });

  return row ? rowToAuthUser(row) : null;
}
