user
This commit is contained in:
@@ -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;
|
||||
// }
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user