forked from Shared/esg
update the esg
This commit is contained in:
72
src/claims/services/claim-expert-resolver.service.ts
Normal file
72
src/claims/services/claim-expert-resolver.service.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user