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,13 +180,47 @@ 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 hasLegalUpdateData = Boolean(
registrationName || economicCode || nationalIdentity || registrationCode || postalCode || address || cityId,
);
if (hasLegalUpdateData) {
const legalUser = await queryRunner.manager.findOne(this.legalUserRepository.target, { const legalUser = await queryRunner.manager.findOne(this.legalUserRepository.target, {
where: { user: { id: customerId } }, where: { user: { id: customerId } },
relations: { address: true }, relations: { address: true },
}); });
if (!legalUser) throw new BadRequestException(UserMessage.LEGAL_DATA_NOT_FOUND); if (!legalUser) {
if (user.financialType === FinancialType.REAL) {
throw new BadRequestException(UserMessage.FINANCIAL_TYPE_IS_REAL);
}
const legalPhone = phone ?? user.phone;
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 this.createLegalUserData(
{
economicCode,
registrationCode,
registrationName,
nationalIdentity,
phone: legalPhone,
address,
postalCode,
cityId: +cityId,
},
customerId,
queryRunner,
);
} else {
if (economicCode) { if (economicCode) {
const existingEconomicCode = await queryRunner.manager.findOne(this.legalUserRepository.target, { const existingEconomicCode = await queryRunner.manager.findOne(this.legalUserRepository.target, {
where: { economicCode, id: Not(legalUser.id) }, where: { economicCode, id: Not(legalUser.id) },
@@ -217,14 +251,11 @@ export class CustomersService {
await queryRunner.manager.update(this.legalUserRepository.target, { id: legalUser.id }, legalUserUpdateData); await queryRunner.manager.update(this.legalUserRepository.target, { id: legalUser.id }, legalUserUpdateData);
// Update address if needed
if (cityId || address || postalCode) { if (cityId || address || postalCode) {
// Get the address entity
const addressEntity = legalUser.address; const addressEntity = legalUser.address;
if (!addressEntity) throw new BadRequestException(UserMessage.ADDRESS_NOT_FOUND); if (!addressEntity) throw new BadRequestException(UserMessage.ADDRESS_NOT_FOUND);
// Prepare address updates
const addressUpdates: Record<string, unknown> = {}; const addressUpdates: Record<string, unknown> = {};
if (cityId) { if (cityId) {
@@ -242,6 +273,8 @@ export class CustomersService {
await queryRunner.manager.update(Address, { id: addressEntity.id }, addressUpdates); await queryRunner.manager.update(Address, { id: addressEntity.id }, addressUpdates);
} }
}
}
await queryRunner.commitTransaction(); await queryRunner.commitTransaction();