Skip to content

Commit 87769c6

Browse files
Fix team import when importing multiple teams with the same non-existing category or affiliation.
1 parent 3f10304 commit 87769c6

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

webapp/src/Service/ImportExportService.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,9 @@ public function importAccountsJson(array $data, ?string &$message = null, ?array
11271127
*/
11281128
protected function importTeamData(array $teamData, ?string &$message, ?array &$saved = null): int
11291129
{
1130+
/** @var TeamAffiliation[] $createdAffiliations */
11301131
$createdAffiliations = [];
1132+
/** @var TeamCategory[] $createdCategories */
11311133
$createdCategories = [];
11321134
$createdTeams = [];
11331135
$updatedTeams = [];
@@ -1140,6 +1142,14 @@ protected function importTeamData(array $teamData, ?string &$message, ?array &$s
11401142
if (!empty($teamItem['team_affiliation']['shortname'])) {
11411143
// First look up if the affiliation already exists.
11421144
$teamAffiliation = $this->em->getRepository(TeamAffiliation::class)->findOneBy(['shortname' => $teamItem['team_affiliation']['shortname']]);
1145+
if (!$teamAffiliation) {
1146+
foreach ($createdAffiliations as $createdAffiliation) {
1147+
if ($createdAffiliation->getShortname() === $teamItem['team_affiliation']['shortname']) {
1148+
$teamAffiliation = $createdAffiliation;
1149+
break;
1150+
}
1151+
}
1152+
}
11431153
if (!$teamAffiliation) {
11441154
$teamAffiliation = new TeamAffiliation();
11451155
$propertyAccessor = PropertyAccess::createPropertyAccessor();
@@ -1166,6 +1176,15 @@ protected function importTeamData(array $teamData, ?string &$message, ?array &$s
11661176
}
11671177
} elseif (!empty($teamItem['team_affiliation']['externalid'])) {
11681178
$teamAffiliation = $this->em->getRepository(TeamAffiliation::class)->findOneBy(['externalid' => $teamItem['team_affiliation']['externalid']]);
1179+
if (!$teamAffiliation) {
1180+
foreach ($createdAffiliations as $createdAffiliation) {
1181+
if ($createdAffiliation->getExternalid() === $teamItem['team_affiliation']['externalid']) {
1182+
$teamAffiliation = $createdAffiliation;
1183+
break;
1184+
}
1185+
}
1186+
}
1187+
11691188
if (!$teamAffiliation) {
11701189
$teamAffiliation = new TeamAffiliation();
11711190
$teamAffiliation
@@ -1196,6 +1215,14 @@ protected function importTeamData(array $teamData, ?string &$message, ?array &$s
11961215

11971216
if (!empty($teamItem['team']['categoryid'])) {
11981217
$teamCategory = $this->em->getRepository(TeamCategory::class)->findOneBy(['externalid' => $teamItem['team']['categoryid']]);
1218+
if (!$teamCategory) {
1219+
foreach ($createdCategories as $createdCategory) {
1220+
if ($createdCategory->getExternalid() === $teamItem['team']['categoryid']) {
1221+
$teamCategory = $createdCategory;
1222+
break;
1223+
}
1224+
}
1225+
}
11991226
if (!$teamCategory) {
12001227
$teamCategory = new TeamCategory();
12011228
$teamCategory

webapp/tests/Unit/Service/ImportExportServiceTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ public function testImportTeamsTsv(): void
679679
File_Version 2
680680
11 447047 24 ¡i¡i¡ Lund University LU SWE INST-42
681681
12 447837 25 Pleading not FAUlty Friedrich-Alexander-University Erlangen-Nuremberg FAU DEU INST-43
682+
13 447057 24 Another team from Lund Lund University LU SWE INST-42
682683
EOF;
683684

684685
$expectedTeams = [
@@ -710,6 +711,20 @@ public function testImportTeamsTsv(): void
710711
'name' => 'Friedrich-Alexander-University Erlangen-Nuremberg',
711712
'country' => 'DEU',
712713
],
714+
], [
715+
'externalid' => '13',
716+
'icpcid' => '447057',
717+
'label' => null,
718+
'name' => 'Another team from Lund',
719+
'category' => [
720+
'externalid' => '24',
721+
],
722+
'affiliation' => [
723+
'externalid' => '42',
724+
'shortname' => 'LU',
725+
'name' => 'Lund University',
726+
'country' => 'SWE',
727+
],
713728
],
714729
];
715730

0 commit comments

Comments
 (0)