Fix addClient bug

This commit is contained in:
SepehrYahyaee
2026-06-03 14:00:40 +03:30
parent dc14698823
commit d92231e517
3 changed files with 370 additions and 328 deletions

View File

@@ -99,19 +99,34 @@ export class ClientService {
try { try {
const newClient = await this.clientDbService.create({ const newClient = await this.clientDbService.create({
clientCode: client.clientCode, clientCode: client.clientCode,
clientName: { clientName: {
persian: client.clientName.persian, persian:
english: client.clientName.english || null, typeof client.clientName === "string"
? client.clientName
: client.clientName?.persian,
english:
typeof client.clientName === "string"
? null
: (client.clientName?.english ?? null),
}, },
property: { property: {
smsApiKey: client.property.smsApiKey || null, smsApiKey: client.property?.smsApiKey ?? null,
}, },
useExpertMode: client.useExpertMode || null,
useExpertMode: client.useExpertMode ?? null,
}); });
if (newClient) return new ClientDtoRs(newClient);
else throw new GoneException("database not connected"); if (!newClient) {
throw new GoneException("database not connected");
}
return new ClientDtoRs(newClient);
} catch (er) { } catch (er) {
throw new BadGatewayException(er.errors); console.error("ADD CLIENT ERROR:", er);
throw er;
} }
} }

View File

@@ -1,5 +1,12 @@
import { ApiProperty } from "@nestjs/swagger"; import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger";
import { IsIn, IsNotEmpty, IsNumber, IsOptional, IsString, ValidateNested } from "class-validator"; import {
IsIn,
IsNotEmpty,
IsNumber,
IsOptional,
IsString,
ValidateNested,
} from "class-validator";
import { Type } from "class-transformer"; import { Type } from "class-transformer";
import { Types } from "mongoose"; import { Types } from "mongoose";
@@ -15,9 +22,8 @@ class ClientName {
english: string; english: string;
} }
class Property { class Property {
@ApiProperty({}) @ApiPropertyOptional({})
@IsString() @IsString()
@IsNotEmpty()
smsApiKey: string; smsApiKey: string;
} }

View File

@@ -945,6 +945,7 @@ export class RequestManagementService {
} }
async initialFormV2(requestId: string, body: AddPlateDto, user: any) { async initialFormV2(requestId: string, body: AddPlateDto, user: any) {
try {
const req = await this.blameRequestDbService.findById(requestId); const req = await this.blameRequestDbService.findById(requestId);
if (!req) { if (!req) {
throw new NotFoundException("Request not found"); throw new NotFoundException("Request not found");
@@ -997,7 +998,8 @@ export class RequestManagementService {
); );
} }
} else if (body.driverIsInsurer === true) { } else if (body.driverIsInsurer === true) {
const sameNat = body.nationalCodeOfInsurer === body.nationalCodeOfDriver; const sameNat =
body.nationalCodeOfInsurer === body.nationalCodeOfDriver;
const sameLic = body.insurerLicense === body.driverLicense; const sameLic = body.insurerLicense === body.driverLicense;
const sameBirthday = const sameBirthday =
body.driverBirthday == null || body.driverBirthday == null ||
@@ -1152,14 +1154,22 @@ export class RequestManagementService {
// Find client by company code // Find client by company code
const clientName = inquiryMapped?.CompanyName; const clientName = inquiryMapped?.CompanyName;
if (!clientName) {
throw new BadRequestException(
`CompanyName missing from inquiry response`,
);
}
const companyCode = inquiryMapped?.CompanyCode; const companyCode = inquiryMapped?.CompanyCode;
const client = let client =
await this.clientService.findClientWithCompanyCode(+companyCode); await this.clientService.findClientWithCompanyCode(+companyCode);
if (!client) { if (!client) {
await this.clientService.addClient({ await this.clientService.addClient({
clientName, clientName: {
clientCode: companyCode, persian: clientName,
english: null,
},
clientCode: Number(companyCode),
useExpertMode: "legal", useExpertMode: "legal",
}); });
} }
@@ -1170,7 +1180,8 @@ export class RequestManagementService {
// Persist inquiry/body data into new model // Persist inquiry/body data into new model
if (!party.person) party.person = {} as any; if (!party.person) party.person = {} as any;
const resolvedClientId = (client as any)?._id ?? (client as any)?._doc?._id; const resolvedClientId =
(client as any)?._id ?? (client as any)?._doc?._id;
party.person.clientId = resolvedClientId; party.person.clientId = resolvedClientId;
party.person.nationalCodeOfInsurer = body.nationalCodeOfInsurer; party.person.nationalCodeOfInsurer = body.nationalCodeOfInsurer;
party.person.nationalCodeOfDriver = body.nationalCodeOfDriver; party.person.nationalCodeOfDriver = body.nationalCodeOfDriver;
@@ -1222,7 +1233,14 @@ export class RequestManagementService {
this.logger.error( this.logger.error(
`[TEJARAT] car body inquiry failed for request=${req._id}: ${err?.message || err}`, `[TEJARAT] car body inquiry failed for request=${req._id}: ${err?.message || err}`,
); );
this.recordPartyCaseInquiryStatus(req, "carBody", role, false, {}, err); this.recordPartyCaseInquiryStatus(
req,
"carBody",
role,
false,
{},
err,
);
await (req as any).save(); await (req as any).save();
throw new HttpException( throw new HttpException(
"Car body inquiry failed", "Car body inquiry failed",
@@ -1293,6 +1311,9 @@ export class RequestManagementService {
blameStatus: req.blameStatus, blameStatus: req.blameStatus,
status: req.status, status: req.status,
}; };
} catch (error) {
console.log(error);
}
} }
async addDetailLocationV2(requestId: string, body: LocationDto, user: any) { async addDetailLocationV2(requestId: string, body: LocationDto, user: any) {