update the esg

This commit is contained in:
2026-06-20 18:18:48 +03:30
parent 539e7a4060
commit b3adb6f583
17 changed files with 1086 additions and 1 deletions

View File

@@ -0,0 +1,72 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model, Types } from 'mongoose';
import { ConfigService } from '@nestjs/config';
import { FanavaranConfig } from '../../config/configuration';
import { FieldExpert, FieldExpertDocument } from '../schemas/field-expert.schema';
import { EvaluationRequestService } from './evaluation-request.service';
export interface ClaimExpertResolutionInput {
claimExpertId?: number;
evaluationRequestId?: string;
}
@Injectable()
export class ClaimExpertResolverService {
private readonly config: FanavaranConfig;
constructor(
configService: ConfigService,
@InjectModel(FieldExpert.name)
private readonly fieldExpertModel: Model<FieldExpertDocument>,
private readonly evaluationRequestService: EvaluationRequestService,
) {
this.config = configService.get<FanavaranConfig>('fanavaran')!;
}
async resolveClaimExpertId(input: ClaimExpertResolutionInput): Promise<number> {
if (input.claimExpertId !== undefined && input.claimExpertId !== null) {
return input.claimExpertId;
}
if (input.evaluationRequestId) {
const resolved = await this.resolveFromEvaluationHistory(input.evaluationRequestId);
if (resolved !== undefined) {
return resolved;
}
}
return this.config.defaultClaimExpertId;
}
private async resolveFromEvaluationHistory(
evaluationRequestId: string,
): Promise<number | undefined> {
const evaluationRequest =
await this.evaluationRequestService.findByRequestId(evaluationRequestId);
if (!evaluationRequest?.evaluations?.length) {
return undefined;
}
const latestEvaluation = [...evaluationRequest.evaluations].sort(
(left, right) => right.evaluatedAt.getTime() - left.evaluatedAt.getTime(),
)[0];
if (latestEvaluation.expertCode !== undefined && latestEvaluation.expertCode !== null) {
return latestEvaluation.expertCode;
}
if (!latestEvaluation.expertId) {
return undefined;
}
const expert = await this.fieldExpertModel
.findOne({
_id: new Types.ObjectId(latestEvaluation.expertId),
active: true,
})
.exec();
return expert?.expertCode;
}
}