import { BadRequestException, ConflictException, Injectable, NotFoundException, } from "@nestjs/common"; import { Types } from "mongoose"; import { HashService } from "src/utils/hash/hash.service"; import { ClientService } from "src/client/client.service"; import { ClientDto } from "src/client/dto/create-client.dto"; import { ActorAuthService } from "src/auth/auth-services/actor.auth.service"; import { InsurerRegisterDto, GenuineRegisterDto, LegalRegisterDto, } from "src/auth/dto/actor/register.actor.dto"; import { SuperAdminDbService } from "./entities/db-service/super-admin.db.service"; import { CreateSuperAdminDto, CreateFieldExpertAdminDto, CreateRegistrarAdminDto, CreateCallCenterAgentDto, } from "./dto/super-admin.dto"; import { RoleEnum } from "src/Types&Enums/role.enum"; import { FieldExpertDbService } from "src/users/entities/db-service/field-expert.db.service"; import { RegistrarDbService } from "src/users/entities/db-service/registrar.db.service"; import { CallCenterAgentDbService } from "src/users/entities/db-service/call-center-agent.db.service"; @Injectable() export class SuperAdminService { constructor( private readonly superAdminDbService: SuperAdminDbService, private readonly hashService: HashService, private readonly clientService: ClientService, private readonly actorAuthService: ActorAuthService, private readonly fieldExpertDbService: FieldExpertDbService, private readonly registrarDbService: RegistrarDbService, private readonly callCenterAgentDbService: CallCenterAgentDbService, ) {} /** List all super-admin accounts (sans password). */ async listSuperAdmins() { const admins = await this.superAdminDbService.findAll(); return admins.map(({ password: _pw, ...rest }) => rest); } /** Create an additional super-admin account. */ async createSuperAdmin(body: CreateSuperAdminDto) { const email = body.email.toLowerCase().trim(); const existing = await this.superAdminDbService.findOne({ email }); if (existing) { throw new ConflictException( "A super-admin with this email already exists.", ); } const password = await this.hashService.hash(body.password); const created = await this.superAdminDbService.create({ email, password, role: RoleEnum.SUPER_ADMIN, firstName: body.firstName, lastName: body.lastName, }); const { password: _pw, ...rest } = (created as any).toObject ? (created as any).toObject() : (created as any); return rest; } // ── Client management ──────────────────────────────────────────────────── async createClient(body: ClientDto) { return this.clientService.addClient(body); } async listClients() { return this.clientService.getClients(); } async listClientNames() { return this.clientService.getClientList(); } // ── Actor registration proxies ─────────────────────────────────────────── async registerInsurer(body: InsurerRegisterDto) { return this.actorAuthService.insurerRegister(body); } async registerGenuine(body: GenuineRegisterDto) { return this.actorAuthService.genuineRegister(body); } async registerLegal(body: LegalRegisterDto) { return this.actorAuthService.legalRegister(body); } async createFieldExpert(body: CreateFieldExpertAdminDto) { return this.actorAuthService.createFieldExpertMock(body); } async createRegistrar(body: CreateRegistrarAdminDto) { return this.actorAuthService.createRegistrarMock(body); } async createCallCenterAgent(body: CreateCallCenterAgentDto) { const email = body.email.toLowerCase().trim(); const existing = await this.callCenterAgentDbService.findOne({ email }); if (existing) { throw new ConflictException( "A call-center agent with this email already exists.", ); } const password = await this.hashService.hash(body.password); const created = await this.callCenterAgentDbService.create({ email, password, firstName: body.firstName, lastName: body.lastName, clientKey: new Types.ObjectId(body.clientId), mobile: body.mobile, role: RoleEnum.CALL_CENTER, }); const { password: _pw, ...rest } = (created as any).toObject ? (created as any).toObject() : (created as any); return rest; } }