mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-24 23:55:02 +00:00
Compare commits
2 Commits
ticket/64-
...
ticket/WP1
Author | SHA1 | Date | |
---|---|---|---|
812e62fe07
|
|||
0e1ec389a5
|
@@ -13,6 +13,7 @@ namespace Chill\TicketBundle\Controller;
|
||||
|
||||
use Chill\DocStoreBundle\Serializer\Normalizer\StoredObjectNormalizer;
|
||||
use Chill\MainBundle\CRUD\Controller\ApiController;
|
||||
use Chill\TicketBundle\Serializer\Normalizer\MotiveNormalizer;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
@@ -21,13 +22,13 @@ final class MotiveApiController extends ApiController
|
||||
protected function customizeQuery(string $action, Request $request, $query): void
|
||||
{
|
||||
/* @var $query QueryBuilder */
|
||||
$query->andWhere('e.active = TRUE');
|
||||
$query->andWhere('e.active = TRUE')->andWhere('e.parent IS NULL');
|
||||
}
|
||||
|
||||
protected function getContextForSerialization(string $action, Request $request, string $_format, $entity): array
|
||||
{
|
||||
return match ($request->getMethod()) {
|
||||
Request::METHOD_GET => ['groups' => ['read', StoredObjectNormalizer::DOWNLOAD_LINK_ONLY]],
|
||||
Request::METHOD_GET => ['groups' => ['read', 'read:extended', StoredObjectNormalizer::DOWNLOAD_LINK_ONLY, MotiveNormalizer::GROUP_PARENT_TO_CHILDREN]],
|
||||
default => parent::getContextForSerialization($action, $request, $_format, $entity),
|
||||
};
|
||||
}
|
||||
|
@@ -15,30 +15,25 @@ use Chill\DocStoreBundle\Entity\StoredObject;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\Common\Collections\ReadableCollection;
|
||||
use Doctrine\Common\Collections\Selectable;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Serializer\Annotation as Serializer;
|
||||
|
||||
#[ORM\Entity()]
|
||||
#[ORM\Table(name: 'motive', schema: 'chill_ticket')]
|
||||
#[Serializer\DiscriminatorMap(typeProperty: 'type', mapping: ['ticket_motive' => Motive::class])]
|
||||
class Motive
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::INTEGER, nullable: false)]
|
||||
#[ORM\GeneratedValue(strategy: 'AUTO')]
|
||||
#[Serializer\Groups(['read'])]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::JSON, nullable: false, options: ['default' => '[]'])]
|
||||
#[Serializer\Groups(['read'])]
|
||||
private array $label = [];
|
||||
|
||||
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::BOOLEAN, nullable: false, options: ['default' => true])]
|
||||
#[Serializer\Groups(['read'])]
|
||||
private bool $active = true;
|
||||
|
||||
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::STRING, nullable: true, enumType: EmergencyStatusEnum::class)]
|
||||
#[Serializer\Groups(['read'])]
|
||||
private ?EmergencyStatusEnum $makeTicketEmergency = null;
|
||||
|
||||
/**
|
||||
@@ -49,12 +44,22 @@ class Motive
|
||||
private Collection $storedObjects;
|
||||
|
||||
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::JSON, nullable: false, options: ['jsonb' => true, 'default' => '[]'])]
|
||||
#[Serializer\Groups(['read'])]
|
||||
private array $supplementaryComments = [];
|
||||
|
||||
#[ORM\ManyToOne(targetEntity: Motive::class, inversedBy: 'children')]
|
||||
private ?Motive $parent = null;
|
||||
|
||||
|
||||
/**
|
||||
* @var Collection<int, Motive>&Selectable<int, Motive>
|
||||
*/
|
||||
#[ORM\OneToMany(targetEntity: Motive::class, mappedBy: 'parent')]
|
||||
private Collection&Selectable $children;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->storedObjects = new ArrayCollection();
|
||||
$this->children = new ArrayCollection();
|
||||
}
|
||||
|
||||
public function addStoredObject(StoredObject $storedObject): void
|
||||
@@ -69,7 +74,6 @@ class Motive
|
||||
$this->storedObjects->removeElement($storedObject);
|
||||
}
|
||||
|
||||
#[Serializer\Groups(['read'])]
|
||||
public function getStoredObjects(): ReadableCollection
|
||||
{
|
||||
return $this->storedObjects;
|
||||
@@ -142,4 +146,53 @@ class Motive
|
||||
$this->supplementaryComments[$key] = $supplementaryComment;
|
||||
}
|
||||
}
|
||||
|
||||
public function isParent(): bool
|
||||
{
|
||||
return $this->children->count() > 0;
|
||||
}
|
||||
|
||||
public function isChild(): bool
|
||||
{
|
||||
return null !== $this->parent;
|
||||
}
|
||||
|
||||
public function setParent(?Motive $parent): void
|
||||
{
|
||||
if (null !== $parent) {
|
||||
$parent->addChild($this);
|
||||
} else {
|
||||
$this->parent->removeChild($this);
|
||||
}
|
||||
|
||||
$this->parent = $parent;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal use @see{setParent} instead
|
||||
*/
|
||||
public function addChild(Motive $child): void
|
||||
{
|
||||
if (!$this->children->contains($child)) {
|
||||
$this->children->add($child);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal use @see{setParent} with null as argument instead
|
||||
*/
|
||||
public function removeChild(Motive $child): void
|
||||
{
|
||||
$this->children->removeElement($child);
|
||||
}
|
||||
|
||||
public function getChildren(): ReadableCollection&Selectable
|
||||
{
|
||||
return $this->children;
|
||||
}
|
||||
|
||||
public function getParent(): ?Motive
|
||||
{
|
||||
return $this->parent;
|
||||
}
|
||||
}
|
||||
|
@@ -42,7 +42,7 @@ class MotiveHistory implements TrackCreationInterface
|
||||
public function __construct(
|
||||
#[ORM\ManyToOne(targetEntity: Motive::class)]
|
||||
#[ORM\JoinColumn(nullable: false)]
|
||||
#[Serializer\Groups(['read'])]
|
||||
#[Serializer\Groups(['read', 'read:children-to-parent'])]
|
||||
private Motive $motive,
|
||||
#[ORM\ManyToOne(targetEntity: Ticket::class)]
|
||||
#[ORM\JoinColumn(nullable: false)]
|
||||
|
@@ -8,14 +8,32 @@ import { Person } from "ChillPersonAssets/types";
|
||||
import { Thirdparty } from "../../../../ChillThirdPartyBundle/Resources/public/types";
|
||||
import { StoredObject } from "ChillDocStoreAssets/types";
|
||||
|
||||
export interface Motive {
|
||||
interface MotiveBase {
|
||||
type: "ticket_motive";
|
||||
id: number;
|
||||
active: boolean;
|
||||
label: TranslatableString;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represent a motive with basic information and parent motive.
|
||||
*
|
||||
* Match the "read" and "read:children-to-parent" serializer groups.
|
||||
*/
|
||||
export interface MotiveWithParent extends MotiveBase {
|
||||
parent: MotiveWithParent|null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a motive for a ticket, including details like emergency status, stored objects, and supplementary comments.
|
||||
*
|
||||
* Match the "read:extended" serializer group in MotiveNormalizer.
|
||||
*/
|
||||
export interface Motive extends MotiveBase {
|
||||
makeTicketEmergency: TicketEmergencyState;
|
||||
storedObjects: StoredObject[];
|
||||
supplementaryComments: { label: string }[];
|
||||
children: Motive[];
|
||||
}
|
||||
|
||||
export type TicketState = "open" | "closed" | "close";
|
||||
@@ -45,7 +63,7 @@ export interface MotiveHistory {
|
||||
id: number;
|
||||
startDate: null;
|
||||
endDate: null | DateTime;
|
||||
motive: Motive;
|
||||
motive: MotiveWithParent;
|
||||
createdBy: User | null;
|
||||
createdAt: DateTime | null;
|
||||
}
|
||||
@@ -140,7 +158,7 @@ interface BaseTicket<
|
||||
createdAt: DateTime | null;
|
||||
currentAddressees: UserGroupOrUser[];
|
||||
currentPersons: Person[];
|
||||
currentMotive: null | Motive;
|
||||
currentMotive: null | MotiveWithParent;
|
||||
currentState: TicketState | null;
|
||||
emergency: TicketEmergencyState | null;
|
||||
caller: Person | Thirdparty | null;
|
||||
|
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\TicketBundle\Serializer\Normalizer;
|
||||
|
||||
use Chill\TicketBundle\Entity\Motive;
|
||||
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
|
||||
/**
|
||||
* Normalizes a Motive object into an array format, supporting different serialization groups
|
||||
* to customize the output depending on the context.
|
||||
*
|
||||
* There are several serialization groups available:
|
||||
* - 'read': Basic information about the motive.
|
||||
* - 'read:extended': Includes additional details like stored objects and supplementary comments.
|
||||
* - 'read:parent-to-children': Normalizes children recursively without exposing parent to avoid cycles.
|
||||
* - 'read:children-to-parent': Normalizes parent recursively without exposing children to avoid cycles.
|
||||
*/
|
||||
final class MotiveNormalizer implements NormalizerInterface, NormalizerAwareInterface
|
||||
{
|
||||
use NormalizerAwareTrait;
|
||||
|
||||
public const GROUP_PARENT_TO_CHILDREN = 'read:parent-to-children';
|
||||
public const GROUP_CHILDREN_TO_PARENT = 'read:children-to-parent';
|
||||
|
||||
public function normalize($object, ?string $format = null, array $context = []): array
|
||||
{
|
||||
if (!$object instanceof Motive) {
|
||||
throw new UnexpectedValueException('Expected instance of '.Motive::class);
|
||||
}
|
||||
|
||||
$groups = $context[AbstractNormalizer::GROUPS] ?? [];
|
||||
if (is_string($groups)) {
|
||||
$groups = [$groups];
|
||||
}
|
||||
|
||||
$data = [];
|
||||
|
||||
if (in_array('read', $groups, true) || in_array('read:extended', $groups, true)) {
|
||||
// Build base representation
|
||||
$data = [
|
||||
'type' => 'ticket_motive',
|
||||
'id' => $object->getId(),
|
||||
'label' => $object->getLabel(),
|
||||
'active' => $object->isActive(),
|
||||
];
|
||||
}
|
||||
|
||||
if (in_array('read:extended', $groups, true)) {
|
||||
$data['makeTicketEmergency'] = $object->getMakeTicketEmergency();
|
||||
$data['supplementaryComments'] = $object->getSupplementaryComments();
|
||||
// Normalize stored objects (delegated to their own normalizer when present)
|
||||
$storedObjects = [];
|
||||
foreach ($object->getStoredObjects() as $storedObject) {
|
||||
$storedObjects[] = $this->normalizer->normalize($storedObject, $format, $context);
|
||||
}
|
||||
$data['storedObjects'] = $storedObjects;
|
||||
|
||||
}
|
||||
|
||||
if (in_array(self::GROUP_PARENT_TO_CHILDREN, $groups, true)) {
|
||||
// Normalize children recursively (but we do not expose parent to avoid cycles)
|
||||
$children = [];
|
||||
foreach ($object->getChildren() as $child) {
|
||||
$children[] = $this->normalizer->normalize($child, $format, $context);
|
||||
}
|
||||
$data['children'] = $children;
|
||||
} elseif (in_array(self::GROUP_CHILDREN_TO_PARENT, $groups, true)) {
|
||||
$data['parent'] = $this->normalizer->normalize($object->getParent(), $format, $context);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function supportsNormalization($data, ?string $format = null, array $context = []): bool
|
||||
{
|
||||
return $data instanceof Motive;
|
||||
}
|
||||
|
||||
/**
|
||||
* Optimization hint for the Serializer (available since Symfony 5.3+).
|
||||
*
|
||||
* @return array<class-string, bool>
|
||||
*/
|
||||
public function getSupportedTypes(?string $format): array
|
||||
{
|
||||
return [
|
||||
Motive::class => true,
|
||||
];
|
||||
}
|
||||
}
|
@@ -51,7 +51,7 @@ final class TicketNormalizer implements NormalizerInterface, NormalizerAwareInte
|
||||
]),
|
||||
'currentAddressees' => $this->normalizer->normalize($object->getCurrentAddressee(), $format, ['groups' => 'read']),
|
||||
'currentInputs' => $this->normalizer->normalize($object->getCurrentInputs(), $format, ['groups' => 'read']),
|
||||
'currentMotive' => $this->normalizer->normalize($object->getMotive(), $format, ['groups' => 'read']),
|
||||
'currentMotive' => $this->normalizer->normalize($object->getMotive(), $format, ['groups' => ['read', MotiveNormalizer::GROUP_CHILDREN_TO_PARENT]]),
|
||||
'currentState' => $object->getState()?->value ?? 'open',
|
||||
'emergency' => $object->getEmergencyStatus()?->value ?? 'no',
|
||||
'caller' => $this->normalizer->normalize($object->getCaller(), $format, ['groups' => 'read']),
|
||||
|
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\Migrations\Ticket;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
final class Version20250924124214 extends AbstractMigration
|
||||
{
|
||||
public function getDescription(): string
|
||||
{
|
||||
return 'Add parent to motive';
|
||||
}
|
||||
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
$this->addSql('ALTER TABLE chill_ticket.motive ADD parent_id INT DEFAULT NULL');
|
||||
$this->addSql('ALTER TABLE chill_ticket.motive ADD CONSTRAINT FK_DE298BF8727ACA70 FOREIGN KEY (parent_id) REFERENCES chill_ticket.motive (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
|
||||
$this->addSql('CREATE INDEX IDX_DE298BF8727ACA70 ON chill_ticket.motive (parent_id)');
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
$this->addSql('ALTER TABLE chill_ticket.motive DROP CONSTRAINT FK_DE298BF8727ACA70');
|
||||
$this->addSql('DROP INDEX chill_ticket.IDX_DE298BF8727ACA70');
|
||||
$this->addSql('ALTER TABLE chill_ticket.motive DROP parent_id');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user