import { MigrationInterface, QueryRunner } from 'typeorm';

export class OwnerScopeUseAllMembers1780940000000 implements MigrationInterface {
  name = 'OwnerScopeUseAllMembers1780940000000';

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      UPDATE role_permissions rp
      INNER JOIN access_roles ar ON ar.id = rp.role_id
      SET rp.permission_value = 'all'
      WHERE ar.slug = 'owner'
        AND rp.permission_value = 'own_and_all'
        AND rp.permission_key IN (
          'dashboard.view',
          'members.view',
          'members.update',
          'members.delete',
          'attendance.view',
          'leave.view',
          'leave.create',
          'leave.update',
          'leave.delete',
          'reports.view',
          'calendar.view',
          'holidays.view',
          'holidays.create',
          'holidays.update',
          'holidays.delete',
          'notice.view',
          'notice.create',
          'notice.update',
          'notice.delete'
        )
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      UPDATE role_permissions rp
      INNER JOIN access_roles ar ON ar.id = rp.role_id
      SET rp.permission_value = 'own_and_all'
      WHERE ar.slug = 'owner'
        AND rp.permission_value = 'all'
        AND rp.permission_key IN (
          'dashboard.view',
          'members.view',
          'members.update',
          'members.delete',
          'attendance.view',
          'leave.view',
          'leave.create',
          'leave.update',
          'leave.delete',
          'reports.view',
          'calendar.view',
          'holidays.view',
          'holidays.create',
          'holidays.update',
          'holidays.delete',
          'notice.view',
          'notice.create',
          'notice.update',
          'notice.delete'
        )
    `);
  }
}
