From 2449ac447d139fcbd2ab4a063776698b8af67491 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Sun, 25 Jan 2026 09:35:01 +0330 Subject: [PATCH] user --- src/modules/order/entities/order.entity.ts | 7 - .../user/controllers/users.controller.ts | 11 +- src/modules/user/dto/create-user.dto.ts | 5 + .../user/entities/user-address.entity.ts | 39 ------ src/modules/user/entities/user.entity.ts | 8 +- src/modules/user/providers/user.service.ts | 131 ------------------ src/modules/user/user.module.ts | 5 +- src/seeders/users.seeder.ts | 49 +------ 8 files changed, 13 insertions(+), 242 deletions(-) delete mode 100644 src/modules/user/entities/user-address.entity.ts diff --git a/src/modules/order/entities/order.entity.ts b/src/modules/order/entities/order.entity.ts index b4151fa..6a86bf8 100644 --- a/src/modules/order/entities/order.entity.ts +++ b/src/modules/order/entities/order.entity.ts @@ -50,8 +50,6 @@ export class Order extends BaseEntity { @ManyToOne(() => Admin, { nullable: true }) designer?: Admin - // @Property({ nullable: true }) - // userAddress?: string | null; @Property({ type: 'int', nullable: true }) orderNumber?: number; @@ -67,9 +65,6 @@ export class Order extends BaseEntity { taxAmount: number; - // @Property({ type: 'decimal', precision: 10, scale: 0, default: 0 }) - // deliveryFee: number = 0; - @Property({ type: 'decimal', precision: 10, scale: 0 }) total!: number; @@ -97,8 +92,6 @@ export class Order extends BaseEntity { @Enum(() => OrderStatusEnum) status!: OrderStatusEnum; - // @Property({ nullable: true, columnType: 'timestamptz' }) - // confirmedAt?: Date; @Property({ type: 'json', nullable: true }) attachments?: string[] diff --git a/src/modules/user/controllers/users.controller.ts b/src/modules/user/controllers/users.controller.ts index e2d98c9..bf9ff61 100644 --- a/src/modules/user/controllers/users.controller.ts +++ b/src/modules/user/controllers/users.controller.ts @@ -44,16 +44,9 @@ export class UsersController { return user; } - @UseGuards(AuthGuard) - @ApiBearerAuth() - @ApiOperation({ summary: 'Get all addresses for the authenticated user' }) + - @ApiOkResponse({ description: 'List of user addresses' }) - @Get('public/user/addresses') - async getUserAddresses(@UserId() userId: string) { - const addresses = await this.userService.getUserAddresses(userId); - return addresses; - } + // @UseGuards(AuthGuard) // @ApiBearerAuth() diff --git a/src/modules/user/dto/create-user.dto.ts b/src/modules/user/dto/create-user.dto.ts index 83a133b..7482e25 100644 --- a/src/modules/user/dto/create-user.dto.ts +++ b/src/modules/user/dto/create-user.dto.ts @@ -18,6 +18,11 @@ export class CreateUserDto { @IsOptional() lastName?: string; + @ApiProperty({ description: "Address", example: '' }) + @IsString() + @IsOptional() + address?: string; + @ApiProperty({ description: 'Gender flag (boolean)', example: true }) @IsBoolean() gender: boolean; diff --git a/src/modules/user/entities/user-address.entity.ts b/src/modules/user/entities/user-address.entity.ts deleted file mode 100644 index 0359fcd..0000000 --- a/src/modules/user/entities/user-address.entity.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { Entity, Property, ManyToOne, PrimaryKey } from '@mikro-orm/core'; -import { BaseEntity } from '../../../common/entities/base.entity'; -import { User } from './user.entity'; - -@Entity({ tableName: 'user_addresses' }) -export class UserAddress extends BaseEntity { - @PrimaryKey({ type: 'bigint', autoincrement: true }) - id: bigint - - @ManyToOne(() => User) - user!: User; - - @Property() - title!: string; // e.g., "Home", "Work", "Office" - - @Property() - address!: string; - - @Property() - city!: string; - - @Property({ nullable: true }) - province?: string; - - @Property({ nullable: true }) - postalCode?: string | null = null; - - @Property({ type: 'float' }) - latitude!: number; - - @Property({ type: 'float' }) - longitude!: number; - - @Property({ nullable: true }) - phone?: string; // Contact phone for this address - - @Property({ default: false }) - isDefault: boolean = false; // Whether this is the default address -} diff --git a/src/modules/user/entities/user.entity.ts b/src/modules/user/entities/user.entity.ts index 667ed49..abca8d0 100644 --- a/src/modules/user/entities/user.entity.ts +++ b/src/modules/user/entities/user.entity.ts @@ -1,6 +1,5 @@ import { Entity, Index, Property, OneToMany, Collection, Cascade, PrimaryKey } from '@mikro-orm/core'; import { BaseEntity } from '../../../common/entities/base.entity'; -import { UserAddress } from './user-address.entity'; import { Order } from 'src/modules/order/entities/order.entity'; import { normalizePhone } from '../../util/phone.util'; import { ulid } from 'ulid'; @@ -41,9 +40,6 @@ export class User extends BaseEntity { @Property({ default: 0 }) maxCredit: number; - @OneToMany(() => UserAddress, address => address.user, { - cascade: [Cascade.ALL], - orphanRemoval: true, - }) - addresses = new Collection(this); + @Property({ type: 'string', nullable: true }) + addresse?: string } diff --git a/src/modules/user/providers/user.service.ts b/src/modules/user/providers/user.service.ts index c38d1ef..620f6fe 100644 --- a/src/modules/user/providers/user.service.ts +++ b/src/modules/user/providers/user.service.ts @@ -1,7 +1,6 @@ import { Injectable, NotFoundException, BadRequestException } from '@nestjs/common'; import { RequiredEntityData } from '@mikro-orm/core'; import { User } from '../entities/user.entity'; -import { UserAddress } from '../entities/user-address.entity'; import { EntityManager } from '@mikro-orm/postgresql'; import { CreateUserDto } from '../dto/create-user.dto'; import { FindUsersDto } from '../dto/find-user.dto'; @@ -69,135 +68,5 @@ export class UserService { return this.userRepository.findAllPaginated(dto) } - async getUserAddresses(userId: string): Promise { - const user = await this.userRepository.findOne({ id: userId }, { populate: ['addresses'] }); - if (!user) { - throw new NotFoundException(`User with ID ${userId} not found.`); - } - - // Load addresses if not already loaded - await user.addresses.loadItems(); - return user.addresses.getItems(); - } - - // async createUserAddress(userId: string, dto: CreateUserAddressDto): Promise { - // const user = await this.userRepository.findOne({ id: userId }); - // if (!user) { - // throw new NotFoundException(`User with ID ${userId} not found.`); - // } - - // // If setting as default, unset other default addresses - // if (dto.isDefault) { - // const existingAddresses = await this.em.find(UserAddress, { user: { id: userId }, isDefault: true }); - // for (const address of existingAddresses) { - // address.isDefault = false; - // } - // await this.em.flush(); - // } - - // // Create new address - // const address = this.em.create(UserAddress, { - // user, - // title: dto.title, - // address: dto.address, - // city: dto.city, - // province: dto.province, - // postalCode: dto.postalCode, - // latitude: dto.latitude, - // longitude: dto.longitude, - // phone: dto.phone, - // isDefault: dto.isDefault ?? false, - // }); - - // await this.em.persistAndFlush(address); - // return address; - // } - - // async getUserAddress(userId: string, addressId: string): Promise { - // const address = await this.em.findOne(UserAddress, { - // id: addressId, - // user: { id: userId }, - // }); - - // if (!address) { - // throw new NotFoundException(`Address with ID ${addressId} not found for user ${userId}.`); - // } - - // return address; - // } - - // async updateUserAddress(userId: string, addressId: string, dto: UpdateUserAddressDto): Promise { - // const address = await this.em.findOne(UserAddress, { - // id: addressId, - // user: { id: userId }, - // }); - - // if (!address) { - // throw new NotFoundException(`Address with ID ${addressId} not found for user ${userId}.`); - // } - - // // If setting as default, unset other default addresses - // if (dto.isDefault === true) { - // const existingAddresses = await this.em.find(UserAddress, { - // user: { id: userId }, - // isDefault: true, - // id: { $ne: addressId }, - // }); - // for (const existingAddress of existingAddresses) { - // existingAddress.isDefault = false; - // } - // await this.em.flush(); - // } - - // // Update address fields - // this.em.assign(address, dto); - // await this.em.flush(); - - // return address; - // } - - // async deleteUserAddress(userId: string, addressId: string): Promise { - // const address = await this.em.findOne(UserAddress, { - // id: addressId, - // user: { id: userId }, - // }); - - // if (!address) { - // throw new NotFoundException(`Address with ID ${addressId} not found for user ${userId}.`); - // } - - // // Soft delete the address - // address.deletedAt = new Date(); - // await this.em.persistAndFlush(address); - // } - - // async setDefaultAddress(userId: string, addressId: string): Promise { - // const address = await this.em.findOne(UserAddress, { - // id: addressId, - // user: { id: userId }, - // }); - - // if (!address) { - // throw new NotFoundException(`Address with ID ${addressId} not found for user ${userId}.`); - // } - - // // Unset all other default addresses - // const existingAddresses = await this.em.find(UserAddress, { - // user: { id: userId }, - // isDefault: true, - // id: { $ne: addressId }, - // }); - // for (const existingAddress of existingAddresses) { - // existingAddress.isDefault = false; - // } - - // // Set this address as default - // address.isDefault = true; - // await this.em.flush(); - - // return address; - // } - - } diff --git a/src/modules/user/user.module.ts b/src/modules/user/user.module.ts index 8143787..9676d5c 100644 --- a/src/modules/user/user.module.ts +++ b/src/modules/user/user.module.ts @@ -3,8 +3,7 @@ import { UserService } from './providers/user.service'; import { UsersController } from './controllers/users.controller'; import { MikroOrmModule } from '@mikro-orm/nestjs'; import { User } from './entities/user.entity'; -import { UserAddress } from './entities/user-address.entity'; -import { JwtModule } from '@nestjs/jwt'; + import { JwtModule } from '@nestjs/jwt'; import { UserRepository } from './repositories/user.repository'; import { CreditRepository } from './repositories/credit.repository'; import { CreditService } from './providers/credit.service'; @@ -19,7 +18,7 @@ import { CreditTransaction } from './entities/credit-transaction.entity'; ], controllers: [UsersController], imports: [ - MikroOrmModule.forFeature([User, UserAddress, CreditTransaction]), + MikroOrmModule.forFeature([User, CreditTransaction]), JwtModule, ], exports: [ diff --git a/src/seeders/users.seeder.ts b/src/seeders/users.seeder.ts index d0bfddb..55e64fc 100644 --- a/src/seeders/users.seeder.ts +++ b/src/seeders/users.seeder.ts @@ -1,8 +1,6 @@ import type { EntityManager } from '@mikro-orm/core'; import { User } from '../modules/user/entities/user.entity'; -import { UserAddress } from '../modules/user/entities/user-address.entity'; import { usersData } from './data/users.data'; -import { userAddressesData } from './data/user-addresses.data'; import { normalizePhone } from '../modules/util/phone.util'; export class UsersSeeder { @@ -20,57 +18,14 @@ export class UsersSeeder { lastName: userData.lastName, isActive: userData.isActive, gender: userData.gender, - maxCredit: 0 + maxCredit: 0, + addresse: 'اراک' }); em.persist(user); } usersMap.set(normalizedPhone, user); // Use normalized phone as key } - await em.flush(); - - // Create addresses for users - for (const addressData of userAddressesData) { - const normalizedPhone = normalizePhone(addressData.userPhone); - const user = usersMap.get(normalizedPhone); - if (!user) continue; - - // Check if address already exists for this user - const existingAddress = await em.findOne(UserAddress, { - user: { id: user.id }, - title: addressData.title, - address: addressData.address, - }); - - if (!existingAddress) { - // If setting as default, unset other default addresses for this user - if (addressData.isDefault) { - const existingDefaultAddresses = await em.find(UserAddress, { - user: { id: user.id }, - isDefault: true, - }); - for (const defaultAddress of existingDefaultAddresses) { - defaultAddress.isDefault = false; - em.persist(defaultAddress); - } - } - - const address = em.create(UserAddress, { - user, - title: addressData.title, - address: addressData.address, - city: addressData.city, - province: addressData.province, - postalCode: addressData.postalCode, - latitude: addressData.latitude, - longitude: addressData.longitude, - phone: addressData.phone, - isDefault: addressData.isDefault, - }); - em.persist(address); - } - } - await em.flush(); return usersMap; }