import { MigrationInterface, QueryRunner } from 'typeorm'

import { User } from '@things-factory/auth-base'
import { getRepository } from '@things-factory/shell'

const SEED = [
  {
    email: 'admin@ai-doop.com'
  }
]

export class SeedUpdateUser1641348681524 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<any> {
    const repository = getRepository(User)
    // const domain: Domain = await getRepository(Domain).findOneBy({
    //   name: 'SYSTEM'
    // })
    return await Promise.all(
      SEED.map(async updateUser => {
        await repository.update({ email: 'admin@hatiolab.com' }, updateUser)
      })
    )
  }

  public async down(queryRunner: QueryRunner): Promise<any> {
    const repository = getRepository(User)
    // const domain: Domain = await getRepository(Domain).findOneBy({
    //   name: 'SYSTEM'
    // })

    return await Promise.all(
      SEED.reverse().map(async user => {
        await repository.update({ email: user.email }, { email: 'admin@hatiolab.com' })
      })
    )
  }
}
