Skip to content

Commit cae06ad

Browse files
Merge pull request #46 from lepidus/main
fix/check publication with type already exisits before registration (OMP 3.4.0)
2 parents ea9c1d8 + 1b72d37 commit cae06ad

5 files changed

Lines changed: 77 additions & 6 deletions

File tree

classes/repositories/ThothPublicationRepository.inc.php

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

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

classes/services/ThothPublicationService.inc.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,15 @@ public function register($publicationFormat, $thothWorkId, $chapterId = null)
4141
$thothPublication->setIsbn(null);
4242
}
4343

44-
$thothPublicationId = $this->repository->add($thothPublication);
44+
$thothPublicationId = $this->repository->getIdByType(
45+
$thothWorkId,
46+
$thothPublication->getPublicationType()
47+
);
48+
49+
if ($thothPublicationId === null) {
50+
$thothPublicationId = $this->repository->add($thothPublication);
51+
}
52+
4553
$publicationFormat->setData('thothPublicationId', $thothPublicationId);
4654

4755
ThothService::location()->registerByPublicationFormat($publicationFormat, $chapterId);
@@ -72,14 +80,27 @@ public function registerByChapter($chapter)
7280
->filterByAssoc(Application::ASSOC_TYPE_PUBLICATION_FORMAT)
7381
->getMany()
7482
);
83+
7584
$chapterSubmissionFiles = array_filter($submissionFiles, function ($submissionFile) use ($chapter) {
7685
return $submissionFile->getData('chapterId') == $chapter->getId();
7786
});
7887

88+
$publicationFormatIds = array_map(function ($file) {
89+
return $file->getData('assocId');
90+
}, $chapterSubmissionFiles);
91+
7992
$thothChapterId = $chapter->getData('thothChapterId');
8093
$publicationFormatDao = DAORegistry::getDAO('PublicationFormatDAO');
81-
foreach ($chapterSubmissionFiles as $chapterSubmissionFile) {
82-
$publicationFormat = $publicationFormatDao->getById($chapterSubmissionFile->getData('assocId'));
94+
95+
$publicationFormats = [];
96+
foreach (array_unique($publicationFormatIds) as $publicationFormatId) {
97+
$publicationFormat = $publicationFormatDao->getById($publicationFormatId);
98+
if ($publicationFormat) {
99+
$publicationFormats[$publicationFormatId] = $publicationFormat;
100+
}
101+
}
102+
103+
foreach ($publicationFormats as $publicationFormat) {
83104
$this->register($publicationFormat, $thothChapterId, $chapter->getId());
84105
}
85106
}

tests/classes/repositories/ThothPublicationRepositoryTest.php

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

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

tests/classes/services/ThothPublicationServiceTest.php

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

5353
$mockRepository = $this->getMockBuilder(ThothPublicationRepository::class)
5454
->setConstructorArgs([$this->getMockBuilder(ThothClient::class)->getMock()])
55-
->setMethods(['add'])
55+
->setMethods(['add', 'getIdByType'])
5656
->getMock();
5757
$mockRepository->expects($this->once())
5858
->method('add')
5959
->will($this->returnValue('4296c934-0f05-4920-a208-a5ab214b908a'));
60+
$mockRepository->expects($this->once())
61+
->method('getIdByType')
62+
->will($this->returnValue(null));
6063

6164
$mockPubFormat = $this->getMockBuilder(PublicationFormat::class)->getMock();
6265

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.2.8.3</release>
7-
<date>2025-08-05</date>
6+
<release>0.2.8.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)