-
Notifications
You must be signed in to change notification settings - Fork 1
Ensure coordinator fallback preserves access when API is down #191
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,12 +60,55 @@ function get_coordinator() | |
| $query = "SELECT id, user FROM coordinator order by id"; | ||
| //--- | ||
| $u_data = super_function($api_params, [], $query); | ||
| // Ensure all coordinator rows expose an `active` flag for downstream | ||
| // consumers. The SQL fallback currently only returns the `id` and | ||
| // `user` columns, so we inject a default active=1 flag in that case to | ||
| // match the API payload. | ||
| $u_data = normalize_coordinator_rows($u_data); | ||
| // --- | ||
| $coordinator = $u_data; | ||
| // --- | ||
| return $u_data; | ||
| } | ||
|
|
||
| function normalize_coordinator_rows(array $rows): array | ||
| { | ||
| return array_map(static function ($row) { | ||
| if (!is_array($row)) { | ||
| return $row; | ||
| } | ||
|
|
||
| $active = $row['active'] ?? null; | ||
| if ($active === null || $active === '') { | ||
| $active = 1; | ||
| } | ||
| $row['active'] = (int) $active; | ||
|
|
||
| return $row; | ||
| }, $rows); | ||
| } | ||
|
|
||
| function coordinator_active_index(array $rows): array | ||
| { | ||
| $normalized = normalize_coordinator_rows($rows); | ||
| $coordinators = []; | ||
|
|
||
| foreach ($normalized as $row) { | ||
| if (!is_array($row)) { | ||
| continue; | ||
| } | ||
|
|
||
| $user = $row['user'] ?? ''; | ||
| if ($user === '') { | ||
| continue; | ||
| } | ||
|
|
||
| $coordinators[$user] = (int) ($row['active'] ?? 0); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since $coordinators[$user] = $row['active']; |
||
| } | ||
|
|
||
| return $coordinators; | ||
| } | ||
|
|
||
| function get_user_pages($user_main, $year_y, $lang_y) | ||
| { | ||
| // --- | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use function SQLorAPI\Funcs\coordinator_active_index; | ||
| use function SQLorAPI\Funcs\normalize_coordinator_rows; | ||
|
|
||
| final class CoordinatorFallbackTest extends TestCase | ||
| { | ||
| public function testNormalizeCoordinatorRowsAddsDefaultActive(): void | ||
| { | ||
| $rows = [ | ||
| ['user' => 'Alice'], | ||
| ['user' => 'Bob', 'active' => '0'], | ||
| ['user' => 'Carol', 'active' => 1], | ||
| ]; | ||
|
|
||
| $normalized = normalize_coordinator_rows($rows); | ||
|
|
||
| $this->assertSame(1, $normalized[0]['active']); | ||
| $this->assertSame(0, $normalized[1]['active']); | ||
| $this->assertSame(1, $normalized[2]['active']); | ||
| } | ||
|
|
||
| public function testCoordinatorActiveIndexTreatsMissingActiveAsActive(): void | ||
| { | ||
| $rows = [ | ||
| ['user' => 'Alice'], | ||
| ['user' => 'Bob', 'active' => 0], | ||
| ]; | ||
|
|
||
| $index = coordinator_active_index($rows); | ||
|
|
||
| $this->assertSame([ | ||
| 'Alice' => 1, | ||
| 'Bob' => 0, | ||
| ], $index); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
get_coordinatorfunction now normalizes the data, andcoordinator_active_indexalso normalizes the data. When called ascoordinator_active_index(get_coordinator())insrc/header.php, the data is normalized twice. While this is not incorrect, it is inefficient. Consider removing the normalization from one of the functions to avoid redundant work. A good approach might be to remove this line and letcoordinator_active_indexbe responsible for normalization, which aligns with its test case and its role as a 'reusable helper'.