forked from Yara724/api
Remove profile plate management and restrict personal profile updates
This commit is contained in:
@@ -16,7 +16,6 @@ import { ExpertClaimModule } from "./expert-claim/expert-claim.module";
|
||||
import { ExpertInsurerModule } from "./expert-insurer/expert-insurer.module";
|
||||
import { CaseExpertReportModule } from "./case-expert-report/case-expert-report.module";
|
||||
import { LookupsModule } from "./lookups/lookups.module";
|
||||
import { PlatesModule } from "./plates/plates.module";
|
||||
import { ProfileModule } from "./profile/profile.module";
|
||||
import { SandHubModule } from "./sand-hub/sand-hub.module";
|
||||
import { SystemSettingsModule } from "./system-settings/system-settings.module";
|
||||
@@ -54,7 +53,6 @@ import { PriceEstimationModule } from "./price-estimation/price-estimation.modul
|
||||
AuthModule,
|
||||
ClientModule,
|
||||
ProfileModule,
|
||||
PlatesModule,
|
||||
RequestManagementModule,
|
||||
SandHubModule,
|
||||
SystemSettingsModule,
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
import { ClientModule } from "src/client/client.module";
|
||||
import { PlatesModule } from "src/plates/plates.module";
|
||||
import { UsersModule } from "src/users/users.module";
|
||||
import { ExpertBlameController } from "./expert-blame.controller";
|
||||
import { ExpertBlameV2Controller } from "./expert-blame.v2.controller";
|
||||
@@ -15,7 +14,6 @@ import { ClaimRequestManagementModule } from "src/claim-request-management/claim
|
||||
UsersModule,
|
||||
ClientModule,
|
||||
RequestManagementModule,
|
||||
PlatesModule,
|
||||
SmsOrchestrationModule,
|
||||
ClaimRequestManagementModule,
|
||||
LookupsModule,
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
import { InjectModel } from "@nestjs/mongoose";
|
||||
import { Model, Types } from "mongoose";
|
||||
import { PlatesModel } from "../schema/plate.schema";
|
||||
|
||||
export class PlateDbService {
|
||||
constructor(
|
||||
@InjectModel(PlatesModel.name)
|
||||
private readonly plateModel: Model<PlatesModel>,
|
||||
) {}
|
||||
|
||||
async create(plate: PlatesModel): Promise<PlatesModel> {
|
||||
return await this.plateModel.create(plate);
|
||||
}
|
||||
|
||||
async findOne(plate: PlatesModel): Promise<PlatesModel> {
|
||||
return await this.plateModel.findOne(plate);
|
||||
}
|
||||
|
||||
async findAndRemove(id: Types.ObjectId): Promise<PlatesModel> {
|
||||
return await this.plateModel.findOneAndUpdate(
|
||||
{ _id: id },
|
||||
{ isDelete: true },
|
||||
{ upsert: true },
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
|
||||
|
||||
@Schema({ collection: "plates", versionKey: false })
|
||||
export class PlatesModel {
|
||||
@Prop({ required: true, unique: false })
|
||||
userId: string;
|
||||
|
||||
@Prop({ type: "number", maxlength: 2 })
|
||||
leftDigits: number;
|
||||
|
||||
@Prop({ type: "string", maxlength: 1 })
|
||||
centerAlphabet: string;
|
||||
|
||||
@Prop({ type: "number", maxlength: 3 })
|
||||
centerDigits: number;
|
||||
|
||||
@Prop({ type: "number", maxlength: 2 })
|
||||
ir: number;
|
||||
|
||||
@Prop()
|
||||
nationalCodeOfInsurer: string;
|
||||
}
|
||||
|
||||
export const PlatesSchema = SchemaFactory.createForClass(PlatesModel);
|
||||
@@ -1,24 +0,0 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
import { MongooseModule } from "@nestjs/mongoose";
|
||||
import { ClientModule } from "src/client/client.module";
|
||||
import { PlateDbService } from "src/plates/entites/db-service/plate.db.service";
|
||||
import { SandHubModule } from "src/sand-hub/sand-hub.module";
|
||||
import { UsersModule } from "src/users/users.module";
|
||||
import { PlateNormalizerModule } from "src/utils/plate-normalizer/plate-normalizer.module";
|
||||
import { PlatesModel, PlatesSchema } from "./entites/schema/plate.schema";
|
||||
import { PlatesService } from "./plates.service";
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
UsersModule,
|
||||
SandHubModule,
|
||||
ClientModule,
|
||||
PlateNormalizerModule,
|
||||
MongooseModule.forFeature([
|
||||
{ name: PlatesModel.name, schema: PlatesSchema },
|
||||
]),
|
||||
],
|
||||
providers: [PlatesService, PlateDbService],
|
||||
exports: [PlatesService, PlateDbService],
|
||||
})
|
||||
export class PlatesModule {}
|
||||
@@ -1,55 +0,0 @@
|
||||
import {
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from "@nestjs/common";
|
||||
import { Types } from "mongoose";
|
||||
import { PlateDbService } from "src/plates/entites/db-service/plate.db.service";
|
||||
import { AddPlateDto } from "src/profile/dto/user/AddPlateDto";
|
||||
import { PlateNormalizerService } from "src/utils/plate-normalizer/plate-normalizer.service";
|
||||
|
||||
@Injectable()
|
||||
export class PlatesService {
|
||||
constructor(
|
||||
private readonly plateDbService: PlateDbService,
|
||||
private readonly plateNormalizer: PlateNormalizerService,
|
||||
) {}
|
||||
|
||||
async addPlate(currentUser, body: AddPlateDto) {
|
||||
const { plate, nationalCodeOfInsurer } = body;
|
||||
|
||||
const normalizedAlphabet = this.plateNormalizer.normalizePlateText(
|
||||
String(plate.centerAlphabet || ""),
|
||||
);
|
||||
const duplicate = await this.plateDbService.findOne({
|
||||
userId: currentUser,
|
||||
leftDigits: plate.leftDigits,
|
||||
centerAlphabet: normalizedAlphabet,
|
||||
centerDigits: plate.centerDigits,
|
||||
ir: plate.ir,
|
||||
nationalCodeOfInsurer,
|
||||
});
|
||||
|
||||
if (duplicate)
|
||||
throw new HttpException("duplicate plate", HttpStatus.CONFLICT);
|
||||
|
||||
const newPlateData = await this.plateDbService.create({
|
||||
...plate,
|
||||
centerAlphabet: normalizedAlphabet,
|
||||
userId: currentUser,
|
||||
nationalCodeOfInsurer: body.nationalCodeOfInsurer,
|
||||
});
|
||||
|
||||
return {
|
||||
message: "Plate successfully added",
|
||||
plate: newPlateData,
|
||||
};
|
||||
}
|
||||
|
||||
async deletePlate(id: Types.ObjectId) {
|
||||
const deleted = await this.plateDbService.findAndRemove(id);
|
||||
if (!deleted) throw new NotFoundException("Plate not found");
|
||||
return { message: "Plate deleted successfully" };
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,3 @@
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { PlatesDto } from "src/plates/dto/plate.dto";
|
||||
import { Plates } from "src/Types&Enums/plate.interface";
|
||||
import { InquiryParticipantFieldsDto } from "src/common/dto/inquiry-participants.dto";
|
||||
|
||||
export class AddPlateDto extends InquiryParticipantFieldsDto {}
|
||||
|
||||
export class AddPlateProfileDto {
|
||||
plateId: string;
|
||||
|
||||
@ApiProperty({ type: PlatesDto, required: true })
|
||||
plate: Plates;
|
||||
|
||||
@ApiProperty({ type: String, required: true })
|
||||
nationalCodeOfInsurer: string;
|
||||
}
|
||||
|
||||
@@ -6,20 +6,21 @@ export class ProfileUserRq {
|
||||
id: string;
|
||||
}
|
||||
|
||||
export class ProfileUserRs extends UserModel {
|
||||
export class ProfileUserRs {
|
||||
userId: string;
|
||||
fullName: string;
|
||||
constructor(profile: UserModel, carDetail) {
|
||||
super();
|
||||
mobile: string;
|
||||
nationalCode: string;
|
||||
birthDay: string;
|
||||
gender?: "male" | "female";
|
||||
city: string;
|
||||
constructor(profile: UserModel) {
|
||||
this.userId = profile["_id"];
|
||||
this.fullName = profile.fullName;
|
||||
this.mobile = profile.mobile;
|
||||
this.plates = profile.plates || null;
|
||||
this.nationalCode = profile.nationalCode;
|
||||
this.birthDay = profile.birthDay;
|
||||
this.gender = profile.gender;
|
||||
this.plates = profile.plates;
|
||||
this.city = profile.city;
|
||||
// this.carDetail
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import { ProfileController } from "./profile.controller";
|
||||
import { ProfileService } from "./profile.service";
|
||||
import { PlatesService } from "src/plates/plates.service";
|
||||
import { JwtService } from "@nestjs/jwt";
|
||||
|
||||
describe("ProfileController", () => {
|
||||
@@ -12,7 +11,6 @@ describe("ProfileController", () => {
|
||||
controllers: [ProfileController],
|
||||
providers: [
|
||||
{ provide: ProfileService, useValue: {} },
|
||||
{ provide: PlatesService, useValue: {} },
|
||||
{ provide: JwtService, useValue: {} },
|
||||
],
|
||||
}).compile();
|
||||
|
||||
@@ -1,19 +1,8 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Body,
|
||||
Patch,
|
||||
Param,
|
||||
Delete,
|
||||
UseGuards,
|
||||
} from "@nestjs/common";
|
||||
import { ApiBearerAuth, ApiBody, ApiParam, ApiTags } from "@nestjs/swagger";
|
||||
import { Controller, Get, Body, Patch, UseGuards } from "@nestjs/common";
|
||||
import { ApiBearerAuth, ApiBody, ApiTags } from "@nestjs/swagger";
|
||||
import { GlobalGuard } from "src/auth/guards/global.guard";
|
||||
import { CurrentUser } from "src/decorators/user.decorator";
|
||||
import { Roles } from "src/decorators/roles.decorator";
|
||||
import { PlatesService } from "src/plates/plates.service";
|
||||
import { AddPlateDto, AddPlateProfileDto } from "./dto/user/AddPlateDto";
|
||||
import { UpdateUserProfileDtoRq } from "./dto/user/update-profile.dto";
|
||||
import { ProfileService } from "./profile.service";
|
||||
|
||||
@@ -22,10 +11,7 @@ import { ProfileService } from "./profile.service";
|
||||
@ApiBearerAuth()
|
||||
@Roles()
|
||||
export class ProfileController {
|
||||
constructor(
|
||||
private readonly profileService: ProfileService,
|
||||
private readonly plateService: PlatesService,
|
||||
) {}
|
||||
constructor(private readonly profileService: ProfileService) {}
|
||||
|
||||
@Get()
|
||||
@UseGuards(GlobalGuard)
|
||||
@@ -42,18 +28,4 @@ export class ProfileController {
|
||||
) {
|
||||
return await this.profileService.updateProfile(user.username, body);
|
||||
}
|
||||
|
||||
@Post("plate")
|
||||
@UseGuards(GlobalGuard)
|
||||
@ApiBody({ type: AddPlateProfileDto })
|
||||
async addPlate(@CurrentUser() user, @Body() body: AddPlateDto) {
|
||||
return await this.plateService.addPlate(user.sub, body);
|
||||
}
|
||||
|
||||
@Delete("plate/:id")
|
||||
@UseGuards(GlobalGuard)
|
||||
@ApiParam({ name: "id", required: true })
|
||||
async deletePlate(@Param("id") id) {
|
||||
return await this.plateService.deletePlate(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
import { PlatesModule } from "src/plates/plates.module";
|
||||
import { UsersModule } from "src/users/users.module";
|
||||
import { ProfileController } from "./profile.controller";
|
||||
import { ProfileService } from "./profile.service";
|
||||
|
||||
@Module({
|
||||
imports: [UsersModule, PlatesModule],
|
||||
imports: [UsersModule],
|
||||
controllers: [ProfileController],
|
||||
providers: [ProfileService],
|
||||
})
|
||||
|
||||
@@ -4,6 +4,7 @@ import { UserDbService } from "src/users/entities/db-service/user.db.service";
|
||||
|
||||
describe("ProfileService", () => {
|
||||
let service: ProfileService;
|
||||
let users: { findOne: jest.Mock; findOneAndUpdate: jest.Mock };
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
@@ -20,9 +21,50 @@ describe("ProfileService", () => {
|
||||
}).compile();
|
||||
|
||||
service = module.get<ProfileService>(ProfileService);
|
||||
users = module.get(UserDbService);
|
||||
});
|
||||
|
||||
it("should be defined", () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
it("does not expose historical saved plates in the profile", async () => {
|
||||
users.findOne.mockResolvedValue({
|
||||
_id: "user-id",
|
||||
fullName: "Test user",
|
||||
mobile: "09120000000",
|
||||
plates: [{ plateId: "historical-plate" }],
|
||||
});
|
||||
const profile = await service.getProfileDetail("09120000000");
|
||||
expect(profile).toMatchObject({ userId: "user-id", fullName: "Test user" });
|
||||
expect(profile).not.toHaveProperty("plates");
|
||||
});
|
||||
|
||||
it.each([
|
||||
{ plates: [{ plateId: "new-plate" }] },
|
||||
{ $push: { plates: { plateId: "new-plate" } } },
|
||||
{ $set: { plates: [] } },
|
||||
])("cannot manage plates through the profile PATCH: %j", async (update) => {
|
||||
await expect(service.updateProfile("09120000000", update)).rejects.toThrow(
|
||||
"Only personal profile fields can be updated",
|
||||
);
|
||||
expect(users.findOneAndUpdate).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("still updates personal details without modifying saved data", async () => {
|
||||
users.findOneAndUpdate.mockResolvedValue({ _id: "user-id", plates: [] });
|
||||
const update = {
|
||||
birthDay: "1999-03-05",
|
||||
gender: "male",
|
||||
city: "Tehran",
|
||||
state: "Tehran",
|
||||
address: "Test address",
|
||||
};
|
||||
const profile = await service.updateProfile("09120000000", update);
|
||||
expect(users.findOneAndUpdate).toHaveBeenCalledWith(
|
||||
{ username: "09120000000" },
|
||||
{ $set: update },
|
||||
);
|
||||
expect(profile).not.toHaveProperty("plates");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
import { UserDbService } from "src/users/entities/db-service/user.db.service";
|
||||
import { ProfileUserRs } from "./dto/user/profile.dto";
|
||||
|
||||
@@ -8,10 +8,18 @@ export class ProfileService {
|
||||
|
||||
async getProfileDetail(username: string) {
|
||||
const userDetail = await this.userDbService.findOne({ username });
|
||||
return new ProfileUserRs(userDetail, {});
|
||||
return new ProfileUserRs(userDetail);
|
||||
}
|
||||
|
||||
async updateProfile(username: string, update) {
|
||||
return await this.userDbService.findOneAndUpdate({ username }, update);
|
||||
const allowedFields = ["birthDay", "gender", "city", "state", "address"];
|
||||
if (Object.keys(update).some((field) => !allowedFields.includes(field))) {
|
||||
throw new BadRequestException("Only personal profile fields can be updated");
|
||||
}
|
||||
const userDetail = await this.userDbService.findOneAndUpdate(
|
||||
{ username },
|
||||
{ $set: update },
|
||||
);
|
||||
return new ProfileUserRs(userDetail);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ import { MongooseModule } from "@nestjs/mongoose";
|
||||
import { MulterModule } from "@nestjs/platform-express";
|
||||
import { ClaimRequestManagementModule } from "src/claim-request-management/claim-request-management.module";
|
||||
import { ClientModule } from "src/client/client.module";
|
||||
import { PlatesModule } from "src/plates/plates.module";
|
||||
import { BlameVideoDbService } from "src/request-management/entities/db-service/blame-video.db.service";
|
||||
import { RequestManagementDbService } from "src/request-management/entities/db-service/request-management.db.service";
|
||||
import { BlameRequestDbService } from "src/request-management/entities/db-service/blame-request.db.service";
|
||||
@@ -69,7 +68,6 @@ import { CallCenterBlameV6Controller } from "./call-center-blame-v6.controller";
|
||||
HashModule,
|
||||
PlateNormalizerModule,
|
||||
WorkflowStepManagementModule,
|
||||
PlatesModule,
|
||||
FanavaranLookupModule,
|
||||
LookupsModule,
|
||||
MulterModule.register({
|
||||
|
||||
Reference in New Issue
Block a user