feat: admin view listing a travel's mailing recipients from bpn-connect
This commit is contained in:
1 parent
63ffe0b0b4
commit
e796dd01ee
10 files changed
+347
No files matched your search
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\BpnConnect\Model;
|
||||
|
||||
readonly class TravelMailingRecipient
|
||||
{
|
||||
public function __construct(
|
||||
public string $email,
|
||||
public string $firstName,
|
||||
public string $lastName,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\BpnConnect\Model;
|
||||
|
||||
readonly class TravelMailingResponse
|
||||
{
|
||||
/**
|
||||
* @param TravelMailingRecipient[] $data
|
||||
*/
|
||||
public function __construct(
|
||||
public string $travelCode,
|
||||
public array $data,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\BpnConnect;
|
||||
|
||||
use App\BpnConnect\Model\TravelMailingRecipient;
|
||||
use App\BpnConnect\Model\TravelMailingResponse;
|
||||
|
||||
class TravelsClient extends AbstractApiClient
|
||||
{
|
||||
private const string MAILING_PATH = '/api/v1/travels/mailing';
|
||||
|
||||
/**
|
||||
* An unknown travel code yields an empty list, not an error: bpn-connect cannot tell it
|
||||
* apart from a travel without confirmed participants.
|
||||
*/
|
||||
public function getMailingRecipients(string $travelCode): TravelMailingResponse
|
||||
{
|
||||
$payload = $this->request(self::MAILING_PATH, [
|
||||
'travelCode' => $travelCode,
|
||||
]);
|
||||
|
||||
$recipients = array_map(
|
||||
static fn (array $item) => new TravelMailingRecipient(
|
||||
email: $item['email'],
|
||||
firstName: $item['first_name'] ?? '',
|
||||
lastName: $item['last_name'] ?? '',
|
||||
),
|
||||
$payload['data'] ?? [],
|
||||
);
|
||||
|
||||
return new TravelMailingResponse($payload['meta']['travel_code'] ?? $travelCode, $recipients);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller\Admin\TravelMailing;
|
||||
|
||||
use App\BpnConnect\Exception\BpnConnectException;
|
||||
use App\BpnConnect\TravelsClient;
|
||||
use App\Form\Admin\TravelMailingLookupType;
|
||||
use App\Form\Model\TravelMailingLookupDto;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
|
||||
#[IsGranted('ROLE_CUSTOMER_EXPERT')]
|
||||
class IndexController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly TravelsClient $travelsClient,
|
||||
) {
|
||||
}
|
||||
|
||||
#[Route('/admin/travel-mailing', name: 'app_admin_travelmailing')]
|
||||
public function index(Request $request): Response
|
||||
{
|
||||
$lookup = new TravelMailingLookupDto();
|
||||
$form = $this->createForm(TravelMailingLookupType::class, $lookup, [
|
||||
'action' => $this->generateUrl('app_admin_travelmailing'),
|
||||
]);
|
||||
$form->handleRequest($request);
|
||||
|
||||
$result = null;
|
||||
$failed = false;
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
try {
|
||||
$result = $this->travelsClient->getMailingRecipients((string) $lookup->travelCode);
|
||||
} catch (BpnConnectException) {
|
||||
// The client has already logged the cause; the page only needs to say it failed.
|
||||
$failed = true;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render('admin/travel_mailing/index.html.twig', [
|
||||
'form' => $form,
|
||||
'result' => $result,
|
||||
'failed' => $failed,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Form\Admin;
|
||||
|
||||
use App\Form\Model\TravelMailingLookupDto;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
/**
|
||||
* Submits as GET with an empty block prefix, so a lookup is the plain URL `?travelCode=…`
|
||||
* and can be bookmarked or shared.
|
||||
*
|
||||
* @extends AbstractType<TravelMailingLookupDto>
|
||||
*/
|
||||
class TravelMailingLookupType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$builder->add('travelCode', TextType::class, [
|
||||
'label' => 'Reisecode',
|
||||
'attr' => [
|
||||
'placeholder' => 'z. B. LPJMP281126',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => TravelMailingLookupDto::class,
|
||||
'method' => 'GET',
|
||||
'csrf_protection' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
public function getBlockPrefix(): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Form\Model;
|
||||
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
class TravelMailingLookupDto
|
||||
{
|
||||
#[Assert\NotBlank(message: 'Bitte einen Reisecode angeben.')]
|
||||
public ?string $travelCode = null;
|
||||
}
|
||||
@@ -80,6 +80,15 @@ class AdminMenuBuilder extends AbstractMenuBuilder
|
||||
'routes' => [['pattern' => '/^app_admin_log/']],
|
||||
],
|
||||
]);
|
||||
$menu->addChild('Reise-Mailingliste', [
|
||||
'route' => 'app_admin_travelmailing',
|
||||
'linkAttributes' => [
|
||||
'title' => 'Reise-Mailingliste',
|
||||
],
|
||||
'extras' => [
|
||||
'icon' => 'mail',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
$this->addLogoutItem($menu);
|
||||
|
||||
Reference in new issue
Block a user