Skip to content

Commit

Permalink
[Admin] allow empty value for template identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
ottaviano committed Oct 11, 2024
1 parent 5405fa4 commit 2713be1
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
22 changes: 22 additions & 0 deletions migrations/Version20241011115333.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20241011115333 extends AbstractMigration
{
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE
transactional_email_template
CHANGE
identifier identifier VARCHAR(255) DEFAULT NULL');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE transactional_email_template CHANGE identifier identifier VARCHAR(255) NOT NULL');
}
}
1 change: 1 addition & 0 deletions src/Admin/Email/TransactionalEmailTemplateAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ protected function configureFormFields(FormMapper $form): void
$form
->add('identifier', ChoiceType::class, [
'label' => 'Identifiant',
'required' => false,
'choices' => array_combine($classes = $this->getMessageClassNames($template?->getId()), $classes),
'placeholder' => 'Choisir un identifiant',
])
Expand Down
13 changes: 11 additions & 2 deletions src/Controller/Admin/AdminEmailCRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ public function sendToProdAction(TransactionalEmailTemplate $template, HttpClien
{
$this->admin->checkAccess('content', $template);

$listRedirectResponse = $this->redirect($this->admin->generateObjectUrl('list', $template));

if (!$template->identifier) {
$this->addFlash('sonata_flash_error', 'Vous ne pouvez pas envoyer en prod un template sans identifier');

return $listRedirectResponse;
}

$response = $templateWebhookClient->request('POST', '/templates', [
'json' => [
'identifier' => $template->identifier,
Expand All @@ -111,10 +119,11 @@ public function sendToProdAction(TransactionalEmailTemplate $template, HttpClien
if (200 === $response->getStatusCode()) {
$this->addFlash('sonata_flash_success', 'Le template a été envoyé en production');
} else {
$this->addFlash('sonata_flash_error', 'Erreur lors de l\'envoi du template en production');
$content = $response->toArray(false);
$this->addFlash('sonata_flash_error', $content['message'] ?? 'Erreur lors de l\'envoi du template en production');
}

return $this->redirect($this->admin->generateObjectUrl('list', $template));
return $listRedirectResponse;
}

public function previewContentAction(TransactionalEmailTemplate $template, Manager $templateManager): Response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ public function __invoke(Request $request, SerializerInterface $serializer, Enti
}

if ($command->parent) {
$command->parentObject = $repository->findOneBy(['identifier' => $command->parent]);
if (!$parent = $repository->findOneBy(['identifier' => $command->parent.'123'])) {
return $this->json([
'status' => 'error',
'message' => 'Template parent n\'existe pas',
], Response::HTTP_BAD_REQUEST);
}
$command->parentObject = $parent;
}

/** @var TransactionalEmailTemplate $existingTemplate */
Expand Down
5 changes: 2 additions & 3 deletions src/Entity/Email/TransactionalEmailTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ class TransactionalEmailTemplate implements EntityAdministratorBlameableInterfac
use EntityAdministratorBlameableTrait;

#[Assert\Length(max: '255')]
#[Assert\NotBlank]
#[ORM\Column(unique: true)]
#[ORM\Column(unique: true, nullable: true)]
public ?string $identifier = null;

#[Assert\Length(max: '255')]
Expand All @@ -48,7 +47,7 @@ public function __construct(?UuidInterface $uuid = null)

public function __toString(): string
{
return (string) $this->identifier;
return (string) ($this->identifier ?? $this->subject);
}

public function __clone()
Expand Down

0 comments on commit 2713be1

Please sign in to comment.