fix: join address to the customer get by admin

This commit is contained in:
mahyargdz
2025-03-06 10:35:12 +03:30
parent b92a65c22c
commit 144bfb2ac1
12 changed files with 257 additions and 75 deletions
@@ -2,6 +2,7 @@ import { BadRequestException, Injectable, Logger } from "@nestjs/common";
import { DataSource, Not } from "typeorm";
import { CommonMessage, UserMessage } from "../../../common/enums/message.enum";
import { Address } from "../../address/entities/address.entity";
import { AddressService } from "../../address/providers/address.service";
import { UserSettingsService } from "../../settings/providers/user-settings.service";
import { PasswordService } from "../../utils/providers/password.service";
@@ -83,67 +84,144 @@ export class CustomersService {
await queryRunner.connect();
await queryRunner.startTransaction();
const {
registrationName,
economicCode,
nationalIdentity,
registrationCode,
postalCode,
nationalCode,
address,
cityId,
birthDate,
email,
firstName,
lastName,
password,
phone,
} = updateDto;
const legalUserUpdateData = {
registrationName,
economicCode,
nationalIdentity,
registrationCode,
phone,
};
const userUpdateData = {
email,
firstName,
lastName,
password,
phone,
birthDate,
};
// const addressUpdateData = {
// postalCode,
// address,
// cityId,
// };
const user = await queryRunner.manager.findOne(this.userRepository.target, { where: { id: customerId } });
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
if (updateDto.email) {
if (email) {
const existingEmail = await queryRunner.manager.findOne(this.userRepository.target, {
where: { email: updateDto.email, id: Not(user.id) },
where: { email, id: Not(user.id) },
});
if (existingEmail) throw new BadRequestException(UserMessage.EMAIL_EXIST);
}
if (updateDto.nationalCode) {
if (nationalCode) {
const existingNationalCode = await queryRunner.manager.findOne(this.userRepository.target, {
where: { nationalCode: updateDto.nationalCode, id: Not(user.id) },
where: { nationalCode: nationalCode, id: Not(user.id) },
});
if (existingNationalCode) throw new BadRequestException(UserMessage.NATIONAL_CODE_EXIST);
}
if (updateDto.phone) {
if (phone) {
const existingPhone = await queryRunner.manager.findOne(this.userRepository.target, {
where: { phone: updateDto.phone, id: Not(user.id) },
where: { phone: phone, id: Not(user.id) },
});
if (existingPhone) throw new BadRequestException(UserMessage.PHONE_EXIST);
}
await queryRunner.manager.update(this.userRepository.target, { id: customerId }, updateDto);
if (nationalCode) {
const existingNationalCode = await queryRunner.manager.findOne(this.realUserRepository.target, {
where: { nationalCode: nationalCode, id: Not(user.id) },
});
if (existingNationalCode) throw new BadRequestException(UserMessage.NATIONAL_CODE_EXIST);
}
const legalUser = await queryRunner.manager.findOne(this.legalUserRepository.target, { where: { user: { id: customerId } } });
await queryRunner.manager.update(this.userRepository.target, { id: customerId }, userUpdateData);
if (password) {
const hashPassword = await this.passwordService.hashPassword(password);
await queryRunner.manager.update(this.userRepository.target, { id: customerId }, { password: hashPassword });
}
const legalUser = await queryRunner.manager.findOne(this.legalUserRepository.target, {
where: { user: { id: customerId } },
relations: { address: true },
});
if (!legalUser) throw new BadRequestException(UserMessage.LEGAL_DATA_NOT_FOUND);
if (updateDto.economicCode) {
if (economicCode) {
const existingEconomicCode = await queryRunner.manager.findOne(this.legalUserRepository.target, {
where: { economicCode: updateDto.economicCode, id: Not(legalUser.id) },
where: { economicCode, id: Not(legalUser.id) },
});
if (existingEconomicCode) throw new BadRequestException(UserMessage.ECONOMIC_CODE_EXIST);
}
if (updateDto.registrationCode) {
if (registrationCode) {
const existingRegistrationCode = await queryRunner.manager.findOne(this.legalUserRepository.target, {
where: { registrationCode: updateDto.registrationCode, id: Not(legalUser.id) },
where: { registrationCode, id: Not(legalUser.id) },
});
if (existingRegistrationCode) throw new BadRequestException(UserMessage.REGISTRATION_CODE_EXIST);
}
if (updateDto.nationalIdentity) {
if (nationalIdentity) {
const existingNationalIdentity = await queryRunner.manager.findOne(this.legalUserRepository.target, {
where: { nationalIdentity: updateDto.nationalIdentity, id: Not(legalUser.id) },
where: { nationalIdentity, id: Not(legalUser.id) },
});
if (existingNationalIdentity) throw new BadRequestException(UserMessage.NATIONAL_IDENTITY_EXIST);
}
if (updateDto.cityId) {
const { city } = await this.addressService.getCity(+updateDto.cityId);
await queryRunner.manager.update(this.legalUserRepository.target, { id: legalUser.id }, { address: { city } });
if (registrationName) {
const existingRegistrationName = await queryRunner.manager.findOne(this.legalUserRepository.target, {
where: { registrationName, id: Not(legalUser.id) },
});
if (existingRegistrationName) throw new BadRequestException(UserMessage.REGISTRATION_NAME_EXIST);
}
const { address, ...legalUserUpdateData } = updateDto;
await queryRunner.manager.update(this.legalUserRepository.target, { id: legalUser.id }, legalUserUpdateData);
if (address) {
await queryRunner.manager.update(this.legalUserRepository.target, { id: legalUser.id }, { address: { fullAddress: address } });
// Update address if needed
if (cityId || address || postalCode) {
// Get the address entity
const addressEntity = legalUser.address;
if (!addressEntity) throw new BadRequestException(UserMessage.ADDRESS_NOT_FOUND);
// Prepare address updates
const addressUpdates: any = {};
if (cityId) {
const { city } = await this.addressService.getCity(+cityId);
addressUpdates.city = city;
}
if (address) {
addressUpdates.fullAddress = address;
}
if (postalCode) {
addressUpdates.postalCode = postalCode;
}
await queryRunner.manager.update(Address, { id: addressEntity.id }, addressUpdates);
}
await queryRunner.commitTransaction();
@@ -306,7 +384,7 @@ export class CustomersService {
async getCustomerById(userId: string) {
const customer = await this.userRepository.findOne({
where: { id: userId, roles: { name: RoleEnum.USER } },
relations: { legalUser: true, realUser: true },
relations: { legalUser: { address: { city: { province: true } } }, realUser: { address: { city: { province: true } } } },
});
return { customer };