From 6d445127c9d7bd7039e23e4cc9c16aab31dddf21 Mon Sep 17 00:00:00 2001 From: Nicky Gerritsen Date: Fri, 1 Nov 2024 08:53:02 +0100 Subject: [PATCH] Fix team import when importing multiple teams with the same non-existing category or affiliation. --- webapp/src/Service/ImportExportService.php | 27 +++++++++++++++++++ .../Unit/Service/ImportExportServiceTest.php | 15 +++++++++++ 2 files changed, 42 insertions(+) diff --git a/webapp/src/Service/ImportExportService.php b/webapp/src/Service/ImportExportService.php index bfea25e3fe..be947c60a0 100644 --- a/webapp/src/Service/ImportExportService.php +++ b/webapp/src/Service/ImportExportService.php @@ -1127,7 +1127,9 @@ public function importAccountsJson(array $data, ?string &$message = null, ?array */ protected function importTeamData(array $teamData, ?string &$message, ?array &$saved = null): int { + /** @var TeamAffiliation[] $createdAffiliations */ $createdAffiliations = []; + /** @var TeamCategory[] $createdCategories */ $createdCategories = []; $createdTeams = []; $updatedTeams = []; @@ -1140,6 +1142,14 @@ protected function importTeamData(array $teamData, ?string &$message, ?array &$s if (!empty($teamItem['team_affiliation']['shortname'])) { // First look up if the affiliation already exists. $teamAffiliation = $this->em->getRepository(TeamAffiliation::class)->findOneBy(['shortname' => $teamItem['team_affiliation']['shortname']]); + if (!$teamAffiliation) { + foreach ($createdAffiliations as $createdAffiliation) { + if ($createdAffiliation->getShortname() === $teamItem['team_affiliation']['shortname']) { + $teamAffiliation = $createdAffiliation; + break; + } + } + } if (!$teamAffiliation) { $teamAffiliation = new TeamAffiliation(); $propertyAccessor = PropertyAccess::createPropertyAccessor(); @@ -1166,6 +1176,15 @@ protected function importTeamData(array $teamData, ?string &$message, ?array &$s } } elseif (!empty($teamItem['team_affiliation']['externalid'])) { $teamAffiliation = $this->em->getRepository(TeamAffiliation::class)->findOneBy(['externalid' => $teamItem['team_affiliation']['externalid']]); + if (!$teamAffiliation) { + foreach ($createdAffiliations as $createdAffiliation) { + if ($createdAffiliation->getExternalid() === $teamItem['team_affiliation']['externalid']) { + $teamAffiliation = $createdAffiliation; + break; + } + } + } + if (!$teamAffiliation) { $teamAffiliation = new TeamAffiliation(); $teamAffiliation @@ -1196,6 +1215,14 @@ protected function importTeamData(array $teamData, ?string &$message, ?array &$s if (!empty($teamItem['team']['categoryid'])) { $teamCategory = $this->em->getRepository(TeamCategory::class)->findOneBy(['externalid' => $teamItem['team']['categoryid']]); + if (!$teamCategory) { + foreach ($createdCategories as $createdCategory) { + if ($createdCategory->getExternalid() === $teamItem['team']['categoryid']) { + $teamCategory = $createdCategory; + break; + } + } + } if (!$teamCategory) { $teamCategory = new TeamCategory(); $teamCategory diff --git a/webapp/tests/Unit/Service/ImportExportServiceTest.php b/webapp/tests/Unit/Service/ImportExportServiceTest.php index 0146879233..e7cc24a9e5 100644 --- a/webapp/tests/Unit/Service/ImportExportServiceTest.php +++ b/webapp/tests/Unit/Service/ImportExportServiceTest.php @@ -679,6 +679,7 @@ public function testImportTeamsTsv(): void File_Version 2 11 447047 24 ¡i¡i¡ Lund University LU SWE INST-42 12 447837 25 Pleading not FAUlty Friedrich-Alexander-University Erlangen-Nuremberg FAU DEU INST-43 +13 447057 24 Another team from Lund Lund University LU SWE INST-42 EOF; $expectedTeams = [ @@ -710,6 +711,20 @@ public function testImportTeamsTsv(): void 'name' => 'Friedrich-Alexander-University Erlangen-Nuremberg', 'country' => 'DEU', ], + ], [ + 'externalid' => '13', + 'icpcid' => '447057', + 'label' => null, + 'name' => 'Another team from Lund', + 'category' => [ + 'externalid' => '24', + ], + 'affiliation' => [ + 'externalid' => '42', + 'shortname' => 'LU', + 'name' => 'Lund University', + 'country' => 'SWE', + ], ], ];