import { DataSource } from 'typeorm';
import { MessageEntity } from '../../../entities';
import { DeleteMessageCommand } from '../../types';
import { getCurrentUserScope } from '../../current-user-scope';

export async function handleDeleteMessage(command: DeleteMessageCommand, dataSource: DataSource): Promise<void> {
  const repo = dataSource.getRepository(MessageEntity);
  const currentUserId = await getCurrentUserScope(dataSource);

  if (!currentUserId) {
    return;
  }

  await repo.delete({ id: command.payload.messageId, ownerUserId: currentUserId });
}
