forked from Yara724/api
Fixed legacy requestedCounts methods and statistics + claimLink address
This commit is contained in:
@@ -71,6 +71,11 @@ import { WorkflowStepDbService } from "src/workflow-step-management/entities/db-
|
||||
import { WorkflowStepModel } from "src/workflow-step-management/entities/schema/workflow-step.schema";
|
||||
import { BlameStatus } from "src/Types&Enums/blame-request-management/blameStatus.enum";
|
||||
import { CaseStatus } from "src/Types&Enums/blame-request-management/caseStatus.enum";
|
||||
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";
|
||||
import { UploadContext } from "./entities/schema/blame-voice.schema";
|
||||
import { HashService } from "src/utils/hash/hash.service";
|
||||
import { UserAuthService } from "src/auth/auth-services/user.auth.service";
|
||||
@@ -309,6 +314,7 @@ export class RequestManagementService {
|
||||
private readonly userSign: UserSignDbService,
|
||||
private readonly smsOrchestrationService: SmsOrchestrationService,
|
||||
private readonly expertDbService: ExpertDbService,
|
||||
private readonly expertFileActivityDbService: ExpertFileActivityDbService,
|
||||
private readonly autoCloseRequestService: AutoCloseRequestService,
|
||||
private readonly claimRequestManagementDbService: ClaimRequestManagementDbService,
|
||||
private readonly claimCaseDbService: ClaimCaseDbService,
|
||||
@@ -2370,9 +2376,6 @@ export class RequestManagementService {
|
||||
}
|
||||
|
||||
if (p.firstPartyDetails.firstPartyPhoneNumber == phoneNumber) {
|
||||
console.log(
|
||||
new Date(p.createdAt).toLocaleTimeString("fa-IR").split(","),
|
||||
);
|
||||
res.listOfFirstParty.push({
|
||||
id: p.id,
|
||||
blameStatus: p.blameStatus,
|
||||
@@ -2746,76 +2749,28 @@ export class RequestManagementService {
|
||||
return null;
|
||||
}
|
||||
|
||||
private async updateDamageExpertStats(
|
||||
expertId: string,
|
||||
requestId: string,
|
||||
type: "checked" | "handled",
|
||||
) {
|
||||
if (!expertId || !requestId || !["checked", "handled"].includes(type)) {
|
||||
console.warn("Invalid expertId, requestId, or type");
|
||||
private async recordBlameExpertActivity(args: {
|
||||
expertId: string;
|
||||
tenantId: string;
|
||||
requestId: string;
|
||||
eventType: ExpertFileActivityType;
|
||||
idempotencyKey?: string;
|
||||
}): Promise<void> {
|
||||
if (
|
||||
!Types.ObjectId.isValid(args.expertId) ||
|
||||
!Types.ObjectId.isValid(args.tenantId) ||
|
||||
!Types.ObjectId.isValid(args.requestId)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate that both expertId and requestId are valid ObjectId strings
|
||||
if (!Types.ObjectId.isValid(expertId)) {
|
||||
console.warn("Invalid ObjectId format for expertId:", expertId);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Types.ObjectId.isValid(requestId)) {
|
||||
console.warn("Invalid ObjectId format for requestId:", requestId);
|
||||
return;
|
||||
}
|
||||
|
||||
const expert = await this.expertDbService.findOne({
|
||||
_id: new Types.ObjectId(expertId),
|
||||
await this.expertFileActivityDbService.recordEvent({
|
||||
expertId: args.expertId,
|
||||
tenantId: args.tenantId,
|
||||
fileId: args.requestId,
|
||||
fileType: ExpertFileKind.BLAME,
|
||||
eventType: args.eventType,
|
||||
idempotencyKey: args.idempotencyKey,
|
||||
});
|
||||
|
||||
if (!expert) {
|
||||
console.warn("Expert not found:", expertId);
|
||||
return;
|
||||
}
|
||||
|
||||
const requestIdStr = new Types.ObjectId(requestId).toString();
|
||||
const countedRequestIds =
|
||||
expert.countedRequests?.map((id) => id.toString()) || [];
|
||||
|
||||
if (type === "checked" && countedRequestIds.includes(requestIdStr)) {
|
||||
console.log(
|
||||
`Request ${requestIdStr} already checked for expert ${expertId}`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const update: any = { $inc: {}, $push: {} };
|
||||
|
||||
if (type === "checked") {
|
||||
update.$inc["requestStats.totalChecked"] = 1;
|
||||
update.$push["countedRequests"] = requestIdStr;
|
||||
} else if (type === "handled") {
|
||||
update.$inc["requestStats.totalHandled"] = 1;
|
||||
|
||||
if (countedRequestIds.includes(requestIdStr)) {
|
||||
update.$inc["requestStats.totalChecked"] = -1;
|
||||
}
|
||||
|
||||
if (!countedRequestIds.includes(requestIdStr)) {
|
||||
update.$push["countedRequests"] = requestIdStr;
|
||||
} else {
|
||||
delete update.$push;
|
||||
}
|
||||
}
|
||||
|
||||
const updateResult = await this.expertDbService.findOneAndUpdate(
|
||||
{ _id: new Types.ObjectId(expertId) },
|
||||
update,
|
||||
);
|
||||
|
||||
if (!updateResult) {
|
||||
console.warn("Failed to update expert stats for:", expertId);
|
||||
} else {
|
||||
console.log(`Expert stats updated (${type}) for expert:`, expertId);
|
||||
}
|
||||
}
|
||||
|
||||
private async handleDisagreement(
|
||||
@@ -3006,11 +2961,17 @@ export class RequestManagementService {
|
||||
request._id.toString() :
|
||||
String(request._id);
|
||||
|
||||
await this.updateDamageExpertStats(
|
||||
actorIdStr,
|
||||
requestIdStr,
|
||||
"handled",
|
||||
);
|
||||
const tenantIdStr =
|
||||
request.firstPartyDetails?.firstPartyClient?.clientId?.toString?.() ||
|
||||
request.secondPartyDetails?.secondPartyClient?.clientId?.toString?.() ||
|
||||
"";
|
||||
await this.recordBlameExpertActivity({
|
||||
expertId: actorIdStr,
|
||||
tenantId: tenantIdStr,
|
||||
requestId: requestIdStr,
|
||||
eventType: ExpertFileActivityType.HANDLED,
|
||||
idempotencyKey: `blame:${requestIdStr}:handled:${actorIdStr}`,
|
||||
});
|
||||
}
|
||||
await this.requestManagementDbService.findByIdAndUpdate(request._id, {
|
||||
$set: { isHandledStatsUpdated: true },
|
||||
|
||||
Reference in New Issue
Block a user