Fixed legacy requestedCounts methods and statistics + claimLink address

This commit is contained in:
SepehrYahyaee
2026-04-27 15:29:44 +03:30
parent 362e02ddc4
commit c2f996cc28
17 changed files with 441 additions and 394 deletions

View File

@@ -67,6 +67,11 @@ import { resendRequestHasPayload } from "src/helpers/claim-expert-resend";
import { snapshotFromDamageExpert } from "src/helpers/expert-profile-snapshot";
import { DamageExpertModel } from "src/users/entities/schema/damage-expert.schema";
import { SmsOrchestrationService } from "src/sms-orchestration/sms-orchestration.service";
import { ExpertFileActivityDbService } from "src/users/entities/db-service/expert-file-activity.db.service";
import {
ExpertFileActivityType,
ExpertFileKind,
} from "src/users/entities/schema/expert-file-activity.schema";
@Injectable()
export class ExpertClaimService {
@@ -191,6 +196,7 @@ export class ExpertClaimService {
private readonly branchDbService: BranchDbService,
private readonly smsOrchestrationService: SmsOrchestrationService,
private readonly userDbService: UserDbService,
private readonly expertFileActivityDbService: ExpertFileActivityDbService,
) { }
/** Load immutable profile fields from `damage-expert` for the acting expert. */
@@ -199,6 +205,30 @@ export class ExpertClaimService {
return snapshotFromDamageExpert(doc as DamageExpertModel);
}
private async recordClaimExpertActivity(args: {
expertId: string;
tenantId: string;
claimId: string;
eventType: ExpertFileActivityType;
idempotencyKey?: string;
}): Promise<void> {
if (
!Types.ObjectId.isValid(args.expertId) ||
!Types.ObjectId.isValid(args.tenantId) ||
!Types.ObjectId.isValid(args.claimId)
) {
return;
}
await this.expertFileActivityDbService.recordEvent({
expertId: args.expertId,
tenantId: args.tenantId,
fileId: args.claimId,
fileType: ExpertFileKind.CLAIM,
eventType: args.eventType,
idempotencyKey: args.idempotencyKey,
});
}
/** Owner mobile: linked blame party phone when available, else `users.mobile`. */
private async resolveClaimOwnerPhone(claim: any): Promise<string | undefined> {
if (!claim?.owner?.userId) return undefined;
@@ -366,7 +396,6 @@ export class ExpertClaimService {
}
async getClaimRequestsListForExpert(actor): Promise<ClaimListDtoRs> {
console.log(actor);
// const requests = await this.claimRequestManagementDbService.findAllByStatus(
// {
// claimStatus: {
@@ -545,11 +574,13 @@ export class ExpertClaimService {
},
);
await this.damageExpertDbService.updateStats(
actorDetail.sub,
"checked",
requestId,
);
await this.recordClaimExpertActivity({
expertId: String(actorDetail.sub),
tenantId: String(request.userClientKey ?? actorDetail.clientKey ?? ""),
claimId: String(requestId),
eventType: ExpertFileActivityType.CHECKED,
idempotencyKey: `claim:${requestId}:checked:${actorDetail.sub}`,
});
this.unlockApi(request, fifteenMinutes.getTime() - Date.now());
@@ -1420,11 +1451,13 @@ export class ExpertClaimService {
);
if (!isObjection) {
await this.damageExpertDbService.updateStats(
userId.sub,
"handled",
requestId,
);
await this.recordClaimExpertActivity({
expertId: String(userId.sub),
tenantId: String(request.userClientKey ?? userId.clientKey ?? ""),
claimId: String(requestId),
eventType: ExpertFileActivityType.HANDLED,
idempotencyKey: `claim:${requestId}:handled:${userId.sub}`,
});
}
return {
@@ -1492,7 +1525,6 @@ export class ExpertClaimService {
async streamServiceV2(requestId, query, res, header) {
const request = await this.claimCaseDbService.findById(requestId);
console.log(request)
// const blameCase = await this.blameCaseDbService
return request;
// let videoPath: string = "";
@@ -1942,6 +1974,14 @@ export class ExpertClaimService {
$set,
});
await this.recordClaimExpertActivity({
expertId: String(actor.sub),
tenantId: String(actor.clientKey),
claimId: String(claimRequestId),
eventType: ExpertFileActivityType.CHECKED,
idempotencyKey: `claim:${claimRequestId}:checked:${actor.sub}`,
});
const updatedClaim = await this.claimCaseDbService.findById(claimRequestId);
const updatedReply =
replyField === "damageExpertReplyFinal"
@@ -2034,6 +2074,14 @@ export class ExpertClaimService {
},
});
await this.recordClaimExpertActivity({
expertId: String(actor.sub),
tenantId: String(actor.clientKey),
claimId: String(claimRequestId),
eventType: ExpertFileActivityType.UNCHECKED,
idempotencyKey: `claim:${claimRequestId}:unchecked:resend:${actor.sub}`,
});
const ownerPhoneFactors = await this.resolveClaimOwnerPhone(claim);
if (ownerPhoneFactors) {
const expertLastName =
@@ -2076,11 +2124,13 @@ export class ExpertClaimService {
},
);
await this.damageExpertDbService.updateStats(
actorDetail.sub,
"handled",
requestId,
);
await this.recordClaimExpertActivity({
expertId: String(actorDetail.sub),
tenantId: String(request.userClientKey ?? actorDetail.clientKey ?? ""),
claimId: String(requestId),
eventType: ExpertFileActivityType.HANDLED,
idempotencyKey: `claim:${requestId}:handled:${actorDetail.sub}`,
});
return updated;
}
@@ -2459,6 +2509,14 @@ export class ExpertClaimService {
await this.claimCaseDbService.findByIdAndUpdate(claimRequestId, updatePayload);
await this.recordClaimExpertActivity({
expertId: String(actor.sub),
tenantId: String(actor.clientKey),
claimId: String(claimRequestId),
eventType: ExpertFileActivityType.HANDLED,
idempotencyKey: `claim:${claimRequestId}:handled:${actor.sub}`,
});
// Same effective step as WAITING_FOR_INSURER_APPROVAL @ INSURER_REVIEW: owner reviews/signs (yara-signature).
if (!needsFactorUpload) {
const ownerPhone = await this.resolveClaimOwnerPhone(claim);
@@ -2556,6 +2614,14 @@ export class ExpertClaimService {
},
});
await this.recordClaimExpertActivity({
expertId: String(actor.sub),
tenantId: String(actor.clientKey),
claimId: String(claimRequestId),
eventType: ExpertFileActivityType.HANDLED,
idempotencyKey: `claim:${claimRequestId}:handled:inperson:${actor.sub}`,
});
return {
claimRequestId,
status: claim.status,
@@ -2992,6 +3058,15 @@ export class ExpertClaimService {
update as any,
{ new: false },
);
if (cleared && lockedById && claim.owner?.userClientKey) {
await this.recordClaimExpertActivity({
expertId: String(lockedById),
tenantId: String(claim.owner.userClientKey),
claimId: String(claimRequestId),
eventType: ExpertFileActivityType.UNCHECKED,
idempotencyKey: `claim:${claimRequestId}:unchecked:expired:${lockedById}`,
});
}
return !!cleared;
}