Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Bandaid::getEmployees #117

Merged
merged 3 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion app/Library/Bandaid.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function cachedPost($url, $body) {
$response = Http::withHeaders($this->headers)
->post(
$this->baseUri . $url,
['form_params' => $body]
$body
);

$value = json_decode($response->body());
Expand Down Expand Up @@ -243,4 +243,30 @@ public function getTermsOverlappingDates($startDate, $endDate) {
return ($isTermStartInRange || $isTermEndInRange);
});
}

/**
* This gets name info for a list of emplids. This can be used
* as a fallback when a user is not found in LDAP.
*
* @param int[] $emplids
* @return Collection<array{
* id: int,
* EMPLID: int,
* NAME: string,
* FIRST_NAME: string,
* LAST_NAME: string,
* MIDDLE_NAME: string,
* INTERNET_ID: string,
* }>
*/
public function getNames(array $emplids): Collection {
try {
$result = $this->cachedPost('/names', ["emplids" => $emplids]);
return collect($result);
} catch (RequestException $e) {
$msg = $e->getMessage();
$errorMessage = 'getEmployees Error: ' . $msg;
throw new RuntimeException($errorMessage);
}
}
}
65 changes: 50 additions & 15 deletions app/Library/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,24 @@ public function findOrCreateManyByEmplId(array $emplids): Collection {
return $user;
})->filter();

// identify users that dont exist in the DB
// find emplids that don't exist in the DB
$missingEmplids = $uniqueEmplids->diff($dbUsers->pluck('emplid'));

// we'll use this to look up fallback names info from Bandaid
$emplidsNotInLDAP = [];

// lookup and created missing users from LDAP
$newUsers = $missingEmplids
->map(function ($emplid) {
$newUsersFromLDAPInfo = $missingEmplids
->map(function ($emplid) use (&$emplidsNotInLDAP) {
$paddedEmplid = str_pad($emplid, 7, 0, STR_PAD_LEFT);
$ldapUser = LDAP::lookupUserCached($paddedEmplid, 'umnemplid');

if (!$ldapUser || !$ldapUser->emplid) return;
if (!$ldapUser || !$ldapUser->emplid) {
// add the $emplid to the list of users not found in LDAP
// so we can look up fallback names info from Bandaid
$emplidsNotInLDAP[] = $emplid;
return null;
};

return User::updateOrCreate(
// emplid may be null for some users, so we're using
Expand All @@ -61,8 +69,29 @@ public function findOrCreateManyByEmplId(array $emplids): Collection {
);
})->filter();

// now we handle emplids that aren't found in LDAP
// getting as much info as possible from bandaid
$newUsersFromBandaidInfo = $this->bandaid
->getNames($emplidsNotInLDAP)
->map(function ($bandaidUser) {
return User::updateOrCreate(
['umndid' => $bandaidUser->INTERNET_ID],
[
'givenname' => $bandaidUser->FIRST_NAME,
'surname' => $bandaidUser->LAST_NAME,
'displayName' => $bandaidUser->NAME,
'umndid' => $bandaidUser->INTERNET_ID,
'email' => $bandaidUser->INTERNET_ID . '@umn.edu',
'emplid' => (int) $bandaidUser->EMPLID,
]
);
})
->filter();

// return the requested users if they exist
return $dbUsers->concat($newUsers);
return $dbUsers
->concat($newUsersFromLDAPInfo)
->concat($newUsersFromBandaidInfo);
}

public function findOrCreateByEmplId(int $emplid): ?User {
Expand All @@ -71,24 +100,30 @@ public function findOrCreateByEmplId(int $emplid): ?User {
}

public function getDeptEmployees(string $deptId): Collection {
$sisDeptEmployees = $this->bandaid->getEmployeesForDepartment($deptId);
$deptCourses = $this->bandaid->getDeptClassList($deptId);
$allDeptEmplids = collect($deptCourses)
->pluck('INSTRUCTOR_EMPLID')
->unique()
->filter()
->values()
->toArray();

$sisDeptEmployeeLookup = collect($sisDeptEmployees)->keyBy('EMPLID');
// get employee info from bandaid for job code and category
// note: only active employees will have a job code
$activeDeptEmployees = $this->bandaid->getEmployees($allDeptEmplids);

$deptEmplIds = $sisDeptEmployeeLookup->keys()->toArray();
$activeDeptEmployeeLookup = collect($activeDeptEmployees)->keyBy('EMPLID');

$users = $this
->findOrCreateManyByEmplId($deptEmplIds)
->map(function ($user) use ($sisDeptEmployeeLookup) {
$sisDeptEmployee = $sisDeptEmployeeLookup[$user->emplid];
->findOrCreateManyByEmplId($allDeptEmplids)
->map(function ($user) use ($activeDeptEmployeeLookup) {
$activeDeptEmployee = $activeDeptEmployeeLookup->get($user->emplid) ?? null;

// add the sis employee info to the user
$user->jobCategory = $sisDeptEmployee->CATEGORY;
$user->jobCode = $sisDeptEmployee?->JOBCODE;
$user->jobCategory = $activeDeptEmployee?->CATEGORY ?? null;
$user->jobCode = $activeDeptEmployee?->JOBCODE ?? null;
return $user;
});


return $users->values();
}
}
Loading