Merge branch 'ticket-app/backend-3' into 'ticket-app-master'

Add functionality to set addressees for a ticket

See merge request Chill-Projet/chill-bundles!683
This commit is contained in:
Julien Fastré 2024-04-24 16:50:29 +00:00
commit 34c171659b
24 changed files with 1065 additions and 15 deletions

View File

@ -51,6 +51,8 @@ export interface UserGroup {
excludeKey: string, excludeKey: string,
} }
export type UserGroupOrUser = User | UserGroup;
export interface UserAssociatedInterface { export interface UserAssociatedInterface {
type: "user"; type: "user";
id: number; id: number;

View File

@ -29,6 +29,42 @@ components:
type: string type: string
text: text:
type: string type: string
UserById:
type: object
properties:
id:
type: integer
type:
type: string
enum:
- user
UserGroup:
type: object
properties:
id:
type: integer
type:
type: string
enum:
- user_group
label:
type: object
additionalProperties: true
backgroundColor:
type: string
foregroundColor:
type: string
exclusionKey:
type: string
UserGroupById:
type: object
properties:
id:
type: integer
type:
type: string
enum:
- user_group
Center: Center:
type: object type: object
properties: properties:
@ -921,6 +957,6 @@ paths:
schema: schema:
type: array type: array
items: items:
$ref: '#/components/schemas/NewsItem' $ref: '#/components/schemas/UserGroup'
403: 403:
description: "Unauthorized" description: "Unauthorized"

View File

@ -3,6 +3,9 @@ services:
autowire: true autowire: true
autoconfigure: true autoconfigure: true
Chill\MainBundle\Validation\:
resource: '../../Validation'
chill_main.validator_user_circle_consistency: chill_main.validator_user_circle_consistency:
class: Chill\MainBundle\Validator\Constraints\Entity\UserCircleConsistencyValidator class: Chill\MainBundle\Validator\Constraints\Entity\UserCircleConsistencyValidator
arguments: arguments:

View File

@ -93,3 +93,71 @@ paths:
description: "ACCEPTED" description: "ACCEPTED"
422: 422:
description: "UNPROCESSABLE ENTITY" description: "UNPROCESSABLE ENTITY"
/1.0/ticket/{id}/addressees/set:
post:
tags:
- ticket
summary: Set the addresses for an existing ticket (will replace all the existing addresses)
parameters:
- name: id
in: path
required: true
description: The ticket id
schema:
type: integer
format: integer
minimum: 1
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
addressees:
type: array
items:
oneOf:
- $ref: '#/components/schemas/UserGroupById'
- $ref: '#/components/schemas/UserById'
responses:
201:
description: "ACCEPTED"
422:
description: "UNPROCESSABLE ENTITY"
/1.0/ticket/{id}/addressee/add:
post:
tags:
- ticket
summary: Add an addressee to a ticket, without removing existing ones.
parameters:
- name: id
in: path
required: true
description: The ticket id
schema:
type: integer
format: integer
minimum: 1
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
addressee:
oneOf:
- $ref: '#/components/schemas/UserGroupById'
- $ref: '#/components/schemas/UserById'
responses:
201:
description: "ACCEPTED"
422:
description: "UNPROCESSABLE ENTITY"

View File

@ -0,0 +1,29 @@
<?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\Action\Ticket;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserGroup;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* Add a single addressee to the ticket.
*
* This command is converted into an "SetAddresseesCommand" for handling
*/
final readonly class AddAddresseeCommand
{
public function __construct(
#[Groups(['read'])]
public User|UserGroup $addressee
) {}
}

View File

@ -0,0 +1,56 @@
<?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\Action\Ticket\Handler;
use Chill\MainBundle\Entity\User;
use Chill\TicketBundle\Action\Ticket\SetAddresseesCommand;
use Chill\TicketBundle\Entity\AddresseeHistory;
use Chill\TicketBundle\Entity\Ticket;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Security\Core\Security;
final readonly class SetAddresseesCommandHandler
{
public function __construct(
private ClockInterface $clock,
private EntityManagerInterface $entityManager,
private Security $security,
) {}
public function handle(Ticket $ticket, SetAddresseesCommand $command): void
{
// remove existing addresses which are not in the new addresses
foreach ($ticket->getAddresseeHistories() as $addressHistory) {
if (null !== $addressHistory->getEndDate()) {
continue;
}
if (!in_array($addressHistory->getAddressee(), $command->addressees, true)) {
$addressHistory->setEndDate($this->clock->now());
if (($user = $this->security->getUser()) instanceof User) {
$addressHistory->setRemovedBy($user);
}
}
}
// add new addresses
foreach ($command->addressees as $address) {
if (in_array($address, $ticket->getCurrentAddressee(), true)) {
continue;
}
$history = new AddresseeHistory($address, $this->clock->now(), $ticket);
$this->entityManager->persist($history);
}
}
}

View File

@ -0,0 +1,40 @@
<?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\Action\Ticket;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserGroup;
use Chill\MainBundle\Validation\Constraint\UserGroupDoNotExclude;
use Chill\TicketBundle\Entity\Ticket;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints\GreaterThan;
final readonly class SetAddresseesCommand
{
public function __construct(
/**
* @var list<UserGroup|User>
*/
#[UserGroupDoNotExclude]
#[GreaterThan(0)]
#[Groups(['read'])]
public array $addressees
) {}
public static function fromAddAddresseeCommand(AddAddresseeCommand $command, Ticket $ticket): self
{
return new self([
$command->addressee,
...$ticket->getCurrentAddressee(),
]);
}
}

View File

@ -15,6 +15,7 @@ use Chill\TicketBundle\Action\Ticket\AssociateByPhonenumberCommand;
use Chill\TicketBundle\Action\Ticket\Handler\AssociateByPhonenumberCommandHandler; use Chill\TicketBundle\Action\Ticket\Handler\AssociateByPhonenumberCommandHandler;
use Chill\TicketBundle\Action\Ticket\CreateTicketCommand; use Chill\TicketBundle\Action\Ticket\CreateTicketCommand;
use Chill\TicketBundle\Action\Ticket\Handler\CreateTicketCommandHandler; use Chill\TicketBundle\Action\Ticket\Handler\CreateTicketCommandHandler;
use Chill\TicketBundle\Repository\TicketRepositoryInterface;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@ -31,7 +32,8 @@ final readonly class CreateTicketController
private AssociateByPhonenumberCommandHandler $associateByPhonenumberCommandHandler, private AssociateByPhonenumberCommandHandler $associateByPhonenumberCommandHandler,
private Security $security, private Security $security,
private UrlGeneratorInterface $urlGenerator, private UrlGeneratorInterface $urlGenerator,
private EntityManagerInterface $entityManager private EntityManagerInterface $entityManager,
private TicketRepositoryInterface $ticketRepository,
) {} ) {}
#[Route('{_locale}/ticket/ticket/create')] #[Route('{_locale}/ticket/ticket/create')]
@ -41,6 +43,14 @@ final readonly class CreateTicketController
throw new AccessDeniedHttpException('Only users are allowed to create tickets.'); throw new AccessDeniedHttpException('Only users are allowed to create tickets.');
} }
if ('' !== $extId = $request->query->get('extId', '')) {
if (null !== $ticket = $this->ticketRepository->findOneByExternalRef($extId)) {
return new RedirectResponse(
$this->urlGenerator->generate('chill_ticket_ticket_edit', ['id' => $ticket->getId()])
);
}
}
$createCommand = new CreateTicketCommand($request->query->get('extId', '')); $createCommand = new CreateTicketCommand($request->query->get('extId', ''));
$ticket = $this->createTicketCommandHandler->__invoke($createCommand); $ticket = $this->createTicketCommandHandler->__invoke($createCommand);

View File

@ -0,0 +1,85 @@
<?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\Controller;
use Chill\TicketBundle\Action\Ticket\AddAddresseeCommand;
use Chill\TicketBundle\Action\Ticket\Handler\SetAddresseesCommandHandler;
use Chill\TicketBundle\Action\Ticket\SetAddresseesCommand;
use Chill\TicketBundle\Entity\Ticket;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
final readonly class SetAddresseesController
{
public function __construct(
private Security $security,
private EntityManagerInterface $entityManager,
private SerializerInterface $serializer,
private SetAddresseesCommandHandler $addressesCommandHandler,
private ValidatorInterface $validator,
) {}
#[Route('/api/1.0/ticket/{id}/addressees/set', methods: ['POST'])]
public function setAddressees(Ticket $ticket, Request $request): Response
{
if (!$this->security->isGranted('ROLE_USER')) {
throw new AccessDeniedHttpException('Only users can set addressees.');
}
$command = $this->serializer->deserialize($request->getContent(), SetAddresseesCommand::class, 'json', [AbstractNormalizer::GROUPS => ['read']]);
return $this->registerSetAddressees($command, $ticket);
}
#[Route('/api/1.0/ticket/{id}/addressee/add', methods: ['POST'])]
public function addAddressee(Ticket $ticket, Request $request): Response
{
if (!$this->security->isGranted('ROLE_USER')) {
throw new AccessDeniedHttpException('Only users can add addressees.');
}
$command = $this->serializer->deserialize($request->getContent(), AddAddresseeCommand::class, 'json', [AbstractNormalizer::GROUPS => ['read']]);
return $this->registerSetAddressees(SetAddresseesCommand::fromAddAddresseeCommand($command, $ticket), $ticket);
}
private function registerSetAddressees(SetAddresseesCommand $command, Ticket $ticket): Response
{
if (0 < count($errors = $this->validator->validate($command))) {
return new JsonResponse(
$this->serializer->serialize($errors, 'json'),
Response::HTTP_UNPROCESSABLE_ENTITY,
[],
true
);
}
$this->addressesCommandHandler->handle($ticket, $command);
$this->entityManager->flush();
return new JsonResponse(
$this->serializer->serialize($ticket, 'json', ['groups' => 'read']),
Response::HTTP_OK,
[],
true,
);
}
}

View File

@ -18,9 +18,11 @@ use Chill\MainBundle\Doctrine\Model\TrackUpdateTrait;
use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserGroup; use Chill\MainBundle\Entity\UserGroup;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation as Serializer;
#[ORM\Entity()] #[ORM\Entity()]
#[ORM\Table(name: 'addressee_history', schema: 'chill_ticket')] #[ORM\Table(name: 'addressee_history', schema: 'chill_ticket')]
#[Serializer\DiscriminatorMap(typeProperty: 'type', mapping: ['ticket_addressee_history' => AddresseeHistory::class])]
class AddresseeHistory implements TrackUpdateInterface, TrackCreationInterface class AddresseeHistory implements TrackUpdateInterface, TrackCreationInterface
{ {
use TrackCreationTrait; use TrackCreationTrait;
@ -29,6 +31,7 @@ class AddresseeHistory implements TrackUpdateInterface, TrackCreationInterface
#[ORM\Id] #[ORM\Id]
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::INTEGER, nullable: false)] #[ORM\Column(type: \Doctrine\DBAL\Types\Types::INTEGER, nullable: false)]
#[ORM\GeneratedValue(strategy: 'AUTO')] #[ORM\GeneratedValue(strategy: 'AUTO')]
#[Serializer\Groups(['read'])]
private ?int $id = null; private ?int $id = null;
#[ORM\ManyToOne(targetEntity: User::class)] #[ORM\ManyToOne(targetEntity: User::class)]
@ -39,11 +42,19 @@ class AddresseeHistory implements TrackUpdateInterface, TrackCreationInterface
#[ORM\JoinColumn(nullable: true)] #[ORM\JoinColumn(nullable: true)]
private ?UserGroup $addresseeGroup = null; private ?UserGroup $addresseeGroup = null;
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::DATETIME_IMMUTABLE, nullable: true, options: ['default' => 'null'])]
#[Serializer\Groups(['read'])]
private ?\DateTimeImmutable $endDate = null; private ?\DateTimeImmutable $endDate = null;
#[ORM\ManyToOne(targetEntity: User::class)]
#[ORM\JoinColumn(nullable: true)]
#[Serializer\Groups(['read'])]
private ?User $removedBy = null;
public function __construct( public function __construct(
User|UserGroup $addressee, User|UserGroup $addressee,
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::DATETIME_IMMUTABLE, nullable: false)] #[ORM\Column(type: \Doctrine\DBAL\Types\Types::DATETIME_IMMUTABLE, nullable: false)]
#[Serializer\Groups(['read'])]
private \DateTimeImmutable $startDate, private \DateTimeImmutable $startDate,
#[ORM\ManyToOne(targetEntity: Ticket::class)] #[ORM\ManyToOne(targetEntity: Ticket::class)]
#[ORM\JoinColumn(nullable: false)] #[ORM\JoinColumn(nullable: false)]
@ -54,8 +65,11 @@ class AddresseeHistory implements TrackUpdateInterface, TrackCreationInterface
} else { } else {
$this->addresseeGroup = $addressee; $this->addresseeGroup = $addressee;
} }
$this->ticket->addAddresseeHistory($this);
} }
#[Serializer\Groups(['read'])]
public function getAddressee(): UserGroup|User public function getAddressee(): UserGroup|User
{ {
if (null !== $this->addresseeGroup) { if (null !== $this->addresseeGroup) {
@ -94,4 +108,23 @@ class AddresseeHistory implements TrackUpdateInterface, TrackCreationInterface
{ {
return $this->ticket; return $this->ticket;
} }
public function getRemovedBy(): ?User
{
return $this->removedBy;
}
public function setRemovedBy(?User $removedBy): self
{
$this->removedBy = $removedBy;
return $this;
}
public function setEndDate(?\DateTimeImmutable $endDate): self
{
$this->endDate = $endDate;
return $this;
}
} }

View File

@ -84,4 +84,11 @@ class PersonHistory implements TrackCreationInterface
{ {
return $this->removedBy; return $this->removedBy;
} }
public function setEndDate(?\DateTimeImmutable $endDate): self
{
$this->endDate = $endDate;
return $this;
}
} }

View File

@ -130,6 +130,14 @@ class Ticket implements TrackCreationInterface, TrackUpdateInterface
$this->motiveHistories->add($motiveHistory); $this->motiveHistories->add($motiveHistory);
} }
/**
* @internal use @see{AddresseHistory::__construct} instead
*/
public function addAddresseeHistory(AddresseeHistory $addresseeHistory): void
{
$this->addresseeHistory->add($addresseeHistory);
}
/** /**
* @return list<UserGroup|User> * @return list<UserGroup|User>
*/ */
@ -195,4 +203,12 @@ class Ticket implements TrackCreationInterface, TrackUpdateInterface
{ {
return $this->personHistories; return $this->personHistories;
} }
/**
* @return ReadableCollection<int, AddresseeHistory>
*/
public function getAddresseeHistories(): ReadableCollection
{
return $this->addresseeHistory;
}
} }

View File

@ -0,0 +1,56 @@
<?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\Repository;
use Chill\TicketBundle\Entity\Ticket;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ObjectRepository;
final readonly class TicketRepository implements TicketRepositoryInterface
{
private ObjectRepository $repository;
public function __construct(EntityManagerInterface $objectManager)
{
$this->repository = $objectManager->getRepository($this->getClassName());
}
public function find($id): ?Ticket
{
return $this->repository->find($id);
}
public function findAll(): array
{
return $this->repository->findAll();
}
public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array
{
return $this->repository->findBy($criteria, $orderBy, $limit, $offset);
}
public function findOneBy(array $criteria): ?Ticket
{
return $this->repository->findOneBy($criteria);
}
public function getClassName()
{
return Ticket::class;
}
public function findOneByExternalRef(string $extId): ?Ticket
{
return $this->repository->findOneBy(['externalRef' => $extId]);
}
}

View File

@ -0,0 +1,23 @@
<?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\Repository;
use Chill\TicketBundle\Entity\Ticket;
use Doctrine\Persistence\ObjectRepository;
/**
* @extends ObjectRepository<Ticket>
*/
interface TicketRepositoryInterface extends ObjectRepository
{
public function findOneByExternalRef(string $extId): ?Ticket;
}

View File

@ -1,4 +1,10 @@
import {DateTime, TranslatableString, User} from "../../../../ChillMainBundle/Resources/public/types"; import {
DateTime,
TranslatableString,
User,
UserGroup,
UserGroupOrUser
} from "../../../../ChillMainBundle/Resources/public/types";
import {Person} from "../../../../ChillPersonBundle/Resources/public/types"; import {Person} from "../../../../ChillPersonBundle/Resources/public/types";
export interface Motive { export interface Motive {
@ -46,18 +52,33 @@ interface Comment {
updatedAt: DateTime|null, updatedAt: DateTime|null,
} }
interface AddresseeHistory {
type: "ticket_addressee_history",
id: number,
startDate: DateTime|null,
addressee: UserGroupOrUser,
endDate: DateTime|null,
removedBy: User|null,
createdBy: User|null,
createdAt: DateTime|null,
updatedBy: User|null,
updatedAt: DateTime|null,
}
interface AddPersonEvent extends TicketHistory<"add_person", PersonHistory> {}; interface AddPersonEvent extends TicketHistory<"add_person", PersonHistory> {};
interface AddCommentEvent extends TicketHistory<"add_comment", Comment> {}; interface AddCommentEvent extends TicketHistory<"add_comment", Comment> {};
interface SetMotiveEvent extends TicketHistory<"set_motive", MotiveHistory> {}; interface SetMotiveEvent extends TicketHistory<"set_motive", MotiveHistory> {};
interface AddAddressee extends TicketHistory<"add_addressee", AddresseeHistory> {};
type TicketHistoryLine = AddPersonEvent | AddCommentEvent | SetMotiveEvent; type TicketHistoryLine = AddPersonEvent | AddCommentEvent | SetMotiveEvent | AddAddressee;
export interface Ticket { export interface Ticket {
type: "ticket_ticket" type: "ticket_ticket",
id: number id: number,
externalRef: string externalRef: string,
currentPersons: Person[] currentAddressees: UserGroupOrUser[],
currentMotive: null|Motive currentPersons: Person[],
currentMotive: null|Motive,
history: TicketHistoryLine[], history: TicketHistoryLine[],
} }

View File

@ -0,0 +1,56 @@
<?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\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserGroup;
use Chill\TicketBundle\Action\Ticket\SetAddresseesCommand;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
final class SetAddresseesCommandDenormalizer implements DenormalizerInterface, DenormalizerAwareInterface
{
use DenormalizerAwareTrait;
public function denormalize($data, string $type, ?string $format = null, array $context = [])
{
if (null === $data) {
return null;
}
if (!array_key_exists('addressees', $data)) {
throw new UnexpectedValueException("key 'addressees' does exists");
}
if (!is_array($data['addressees'])) {
throw new UnexpectedValueException("key 'addressees' must be an array");
}
$addresses = [];
foreach ($data['addressees'] as $address) {
$addresses[] = match ($address['type'] ?? '') {
'user_group' => $this->denormalizer->denormalize($address, UserGroup::class, $format, $context),
'user' => $this->denormalizer->denormalize($address, User::class, $format, $context),
default => throw new UnexpectedValueException('the type is not set or not supported')
};
}
return new SetAddresseesCommand($addresses);
}
public function supportsDenormalization($data, string $type, ?string $format = null)
{
return SetAddresseesCommand::class === $type && 'json' === $format;
}
}

View File

@ -11,6 +11,7 @@ declare(strict_types=1);
namespace Chill\TicketBundle\Serializer\Normalizer; namespace Chill\TicketBundle\Serializer\Normalizer;
use Chill\TicketBundle\Entity\AddresseeHistory;
use Chill\TicketBundle\Entity\Comment; use Chill\TicketBundle\Entity\Comment;
use Chill\TicketBundle\Entity\MotiveHistory; use Chill\TicketBundle\Entity\MotiveHistory;
use Chill\TicketBundle\Entity\PersonHistory; use Chill\TicketBundle\Entity\PersonHistory;
@ -79,6 +80,24 @@ final class TicketNormalizer implements NormalizerInterface, NormalizerAwareInte
], ],
$ticket->getComments()->toArray(), $ticket->getComments()->toArray(),
), ),
...array_map(
fn (AddresseeHistory $history) => [
'event_type' => 'add_addressee',
'at' => $history->getStartDate(),
'by' => $history->getCreatedBy(),
'data' => $history,
],
$ticket->getAddresseeHistories()->toArray(),
),
...array_map(
fn (AddresseeHistory $history) => [
'event_type' => 'remove_addressee',
'at' => $history->getStartDate(),
'by' => $history->getRemovedBy(),
'data' => $history,
],
$ticket->getAddresseeHistories()->filter(fn (AddresseeHistory $history) => null !== $history->getEndDate())->toArray()
),
]; ];
usort( usort(

View File

@ -3,16 +3,21 @@ services:
autoconfigure: true autoconfigure: true
autowire: true autowire: true
Chill\TicketBundle\Action\Ticket\Handler\:
resource: '../Action/Ticket/Handler/'
Chill\TicketBundle\Controller\: Chill\TicketBundle\Controller\:
resource: '../Controller/' resource: '../Controller/'
tags: tags:
- controller.service_arguments - controller.service_arguments
Chill\TicketBundle\Action\Ticket\Handler\: Chill\TicketBundle\Repository\:
resource: '../Action/Ticket/Handler/' resource: '../Repository/'
Chill\TicketBundle\Serializer\: Chill\TicketBundle\Serializer\:
resource: '../Serializer/' resource: '../Serializer/'
Chill\TicketBundle\DataFixtures\: when@dev:
resource: '../DataFixtures/' services:
Chill\TicketBundle\DataFixtures\:
resource: '../DataFixtures/'

View File

@ -0,0 +1,40 @@
<?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 Version20240423212824 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add endDate and removedBy columns on addressee history (ticket)';
}
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_ticket.addressee_history ADD endDate TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT null');
$this->addSql('ALTER TABLE chill_ticket.addressee_history ADD removedBy_id INT DEFAULT NULL');
$this->addSql('COMMENT ON COLUMN chill_ticket.addressee_history.endDate IS \'(DC2Type:datetime_immutable)\'');
$this->addSql('ALTER TABLE chill_ticket.addressee_history ADD CONSTRAINT FK_434EBDBDB8346CCF FOREIGN KEY (removedBy_id) REFERENCES users (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('CREATE INDEX IDX_434EBDBDB8346CCF ON chill_ticket.addressee_history (removedBy_id)');
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_ticket.addressee_history DROP CONSTRAINT FK_434EBDBDB8346CCF');
$this->addSql('DROP INDEX chill_ticket.IDX_434EBDBDB8346CCF');
$this->addSql('ALTER TABLE chill_ticket.addressee_history DROP endDate');
$this->addSql('ALTER TABLE chill_ticket.addressee_history DROP removedBy_id');
}
}

View File

@ -0,0 +1,122 @@
<?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\Tests\Action\Ticket\Handler;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserGroup;
use Chill\TicketBundle\Action\Ticket\Handler\SetAddresseesCommandHandler;
use Chill\TicketBundle\Action\Ticket\SetAddresseesCommand;
use Chill\TicketBundle\Entity\AddresseeHistory;
use Chill\TicketBundle\Entity\Ticket;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\Clock\MockClock;
use Symfony\Component\Security\Core\Security;
/**
* @internal
*
* @coversNothing
*/
final class SetAddressesCommandHandlerTest extends TestCase
{
use ProphecyTrait;
public function testHandleOnEmptyAddresses(): void
{
$ticket = new Ticket();
$command = new SetAddresseesCommand([$user1 = new User(), $group1 = new UserGroup()]);
$entityManager = $this->prophesize(EntityManagerInterface::class);
$entityManager->persist(Argument::that(function ($arg) use ($user1) {
return $arg instanceof AddresseeHistory && $arg->getAddressee() === $user1;
}))->shouldBeCalledOnce();
$entityManager->persist(Argument::that(function ($arg) use ($group1) {
return $arg instanceof AddresseeHistory && $arg->getAddressee() === $group1;
}))->shouldBeCalledOnce();
$handler = $this->buildHandler($entityManager->reveal());
$handler->handle($ticket, $command);
self::assertCount(2, $ticket->getCurrentAddressee());
}
public function testHandleExistingUserIsNotRemovedNorCreatingDouble(): void
{
$ticket = new Ticket();
$user = new User();
$history = new AddresseeHistory($user, new \DateTimeImmutable('1 month ago'), $ticket);
$command = new SetAddresseesCommand([$user]);
$entityManager = $this->prophesize(EntityManagerInterface::class);
$entityManager->persist(Argument::that(function ($arg) use ($user) {
return $arg instanceof AddresseeHistory && $arg->getAddressee() === $user;
}))->shouldNotBeCalled();
$handler = $this->buildHandler($entityManager->reveal());
$handler->handle($ticket, $command);
self::assertNull($history->getEndDate());
self::assertCount(1, $ticket->getCurrentAddressee());
}
public function testHandleRemoveExistingAddressee(): void
{
$ticket = new Ticket();
$user = new User();
$group = new UserGroup();
$history = new AddresseeHistory($user, new \DateTimeImmutable('1 month ago'), $ticket);
$command = new SetAddresseesCommand([$group]);
$entityManager = $this->prophesize(EntityManagerInterface::class);
$entityManager->persist(Argument::that(function ($arg) use ($group) {
return $arg instanceof AddresseeHistory && $arg->getAddressee() === $group;
}))->shouldBeCalled();
$handler = $this->buildHandler($entityManager->reveal());
$handler->handle($ticket, $command);
self::assertNotNull($history->getEndDate());
self::assertContains($group, $ticket->getCurrentAddressee());
}
public function testAddingDoublingAddresseeDoesNotCreateDoubleHistories(): void
{
$ticket = new Ticket();
$group = new UserGroup();
$command = new SetAddresseesCommand([$group, $group]);
$entityManager = $this->prophesize(EntityManagerInterface::class);
$entityManager->persist(Argument::that(function ($arg) use ($group) {
return $arg instanceof AddresseeHistory && $arg->getAddressee() === $group;
}))->shouldBeCalledOnce();
$handler = $this->buildHandler($entityManager->reveal());
$handler->handle($ticket, $command);
self::assertCount(1, $ticket->getCurrentAddressee());
}
private function buildHandler(EntityManagerInterface $entityManager): SetAddresseesCommandHandler
{
$security = $this->prophesize(Security::class);
$security->getUser()->willReturn(new User());
return new SetAddresseesCommandHandler(new MockClock(), $entityManager, $security->reveal());
}
}

View File

@ -0,0 +1,212 @@
<?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\Tests\Controller;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserGroup;
use Chill\TicketBundle\Action\Ticket\Handler\SetAddresseesCommandHandler;
use Chill\TicketBundle\Action\Ticket\SetAddresseesCommand;
use Chill\TicketBundle\Controller\SetAddresseesController;
use Chill\TicketBundle\Entity\AddresseeHistory;
use Chill\TicketBundle\Entity\Ticket;
use Doctrine\ORM\EntityManagerInterface;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Clock\MockClock;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
* @internal
*
* @coversNothing
*/
class SetAddresseesControllerTest extends KernelTestCase
{
use ProphecyTrait;
private SerializerInterface $serializer;
protected function setUp(): void
{
self::bootKernel();
$this->serializer = self::getContainer()->get(SerializerInterface::class);
}
/**
* @dataProvider getContentData
*/
public function testSetAddresseesWithValidData(array $bodyAsArray): void
{
$controller = $this->buildController(true, true);
$request = new Request(content: json_encode(['addressees' => $bodyAsArray], JSON_THROW_ON_ERROR, 512));
$ticket = new Ticket();
$response = $controller->setAddressees($ticket, $request);
self::assertEquals(200, $response->getStatusCode());
$asArray = json_decode($response->getContent(), true, 512, JSON_THROW_ON_ERROR);
self::assertIsArray($asArray);
self::assertArrayHasKey('type', $asArray);
self::assertEquals('ticket_ticket', $asArray['type']);
}
/**
* @dataProvider getContentData
*/
public function testSetAddresseesWithInvalidData(array $bodyAsArray): void
{
$controller = $this->buildController(false, false);
$request = new Request(content: json_encode(['addressees' => $bodyAsArray], JSON_THROW_ON_ERROR, 512));
$ticket = new Ticket();
$response = $controller->setAddressees($ticket, $request);
self::assertEquals(422, $response->getStatusCode());
$asArray = json_decode($response->getContent(), true, 512, JSON_THROW_ON_ERROR);
self::assertIsArray($asArray);
self::arrayHasKey('violations', $asArray);
self::assertGreaterThan(0, count($asArray['violations']));
}
/**
* @dataProvider getContentDataUnique
*/
public function testAddAddresseeWithValidData(array $bodyAsArray): void
{
$controller = $this->buildController(true, true);
$request = new Request(content: json_encode(['addressee' => $bodyAsArray], JSON_THROW_ON_ERROR, 512));
$ticket = new Ticket();
$response = $controller->addAddressee($ticket, $request);
self::assertEquals(200, $response->getStatusCode());
$asArray = json_decode($response->getContent(), true, 512, JSON_THROW_ON_ERROR);
self::assertIsArray($asArray);
self::assertArrayHasKey('type', $asArray);
self::assertEquals('ticket_ticket', $asArray['type']);
}
/**
* @throws \JsonException
*
* @dataProvider getContentDataUnique
*/
public function testAddAddresseeWithInvalidData(array $bodyAsArray): void
{
$controller = $this->buildController(false, false);
$request = new Request(content: json_encode(['addressee' => $bodyAsArray], JSON_THROW_ON_ERROR, 512));
$ticket = new Ticket();
$response = $controller->addAddressee($ticket, $request);
self::assertEquals(422, $response->getStatusCode());
$asArray = json_decode($response->getContent(), true, 512, JSON_THROW_ON_ERROR);
self::assertIsArray($asArray);
self::arrayHasKey('violations', $asArray);
self::assertGreaterThan(0, count($asArray['violations']));
}
public static function getContentDataUnique(): iterable
{
$entityManager = self::getContainer()->get(EntityManagerInterface::class);
$userGroup = $entityManager->createQuery('SELECT ug FROM '.UserGroup::class.' ug ')
->setMaxResults(1)->getOneOrNullResult();
if (null === $userGroup) {
throw new \RuntimeException('User group not existing in database');
}
$user = $entityManager->createQuery('SELECT u FROM '.User::class.' u')
->setMaxResults(1)->getOneOrNullResult();
if (null === $user) {
throw new \RuntimeException('User not existing in database');
}
self::ensureKernelShutdown();
yield [['type' => 'user', 'id' => $user->getId()]];
yield [['type' => 'user_group', 'id' => $userGroup->getId()]];
}
public static function getContentData(): iterable
{
self::bootKernel();
$entityManager = self::getContainer()->get(EntityManagerInterface::class);
$userGroup = $entityManager->createQuery('SELECT ug FROM '.UserGroup::class.' ug ')
->setMaxResults(1)->getOneOrNullResult();
if (null === $userGroup) {
throw new \RuntimeException('User group not existing in database');
}
$user = $entityManager->createQuery('SELECT u FROM '.User::class.' u')
->setMaxResults(1)->getOneOrNullResult();
if (null === $user) {
throw new \RuntimeException('User not existing in database');
}
self::ensureKernelShutdown();
yield [[['type' => 'user', 'id' => $user->getId()]]];
yield [[['type' => 'user', 'id' => $user->getId()], ['type' => 'user_group', 'id' => $userGroup->getId()]]];
yield [[['type' => 'user_group', 'id' => $userGroup->getId()]]];
}
private function buildController(bool $willSave, bool $isValid): SetAddresseesController
{
$user = new User();
$security = $this->prophesize(Security::class);
$security->isGranted('ROLE_USER')->willReturn(true);
$security->getUser()->willReturn($user);
$entityManager = $this->prophesize(EntityManagerInterface::class);
if ($willSave) {
$entityManager->flush()->shouldBeCalled();
$entityManager->persist(Argument::type(AddresseeHistory::class))->shouldBeCalled();
}
$validator = $this->prophesize(ValidatorInterface::class);
if ($isValid) {
$validator->validate(Argument::type(SetAddresseesCommand::class))->willReturn(new ConstraintViolationList([]));
} else {
$validator->validate(Argument::type(SetAddresseesCommand::class))->willReturn(
new ConstraintViolationList([
new ConstraintViolation('Fake constraint', 'fake message template', [], [], 'addresses', []),
])
);
}
return new SetAddresseesController(
$security->reveal(),
$entityManager->reveal(),
$this->serializer,
new SetAddresseesCommandHandler(new MockClock(), $entityManager->reveal(), $security->reveal()),
$validator->reveal()
);
}
}

View File

@ -11,7 +11,10 @@ declare(strict_types=1);
namespace Chill\TicketBundle\Tests\Entity; namespace Chill\TicketBundle\Tests\Entity;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserGroup;
use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Entity\Person;
use Chill\TicketBundle\Entity\AddresseeHistory;
use Chill\TicketBundle\Entity\Motive; use Chill\TicketBundle\Entity\Motive;
use Chill\TicketBundle\Entity\MotiveHistory; use Chill\TicketBundle\Entity\MotiveHistory;
use Chill\TicketBundle\Entity\PersonHistory; use Chill\TicketBundle\Entity\PersonHistory;
@ -57,5 +60,33 @@ class TicketTest extends KernelTestCase
self::assertCount(1, $ticket->getPersons()); self::assertCount(1, $ticket->getPersons());
self::assertSame($person, $ticket->getPersons()[0]); self::assertSame($person, $ticket->getPersons()[0]);
$history->setEndDate(new \DateTimeImmutable('now'));
self::assertCount(0, $ticket->getPersons());
}
public function testGetAddresse(): void
{
$ticket = new Ticket();
$user = new User();
$group = new UserGroup();
self::assertEquals([], $ticket->getCurrentAddressee());
$history = new AddresseeHistory($user, new \DateTimeImmutable('now'), $ticket);
self::assertCount(1, $ticket->getCurrentAddressee());
self::assertSame($user, $ticket->getCurrentAddressee()[0]);
$history2 = new AddresseeHistory($group, new \DateTimeImmutable('now'), $ticket);
self::assertCount(2, $ticket->getCurrentAddressee());
self::assertContains($group, $ticket->getCurrentAddressee());
$history->setEndDate(new \DateTimeImmutable('now'));
self::assertCount(1, $ticket->getCurrentAddressee());
self::assertSame($group, $ticket->getCurrentAddressee()[0]);
} }
} }

View File

@ -0,0 +1,66 @@
<?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\Tests\Serializer\Normalizer;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserGroup;
use Chill\TicketBundle\Action\Ticket\SetAddresseesCommand;
use Chill\TicketBundle\Serializer\Normalizer\SetAddresseesCommandDenormalizer;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
/**
* @internal
*
* @coversNothing
*/
class SetAddresseesCommandDenormalizerTest extends TestCase
{
use ProphecyTrait;
public function testSupportsDenormalization()
{
$denormalizer = new SetAddresseesCommandDenormalizer();
self::assertTrue($denormalizer->supportsDenormalization('', SetAddresseesCommand::class, 'json'));
self::assertFalse($denormalizer->supportsDenormalization('', stdClass::class, 'json'));
}
public function testDenormalize()
{
$denormalizer = $this->buildDenormalizer();
$actual = $denormalizer->denormalize(['addressees' => [['type' => 'user'], ['type' => 'user_group']]], SetAddresseesCommand::class, 'json');
self::assertInstanceOf(SetAddresseesCommand::class, $actual);
self::assertIsArray($actual->addressees);
self::assertCount(2, $actual->addressees);
self::assertInstanceOf(User::class, $actual->addressees[0]);
self::assertInstanceOf(UserGroup::class, $actual->addressees[1]);
}
private function buildDenormalizer(): SetAddresseesCommandDenormalizer
{
$normalizer = $this->prophesize(DenormalizerInterface::class);
$normalizer->denormalize(Argument::any(), User::class, 'json', Argument::any())
->willReturn(new User());
$normalizer->denormalize(Argument::any(), UserGroup::class, 'json', Argument::any())
->willReturn(new UserGroup());
$denormalizer = new SetAddresseesCommandDenormalizer();
$denormalizer->setDenormalizer($normalizer->reveal());
return $denormalizer;
}
}

View File

@ -12,7 +12,9 @@ declare(strict_types=1);
namespace Chill\TicketBundle\Tests\Serializer\Normalizer; namespace Chill\TicketBundle\Tests\Serializer\Normalizer;
use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserGroup;
use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Entity\Person;
use Chill\TicketBundle\Entity\AddresseeHistory;
use Chill\TicketBundle\Entity\Comment; use Chill\TicketBundle\Entity\Comment;
use Chill\TicketBundle\Entity\Motive; use Chill\TicketBundle\Entity\Motive;
use Chill\TicketBundle\Entity\MotiveHistory; use Chill\TicketBundle\Entity\MotiveHistory;
@ -108,6 +110,8 @@ class TicketNormalizerTest extends KernelTestCase
->willReturn(['motiveHistory']); ->willReturn(['motiveHistory']);
$normalizer->normalize(Argument::type(Comment::class), 'json', Argument::type('array')) $normalizer->normalize(Argument::type(Comment::class), 'json', Argument::type('array'))
->willReturn(['comment']); ->willReturn(['comment']);
$normalizer->normalize(Argument::type(AddresseeHistory::class), 'json', Argument::type('array'))
->willReturn(['addresseeHistory']);
// null values // null values
$normalizer->normalize(null, 'json', Argument::type('array'))->willReturn(null); $normalizer->normalize(null, 'json', Argument::type('array'))->willReturn(null);
@ -142,6 +146,9 @@ class TicketNormalizerTest extends KernelTestCase
$comment = new Comment('blabla test', $ticket); $comment = new Comment('blabla test', $ticket);
$comment->setCreatedAt(new \DateTimeImmutable('2024-04-01T12:04:00')); $comment->setCreatedAt(new \DateTimeImmutable('2024-04-01T12:04:00'));
$comment->setCreatedBy(new User()); $comment->setCreatedBy(new User());
$addresseeHistory = new AddresseeHistory(new User(), new \DateTimeImmutable('2024-04-01T12:05:00'), $ticket);
$addresseeHistory->setEndDate(new \DateTimeImmutable('2024-04-01T12:06:00'));
new AddresseeHistory(new UserGroup(), new \DateTimeImmutable('2024-04-01T12:07:00'), $ticket);
yield [ yield [
$ticket, $ticket,
@ -150,10 +157,17 @@ class TicketNormalizerTest extends KernelTestCase
'id' => null, 'id' => null,
'externalRef' => '2134', 'externalRef' => '2134',
'currentPersons' => ['embedded'], 'currentPersons' => ['embedded'],
'currentAddressees' => [], 'currentAddressees' => ['embedded'],
'currentInputs' => [], 'currentInputs' => [],
'currentMotive' => ['type' => 'motive', 'id' => 0], 'currentMotive' => ['type' => 'motive', 'id' => 0],
'history' => [['event_type' => 'add_person'], ['event_type' => 'set_motive'], ['event_type' => 'add_comment']], 'history' => [
['event_type' => 'add_person'],
['event_type' => 'set_motive'],
['event_type' => 'add_comment'],
['event_type' => 'add_addressee'],
['event_type' => 'remove_addressee'],
['event_type' => 'add_addressee'],
],
], ],
]; ];
} }