forked from Yara724/api
Merge branch 'main' into integrate-my-work
This commit is contained in:
@@ -22,6 +22,7 @@ import {
|
||||
} 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 {
|
||||
GenuineRegisterDto,
|
||||
InsurerRegisterDto,
|
||||
@@ -55,6 +56,14 @@ export class ActorAuthController {
|
||||
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);
|
||||
}
|
||||
|
||||
@UseGuards(LocalActorAuthGuard)
|
||||
@Post("login")
|
||||
@Roles()
|
||||
|
||||
@@ -24,6 +24,7 @@ import { UserType } from "src/Types&Enums/userType.enum";
|
||||
import { InsurerExpertDbService } from "src/users/entities/db-service/insurer-expert.db.service";
|
||||
import { DamageExpertDbService } from "src/users/entities/db-service/damage-expert.db.service";
|
||||
import { ExpertDbService } from "src/users/entities/db-service/expert.db.service";
|
||||
import { FieldExpertDbService } from "src/users/entities/db-service/field-expert.db.service";
|
||||
import { HashService } from "src/utils/hash/hash.service";
|
||||
// import { MailService } from "src/utils/mail/mail.service";
|
||||
import { OtpService } from "src/utils/otp/otp.service";
|
||||
@@ -44,6 +45,7 @@ export class ActorAuthService {
|
||||
private readonly hashService: HashService,
|
||||
private readonly expertDbService: ExpertDbService,
|
||||
private readonly damageExpertDbService: DamageExpertDbService,
|
||||
private readonly fieldExpertDbService: FieldExpertDbService,
|
||||
private readonly insurerExpertDbService: InsurerExpertDbService,
|
||||
// private readonly mailService: MailService, // Mailer disabled – not used
|
||||
private readonly clientDbService: ClientDbService,
|
||||
@@ -69,6 +71,14 @@ export class ActorAuthService {
|
||||
else
|
||||
res = await this.damageExpertDbService.findOne({ email: username });
|
||||
break;
|
||||
case RoleEnum.FIELD_EXPERT:
|
||||
if (username == null && userId)
|
||||
res = await this.fieldExpertDbService.findOne({
|
||||
_id: new Types.ObjectId(userId),
|
||||
});
|
||||
else
|
||||
res = await this.fieldExpertDbService.findOne({ email: username });
|
||||
break;
|
||||
case RoleEnum.COMPANY:
|
||||
res = await this.insurerExpertDbService.findOne({ email: username });
|
||||
break;
|
||||
@@ -259,6 +269,11 @@ export class ActorAuthService {
|
||||
dbServiceToUpdate = this.insurerExpertDbService as any;
|
||||
}
|
||||
|
||||
if (!actor) {
|
||||
actor = await this.fieldExpertDbService.findOne(filter);
|
||||
dbServiceToUpdate = this.fieldExpertDbService as any;
|
||||
}
|
||||
|
||||
if (!actor) {
|
||||
throw new NotFoundException("actor not found");
|
||||
}
|
||||
@@ -280,18 +295,29 @@ export class ActorAuthService {
|
||||
}
|
||||
|
||||
async forgetPasswordVerify(email, otp, newPassword) {
|
||||
const userExist = await this.expertDbService.findOne({ email });
|
||||
let userExist: any = await this.expertDbService.findOne({ email });
|
||||
let dbServiceToUpdate: any = this.expertDbService;
|
||||
if (!userExist) {
|
||||
userExist = await this.damageExpertDbService.findOne({ email });
|
||||
dbServiceToUpdate = this.damageExpertDbService;
|
||||
}
|
||||
if (!userExist) {
|
||||
userExist = await this.insurerExpertDbService.findOne({ email });
|
||||
dbServiceToUpdate = this.insurerExpertDbService;
|
||||
}
|
||||
if (!userExist) {
|
||||
userExist = await this.fieldExpertDbService.findOne({ email });
|
||||
dbServiceToUpdate = this.fieldExpertDbService;
|
||||
}
|
||||
if (!userExist) throw new NotFoundException("user not found");
|
||||
const decodeOtp = await this.hashService.compare(otp, userExist.otp);
|
||||
if (!decodeOtp) throw new UnauthorizedException("otp invalid");
|
||||
if (decodeOtp) {
|
||||
const hashNewPassword = await this.hashService.hash(newPassword);
|
||||
await this.expertDbService.findOneAndUpdate(
|
||||
{ email },
|
||||
{ password: hashNewPassword },
|
||||
);
|
||||
return new ForgetPasswordVerifyCodeDtoRs(userExist, "update password");
|
||||
}
|
||||
const hashNewPassword = await this.hashService.hash(newPassword);
|
||||
await dbServiceToUpdate.findOneAndUpdate(
|
||||
{ email },
|
||||
{ password: hashNewPassword },
|
||||
);
|
||||
return new ForgetPasswordVerifyCodeDtoRs(userExist, "update password");
|
||||
}
|
||||
|
||||
async getProfiles(currentUser) {
|
||||
@@ -349,7 +375,14 @@ export class ActorAuthService {
|
||||
"city",
|
||||
"state",
|
||||
"address",
|
||||
]
|
||||
],
|
||||
field_expert: [
|
||||
"firstName",
|
||||
"lastName",
|
||||
"email",
|
||||
"phone",
|
||||
"mobile",
|
||||
],
|
||||
};
|
||||
|
||||
const allowedFields = allowedFieldsByRole[role];
|
||||
@@ -409,6 +442,13 @@ export class ActorAuthService {
|
||||
);
|
||||
break;
|
||||
|
||||
case "field_expert":
|
||||
await this.fieldExpertDbService.updateOne(
|
||||
{ _id: new Types.ObjectId(userId) },
|
||||
{ $set: sanitized },
|
||||
);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new ForbiddenException("Unsupported role for update");
|
||||
}
|
||||
@@ -419,4 +459,43 @@ export class ActorAuthService {
|
||||
throw new InternalServerErrorException("Failed to update profile");
|
||||
}
|
||||
}
|
||||
|
||||
/** Mock endpoint: create a field expert for testing. Make private later. */
|
||||
async createFieldExpertMock(body: {
|
||||
email: string;
|
||||
password: string;
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
mobile: string;
|
||||
phone?: string;
|
||||
}) {
|
||||
const hashPassword = await this.hashService.hash(body.password);
|
||||
const payload = {
|
||||
email: body.email.toLowerCase().trim(),
|
||||
password: hashPassword,
|
||||
firstName: body.firstName,
|
||||
lastName: body.lastName,
|
||||
mobile: body.mobile,
|
||||
phone: body.phone,
|
||||
role: RoleEnum.FIELD_EXPERT,
|
||||
};
|
||||
try {
|
||||
const created = await this.fieldExpertDbService.create(payload);
|
||||
return {
|
||||
id: created._id,
|
||||
email: created.email,
|
||||
firstName: created.firstName,
|
||||
lastName: created.lastName,
|
||||
mobile: created.mobile,
|
||||
role: created.role,
|
||||
};
|
||||
} catch (er) {
|
||||
if (er.code === 11000) {
|
||||
throw new BadRequestException(
|
||||
"A field expert with this email already exists.",
|
||||
);
|
||||
}
|
||||
throw er;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
30
src/auth/dto/actor/create-field-expert.actor.dto.ts
Normal file
30
src/auth/dto/actor/create-field-expert.actor.dto.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { IsEmail, IsOptional, IsString, MinLength } from "class-validator";
|
||||
|
||||
export class CreateFieldExpertDto {
|
||||
@ApiProperty({ example: "field.expert@example.com" })
|
||||
@IsEmail()
|
||||
email: string;
|
||||
|
||||
@ApiProperty({ example: "securePassword123", minLength: 6 })
|
||||
@IsString()
|
||||
@MinLength(6)
|
||||
password: string;
|
||||
|
||||
@ApiProperty({ example: "علی" })
|
||||
@IsString()
|
||||
firstName: string;
|
||||
|
||||
@ApiProperty({ example: "محمدی" })
|
||||
@IsString()
|
||||
lastName: string;
|
||||
|
||||
@ApiProperty({ example: "09121234567" })
|
||||
@IsString()
|
||||
mobile: string;
|
||||
|
||||
@ApiProperty({ example: "02188776655", required: false })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
phone?: string;
|
||||
}
|
||||
@@ -39,9 +39,12 @@ export class LocalActorAuthGuard extends AuthGuard("actor") {
|
||||
});
|
||||
|
||||
if (
|
||||
![RoleEnum.EXPERT, RoleEnum.DAMAGE_EXPERT, RoleEnum.COMPANY].includes(
|
||||
payload.role,
|
||||
)
|
||||
![
|
||||
RoleEnum.EXPERT,
|
||||
RoleEnum.DAMAGE_EXPERT,
|
||||
RoleEnum.COMPANY,
|
||||
RoleEnum.FIELD_EXPERT,
|
||||
].includes(payload.role)
|
||||
) {
|
||||
throw new UnauthorizedException("User role is not authorized");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user