Skip to content

Commit 7afbfbf

Browse files
Merge pull request #45 from lepidus/stable-3_3_0
fix/check publication with type already exisits before registration (OMP 3.3.0)
2 parents 542b229 + c957af7 commit 7afbfbf

5 files changed

Lines changed: 76 additions & 6 deletions

File tree

classes/repositories/ThothPublicationRepository.inc.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,28 @@ public function get($thothPublicationId)
3434
return $this->thothClient->publication($thothPublicationId);
3535
}
3636

37+
public function getIdByType($thothWorkId, $thothPublicationType)
38+
{
39+
$query = <<<GRAPHQL
40+
query(\$workId: Uuid!, \$publicationType: PublicationType!) {
41+
work(workId: \$workId) {
42+
publications(publicationTypes: [\$publicationType]) {
43+
publicationId
44+
}
45+
}
46+
}
47+
GRAPHQL;
48+
49+
$variables = [
50+
'workId' => $thothWorkId,
51+
'publicationType' => $thothPublicationType
52+
];
53+
54+
$result = $this->thothClient->rawQuery($query, $variables);
55+
$thothPublications = $result['work']['publications'];
56+
return !empty($thothPublications) ? $thothPublications[0]['publicationId'] : null;
57+
}
58+
3759
public function find($filter)
3860
{
3961
$thothPublications = $this->thothClient->publications([

classes/services/ThothPublicationService.inc.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,15 @@ public function register($publicationFormat, $thothWorkId, $chapterId = null)
3838
$thothPublication->setIsbn(null);
3939
}
4040

41-
$thothPublicationId = $this->repository->add($thothPublication);
41+
$thothPublicationId = $this->repository->getIdByType(
42+
$thothWorkId,
43+
$thothPublication->getPublicationType()
44+
);
45+
46+
if ($thothPublicationId === null) {
47+
$thothPublicationId = $this->repository->add($thothPublication);
48+
}
49+
4250
$publicationFormat->setData('thothPublicationId', $thothPublicationId);
4351

4452
ThothService::location()->registerByPublicationFormat($publicationFormat, $chapterId);
@@ -74,10 +82,22 @@ public function registerByChapter($chapter)
7482
return $submissionFile->getData('chapterId') == $chapter->getId();
7583
});
7684

85+
$publicationFormatIds = array_map(function ($file) {
86+
return $file->getData('assocId');
87+
}, $chapterSubmissionFiles);
88+
7789
$thothChapterId = $chapter->getData('thothChapterId');
7890
$publicationFormatDao = DAORegistry::getDAO('PublicationFormatDAO');
79-
foreach ($chapterSubmissionFiles as $chapterSubmissionFile) {
80-
$publicationFormat = $publicationFormatDao->getById($chapterSubmissionFile->getData('assocId'));
91+
92+
$publicationFormats = [];
93+
foreach (array_unique($publicationFormatIds) as $publicationFormatId) {
94+
$publicationFormat = $publicationFormatDao->getById($publicationFormatId);
95+
if ($publicationFormat) {
96+
$publicationFormats[$publicationFormatId] = $publicationFormat;
97+
}
98+
}
99+
100+
foreach ($publicationFormats as $publicationFormat) {
81101
$this->register($publicationFormat, $thothChapterId, $chapter->getId());
82102
}
83103
}

tests/classes/repositories/ThothPublicationRepositoryTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,31 @@ public function testGetPublication()
7070
$this->assertEquals($expectedThothPublication, $thothPublication);
7171
}
7272

73+
public function testGetPublicationByType()
74+
{
75+
$mockThothClient = $this->getMockBuilder(ThothClient::class)
76+
->setMethods(['rawQuery'])
77+
->getMock();
78+
$mockThothClient->expects($this->any())
79+
->method('rawQuery')
80+
->will($this->returnValue([
81+
'work' => ['publications' => [
82+
[
83+
'publicationId' => 'efac5d7a-2284-4432-ad50-02b70aadec49',
84+
]
85+
]]
86+
]));
87+
88+
$repository = new ThothPublicationRepository($mockThothClient);
89+
90+
$thothPublicationId = $repository->getIdByType(
91+
'a2c032c6-b09b-4911-a67b-17f97cb57cc1',
92+
ThothPublication::PUBLICATION_TYPE_PDF
93+
);
94+
95+
$this->assertEquals('efac5d7a-2284-4432-ad50-02b70aadec49', $thothPublicationId);
96+
}
97+
7398
public function testFindPublication()
7499
{
75100
$expectedThothPublication = new ThothPublication([

tests/classes/services/ThothPublicationServiceTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,14 @@ public function testRegisterPublication()
5050

5151
$mockRepository = $this->getMockBuilder(ThothPublicationRepository::class)
5252
->setConstructorArgs([$this->getMockBuilder(ThothClient::class)->getMock()])
53-
->setMethods(['add'])
53+
->setMethods(['add', 'getIdByType'])
5454
->getMock();
5555
$mockRepository->expects($this->once())
5656
->method('add')
5757
->will($this->returnValue('4296c934-0f05-4920-a208-a5ab214b908a'));
58+
$mockRepository->expects($this->once())
59+
->method('getIdByType')
60+
->will($this->returnValue(null));
5861

5962
$mockPubFormat = $this->getMockBuilder(PublicationFormat::class)->getMock();
6063

version.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
<version>
44
<application>thoth</application>
55
<type>plugins.generic</type>
6-
<release>0.1.10.3</release>
7-
<date>2025-08-05</date>
6+
<release>0.1.10.4</release>
7+
<date>2025-08-11</date>
88
<lazy-load>1</lazy-load>
99
<class>ThothPlugin</class>
1010
</version>

0 commit comments

Comments
 (0)