Files
dsc-api/database/seeders/admin.seeder.ts
morteza 4ebc5e2cf3
Build and Deploy Docker Images / build_and_deploy (push) Has been cancelled
update: package file
2026-06-01 11:47:27 +03:30

37 lines
1.2 KiB
TypeScript
Executable File

import { Logger } from "@nestjs/common";
import { hash } from "bcryptjs";
import { DataSource, DeepPartial } from "typeorm";
import { Role } from "../../src/modules/users/entities/role.entity";
import { User } from "../../src/modules/users/entities/user.entity";
import { RoleEnum } from "../../src/modules/users/enums/role.enum";
const defaultAdmin: DeepPartial<User> = {
email: "admin-dsc@gmail.com",
phone: "09922320740",
userName: "admin-default",
password: "admin123",
firstName: "DSC",
lastName: "Admin",
nationalCode: "1234567890",
};
export const seedAdmin = async (dataSource: DataSource, logger: Logger) => {
try {
const roleRepo = dataSource.getRepository(Role);
const userRepo = dataSource.getRepository(User);
const adminRole = await roleRepo.findOneBy({ name: RoleEnum.SUPER_ADMIN });
if (!adminRole) throw new Error("Role not found");
const hashedPassword = await hash(defaultAdmin.password!, 10);
const admin = userRepo.create({ ...defaultAdmin, password: hashedPassword, roles: [adminRole] });
await userRepo.save(admin);
logger.log("admin created successfully");
} catch (error) {
logger.error("Error in seeding admin", error);
process.exit(1);
}
};