Skip to content

Commit

Permalink
Fix team import when importing multiple teams with the same non-exist…
Browse files Browse the repository at this point in the history
…ing category or affiliation.
  • Loading branch information
nickygerritsen committed Nov 1, 2024
1 parent 3f10304 commit 87769c6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
27 changes: 27 additions & 0 deletions webapp/src/Service/ImportExportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions webapp/tests/Unit/Service/ImportExportServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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',
],
],
];

Expand Down

0 comments on commit 87769c6

Please sign in to comment.