This commit is contained in:
@@ -2,7 +2,7 @@ import { BadRequestException, Injectable, Logger } from "@nestjs/common";
|
||||
import { DataSource, Not } from "typeorm";
|
||||
|
||||
import { IUserIpAndHeaders } from "../../../common/decorators/user.decorator";
|
||||
import { CommonMessage, UserMessage } from "../../../common/enums/message.enum";
|
||||
import { AuthMessage, CommonMessage, FinancialMessage, UserMessage } from "../../../common/enums/message.enum";
|
||||
import { UserType } from "../../access-logs/enums/user-type.enum";
|
||||
import { AccessLogService } from "../../access-logs/providers/access-log.service";
|
||||
import { Address } from "../../address/entities/address.entity";
|
||||
@@ -180,67 +180,100 @@ export class CustomersService {
|
||||
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 },
|
||||
});
|
||||
const hasLegalUpdateData = Boolean(
|
||||
registrationName || economicCode || nationalIdentity || registrationCode || postalCode || address || cityId,
|
||||
);
|
||||
|
||||
if (!legalUser) throw new BadRequestException(UserMessage.LEGAL_DATA_NOT_FOUND);
|
||||
|
||||
if (economicCode) {
|
||||
const existingEconomicCode = await queryRunner.manager.findOne(this.legalUserRepository.target, {
|
||||
where: { economicCode, id: Not(legalUser.id) },
|
||||
if (hasLegalUpdateData) {
|
||||
const legalUser = await queryRunner.manager.findOne(this.legalUserRepository.target, {
|
||||
where: { user: { id: customerId } },
|
||||
relations: { address: true },
|
||||
});
|
||||
if (existingEconomicCode) throw new BadRequestException(UserMessage.ECONOMIC_CODE_EXIST);
|
||||
}
|
||||
|
||||
if (registrationCode) {
|
||||
const existingRegistrationCode = await queryRunner.manager.findOne(this.legalUserRepository.target, {
|
||||
where: { registrationCode, id: Not(legalUser.id) },
|
||||
});
|
||||
if (existingRegistrationCode) throw new BadRequestException(UserMessage.REGISTRATION_CODE_EXIST);
|
||||
}
|
||||
if (!legalUser) {
|
||||
if (user.financialType === FinancialType.REAL) {
|
||||
throw new BadRequestException(UserMessage.FINANCIAL_TYPE_IS_REAL);
|
||||
}
|
||||
|
||||
if (nationalIdentity) {
|
||||
const existingNationalIdentity = await queryRunner.manager.findOne(this.legalUserRepository.target, {
|
||||
where: { nationalIdentity, id: Not(legalUser.id) },
|
||||
});
|
||||
if (existingNationalIdentity) throw new BadRequestException(UserMessage.NATIONAL_IDENTITY_EXIST);
|
||||
}
|
||||
const legalPhone = phone ?? user.phone;
|
||||
|
||||
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);
|
||||
}
|
||||
if (!economicCode) throw new BadRequestException(FinancialMessage.ECONOMIC_CODE_REQUIRED);
|
||||
if (!registrationCode) throw new BadRequestException(FinancialMessage.REGISTRATION_CODE_REQUIRED);
|
||||
if (!registrationName) throw new BadRequestException(FinancialMessage.REGISTRATION_NAME_REQUIRED);
|
||||
if (!nationalIdentity) throw new BadRequestException(FinancialMessage.NATIONAL_ID_REQUIRED);
|
||||
if (!legalPhone) throw new BadRequestException(AuthMessage.PHONE_NOT_EMPTY);
|
||||
if (!address) throw new BadRequestException(FinancialMessage.ADDRESS_REQUIRED);
|
||||
if (!postalCode) throw new BadRequestException(FinancialMessage.POSTAL_CODE_REQUIRED);
|
||||
if (!cityId) throw new BadRequestException(FinancialMessage.CITY_ID_REQUIRED);
|
||||
|
||||
await queryRunner.manager.update(this.legalUserRepository.target, { id: legalUser.id }, legalUserUpdateData);
|
||||
await this.createLegalUserData(
|
||||
{
|
||||
economicCode,
|
||||
registrationCode,
|
||||
registrationName,
|
||||
nationalIdentity,
|
||||
phone: legalPhone,
|
||||
address,
|
||||
postalCode,
|
||||
cityId: +cityId,
|
||||
},
|
||||
customerId,
|
||||
queryRunner,
|
||||
);
|
||||
} else {
|
||||
if (economicCode) {
|
||||
const existingEconomicCode = await queryRunner.manager.findOne(this.legalUserRepository.target, {
|
||||
where: { economicCode, id: Not(legalUser.id) },
|
||||
});
|
||||
if (existingEconomicCode) throw new BadRequestException(UserMessage.ECONOMIC_CODE_EXIST);
|
||||
}
|
||||
|
||||
// Update address if needed
|
||||
if (cityId || address || postalCode) {
|
||||
// Get the address entity
|
||||
const addressEntity = legalUser.address;
|
||||
if (registrationCode) {
|
||||
const existingRegistrationCode = await queryRunner.manager.findOne(this.legalUserRepository.target, {
|
||||
where: { registrationCode, id: Not(legalUser.id) },
|
||||
});
|
||||
if (existingRegistrationCode) throw new BadRequestException(UserMessage.REGISTRATION_CODE_EXIST);
|
||||
}
|
||||
|
||||
if (!addressEntity) throw new BadRequestException(UserMessage.ADDRESS_NOT_FOUND);
|
||||
if (nationalIdentity) {
|
||||
const existingNationalIdentity = await queryRunner.manager.findOne(this.legalUserRepository.target, {
|
||||
where: { nationalIdentity, id: Not(legalUser.id) },
|
||||
});
|
||||
if (existingNationalIdentity) throw new BadRequestException(UserMessage.NATIONAL_IDENTITY_EXIST);
|
||||
}
|
||||
|
||||
// Prepare address updates
|
||||
const addressUpdates: Record<string, unknown> = {};
|
||||
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);
|
||||
}
|
||||
|
||||
if (cityId) {
|
||||
const { city } = await this.addressService.getCity(+cityId);
|
||||
addressUpdates.city = city;
|
||||
await queryRunner.manager.update(this.legalUserRepository.target, { id: legalUser.id }, legalUserUpdateData);
|
||||
|
||||
if (cityId || address || postalCode) {
|
||||
const addressEntity = legalUser.address;
|
||||
|
||||
if (!addressEntity) throw new BadRequestException(UserMessage.ADDRESS_NOT_FOUND);
|
||||
|
||||
const addressUpdates: Record<string, unknown> = {};
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
if (address) {
|
||||
addressUpdates.fullAddress = address;
|
||||
}
|
||||
|
||||
if (postalCode) {
|
||||
addressUpdates.postalCode = postalCode;
|
||||
}
|
||||
|
||||
await queryRunner.manager.update(Address, { id: addressEntity.id }, addressUpdates);
|
||||
}
|
||||
|
||||
await queryRunner.commitTransaction();
|
||||
|
||||
Reference in New Issue
Block a user