diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php index f62dfc342..91d4d6ef8 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php @@ -122,10 +122,17 @@ class AccompanyingPeriod * cascade={"persist", "remove"}, * orphanRemoval=true * ) - * @Groups({"read"}) */ private $comments; + /** + * @ORM\ManyToOne( + * targetEntity=Comment::class + * ) + * @Groups({"read"}) + */ + private ?Comment $initialComment = null; + /** * @var Collection * @@ -210,21 +217,21 @@ class AccompanyingPeriod /** * @var bool - * @ORM\Column(type="boolean") + * @ORM\Column(type="boolean", options={"default": false} ) * @Groups({"read", "write"}) */ private $requestorAnonymous = false; /** * @var bool - * @ORM\Column(type="boolean") + * @ORM\Column(type="boolean", options={"default": false} ) * @Groups({"read", "write"}) */ private $emergency = false; /** * @var bool - * @ORM\Column(type="boolean") + * @ORM\Column(type="boolean", options={"default": false} ) * @Groups({"read", "write"}) */ private $confidential = false; @@ -264,6 +271,7 @@ class AccompanyingPeriod $this->participations = new ArrayCollection(); $this->scopes = new ArrayCollection(); $this->socialIssues = new ArrayCollection(); + $this->comments = new ArrayCollection(); } /** @@ -357,9 +365,14 @@ class AccompanyingPeriod return $this->remark; } + /** + * @Groups({"read"}) + */ public function getComments(): Collection { - return $this->comments; + return $this->comments->filter(function (Comment $c) { + return $c !== $this->initialComment; + }); } public function addComment(Comment $comment): self @@ -376,6 +389,30 @@ class AccompanyingPeriod $this->comments->removeElement($comment); } + /** + * @Groups({"write"}) + */ + public function setInitialComment(?Comment $comment = null): self + { + if (NULL === $comment && NULL !== $this->initialComment) { + $this->removeComment($this->initialComment); + } elseif ($comment instanceof Comment) { + $this->addComment($comment); + } + + $this->initialComment = $comment; + + return $this; + } + + /** + * @Groups({"read"}) + */ + public function getInitialComment(): ?Comment + { + return $this->initialComment; + } + /** * Get Participations Collection */ diff --git a/src/Bundle/ChillPersonBundle/Tests/Entity/AccompanyingPeriodTest.php b/src/Bundle/ChillPersonBundle/Tests/Entity/AccompanyingPeriodTest.php index 7fa11d6e1..b14f5180e 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Entity/AccompanyingPeriodTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Entity/AccompanyingPeriodTest.php @@ -25,6 +25,7 @@ namespace Chill\PersonBundle\Tests\Entity; use Chill\PersonBundle\Entity\AccompanyingPeriod; use Chill\PersonBundle\Entity\Person; use Chill\ThirdPartyBundle\Entity\ThirdParty; +use Chill\PersonBundle\Entity\AccompanyingPeriod\Comment; class AccompanyingPeriodTest extends \PHPUnit\Framework\TestCase { @@ -131,4 +132,23 @@ class AccompanyingPeriodTest extends \PHPUnit\Framework\TestCase $this->assertNull($period->getRequestorPerson()); $this->assertNull($period->getRequestor()); } + + public function testInitialComment() + { + $period = new AccompanyingPeriod(new \DateTime()); + $comment = new Comment(); + + $period->setInitialComment(NULL); + $this->assertNull($period->getInitialComment()); + + $period->setInitialComment($comment); + $this->assertSame($period->getInitialComment(), $comment); + $this->assertSame($period, $comment->getAccompanyingPeriod()); + $this->assertEquals(0, count($period->getComments()), "The initial comment should not appears in the list of comments"); + + $period->setInitialComment(NULL); + $this->assertNull($period->getInitialComment()); + $this->assertNull($comment->getAccompanyingPeriod()); + $this->assertEquals(0, count($period->getComments()), "The initial comment should not appears in the list of comments"); + } } diff --git a/src/Bundle/ChillPersonBundle/chill.api.specs.yaml b/src/Bundle/ChillPersonBundle/chill.api.specs.yaml index 9010686e8..96d039287 100644 --- a/src/Bundle/ChillPersonBundle/chill.api.specs.yaml +++ b/src/Bundle/ChillPersonBundle/chill.api.specs.yaml @@ -202,7 +202,7 @@ paths: patch: tags: - person - summary: "Return the description for an accompanying course (accompanying period)" + summary: "Alter an accompanying course (accompanying period)" parameters: - name: id in: path @@ -219,6 +219,22 @@ paths: application/json: schema: $ref: '#/components/schemas/AccompanyingPeriod' + examples: + Set the requestor as anonymous: + value: + type: accompanying_period + id: 12345 + requestorAnonymous: true + Adding an initial comment: + value: + type: accompanying_period + id: 2668, + initialComment: + type: accompanying_period_comment + content: > + This is my an initial comment. + + Say hello to the new "parcours"! responses: 401: description: "Unauthorized" diff --git a/src/Bundle/ChillPersonBundle/migrations/Version20210518162439.php b/src/Bundle/ChillPersonBundle/migrations/Version20210518162439.php new file mode 100644 index 000000000..6907a64c6 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/migrations/Version20210518162439.php @@ -0,0 +1,31 @@ +addSql('ALTER TABLE chill_person_accompanying_period ADD initialComment_id INT DEFAULT NULL'); + $this->addSql('ALTER TABLE chill_person_accompanying_period ADD CONSTRAINT FK_E260A8683111D50B FOREIGN KEY (initialComment_id) REFERENCES chill_person_accompanying_period_comment (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('CREATE INDEX IDX_E260A8683111D50B ON chill_person_accompanying_period (initialComment_id)'); + } + + public function down(Schema $schema): void + { + $this->addSql('ALTER TABLE chill_person_accompanying_period DROP initialComment_id'); + } +}