import { Body, Controller, Get, Param, Patch, Post, Req, UseGuards, } from "@nestjs/common"; import { ApiBody, ApiAcceptedResponse, ApiOperation, ApiResponse, ApiTags, ApiBearerAuth, } from "@nestjs/swagger"; import { ActorAuthService } from "src/auth/auth-services/actor.auth.service"; import { ForgetPasswordSendCodeDto, ForgetPasswordVerifyCodeDto, } from "src/auth/dto/actor/forget-password.actor.dto"; import { LoginActorDto } from "src/auth/dto/actor/login.actor.dto"; import { ActorEditUserProfileDto } from "src/auth/dto/actor/profile.actor.dto"; import { CreateFieldExpertDto } from "src/auth/dto/actor/create-field-expert.actor.dto"; import { CreateRegistrarDto } from "src/auth/dto/actor/create-registrar.actor.dto"; import { GenuineRegisterDto, InsurerRegisterDto, LegalRegisterDto, } from "src/auth/dto/actor/register.actor.dto"; import { LocalActorAuthGuard } from "src/auth/guards/actor-local.guard"; import { ClientKey } from "src/decorators/clientKey.decorator"; import { Roles } from "src/decorators/roles.decorator"; import { CurrentUser } from "src/decorators/user.decorator"; @Controller("actor") @ApiTags("actor") export class ActorAuthController { constructor(private readonly actorAuthService: ActorAuthService) {} /** * @deprecated Use the unified actor onboarding flow instead. This endpoint * will be removed in a future release. */ @Post("register/genuine") @ApiOperation({ deprecated: true, summary: "[DEPRECATED] Genuine actor registration", description: "Deprecated — kept only for legacy clients. Use the unified actor onboarding flow instead. Will be removed.", }) @ApiBody({ type: GenuineRegisterDto }) async registerGenuine(@Body() body: GenuineRegisterDto) { return await this.actorAuthService.genuineRegister(body); } /** * @deprecated Use the unified actor onboarding flow instead. This endpoint * will be removed in a future release. */ @Post("register/legal") @ApiOperation({ deprecated: true, summary: "[DEPRECATED] Legal actor registration", description: "Deprecated — kept only for legacy clients. Use the unified actor onboarding flow instead. Will be removed.", }) @ApiBody({ type: LegalRegisterDto }) async registerLegal(@Body() body: LegalRegisterDto) { return await this.actorAuthService.legalRegister(body); } @Post("register/insurer") @ApiBody({ type: InsurerRegisterDto }) async registerInsurer(@Body() body: InsurerRegisterDto) { return await this.actorAuthService.insurerRegister(body); } /** Mock: create a field expert for testing. Make private later. */ @Post("create-field-expert") @ApiBody({ type: CreateFieldExpertDto }) @ApiAcceptedResponse() async createFieldExpert(@Body() body: CreateFieldExpertDto) { return await this.actorAuthService.createFieldExpertMock(body); } /** Mock: create a registrar for testing. Make private later. */ @Post("create-registrar") @ApiBody({ type: CreateRegistrarDto }) @ApiAcceptedResponse() async createRegistrar(@Body() body: CreateRegistrarDto) { return await this.actorAuthService.createRegistrarMock(body); } @UseGuards(LocalActorAuthGuard) @Post("login") @Roles() @ApiOperation({ summary: "Actor login (returns access + refresh tokens)", description: "Authenticate any non-end-user actor (insurer/company, blame expert, damage expert, registrar, field expert, admin). Submit `role` as an array — e.g. `[\"damage_expert\"]` — together with the actor's email/`username` and password. On success the response contains the JWT pair and the resolved profile.", }) @ApiBody({ type: LoginActorDto, description: "Login payload. Pick one of the swagger examples below to see the exact body shape per role.", examples: { company: { summary: "Insurer / company portal", description: "Sample tenant credentials for the insurer (company) panel.", value: { role: "company", username: "saman_insurer@gmail.com", password: "123321", }, }, expert: { summary: "Blame expert panel", description: "Sample credentials for a blame (`expert`) account.", value: { role: "expert", username: "blame@gmail.com", password: "123321", }, }, damage_expert: { summary: "Damage expert (claim) panel", description: "Sample credentials for a damage-expert account.", value: { role: "damage_expert", username: "claim@gmail.com", password: "123321", }, }, }, }) @ApiAcceptedResponse() async login(@Body() body, @Req() req, @ClientKey() client) { return await this.actorAuthService.loginActors(req.user); } @Post("forget-password") @ApiBody({ type: ForgetPasswordSendCodeDto, description: "send otp when call this api", }) @ApiAcceptedResponse() @ApiResponse({ type: ForgetPasswordSendCodeDto }) async forgetPassword(@Body() body: ForgetPasswordSendCodeDto) { return await this.actorAuthService.forgetPasswordSendMail(body.email); } @Post("forget-password-verify") @ApiBody({ type: ForgetPasswordVerifyCodeDto, description: "send otp when call this api", }) @ApiAcceptedResponse() @ApiResponse({ type: ForgetPasswordVerifyCodeDto }) async forgetPasswordVerify(@Body() body: ForgetPasswordVerifyCodeDto) { const { email, otp, newPassword } = body; return await this.actorAuthService.forgetPasswordVerify( email, otp, newPassword, ); } @Get("register/form/states/list") async getStates() { return await this.actorAuthService.getStates(); } @Get("register/form/cities/list/:stateId") async getCities(@Param("stateId") stateId: number) { return await this.actorAuthService.getCities(stateId); } @Get("/profile") @UseGuards(LocalActorAuthGuard) @ApiBearerAuth() async getProfile(@CurrentUser() user) { return await this.actorAuthService.getProfiles(user); } @Patch("/profile") @UseGuards(LocalActorAuthGuard) @ApiBearerAuth() async editProfile( @CurrentUser() user, @Body() update: ActorEditUserProfileDto, ) { return await this.actorAuthService.modifyProfile(user, update); } }