update users
Build and Deploy Docker Images / build_and_deploy (push) Has been cancelled

This commit is contained in:
2026-07-22 11:24:46 +03:30
parent 19c396e9d1
commit 10035f3f25
@@ -2,7 +2,7 @@ import { BadRequestException, Injectable, Logger } from "@nestjs/common";
import { DataSource, Not } from "typeorm"; import { DataSource, Not } from "typeorm";
import { IUserIpAndHeaders } from "../../../common/decorators/user.decorator"; 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 { UserType } from "../../access-logs/enums/user-type.enum";
import { AccessLogService } from "../../access-logs/providers/access-log.service"; import { AccessLogService } from "../../access-logs/providers/access-log.service";
import { Address } from "../../address/entities/address.entity"; 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 }); await queryRunner.manager.update(this.userRepository.target, { id: customerId }, { password: hashPassword });
} }
const legalUser = await queryRunner.manager.findOne(this.legalUserRepository.target, { const hasLegalUpdateData = Boolean(
where: { user: { id: customerId } }, registrationName || economicCode || nationalIdentity || registrationCode || postalCode || address || cityId,
relations: { address: true }, );
});
if (!legalUser) throw new BadRequestException(UserMessage.LEGAL_DATA_NOT_FOUND); if (hasLegalUpdateData) {
const legalUser = await queryRunner.manager.findOne(this.legalUserRepository.target, {
if (economicCode) { where: { user: { id: customerId } },
const existingEconomicCode = await queryRunner.manager.findOne(this.legalUserRepository.target, { relations: { address: true },
where: { economicCode, id: Not(legalUser.id) },
}); });
if (existingEconomicCode) throw new BadRequestException(UserMessage.ECONOMIC_CODE_EXIST);
}
if (registrationCode) { if (!legalUser) {
const existingRegistrationCode = await queryRunner.manager.findOne(this.legalUserRepository.target, { if (user.financialType === FinancialType.REAL) {
where: { registrationCode, id: Not(legalUser.id) }, throw new BadRequestException(UserMessage.FINANCIAL_TYPE_IS_REAL);
}); }
if (existingRegistrationCode) throw new BadRequestException(UserMessage.REGISTRATION_CODE_EXIST);
}
if (nationalIdentity) { const legalPhone = phone ?? user.phone;
const existingNationalIdentity = await queryRunner.manager.findOne(this.legalUserRepository.target, {
where: { nationalIdentity, id: Not(legalUser.id) },
});
if (existingNationalIdentity) throw new BadRequestException(UserMessage.NATIONAL_IDENTITY_EXIST);
}
if (registrationName) { if (!economicCode) throw new BadRequestException(FinancialMessage.ECONOMIC_CODE_REQUIRED);
const existingRegistrationName = await queryRunner.manager.findOne(this.legalUserRepository.target, { if (!registrationCode) throw new BadRequestException(FinancialMessage.REGISTRATION_CODE_REQUIRED);
where: { registrationName, id: Not(legalUser.id) }, if (!registrationName) throw new BadRequestException(FinancialMessage.REGISTRATION_NAME_REQUIRED);
}); if (!nationalIdentity) throw new BadRequestException(FinancialMessage.NATIONAL_ID_REQUIRED);
if (existingRegistrationName) throw new BadRequestException(UserMessage.REGISTRATION_NAME_EXIST); 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 (registrationCode) {
if (cityId || address || postalCode) { const existingRegistrationCode = await queryRunner.manager.findOne(this.legalUserRepository.target, {
// Get the address entity where: { registrationCode, id: Not(legalUser.id) },
const addressEntity = legalUser.address; });
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 if (registrationName) {
const addressUpdates: Record<string, unknown> = {}; 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) { await queryRunner.manager.update(this.legalUserRepository.target, { id: legalUser.id }, legalUserUpdateData);
const { city } = await this.addressService.getCity(+cityId);
addressUpdates.city = city; 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(); await queryRunner.commitTransaction();