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

[OILHI - Interfaçage] Socle technique envoi de données #1969

Merged
merged 6 commits into from
Nov 28, 2023
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
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ ESABORA_CRON_CLOCK_SLEEP_INTERVAL=60 #second
ESABORA_CRON_SCHEDULE_SYNC_SISH='*/2 * * * *'
ESABORA_CRON_SCHEDULE_SYNC_SISH_INTERVENTION='*/3 * * * *'
MATOMO_ENABLE=0
ZAPIER_OILHI_TOKEN=
ZAPIER_OILHI_USER_ID=
ZAPIER_OILHI_CREATE_AIRTABLE_RECORD_ZAP_ID=

### histologe ###

Expand Down
3 changes: 3 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ ESABORA_CRON_CLOCK_SLEEP_INTERVAL=60 #second
ESABORA_CRON_SCHEDULE_SYNC_SISH='*/2 * * * *'
ESABORA_CRON_SCHEDULE_SYNC_SISH_INTERVENTION='*/3 * * * *'
MATOMO_ENABLE=0
ZAPIER_OILHI_TOKEN=
ZAPIER_OILHI_ID=
ZAPIER_OILHI_CREATE_AIRTABLE_RECORD_ZAP_ID=

###> knplabs/knp-snappy-bundle ###
WKHTMLTOPDF_PATH=wkhtmltopdf
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ test: ## Run all tests
@$(DOCKER_COMP) exec histologe_phpfpm sh -c "$(PHPUNIT) $(FILE) --stop-on-failure --testdox -d memory_limit=-1"

test-coverage: ## Generate phpunit coverage report in html
@$(DOCKER_COMP) exec histologe_phpfpm sh -c "XDEBUG_MODE=coverage $(PHPUNIT) --coverage-html coverage"
@$(DOCKER_COMP) exec histologe_phpfpm sh -c "XDEBUG_MODE=coverage $(PHPUNIT) --coverage-html coverage -d memory_limit=-1"

e2e: ## Run E2E tests
@$(NPX) cypress open
Expand Down
4 changes: 2 additions & 2 deletions config/packages/messenger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ framework:
routing:
# Route your messages to the transports
# Symfony\Component\Mailer\Messenger\SendEmailMessage: sync
App\Messenger\Message\DossierMessageSCHS: async
App\Messenger\Message\DossierMessageSISH: async
App\Messenger\Message\Esabora\DossierMessageSCHS: async
App\Messenger\Message\Esabora\DossierMessageSISH: async
App\Messenger\Message\PdfExportMessage: async


Expand Down
2 changes: 1 addition & 1 deletion src/Factory/Esabora/DossierMessageSCHSFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use App\Entity\Affectation;
use App\Entity\Enum\PartnerType;
use App\Entity\Signalement;
use App\Messenger\Message\DossierMessageSCHS;
use App\Messenger\Message\Esabora\DossierMessageSCHS;
use App\Service\UploadHandlerService;
use App\Utils\AddressParser;
use App\Utils\EtageParser;
Expand Down
2 changes: 1 addition & 1 deletion src/Factory/Esabora/DossierMessageSISHFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use App\Entity\Enum\PartnerType;
use App\Entity\Signalement;
use App\Entity\Suivi;
use App\Messenger\Message\DossierMessageSISH;
use App\Messenger\Message\Esabora\DossierMessageSISH;
use App\Repository\SuiviRepository;
use App\Service\DataGouv\AddressService;
use App\Service\Esabora\AbstractEsaboraService;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

namespace App\Messenger\Message;
namespace App\Messenger\Message\Esabora;

use App\Entity\File;
use App\Messenger\Message\DossierMessageInterface;

final class DossierMessageSCHS implements DossierMessageInterface
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

namespace App\Messenger\Message;
namespace App\Messenger\Message\Esabora;

use App\Entity\Enum\SISHDossierType;
use App\Entity\File;
use App\Messenger\Message\DossierMessageInterface;
use App\Service\Esabora\Enum\PersonneType;
use App\Service\Esabora\Model\DossierMessageSISHPersonne;

Expand Down
46 changes: 46 additions & 0 deletions src/Messenger/Message/Oilhi/DossierMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Messenger\Message\Oilhi;

final class DossierMessage
{
private ?string $signalementUrl = null;
private ?int $signalementId = null;
private ?int $partnerId = null;

public function getSignalementUrl(): ?string
{
return $this->signalementUrl;
}

public function setSignalementUrl(?string $signalementUrl): self
{
$this->signalementUrl = $signalementUrl;

return $this;
}

public function getSignalementId(): ?int
{
return $this->signalementId;
}

public function setSignalementId(?int $signalementId): self
{
$this->signalementId = $signalementId;

return $this;
}

public function getPartnerId(): ?int
{
return $this->partnerId;
}

public function setPartnerId(?int $partnerId): self
{
$this->partnerId = $partnerId;

return $this;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace App\Messenger\MessageHandler;
namespace App\Messenger\MessageHandler\Esabora;

use App\Entity\JobEvent;
use App\Manager\JobEventManager;
use App\Messenger\Message\DossierMessageSCHS;
use App\Messenger\Message\Esabora\DossierMessageSCHS;
use App\Repository\PartnerRepository;
use App\Service\Esabora\AbstractEsaboraService;
use App\Service\Esabora\EsaboraSCHSService;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace App\Messenger\MessageHandler;
namespace App\Messenger\MessageHandler\Esabora;

use App\Messenger\Message\DossierMessageSISH;
use App\Messenger\Message\Esabora\DossierMessageSISH;
use App\Service\Esabora\Handler\DossierSISHHandlerInterface;
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
Expand Down
39 changes: 39 additions & 0 deletions src/Messenger/MessageHandler/Oilhi/DossierMessageHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Messenger\MessageHandler\Oilhi;

use App\Entity\JobEvent;
use App\Manager\JobEventManager;
use App\Messenger\Message\Oilhi\DossierMessage;
use App\Repository\PartnerRepository;
use App\Service\Oilhi\HookZapierService;
use Symfony\Component\Serializer\SerializerInterface;

class DossierMessageHandler
{
public function __construct(
private SerializerInterface $serializer,
private JobEventManager $jobEventManager,
private HookZapierService $hookZapierService,
private PartnerRepository $partnerRepository,
) {
}

public function __invoke(DossierMessage $dossierMessage): void
{
$response = $this->hookZapierService->pushDossier($dossierMessage);
$partner = $this->partnerRepository->find($partnerId = $dossierMessage->getPartnerId());

$this->jobEventManager->createJobEvent(
service: HookZapierService::TYPE_SERVICE,
action: HookZapierService::ACTION_PUSH_DOSSIER,
message: $this->serializer->serialize($dossierMessage, 'json'),
response: $response->getContent(throw: false),
status: 200 === $response->getStatusCode() ? JobEvent::STATUS_SUCCESS : JobEvent::STATUS_FAILED,
codeStatus: $response->getStatusCode(),
signalementId: $dossierMessage->getSignalementId(),
partnerId: $partnerId,
partnerType: $partner?->getType(),
);
}
}
2 changes: 1 addition & 1 deletion src/Service/Esabora/EsaboraSCHSService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Service\Esabora;

use App\Entity\Affectation;
use App\Messenger\Message\DossierMessageSCHS;
use App\Messenger\Message\Esabora\DossierMessageSCHS;
use App\Service\Esabora\Response\DossierStateSCHSResponse;
use App\Service\UploadHandlerService;
use Psr\Log\LoggerInterface;
Expand Down
2 changes: 1 addition & 1 deletion src/Service/Esabora/EsaboraSISHService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Service\Esabora;

use App\Entity\Affectation;
use App\Messenger\Message\DossierMessageSISH;
use App\Messenger\Message\Esabora\DossierMessageSISH;
use App\Service\Esabora\Model\DossierMessageSISHPersonne;
use App\Service\Esabora\Response\DossierArreteSISHCollectionResponse;
use App\Service\Esabora\Response\DossierPushSISHResponse;
Expand Down
2 changes: 1 addition & 1 deletion src/Service/Esabora/Handler/AbstractDossierSISHHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use App\Entity\JobEvent;
use App\Entity\Partner;
use App\Manager\JobEventManager;
use App\Messenger\Message\DossierMessageSISH;
use App\Messenger\Message\Esabora\DossierMessageSISH;
use App\Repository\PartnerRepository;
use App\Service\Esabora\AbstractEsaboraService;
use Symfony\Component\Serializer\SerializerInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Service\Esabora\Handler;

use App\Manager\JobEventManager;
use App\Messenger\Message\DossierMessageSISH;
use App\Messenger\Message\Esabora\DossierMessageSISH;
use App\Repository\PartnerRepository;
use App\Service\Esabora\AbstractEsaboraService;
use App\Service\Esabora\EsaboraSISHService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Service\Esabora\Handler;

use App\Manager\JobEventManager;
use App\Messenger\Message\DossierMessageSISH;
use App\Messenger\Message\Esabora\DossierMessageSISH;
use App\Repository\PartnerRepository;
use App\Service\Esabora\AbstractEsaboraService;
use App\Service\Esabora\EsaboraSISHService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Service\Esabora\Handler;

use App\Messenger\Message\DossierMessageSISH;
use App\Messenger\Message\Esabora\DossierMessageSISH;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;

#[AutoconfigureTag('app.dossier_sish_handler')]
Expand Down
2 changes: 1 addition & 1 deletion src/Service/Esabora/Handler/DossierServiceHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Service\Esabora\Handler;

use App\Manager\JobEventManager;
use App\Messenger\Message\DossierMessageSISH;
use App\Messenger\Message\Esabora\DossierMessageSISH;
use App\Repository\PartnerRepository;
use App\Service\Esabora\AbstractEsaboraService;
use App\Service\Esabora\EsaboraSISHService;
Expand Down
54 changes: 54 additions & 0 deletions src/Service/Oilhi/HookZapierService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace App\Service\Oilhi;

use App\Messenger\Message\Oilhi\DossierMessage;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

class HookZapierService
{
public const ZAPIER_HOOK_URL = 'https://hooks.zapier.com/hooks/catch';
public const TYPE_SERVICE = 'oilhi';
public const ACTION_PUSH_DOSSIER = 'push_dossier';

public function __construct(
private HttpClientInterface $httpClient,
private LoggerInterface $logger,
private NormalizerInterface $normalizer,
#[Autowire(env: 'ZAPIER_OILHI_TOKEN')]
private string $token,
#[Autowire(env: 'ZAPIER_OILHI_USER_ID')]
private string $userId,
#[Autowire(env: 'ZAPIER_OILHI_CREATE_AIRTABLE_RECORD_ZAP_ID')]
private string $zapId,
) {
}

public function pushDossier(DossierMessage $dossierMessage): ResponseInterface|JsonResponse
{
$payload = $this->normalizer->normalize($dossierMessage);
$payload['token'] = $this->token;
try {
return $this->httpClient->request(
'POST',
self::ZAPIER_HOOK_URL.'/'.$this->userId.'/'.$this->zapId,
$payload
);
} catch (\Throwable $exception) {
$this->logger->error($exception->getMessage());
}

$response = [
'message' => $exception->getMessage(),
'request' => json_encode($payload),
];

return (new JsonResponse($response))->setStatusCode(Response::HTTP_SERVICE_UNAVAILABLE);
}
}
4 changes: 2 additions & 2 deletions tests/FixturesHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
use App\Entity\Suivi;
use App\Entity\Territory;
use App\Entity\User;
use App\Messenger\Message\DossierMessageSCHS;
use App\Messenger\Message\DossierMessageSISH;
use App\Messenger\Message\Esabora\DossierMessageSCHS;
use App\Messenger\Message\Esabora\DossierMessageSISH;
use App\Service\Esabora\AbstractEsaboraService;
use App\Service\Esabora\Enum\PersonneType;
use App\Service\Esabora\Model\DossierMessageSISHPersonne;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use App\Entity\JobEvent;
use App\EventSubscriber\WorkerMessageEventSubscriber;
use App\Manager\JobEventManager;
use App\Messenger\Message\DossierMessageSCHS;
use App\Messenger\Message\Esabora\DossierMessageSCHS;
use App\Repository\PartnerRepository;
use App\Service\Esabora\AbstractEsaboraService;
use App\Tests\FixturesHelper;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace App\Tests\Unit\Messenger\MessageHandler;
namespace App\Tests\Unit\Messenger\MessageHandler\Esabora;

use App\Entity\Partner;
use App\Manager\JobEventManager;
use App\Messenger\MessageHandler\DossierMessageSCHSHandler;
use App\Messenger\MessageHandler\Esabora\DossierMessageSCHSHandler;
use App\Repository\PartnerRepository;
use App\Service\Esabora\EsaboraSCHSService;
use App\Tests\FixturesHelper;
Expand All @@ -26,7 +26,7 @@ public function testProcessDossierMessage(): void
{
$faker = Factory::create();
$dossierMessage = $this->getDossierMessageSCHS();
$filepath = __DIR__.'/../../../../tools/wiremock/src/Resources/Esabora/schs/ws_import.json';
$filepath = __DIR__.'/../../../../../tools/wiremock/src/Resources/Esabora/schs/ws_import.json';
$mockResponse = new MockResponse(file_get_contents($filepath));
$mockHttpClient = new MockHttpClient($mockResponse);
$response = $mockHttpClient->request('POST', $faker->url());
Expand Down
Loading
Loading