Skip to content

Commit

Permalink
fix issue where hrEmployees.index was getting cached too late
Browse files Browse the repository at this point in the history
  • Loading branch information
fredbradley committed Feb 23, 2022
1 parent 9648e05 commit f8497ff
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions src/Controllers/HumanResourcesEmployeeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,29 @@ public function index(): Collection
{
$key = $this->institution->getConfigName() . 'hrEmployees.index';

$decoded = json_decode($this->pageRequest($this->endpoint, 1, ['expand' => 'customFields']));
$items = collect($decoded->employees)->map(function ($item) {
return new Employee($item);
});

$totalCount = $decoded->totalCount;
$pageNumber = $decoded->page + 1;
while ($pageNumber <= $decoded->totalPages) {
$decoded = json_decode($this->pageRequest($this->endpoint, $pageNumber, ['expand' => 'customFields']));

collect($decoded->employees)->map(function ($item) use ($items) {
$items->push(new Employee($item));
return Cache::remember($key, $this->getCacheDuration(), function() use ($key) {
$decoded = json_decode($this->pageRequest($this->endpoint, 1, ['expand' => 'customFields']));
$items = collect($decoded->employees)->map(function ($item) {
return new Employee($item);
});

$pageNumber++;
}
$totalCount = $decoded->totalCount;
$pageNumber = $decoded->page + 1;
while ($pageNumber <= $decoded->totalPages) {
$decoded = json_decode($this->pageRequest($this->endpoint, $pageNumber, ['expand' => 'customFields']));

if ($totalCount !== $items->count()) {
throw new Exception($items->count() . ' items were returned instead of ' . $totalCount . ' as specified on page 1.');
}
collect($decoded->employees)->map(function ($item) use ($items) {
$items->push(new Employee($item));
});

$pageNumber++;
}

$items = $this->sortBySurname($items);
if ($totalCount !== $items->count()) {
throw new Exception($items->count() . ' items were returned instead of ' . $totalCount . ' as specified on page 1.');
}

return Cache::remember($key, $this->getCacheDuration(), function () use ($items) {
return $items;
return $this->sortBySurname($items);
});
}

Expand Down

0 comments on commit f8497ff

Please sign in to comment.