Skip to content

Commit

Permalink
feat: add more feedback to portals importer
Browse files Browse the repository at this point in the history
  • Loading branch information
simonostendorf committed Aug 23, 2024
1 parent d438f90 commit 2f010b7
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions app/Http/Controllers/PortalsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public function importUsers(Request $request)
], 401);
}

// if "deleteUsers" url parameter is set to true, all users that are not in the import anymore will be deleted
$deleteUsers = ($request->deleteUsers == 'true');

// call APP_PORTALS_URL
$client = new Client();
Expand All @@ -60,14 +62,31 @@ public function importUsers(Request $request)
// get users
$users = json_decode($body, true)['users'];

// save all removed and added users
$removedUsers = [];
$addedUsers = [];
$updatedUsers = [];
$doNotRemovePersonIds = [];

// loop through users
foreach($users as $user) {
// check if Person with id exists if not create new Person
$person = Person::firstOrNew(['id' => $user['id']]);

array_push($doNotRemovePersonIds, $user['id']);

// set attributes
if($person->id == null) {
// this could be a problem if the id is/was used by a another person that was created/imported before
// because we handle the complete user management via portals import and do not manage persons manually here, this is not a problem
$person->id = $user['id'];
array_push($addedUsers, $user['id'] . " (" . $user['email'] . ")");
} else {
if ($person->email == $user['email']) {
array_push($updatedUsers, $person->id . " (" . $person->email . ")");
} else {
array_push($updatedUsers, $person->id . " (" . $person->email . " - " . $user['email'] . ")");
}
}
$person->firstname = $user['firstname'];
$person->lastname = $user['lastname'];
Expand All @@ -93,8 +112,12 @@ public function importUsers(Request $request)
$person->course = $abbreviation;
}

// import image path
$person->img = $user['avatar'];
// import image path, if null set empty string
if (isset($user['avatar'])) {
$person->img = $user['avatar'];
} else {
$person->img = "";
}

// check roles
$roles = $user['roles'];
Expand All @@ -116,13 +139,23 @@ public function importUsers(Request $request)

// save Person
$person->save();
}

if ($deleteUsers) {
$deletePersons = Person::whereNotIn('id', $doNotRemovePersonIds)->get();
foreach($deletePersons as $person) {
$person->delete();
array_push($removedUsers, $person->id . " (" . $person->email . ")");
}
}

// return response
return response()->json([
'message' => 'User imported',
'status' => $statusCode
'status' => $statusCode,
'addedUsers' => $addedUsers,
'removedUsers' => $removedUsers,
'updatedUsers' => $updatedUsers
], 200);
}
}

0 comments on commit 2f010b7

Please sign in to comment.