Files
yara724-api/src/fanavaran/fanavaran.controller.ts
T
s.hajizadeh 99e6fa0850 fix(fanavaran): honor supplied submission payload fields
Apply every supplied JSON field over generated defaults, preserving IDs, nested values and explicit nulls across base, damage and expertise submits. Validate manual hull payloads without rewriting their values or premium, and derive an empty replacement flag for old hull licences.
2026-11-21 13:33:41 +03:30

325 lines
11 KiB
TypeScript

import {
BadRequestException,
Body,
Controller,
Get,
Param,
Post,
Query,
UseGuards,
} from "@nestjs/common";
import {
ApiBearerAuth,
ApiBody,
ApiOperation,
ApiParam,
ApiQuery,
ApiTags,
} from "@nestjs/swagger";
import { LocalActorAuthGuard } from "src/auth/guards/actor-local.guard";
import { ClaimRequestManagementService } from "src/claim-request-management/claim-request-management.service";
import {
FANAVARAN_CLIENT_KEYS,
FANAVARAN_CLIENT_SWAGGER_ENUM,
isFanavaranClientKey,
listFanavaranClientProfiles,
normalizeFanavaranClientKey,
resolveFanavaranClientKey,
} from "src/core/config/fanavaran-client.config";
@ApiTags("fanavaran")
@Controller("v2/fanavaran")
@ApiBearerAuth()
@UseGuards(LocalActorAuthGuard)
export class FanavaranController {
constructor(
private readonly claimRequestManagementService: ClaimRequestManagementService,
) {}
@Get("clients")
@ApiOperation({
summary: "List supported Fanavaran insurance clients",
description:
"Returns configured Fanavaran tenants (parsian, tejaratno, moallem) and which client is active for this deployment.",
})
listClients() {
const activeClient = resolveFanavaranClientKey();
return {
activeClient,
clients: listFanavaranClientProfiles().map((profile) => ({
key: profile.key,
defaults: profile.defaults,
isActive: profile.key === activeClient,
})),
};
}
@Get(":client/claim-cases/:claimCaseId/base-claim/preview")
@ApiOperation({
summary: "Preview Fanavaran base claim create payload",
description:
"GET has no request body. Always rebuilds GEN.03 from live policy + VIN/vehicle inquiries. Does not reuse fanavaranSync lastPayload / PolicyId / UsedId cache. Missing staff ClaimExpertId does not fail the preview — it returns ClaimExpertId=null plus previewWarnings with the exact missing field/staff. Auth token is still shared until Asia/Tehran midnight.",
})
@ApiParam({
name: "client",
description: "Fanavaran tenant key",
enum: FANAVARAN_CLIENT_SWAGGER_ENUM,
})
@ApiParam({
name: "claimCaseId",
description: "Claim case MongoDB ObjectId",
})
@ApiQuery({
name: "debug",
required: false,
description: "When true, returns payload plus mapping debug steps",
})
@ApiQuery({
name: "forceRefreshPolicy",
required: false,
deprecated: true,
description: "Ignored. Preview always re-inquires PolicyId.",
})
@ApiQuery({
name: "resolvePolicy",
required: false,
deprecated: true,
description: "Ignored. Preview always re-inquires PolicyId.",
})
async preview(
@Param("client") client: string,
@Param("claimCaseId") claimCaseId: string,
@Query("debug") debug?: string,
@Query("forceRefreshPolicy") _forceRefreshPolicy?: string,
@Query("resolvePolicy") _resolvePolicy?: string,
) {
const clientKey = this.parseClientParam(client);
return await this.claimRequestManagementService.previewFanavaranSubmitV2(
claimCaseId,
clientKey,
{
debug: debug === "1" || debug === "true",
forceRefreshPolicy: true,
forceRefreshUsedId: true,
requirePolicyId: false,
requireClaimExpertId: false,
},
);
}
@Post(":client/claim-cases/:claimCaseId/base-claim/submit")
@ApiOperation({
summary: "Submit Fanavaran base claim create request",
description:
"Optional JSON overrides the generated GEN.03 defaults. Every supplied field takes precedence, including lookup IDs, nested objects, arrays and explicit null. Omitted fields retain generated defaults. Validation checks the final payload without rewriting supplied values.",
})
@ApiParam({
name: "client",
description: "Fanavaran tenant key",
enum: FANAVARAN_CLIENT_SWAGGER_ENUM,
})
@ApiParam({
name: "claimCaseId",
description: "Claim case MongoDB ObjectId",
})
@ApiBody({
required: false,
description:
"Optional JSON payload overrides. All supplied fields are applied as-is; arrays and nested objects replace their generated counterparts. Explicit null is preserved.",
schema: { type: "object", additionalProperties: true, example: {} },
})
async submit(
@Param("client") client: string,
@Param("claimCaseId") claimCaseId: string,
@Body() body?: Record<string, unknown>,
) {
const clientKey = this.parseClientParam(client);
return await this.claimRequestManagementService.submitFanavaranV2(
claimCaseId,
clientKey,
body && Object.keys(body).length > 0 ? body : undefined,
);
}
@Get(":client/claim-cases/:claimCaseId/damage-case/preview")
@ApiOperation({
summary: "Preview Fanavaran damage-case payload",
description:
"GET has no request body. Always rebuilds GEN.12 from live VIN/vehicle/driver inquiries. Does not reuse lastPayload / plaqueKindId / DriverId cache.",
})
@ApiParam({
name: "client",
description: "Fanavaran tenant key",
enum: FANAVARAN_CLIENT_SWAGGER_ENUM,
})
@ApiParam({
name: "claimCaseId",
description: "Claim case MongoDB ObjectId",
})
async previewDamageCase(
@Param("client") client: string,
@Param("claimCaseId") claimCaseId: string,
) {
const clientKey = this.parseClientParam(client);
return await this.claimRequestManagementService.previewFanavaranDamageCaseV2(
claimCaseId,
clientKey,
);
}
@Post(":client/claim-cases/:claimCaseId/damage-case/submit")
@ApiOperation({
summary: "Submit Fanavaran damage-case request",
description:
"Optional JSON overrides the generated GEN.12 defaults. Every supplied field takes precedence, including DriverId, plaque/vehicle IDs, nested objects, arrays and null. Omitted fields retain generated defaults.",
})
@ApiParam({
name: "client",
description: "Fanavaran tenant key",
enum: FANAVARAN_CLIENT_SWAGGER_ENUM,
})
@ApiParam({
name: "claimCaseId",
description: "Claim case MongoDB ObjectId",
})
@ApiBody({
required: false,
description:
"Optional JSON overrides. Every supplied field is applied as-is, including IDs and explicit null.",
schema: { type: "object", additionalProperties: true, example: {} },
})
async submitDamageCase(
@Param("client") client: string,
@Param("claimCaseId") claimCaseId: string,
@Body() body?: Record<string, unknown>,
) {
const clientKey = this.parseClientParam(client);
return await this.claimRequestManagementService.submitFanavaranDamageCaseV2(
claimCaseId,
clientKey,
body && Object.keys(body).length > 0 ? body : undefined,
);
}
@Get(":client/claim-cases/:claimCaseId/attachments/preview")
@ApiOperation({
summary: "Preview Fanavaran attachment upload plan",
description:
"Lists local documents and captured images for third-party GEN.07 or hull GEN.05. Returns external file IDs, saved upload status/errors, local file availability, and pending image counts.",
})
@ApiParam({
name: "client",
description: "Fanavaran tenant key",
enum: FANAVARAN_CLIENT_SWAGGER_ENUM,
})
@ApiParam({
name: "claimCaseId",
description: "Claim case MongoDB ObjectId",
})
async previewAttachments(
@Param("client") client: string,
@Param("claimCaseId") claimCaseId: string,
) {
const clientKey = this.parseClientParam(client);
return await this.claimRequestManagementService.previewFanavaranAttachmentsV2(
claimCaseId,
clientKey,
);
}
@Post(":client/claim-cases/:claimCaseId/attachments/submit")
@ApiOperation({
summary: "Submit missing Fanavaran attachments",
description:
"Uploads pending local images to third-party GEN.07 or hull GEN.05 and persists each returned fileId. Reports failures and skipped/missing local files. Uses one multipart request per image.",
})
@ApiParam({
name: "client",
description: "Fanavaran tenant key",
enum: FANAVARAN_CLIENT_SWAGGER_ENUM,
})
@ApiParam({
name: "claimCaseId",
description: "Claim case MongoDB ObjectId",
})
async submitAttachments(
@Param("client") client: string,
@Param("claimCaseId") claimCaseId: string,
) {
const clientKey = this.parseClientParam(client);
return await this.claimRequestManagementService.submitFanavaranAttachmentsV2(
claimCaseId,
clientKey,
);
}
@Get(":client/claim-cases/:claimCaseId/expertise/preview")
@ApiOperation({
summary: "Preview Fanavaran expertise payload",
description:
"GET has no request body. Rebuilds GEN.08 from the active expert reply and lookups on every call. Does not reuse lastPayload cache.",
})
@ApiParam({
name: "client",
description: "Fanavaran tenant key",
enum: FANAVARAN_CLIENT_SWAGGER_ENUM,
})
@ApiParam({
name: "claimCaseId",
description: "Claim case MongoDB ObjectId",
})
async previewExpertise(
@Param("client") client: string,
@Param("claimCaseId") claimCaseId: string,
) {
const clientKey = this.parseClientParam(client);
return await this.claimRequestManagementService.previewFanavaranExpertiseV2(
claimCaseId,
clientKey,
);
}
@Post(":client/claim-cases/:claimCaseId/expertise/submit")
@ApiOperation({
summary: "Submit Fanavaran expertise request",
description:
"Completes missing prerequisite stages in order before expertise: base claim, third-party damage case when applicable, then all pending attachments. Skipped images are retried; any image without a saved Fanavaran file ID blocks expertise with stage and pendingFiles details. Existing IDs/receipts are reused; existing expertise is not duplicated. Optional JSON overrides every supplied GEN.08 third-party or GEN.06 hull field, including DmgCaseId, ClaimExpertId, DmgSections and explicit null. Omitted fields retain generated defaults.",
})
@ApiParam({
name: "client",
description: "Fanavaran tenant key",
enum: FANAVARAN_CLIENT_SWAGGER_ENUM,
})
@ApiParam({
name: "claimCaseId",
description: "Claim case MongoDB ObjectId",
})
@ApiBody({
required: false,
description:
"Optional JSON overrides every supplied field. IDs, arrays, nested objects and explicit null take precedence over generated defaults.",
schema: { type: "object", additionalProperties: true, example: {} },
})
async submitExpertise(
@Param("client") client: string,
@Param("claimCaseId") claimCaseId: string,
@Body() body?: Record<string, unknown>,
) {
const clientKey = this.parseClientParam(client);
return await this.claimRequestManagementService.submitFanavaranExpertiseV2(
claimCaseId,
clientKey,
body && Object.keys(body).length > 0 ? body : undefined,
);
}
private parseClientParam(client: string) {
if (!isFanavaranClientKey(client)) {
throw new BadRequestException(
`Invalid Fanavaran client "${client}". Expected one of: ${FANAVARAN_CLIENT_KEYS.join(", ")}`,
);
}
return normalizeFanavaranClientKey(client);
}
}