forked from Yara724/api
153 lines
4.6 KiB
TypeScript
153 lines
4.6 KiB
TypeScript
import {
|
|
BadRequestException,
|
|
Body,
|
|
Controller,
|
|
Get,
|
|
HttpCode,
|
|
Injectable,
|
|
Param,
|
|
PipeTransform,
|
|
Post,
|
|
UseGuards,
|
|
UsePipes,
|
|
ValidationPipe,
|
|
} from "@nestjs/common";
|
|
import {
|
|
ApiBearerAuth,
|
|
ApiBody,
|
|
ApiOperation,
|
|
ApiParam,
|
|
ApiResponse,
|
|
ApiTags,
|
|
} from "@nestjs/swagger";
|
|
import { LocalActorAuthGuard } from "src/auth/guards/actor-local.guard";
|
|
import { RolesGuard } from "src/auth/guards/role.guard";
|
|
import { Roles } from "src/decorators/roles.decorator";
|
|
import { CurrentUser } from "src/decorators/user.decorator";
|
|
import { RoleEnum } from "src/Types&Enums/role.enum";
|
|
import {
|
|
CreatePriceEstimateDto,
|
|
PriceEstimateJobDto,
|
|
} from "./price-estimation.dto";
|
|
import { PriceEstimationService } from "./price-estimation.service";
|
|
|
|
@Injectable()
|
|
export class EstimateObjectIdPipe implements PipeTransform {
|
|
transform(value: string) {
|
|
if (!/^[a-f\d]{24}$/i.test(value))
|
|
throw new BadRequestException(
|
|
"Expected a 24-character MongoDB identifier",
|
|
);
|
|
return value;
|
|
}
|
|
}
|
|
|
|
@ApiTags("AI market price estimation (MVP)")
|
|
@ApiBearerAuth()
|
|
@Controller("v2/price-estimation/claims/:claimRequestId")
|
|
@UseGuards(LocalActorAuthGuard, RolesGuard)
|
|
@Roles(
|
|
RoleEnum.DAMAGE_EXPERT,
|
|
RoleEnum.FIELD_EXPERT,
|
|
RoleEnum.FILE_REVIEWER,
|
|
RoleEnum.FILE_MAKER,
|
|
)
|
|
@ApiParam({
|
|
name: "claimRequestId",
|
|
description: "Existing claim ID (not the blame ID or public file number).",
|
|
example: "670000000000000000000001",
|
|
})
|
|
@UsePipes(
|
|
new ValidationPipe({
|
|
transform: true,
|
|
whitelist: true,
|
|
forbidNonWhitelisted: true,
|
|
}),
|
|
)
|
|
export class PriceEstimationController {
|
|
constructor(private readonly estimates: PriceEstimationService) {}
|
|
|
|
@Post()
|
|
@HttpCode(202)
|
|
@ApiOperation({
|
|
summary: "Request an advisory parts/vehicle/depreciation estimate",
|
|
description:
|
|
"Requires OPENAI_PRICE_ESTIMATION_ENABLED=true and a separate API key. Queues image analysis and live market web research; returns immediately without awaiting OpenAI. Identical inputs/model/photos are deduplicated for the research day (Asia/Tehran). All output money is RIALS. Existing workflow/prices are unchanged. Empty body {} is supported.",
|
|
})
|
|
@ApiBody({
|
|
type: CreatePriceEstimateDto,
|
|
examples: {
|
|
automatic: { value: {} },
|
|
knownVehicleValue: {
|
|
value: {
|
|
currentMarketValueRial: 8000000000,
|
|
modelYear: 1402,
|
|
mileageKm: 45000,
|
|
city: "تهران",
|
|
partQuality: "oem",
|
|
},
|
|
},
|
|
},
|
|
})
|
|
@ApiResponse({ status: 202, type: PriceEstimateJobDto })
|
|
@ApiResponse({
|
|
status: 503,
|
|
description: "Estimation disabled or job persistence unavailable.",
|
|
})
|
|
request(
|
|
@Param("claimRequestId", EstimateObjectIdPipe) id: string,
|
|
@CurrentUser() actor: any,
|
|
@Body() body: CreatePriceEstimateDto,
|
|
) {
|
|
return this.estimates.request(id, actor, body);
|
|
}
|
|
|
|
@Get()
|
|
@ApiOperation({
|
|
summary: "Get the latest estimate job/result for this claim",
|
|
description:
|
|
"Poll every 5 seconds while queued/processing. Cached results remain readable while estimation is disabled; current claim access is still required.",
|
|
})
|
|
@ApiResponse({ status: 200, type: PriceEstimateJobDto })
|
|
@ApiResponse({ status: 404, description: "No estimate requested yet." })
|
|
latest(
|
|
@Param("claimRequestId", EstimateObjectIdPipe) id: string,
|
|
@CurrentUser() actor: any,
|
|
) {
|
|
return this.estimates.get(id, actor);
|
|
}
|
|
|
|
@Get(":estimateId")
|
|
@ApiParam({ name: "estimateId", description: "Job ID returned by POST." })
|
|
@ApiOperation({
|
|
summary: "Get a particular estimate snapshot",
|
|
description:
|
|
"Job must belong to the authorized claim; returns its own input snapshot, not the latest case data.",
|
|
})
|
|
@ApiResponse({ status: 200, type: PriceEstimateJobDto })
|
|
get(
|
|
@Param("claimRequestId", EstimateObjectIdPipe) id: string,
|
|
@Param("estimateId", EstimateObjectIdPipe) estimateId: string,
|
|
@CurrentUser() actor: any,
|
|
) {
|
|
return this.estimates.get(id, actor, estimateId);
|
|
}
|
|
|
|
@Post(":estimateId/retry")
|
|
@HttpCode(202)
|
|
@ApiParam({ name: "estimateId", description: "Failed job ID." })
|
|
@ApiOperation({
|
|
summary: "Retry a failed estimate",
|
|
description:
|
|
"Retries the immutable snapshot. If vehicle/parts/photos have changed, POST a fresh request instead. Concurrent retries reuse the queued/processing job.",
|
|
})
|
|
@ApiResponse({ status: 202, type: PriceEstimateJobDto })
|
|
retry(
|
|
@Param("claimRequestId", EstimateObjectIdPipe) id: string,
|
|
@Param("estimateId", EstimateObjectIdPipe) estimateId: string,
|
|
@CurrentUser() actor: any,
|
|
) {
|
|
return this.estimates.retry(id, estimateId, actor);
|
|
}
|
|
}
|