import { DataSource } from 'typeorm';
import { Term } from '../../../repositories/terms/entity/term.entity';
import { Account } from '../../../repositories/accounts/entity/account.entity';
import { User } from '../../../repositories/users/entity/user.entity';

export const createTermInfo = async (
  datasource: DataSource,
  documentType: string,
  version: string,
): Promise<void> => {
  const { identifiers } = await datasource.getRepository(Term).insert({
    document_type: documentType,
    version: version,
    created_by: 'test_runner',
    created_at: new Date(),
    updated_by: 'updater',
    updated_at: new Date(),
  });
  identifiers.pop() as Term;
};

export const updateAccountDelegationPermission = async (
  dataSource: DataSource,
  id: number,
  delegationPermission: boolean,
): Promise<void> => {
  await dataSource
    .getRepository(Account)
    .update({ id: id }, { delegation_permission: delegationPermission });
};

export const deleteAccount = async (
  dataSource: DataSource,
  id: number,
): Promise<void> => {
  await dataSource.getRepository(User).delete({ account_id: id });
  await dataSource.getRepository(Account).delete({ id: id });
};
