import { MigrationInterface, QueryRunner } from 'typeorm'
import { getRepository } from 'typeorm'
import { Domain } from '@things-factory/shell'
import { Group, Board } from '@things-factory/board-service'

const SEED_BOARD = []

export class Board1558006512362 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<any> {
    const repository = getRepository(Board)
    const domainRepository = getRepository(Domain)
    const domain = await domainRepository.findOne({ subdomain: 'system' })

    const groupRepository = getRepository(Group)
    const group = await groupRepository.findOne({ name: 'SAMPLE' })

    return await Promise.all(
      SEED_BOARD.map(async board => {
        await repository.save({
          ...board,
          domain,
          group
        })
      })
    )
  }

  public async down(queryRunner: QueryRunner): Promise<any> {
    const repository = getRepository(Board)

    return await Promise.all(
      SEED_BOARD.reverse().map(async board => {
        let record = await repository.findOne({ name: board.name })
        await repository.remove(record)
      })
    )
  }
}
