From 9a65071276f74c594e828cb33b87283c34490a67 Mon Sep 17 00:00:00 2001 From: SepehrYahyaee <7heycallmegray@gmail.com> Date: Wed, 15 Apr 2026 12:32:25 +0330 Subject: [PATCH] YARA-725 --- src/expert-blame/expert-blame.service.ts | 25 +++++++++++++++++++++++- src/expert-claim/expert-claim.service.ts | 14 +++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/expert-blame/expert-blame.service.ts b/src/expert-blame/expert-blame.service.ts index 4100b2a..60c0f42 100644 --- a/src/expert-blame/expert-blame.service.ts +++ b/src/expert-blame/expert-blame.service.ts @@ -617,12 +617,35 @@ export class ExpertBlameService { } async lockRequest(requestId: string, actorDetail) { + const existing = await this.requestManagementDbService.findOne(requestId); + if (!existing) { + throw new NotFoundException("Request not found"); + } + if (existing.blameStatus === ReqBlameStatus.UserPending) { + throw new BadRequestException( + "Cannot lock request because blameStatus is UserPending", + ); + } + + const isLocked = !!existing.lockFile; + const lockedByCurrent = + String(existing?.actorLocked?.actorId || "") === String(actorDetail.sub); + const isLockExpired = + !!existing.unlockTime && Date.now() >= new Date(existing.unlockTime).getTime(); + + // Idempotent behavior: same expert can re-enter their locked file. + if (isLocked && lockedByCurrent && !isLockExpired) { + return { _id: requestId, lock: true, message: "Already locked by you" }; + } + if (isLocked && !lockedByCurrent && !isLockExpired) { + throw new BadRequestException("Request is locked by another expert"); + } + const fifteenMinutes = new Date(Date.now() + 15 * 60 * 1000); const updateResult = await this.requestManagementDbService.findOneAndUpdate( { _id: requestId, - lockFile: false, blameStatus: { $ne: ReqBlameStatus.UserPending }, }, { diff --git a/src/expert-claim/expert-claim.service.ts b/src/expert-claim/expert-claim.service.ts index ee7f0c7..c3d0332 100644 --- a/src/expert-claim/expert-claim.service.ts +++ b/src/expert-claim/expert-claim.service.ts @@ -319,8 +319,18 @@ export class ExpertClaimService { await this.claimRequestManagementDbService.findOne(requestId); if (!request) throw new BadRequestException("Claim request not found"); - if (request.lockFile) { - throw new BadRequestException("Claim request is locked"); + const isLocked = !!request.lockFile; + const lockedByCurrent = + String(request?.actorLocked?.actorId || "") === String(actorDetail.sub); + const isLockExpired = + !!request.unlockTime && Date.now() >= new Date(request.unlockTime).getTime(); + + // Idempotent behavior: same expert can re-enter their locked file. + if (isLocked && lockedByCurrent && !isLockExpired) { + return { _id: requestId, lock: true, message: "Already locked by you" }; + } + if (isLocked && !lockedByCurrent && !isLockExpired) { + throw new BadRequestException("Claim request is locked by another expert"); } if (request.claimStatus === ReqClaimStatus.UserPending) {