chore: update user profile

This commit is contained in:
Matin
2025-02-20 09:50:01 +03:30
parent c306dca107
commit 99fa47356a
5 changed files with 59 additions and 9 deletions
+20 -1
View File
@@ -12,9 +12,28 @@ export class UpdateProfileDto extends PartialType(PickType(CompleteRegistrationD
@ApiPropertyOptional({ description: "User name", example: "mamad24" })
userName?: string;
@ApiPropertyOptional({ description: "Profile picture", example: "https://www.google.com" })
@IsOptional()
@IsNotEmpty({ message: UserMessage.PROFILE_PIC_REQUIRED })
@IsUrl({ protocols: ["http", "https"], require_protocol: true }, { message: UserMessage.PROFILE_PIC_URL })
@ApiPropertyOptional({ description: "Profile picture", example: "https://www.google.com" })
profilePic?: string;
@ApiPropertyOptional({ description: "User address", example: "123 Main St" })
@IsOptional()
@IsNotEmpty({ message: UserMessage.ADDRESS_REQUIRED })
@IsString({ message: UserMessage.ADDRESS_SHOULD_BE_STRING })
@Length(5, 255, { message: UserMessage.ADDRESS_LENGTH })
userAddress?: string;
@ApiPropertyOptional({ description: "Postal code", example: "1234567890" })
@IsOptional()
@IsNotEmpty({ message: UserMessage.POSTAL_CODE_REQUIRED })
@IsString({ message: UserMessage.POSTAL_CODE_SHOULD_BE_STRING })
@Length(10, 10, { message: UserMessage.POSTAL_CODE_LENGTH })
postalCode?: string;
@ApiPropertyOptional({ description: "City id" })
@IsOptional()
@IsNotEmpty({ message: UserMessage.CITY_REQUIRED })
cityId: string;
}
+11 -2
View File
@@ -1,5 +1,5 @@
import { Exclude } from "class-transformer";
import { Column, Entity, JoinColumn, JoinTable, ManyToMany, OneToMany, OneToOne } from "typeorm";
import { Column, Entity, JoinColumn, JoinTable, ManyToMany, ManyToOne, OneToMany, OneToOne } from "typeorm";
import { LegalUser } from "./legal-user.entity";
import { RealUser } from "./real-user.entity";
@@ -7,6 +7,7 @@ import { Role } from "./role.entity";
import { UserGroup } from "./user-group.entity";
import { BaseEntity } from "../../../common/entities/base.entity";
import { Address } from "../../address/entities/address.entity";
import { City } from "../../address/entities/city.entity";
import { UserAnnouncement } from "../../announcements/entities/user-announcement.entity";
import { Criticism } from "../../criticisms/entities/criticism.entity";
import { DanakServiceReview } from "../../danak-services/entities/danak-service-review.entity";
@@ -54,10 +55,18 @@ export class User extends BaseEntity {
@Column({ type: "boolean", default: false })
emailVerified: boolean;
@Column({ type: "varchar", length: 255, nullable: true })
userAddress: string;
@Column({ type: "varchar", length: 10, nullable: true })
postalCode: string;
//-----------------------------------------
@ManyToOne(() => City, { eager: true, nullable: true })
city: City;
// @ManyToOne(() => Role, { eager: true, onDelete: "RESTRICT", nullable: false })
// role: Role;
@ManyToMany(() => Role, (role) => role.users)
@JoinTable({ name: "user_role_relation" })
roles: Role[];
+10 -2
View File
@@ -80,7 +80,7 @@ export class UsersService {
/************************************************************ */
async getMe(userId: string) {
const user = await this.userRepository.findOne({ where: { id: userId }, relations: { roles: true } });
const user = await this.userRepository.findOne({ where: { id: userId }, relations: { roles: true, city: { province: true } } });
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
return { user };
}
@@ -104,9 +104,17 @@ export class UsersService {
/************************************************************ */
async updateProfile(userId: string, updateProfileDto: UpdateProfileDto) {
let city;
const user = await this.userRepository.findOneBy({ id: userId });
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
if (updateProfileDto.cityId) {
const { city: existCity } = await this.addressService.getCity(+updateProfileDto.cityId);
if (!existCity) throw new BadRequestException(UserMessage.CITY_NOT_FOUND);
city = existCity;
}
//
if (updateProfileDto.userName) {
updateProfileDto.userName = slugify(updateProfileDto.userName, { lower: true, trim: true });
@@ -114,7 +122,7 @@ export class UsersService {
if (existUserName) throw new BadRequestException(UserMessage.USERNAME_EXIST);
}
await this.userRepository.save({ ...user, ...updateProfileDto });
await this.userRepository.save({ ...user, ...updateProfileDto, city });
//
return {
message: CommonMessage.UPDATE_SUCCESS,