fix rome appellation selector and admin

This commit is contained in:
Julie Lenaerts 2024-05-23 13:47:33 +02:00
parent a5c2576124
commit 405aad7333
6 changed files with 56 additions and 42 deletions

View File

@ -17,6 +17,7 @@ use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Utils; use GuzzleHttp\Utils;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
/** /**
* Queries for ROME partenaires api. * Queries for ROME partenaires api.
@ -39,9 +40,12 @@ class PartenaireRomeAppellation
*/ */
protected $logger; protected $logger;
private const BASE = 'https://api.pole-emploi.io/partenaire/rome-metiers/v1/metiers/';
public function __construct( public function __construct(
ApiWrapper $wrapper, ApiWrapper $wrapper,
LoggerInterface $logger LoggerInterface $logger,
private \Symfony\Contracts\HttpClient\HttpClientInterface $httpClient,
) { ) {
$this->wrapper = $wrapper; $this->wrapper = $wrapper;
$this->logger = $logger; $this->logger = $logger;
@ -58,10 +62,7 @@ class PartenaireRomeAppellation
]); ]);
} }
/** public function getListeAppellation(string $search): array
* @param string $search
*/
public function getListeAppellation($search)
{ {
$bearer = $this->getBearer(); $bearer = $this->getBearer();
@ -84,27 +85,24 @@ class PartenaireRomeAppellation
} }
} }
public function getAppellation($code) public function getAppellation(string $code): array
{ {
$bearer = $this->getBearer(); $bearer = $this->getBearer();
$request = new Request('GET', sprintf('appellation/%s', $code)); try {
$parameters = [ $response = $this->httpClient->request('GET', sprintf(self::BASE . 'appellation/%s', $code), [
'headers' => [ 'headers' => [
'Authorization' => 'Bearer '.$bearer, 'Authorization' => 'Bearer '.$bearer,
], 'Accept' => 'application/json',
'query' => [ ],
'champs' => 'code,libelle,metier(code,libelle)', 'query' => [
], 'champs' => 'code,libelle,metier(code,libelle)',
]; ],
]);
$response = $this->handleRequest( return $response->toArray();
$request, } catch (HttpExceptionInterface $exception) {
$parameters, throw $exception;
$this->client, }
$this->logger
);
return Utils::jsonDecode((string) $response->getBody());
} }
} }

View File

@ -41,8 +41,8 @@ class RomeController extends AbstractController
$results = []; $results = [];
foreach ($appellations as $appellation) { foreach ($appellations as $appellation) {
$appellation->id = 'original-'.$appellation->code; $appellation['id'] = 'original-'.$appellation['code'];
$appellation->text = $appellation->libelle; $appellation['text'] = $appellation['libelle'];
$results[] = $appellation; $results[] = $appellation;
} }

View File

@ -1,12 +1,14 @@
services: services:
_defaults:
autowire: true
autoconfigure: true
Chill\FranceTravailApiBundle\ApiHelper\ApiWrapper: Chill\FranceTravailApiBundle\ApiHelper\ApiWrapper:
$clientId: '%env(FRANCE_TRAVAIL_CLIENT_ID)%' $clientId: '%env(FRANCE_TRAVAIL_CLIENT_ID)%'
$clientSecret: '%env(FRANCE_TRAVAIL_CLIENT_SECRET)%' $clientSecret: '%env(FRANCE_TRAVAIL_CLIENT_SECRET)%'
$redis: '@Chill\MainBundle\Redis\ChillRedis' $redis: '@Chill\MainBundle\Redis\ChillRedis'
Chill\FranceTravailApiBundle\ApiHelper\PartenaireRomeAppellation: Chill\FranceTravailApiBundle\ApiHelper\PartenaireRomeAppellation: ~
$wrapper: '@Chill\FranceTravailApiBundle\ApiHelper\ApiWrapper'
$logger: '@Psr\Log\LoggerInterface'
Chill\FranceTravailApiBundle\Controller\RomeController: Chill\FranceTravailApiBundle\Controller\RomeController:
autowire: true autowire: true

View File

@ -36,7 +36,7 @@ class Metier
* @var \Doctrine\Common\Collections\Collection<int, \Chill\JobBundle\Entity\Rome\Appellation> * @var \Doctrine\Common\Collections\Collection<int, \Chill\JobBundle\Entity\Rome\Appellation>
*/ */
#[ORM\OneToMany(targetEntity: Appellation::class, mappedBy: 'metier')] #[ORM\OneToMany(targetEntity: Appellation::class, mappedBy: 'metier')]
private readonly \Doctrine\Common\Collections\Collection $appellations; private \Doctrine\Common\Collections\Collection $appellations;
public function __construct() public function __construct()
{ {

View File

@ -11,6 +11,7 @@ declare(strict_types=1);
namespace Chill\JobBundle\Form\ChoiceLoader; namespace Chill\JobBundle\Form\ChoiceLoader;
use App\App;
use Chill\FranceTravailApiBundle\ApiHelper\PartenaireRomeAppellation; use Chill\FranceTravailApiBundle\ApiHelper\PartenaireRomeAppellation;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface; use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
use Symfony\Component\Form\ChoiceList\ChoiceListInterface; use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
@ -50,6 +51,11 @@ class RomeAppellationChoiceLoader implements ChoiceLoaderInterface
*/ */
protected $validator; protected $validator;
/**
* @var array<string, array{0: Appellation, 1: Metier}>
*/
private $toBeCreated = [];
public function __construct( public function __construct(
EntityManagerInterface $em, EntityManagerInterface $em,
PartenaireRomeAppellation $apiAppellation, PartenaireRomeAppellation $apiAppellation,
@ -80,33 +86,41 @@ class RomeAppellationChoiceLoader implements ChoiceLoaderInterface
if (str_starts_with($v, 'original-')) { if (str_starts_with($v, 'original-')) {
$code = \substr($v, \strlen('original-')); $code = \substr($v, \strlen('original-'));
$appellation = $this->appellationRepository->findOneBy(['code' => $code]); $appellation = $this->appellationRepository->findOneBy(['code' => $code]);
if (null === $appellation) {
if (array_key_exists($v, $this->toBeCreated)) {
[$appellation, $metier] = $this->toBeCreated[$v];
}
}
} else { } else {
$id = $v; $id = $v;
$appellation = $this->appellationRepository->find($id); $appellation = $this->appellationRepository->find($id);
$metier = $appellation->getMetier();
} }
if (null === $appellation && '' !== $code) { if (null === $appellation && '' !== $code) {
$def = $this->apiAppellation->getAppellation($code); $def = $this->apiAppellation->getAppellation($code);
$metier = $this->em->getRepository(Metier::class)
->findOneBy(['code' => $def->metier->code])
;
if (null === $metier) { $metier = $metier
$metier = (new Metier()) ?? $this->em->getRepository(Metier::class)
->setCode($def->metier->code) ->findOneBy(['code' => $def['metier']['code']])
->setLibelle($def->metier->libelle) ?? (new Metier())
; ->setCode($def['metier']['code'])
} ->setLibelle($def['metier']['libelle']);
$appellation = new Appellation(); $appellation = new Appellation();
$appellation $appellation
->setCode($def->code) ->setCode($def['code'])
->setLibelle($def->libelle) ->setLibelle($def['libelle'])
->setMetier($metier) ->setMetier($metier)
; ;
if ([] === $this->validator->validate($appellation) && [] === $this->validator->validate($metier)) { $errorsAppellation = $this->validator->validate($appellation);
$errorsMetier = $this->validator->validate($metier);
if ($errorsAppellation->count() === 0 && $errorsMetier->count() === 0) {
$this->toBeCreated[$v] = [$appellation, $metier];
$this->em->persist($appellation); $this->em->persist($appellation);
} }
} }

View File

@ -151,7 +151,7 @@ final class PermissionsGroupController extends AbstractController
/** /**
* remove an association between permissionsGroup and roleScope. * remove an association between permissionsGroup and roleScope.
*/ */
#[\Symfony\Component\Routing\Annotation\Route(path: '/{_locale}/admin/permissionsgroup/{pgid}/delete_link_role_scope/{rsid}', name: 'admin_permissionsgroup_delete_role_scope', methods: ['DELETE'])] #[\Symfony\Component\Routing\Annotation\Route(path: '/{_locale}/admin/permissionsgroup/{pgid}/delete_link_role_scope/{rsid}', name: 'admin_permissionsgroup_delete_role_scope', methods: ['POST'])]
public function deleteLinkRoleScopeAction(int $pgid, int $rsid): Response public function deleteLinkRoleScopeAction(int $pgid, int $rsid): Response
{ {
$permissionsGroup = $this->permissionsGroupRepository->find($pgid); $permissionsGroup = $this->permissionsGroupRepository->find($pgid);