import { DataSource } from 'typeorm';
import { AuthUserEntity } from '../../../entities';
import { RegisterUserCommand } from '../../types';

export async function handleRegisterUser(command: RegisterUserCommand, dataSource: DataSource): Promise<void> {
  const repo = dataSource.getRepository(AuthUserEntity);
  const { user } = command.payload;
  const entity = repo.create({
    id: user.id,
    username: user.username,
    passwordHash: user.passwordHash,
    displayName: user.displayName,
    createdAt: user.createdAt
  });

  await repo.save(entity);
}
