From 821f972b8003a1f11905f2dc7730a38fe9be49ca Mon Sep 17 00:00:00 2001 From: David mattei Date: Fri, 12 Jul 2024 14:37:25 +0200 Subject: [PATCH] fix: throw better DuplicateOuuidException - newDocument is called on api update - createData is called from ui --- .../src/Exception/DuplicateOuuidException.php | 8 +++++ EMS/core-bundle/src/Service/DataService.php | 33 +++++-------------- 2 files changed, 17 insertions(+), 24 deletions(-) diff --git a/EMS/core-bundle/src/Exception/DuplicateOuuidException.php b/EMS/core-bundle/src/Exception/DuplicateOuuidException.php index 5e6eaeddb..319b4f51f 100644 --- a/EMS/core-bundle/src/Exception/DuplicateOuuidException.php +++ b/EMS/core-bundle/src/Exception/DuplicateOuuidException.php @@ -1,7 +1,15 @@ setEndTime(null); $newRevision->setDeleted(false); $newRevision->setDraft(true); - if ($byARealUser) { - $token = $this->tokenStorage->getToken(); - if (null === $token) { - throw new \RuntimeException('Unexpected null token'); - } - $newRevision->setLockBy($token->getUserIdentifier()); - } else { - $newRevision->setLockBy('DATA_SERVICE'); - } + + $lockBy = $byARealUser ? $this->userService->getCurrentUser()->getUserIdentifier() : 'DATA_SERVICE'; + $newRevision->setLockBy($lockBy); + $newRevision->setLockUntil($until); $newRevision->setRawData($rawdata); - $em = $this->doctrine->getManager(); - if (!empty($ouuid)) { - $revisionRepository = $em->getRepository(Revision::class); - $anotherObject = $revisionRepository->findOneBy([ - 'contentType' => $contentType, - 'ouuid' => $newRevision->getOuuid(), - 'endTime' => null, - ]); - - if (!empty($anotherObject)) { - throw new ConflictHttpException('Duplicate OUUID '.$ouuid.' for this content type'); - } + if (null !== $ouuid && null !== $this->revRepository->getCurrentRevision($contentType, $ouuid)) { + throw new DuplicateOuuidException($ouuid, $contentType->getName()); } + $em = $this->doctrine->getManager(); $em->persist($newRevision); $em->flush(); @@ -939,8 +924,8 @@ public function newDocument(ContentType $contentType, ?string $ouuid = null, ?ar $revision = new Revision(); - if (null !== $ouuid && $revisionRepository->countRevisions($ouuid, $contentType)) { - throw new DuplicateOuuidException(); + if (null !== $ouuid && $revisionRepository->getCurrentRevision($contentType, $ouuid)) { + throw new DuplicateOuuidException($ouuid, $contentType->getName()); } if (!empty($contentType->getDefaultValue())) {