forked from Yara724/api
Remove profile plate management and restrict personal profile updates
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user