forked from Yara724/api
79 lines
2.5 KiB
TypeScript
79 lines
2.5 KiB
TypeScript
import type { ExpertFileAssignStatus } from "src/common/dto/expert-file-assign-result.dto";
|
|
|
|
export const BLAME_REVIEW_ASSIGNED_HISTORY_TYPE = "BLAME_ASSIGNED";
|
|
export const CLAIM_REVIEW_ASSIGNED_HISTORY_TYPE = "CLAIM_ASSIGNED";
|
|
|
|
type WorkflowAssigneeRef = {
|
|
assignedForReviewBy?: { actorId?: unknown; actorName?: string };
|
|
lockedBy?: { actorId?: unknown; actorName?: string };
|
|
lockedAt?: Date | string;
|
|
};
|
|
|
|
type HistoryAssigneeRef = {
|
|
type?: string;
|
|
actor?: { actorId?: unknown };
|
|
timestamp?: Date | string;
|
|
};
|
|
|
|
/** Expert who first assigned via the review assign endpoint (survives lock TTL expiry). */
|
|
export function resolvePersistentReviewAssigneeId(
|
|
workflow: WorkflowAssigneeRef | undefined,
|
|
history: HistoryAssigneeRef[] | undefined,
|
|
assignedHistoryType: string,
|
|
): string {
|
|
const fromField = workflow?.assignedForReviewBy?.actorId;
|
|
if (fromField != null && String(fromField).length > 0) {
|
|
return String(fromField);
|
|
}
|
|
const fromHistory = history?.find((h) => h.type === assignedHistoryType)
|
|
?.actor?.actorId;
|
|
if (fromHistory != null && String(fromHistory).length > 0) {
|
|
return String(fromHistory);
|
|
}
|
|
return "";
|
|
}
|
|
|
|
export function buildExpertAssignConflictForOtherReviewer(
|
|
assigneeId: string,
|
|
workflow?: WorkflowAssigneeRef,
|
|
): {
|
|
success: false;
|
|
status: ExpertFileAssignStatus;
|
|
message: string;
|
|
lockedBy: { actorId: string; actorName?: string; lockedAt?: string };
|
|
} {
|
|
const assignee = workflow?.assignedForReviewBy ?? workflow?.lockedBy;
|
|
const lockedAt = workflow?.lockedAt;
|
|
return {
|
|
success: false,
|
|
status: "locked",
|
|
message: "Request is already being answered by another expert",
|
|
lockedBy: {
|
|
actorId: assigneeId,
|
|
actorName: assignee?.actorName,
|
|
lockedAt: lockedAt ? new Date(lockedAt as Date).toISOString() : undefined,
|
|
},
|
|
};
|
|
}
|
|
|
|
export function blameWorkflowAssigneeToPersistOnLockExpiry(
|
|
workflow: any,
|
|
request: any,
|
|
) {
|
|
const hasDecision = !!request?.expert?.decision;
|
|
return hasDecision ? (workflow?.assignedForReviewBy ?? null) : null;
|
|
}
|
|
|
|
export function claimWorkflowAssigneeToPersistOnLockExpiry(
|
|
workflow: any,
|
|
claim: any,
|
|
): typeof workflow.assignedForReviewBy | null {
|
|
// If a decision or resend was already submitted, preserve ownership
|
|
const hasDecision =
|
|
!!claim?.evaluation?.damageExpertReply ||
|
|
!!claim?.evaluation?.damageExpertReplyFinal ||
|
|
!!claim?.evaluation?.damageExpertResend;
|
|
|
|
return hasDecision ? (workflow?.assignedForReviewBy ?? null) : null;
|
|
}
|