Skip to content

Commit

Permalink
feat(api): get organ functions through API
Browse files Browse the repository at this point in the history
  • Loading branch information
rinkp committed Oct 29, 2024
1 parent ef8939a commit f8a8428
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 23 deletions.
16 changes: 11 additions & 5 deletions module/Application/language/en.po

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions module/Application/language/gewisdb.pot

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions module/Application/language/nl.po

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions module/Database/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,15 @@
],
],
],
'organFunctions' => [
'type' => Literal::class,
'options' => [
'route' => '/organFunctions',
'defaults' => [
'action' => 'organFunctions',
],
],
],
],
],
],
Expand Down
19 changes: 19 additions & 0 deletions module/Database/src/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
namespace Database\Controller;

use Database\Model\Enums\ApiResponseStatuses;
use Database\Model\Enums\InstallationFunctions as OrganInstallationFunctions;
use Database\Service\Api as ApiService;
use Laminas\Http\Response as HttpResponse;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\Mvc\I18n\Translator;
use Laminas\View\Model\JsonModel;
use RuntimeException;
use User\Model\Enums\ApiPermissions;
Expand All @@ -18,6 +20,7 @@
class ApiController extends AbstractActionController
{
public function __construct(
private readonly Translator $translator,
private readonly ApiService $apiService,
private readonly ApiAuthenticationService $apiAuthService,
) {
Expand Down Expand Up @@ -118,6 +121,22 @@ public function membersActiveAction(): JsonModel
return new JsonModel($res);
}

/**
* Return organ functions
*/
public function organFunctionsAction(): JsonModel
{
$this->apiAuthService->assertCan(ApiPermissions::OrganFunctionsListR);

$functions = OrganInstallationFunctions::getMultilangArray($this->translator);

$res = [
'data' => $functions,
];

return new JsonModel($res);
}

/**
* To follow best practices, we generate a 204 for empty datasets
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Database\Controller\ApiController;
use Database\Service\Api as ApiService;
use Laminas\Mvc\I18n\Translator;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Psr\Container\ContainerInterface;
use User\Service\ApiAuthenticationService;
Expand All @@ -20,10 +21,12 @@ public function __invoke(
$requestedName,
?array $options = null,
): ApiController {
$translator = $container->get(Translator::class);
$apiService = $container->get(ApiService::class);
$apiAuthService = $container->get(ApiAuthenticationService::class);

return new ApiController(
$translator,
$apiService,
$apiAuthService,
);
Expand Down
41 changes: 41 additions & 0 deletions module/Database/src/Model/Enums/InstallationFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,45 @@ static function ($case) use ($includeAdministrative, $includeLegacy, $includeCur
}, $cases),
);
}

/**
* Returns a list of functions (and its translations)
*
* @return array<non-empty-string, array{
* isAdministrative: bool,
* isLegacy: bool,
* translations: non-empty-array<array-key, string>
* }>
*/
public static function getMultilangArray(
Translator $translator,
bool $includeAdministrative = true,
bool $includeLegacy = true,
bool $includeCurrent = true,
): array {
$cases = array_filter(
self::cases(),
static function ($case) use ($includeAdministrative, $includeLegacy, $includeCurrent) {
return (!$case->isLegacy() || $includeLegacy) &&
(!$case->isAdministrative() || $includeAdministrative) &&
($case->isAdministrative() || $case->isLegacy() || $includeCurrent);
},
);

return array_combine(
array_map(static function ($func) {
return $func->value;
}, $cases),
array_map(static function ($func) use ($translator) {
return [
'translations' => [
AppLanguages::English->getLangParam() => $func->getName($translator, AppLanguages::English),
AppLanguages::Dutch->getLangParam() => $func->getName($translator, AppLanguages::Dutch),
],
'isAdministrative' => $func->isAdministrative(),
'isLegacy' => $func->isLegacy(),
];
}, $cases),
);
}
}
8 changes: 2 additions & 6 deletions module/Database/src/Model/Meeting.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\ORM\Mapping\OneToOne;
use NumberFormatter;
use ValueError;

/**
Expand Down Expand Up @@ -198,12 +199,7 @@ public function getNumberAsOrdinal(?string $locale): string
}

if ('en_GB' === $locale) {
$ends = ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th'];
if (($this->getNumber() % 100 >= 11) && ($this->getNumber() % 100 <= 13)) {
return $this->getNumber() . 'th';
}

return $this->getNumber() . $ends[$this->getNumber() % 10];
return (new NumberFormatter($locale, NumberFormatter::ORDINAL))->format($this->getNumber());
}

return $this->getNumber() . 'e';
Expand Down
4 changes: 2 additions & 2 deletions module/Database/src/Model/SubDecision/Minutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function getTranslatedContent(
): string {
$replacements = [
'%TYPE%' => $this->getTarget()->getType()->value,
'%NUMBERORDINAL%' => strval($this->getTarget()->getNumberAsOrdinal($language->getLangParam())),
'%NUMBERORDINAL%' => strval($this->getTarget()->getNumberAsOrdinal($language->getLocale())),
'%APPROVAL%' => $this->getApproval()
? $translator->translate('goedgekeurd', locale: $language->getLangParam())
: $translator->translate('afgekeurd', locale: $language->getLangParam()),
Expand All @@ -127,7 +127,7 @@ public function getTranslatedContent(
? $translator->translate(' met genoemde wijzigingen', locale: $language->getLangParam())
: '',
'%THANK%' => MeetingTypes::BV === $this->getTarget()->getType()
? ' met dank aan de notulist'
? $translator->translate(' met dank aan de notulist', locale: $language->getLangParam())
: '',
];

Expand Down
4 changes: 3 additions & 1 deletion module/User/src/Model/Enums/ApiPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ enum ApiPermissions: string
case MembersPropertyAge21 = 'members_read_is21';
case MembersDeleted = 'members_deleted';
case OrgansMembershipR = 'organs_members_read';
case OrganFunctionsListR = 'organs_functionslist_read';
case All = '*';

public function getName(Translator $translator): string
Expand All @@ -45,7 +46,8 @@ public function getName(Translator $translator): string
self::MembersPropertyAge18 => $translator->translate('Member¹ - Check if has reached age 18'),
self::MembersPropertyAge21 => $translator->translate('Member¹ - Check if has reached age 21'),
self::MembersDeleted => $translator->translate('Member¹ - Allow operations on `deleted\' members'),
self::OrgansMembershipR => $translator->translate('Read organ membership (per user/organ)'),
self::OrgansMembershipR => $translator->translate('Organs - Read organ membership (per user/organ)'),
self::OrganFunctionsListR => $translator->translate('Organs - List functions and translations'),
self::All => $translator->translate('All API permissions'),
};
}
Expand Down

0 comments on commit f8a8428

Please sign in to comment.