Commit 6d8db2c7 authored by Diego Iván's avatar Diego Iván
Browse files

insertando superadmin y admin cuando se inicia la app

parent fa459a01
Loading
Loading
Loading
Loading
+55 −0
Original line number Diff line number Diff line
@@ -4,12 +4,17 @@ import { State } from 'src/state/entities/state.entity';
import { StateService } from 'src/state/state.service';
import { Repository } from 'typeorm';
import * as data from './states.json';
import { CreateAdminDto } from 'src/admin/dto/create-admin.dto';
import { ADMIN_ROLE } from 'src/shared/enum/admin-role.enum';
import { UserStatus } from 'src/shared/enum/user-status.enum';
import { AuthAdminService } from 'src/auth/admin/authAdminservice';

@Injectable()
export class DatabaseSeederService implements OnModuleInit {
  constructor(
    @InjectRepository(State) private stateRepo: Repository<State>,
    private readonly stateService: StateService,
    private readonly authAdminService: AuthAdminService,
  ) {}

  async insertStates() {
@@ -19,7 +24,57 @@ export class DatabaseSeederService implements OnModuleInit {
    }
  }

  async insertSuperAdmin() {
    const createSuperAdmin: CreateAdminDto = {
      email: 'superadmin@gmail.com',
      password: '123',
      name: 'Super Admin',
      lastName: 'super',
      role: ADMIN_ROLE.SUPERADMIN,
      status: UserStatus.ACTIVE,
    };
    const createAdmin: CreateAdminDto = {
      email: 'admin@gmail.com',
      password: '123',
      name: 'Admin',
      lastName: 'admin',
      role: ADMIN_ROLE.ADMIN,
      status: UserStatus.ACTIVE,
    };
    let tokenSuper = '',
      tokenAdmin = '';
    try {
      tokenSuper = await this.authAdminService.signUp({ ...createSuperAdmin });
    } catch (error) {
      tokenSuper = (
        await this.authAdminService.signIn({
          email: createSuperAdmin.email,
          password: createSuperAdmin.password,
        })
      ).token;
    }
    try {
      tokenAdmin = await this.authAdminService.signUp({ ...createAdmin });
    } catch (error) {
      tokenAdmin = (
        await this.authAdminService.signIn({
          email: createAdmin.email,
          password: createSuperAdmin.password,
        })
      ).token;
    }
    console.log(
      `Super Admin created with email: ${createSuperAdmin.email}
      , password: ${createSuperAdmin.password}, and token: ${tokenSuper}`,
    );
    console.log(
      `Admin created with email: ${createAdmin.email}, 
      password: ${createAdmin.password}, and token: ${tokenAdmin}`,
    );
  }

  async onModuleInit() {
    await this.insertStates();
    await this.insertSuperAdmin();
  }
}