Serialize ticket's Comment

This commit is contained in:
Julien Fastré 2024-04-18 22:10:56 +02:00
parent 613ee8b186
commit 39d701feb2
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
4 changed files with 43 additions and 2 deletions

View File

@ -17,9 +17,11 @@ use Chill\MainBundle\Doctrine\Model\TrackUpdateInterface;
use Chill\MainBundle\Doctrine\Model\TrackUpdateTrait;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\JoinColumn;
use Symfony\Component\Serializer\Annotation as Serializer;
#[ORM\Entity()]
#[ORM\Table(name: 'comment', schema: 'chill_ticket')]
#[Serializer\DiscriminatorMap(typeProperty: 'type', mapping: ['ticket_comment' => Comment::class])]
class Comment implements TrackCreationInterface, TrackUpdateInterface
{
use TrackCreationTrait;
@ -28,10 +30,12 @@ class Comment implements TrackCreationInterface, TrackUpdateInterface
#[ORM\Id]
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::INTEGER, nullable: false)]
#[ORM\GeneratedValue(strategy: 'AUTO')]
#[Serializer\Groups(['read'])]
private ?int $id = null;
public function __construct(
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::TEXT, nullable: false, options: ['default' => ''])]
#[Serializer\Groups(['read'])]
private string $content,
#[ORM\ManyToOne(targetEntity: Ticket::class, inversedBy: 'comments')]
#[JoinColumn(nullable: false)]

View File

@ -36,10 +36,21 @@ interface MotiveHistory {
createdAt: DateTime|null,
}
interface Comment {
type: "ticket_comment",
id: number,
content: string,
createdBy: User|null,
createdAt: DateTime|null,
updatedBy: User|null,
updatedAt: DateTime|null,
}
interface AddPersonEvent extends TicketHistory<"add_person", PersonHistory> {};
interface AddCommentEvent extends TicketHistory<"add_comment", Comment> {};
interface SetMotiveEvent extends TicketHistory<"set_motive", MotiveHistory> {};
type TicketHistoryLine = AddPersonEvent | SetMotiveEvent;
type TicketHistoryLine = AddPersonEvent | AddCommentEvent | SetMotiveEvent;
export interface Ticket {
type: "ticket_ticket"

View File

@ -11,6 +11,7 @@ declare(strict_types=1);
namespace Chill\TicketBundle\Serializer\Normalizer;
use Chill\TicketBundle\Entity\Comment;
use Chill\TicketBundle\Entity\MotiveHistory;
use Chill\TicketBundle\Entity\PersonHistory;
use Chill\TicketBundle\Entity\Ticket;
@ -69,6 +70,15 @@ final class TicketNormalizer implements NormalizerInterface, NormalizerAwareInte
],
$ticket->getPersonHistories()->toArray(),
),
...array_map(
fn (Comment $comment) => [
'event_type' => 'add_comment',
'at' => $comment->getCreatedAt(),
'by' => $comment->getCreatedBy(),
'data' => $comment,
],
$ticket->getComments()->toArray(),
),
];
usort(

View File

@ -11,7 +11,9 @@ declare(strict_types=1);
namespace Chill\TicketBundle\Tests\Serializer\Normalizer;
use Chill\MainBundle\Entity\User;
use Chill\PersonBundle\Entity\Person;
use Chill\TicketBundle\Entity\Comment;
use Chill\TicketBundle\Entity\Motive;
use Chill\TicketBundle\Entity\MotiveHistory;
use Chill\TicketBundle\Entity\PersonHistory;
@ -93,11 +95,20 @@ class TicketNormalizerTest extends KernelTestCase
// datetime
$normalizer->normalize(Argument::type(\DateTimeImmutable::class), 'json', Argument::type('array'))
->will(function ($args) { return $args[0]->getTimestamp(); });
// user
$normalizer->normalize(Argument::type(User::class), 'json', Argument::type('array'))
->willReturn(['user']);
// motive
$normalizer->normalize(Argument::type(Motive::class), 'json', Argument::type('array'))->willReturn(['type' => 'motive', 'id' => 0]);
// person history
$normalizer->normalize(Argument::type(PersonHistory::class), 'json', Argument::type('array'))
->willReturn(['personHistory']);
// motive history
$normalizer->normalize(Argument::type(MotiveHistory::class), 'json', Argument::type('array'))
->willReturn(['motiveHistory']);
$normalizer->normalize(Argument::type(Comment::class), 'json', Argument::type('array'))
->willReturn(['comment']);
// null values
$normalizer->normalize(null, 'json', Argument::type('array'))->willReturn(null);
$ticketNormalizer = new TicketNormalizer();
@ -109,6 +120,7 @@ class TicketNormalizerTest extends KernelTestCase
public static function provideTickets(): iterable
{
yield [
// this a nearly empty ticket
new Ticket(),
[
'type' => 'ticket_ticket',
@ -122,10 +134,14 @@ class TicketNormalizerTest extends KernelTestCase
],
];
// ticket with more features
$ticket = new Ticket();
$ticket->setExternalRef('2134');
$personHistory = new PersonHistory(new Person(), $ticket, new \DateTimeImmutable('2024-04-01T12:00:00'));
$ticketHistory = new MotiveHistory(new Motive(), $ticket, new \DateTimeImmutable('2024-04-01T12:02:00'));
$comment = new Comment('blabla test', $ticket);
$comment->setCreatedAt(new \DateTimeImmutable('2024-04-01T12:04:00'));
$comment->setCreatedBy(new User());
yield [
$ticket,
@ -137,7 +153,7 @@ class TicketNormalizerTest extends KernelTestCase
'currentAddressees' => [],
'currentInputs' => [],
'currentMotive' => ['type' => 'motive', 'id' => 0],
'history' => [['event_type' => 'add_person'], ['event_type' => 'set_motive']],
'history' => [['event_type' => 'add_person'], ['event_type' => 'set_motive'], ['event_type' => 'add_comment']],
],
];
}