This commit is contained in:
2026-01-25 09:35:01 +03:30
parent dc281c4f9a
commit 2449ac447d
8 changed files with 13 additions and 242 deletions
@@ -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()
+5
View File
@@ -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;
@@ -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
}
+2 -6
View File
@@ -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<UserAddress>(this);
@Property({ type: 'string', nullable: true })
addresse?: string
}
-131
View File
@@ -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<UserAddress[]> {
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<UserAddress> {
// 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<UserAddress> {
// 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<UserAddress> {
// 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<void> {
// 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<UserAddress> {
// 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;
// }
}
+2 -3
View File
@@ -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: [