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

export async function handleDeletePluginData(command: DeletePluginDataCommand, dataSource: DataSource): Promise<void> {
  const { payload } = command;
  const ownerUserId = await getCurrentUserScope(dataSource);

  if (!ownerUserId) {
    return;
  }

  await dataSource.getRepository(PluginDataEntity).delete({
    key: payload.key,
    ownerUserId,
    pluginId: payload.pluginId,
    scope: payload.scope,
    serverId: payload.serverId ?? ''
  });
}
