Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tec: Associate time spent with corresponding message #800

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions migrations/Version20241119150300AddMessageToTimeSpent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

// This file is part of Bileto.
// Copyright 2022-2024 Probesys
// SPDX-License-Identifier: AGPL-3.0-or-later

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Platforms\MariaDBPlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

// phpcs:disable Generic.Files.LineLength
final class Version20241119150300AddMessageToTimeSpent extends AbstractMigration
{
public function getDescription(): string
{
return 'Add the message_id column to the time_spent table.';
}

public function up(Schema $schema): void
{
$dbPlatform = $this->connection->getDatabasePlatform();
if ($dbPlatform instanceof PostgreSQLPlatform) {
$this->addSql('ALTER TABLE time_spent ADD message_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE time_spent ADD CONSTRAINT FK_B417D625537A1329 FOREIGN KEY (message_id) REFERENCES message (id) ON DELETE SET NULL NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('CREATE INDEX IDX_B417D625537A1329 ON time_spent (message_id)');
} elseif ($dbPlatform instanceof MariaDBPlatform) {
$this->addSql('ALTER TABLE time_spent ADD message_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE time_spent ADD CONSTRAINT FK_B417D625537A1329 FOREIGN KEY (message_id) REFERENCES message (id) ON DELETE SET NULL');
$this->addSql('CREATE INDEX IDX_B417D625537A1329 ON time_spent (message_id)');
}
}

public function down(Schema $schema): void
{
$dbPlatform = $this->connection->getDatabasePlatform();
if ($dbPlatform instanceof PostgreSQLPlatform) {
$this->addSql('ALTER TABLE time_spent DROP CONSTRAINT FK_B417D625537A1329');
$this->addSql('DROP INDEX IDX_B417D625537A1329');
$this->addSql('ALTER TABLE time_spent DROP message_id');
} elseif ($dbPlatform instanceof MariaDBPlatform) {
$this->addSql('ALTER TABLE time_spent DROP FOREIGN KEY FK_B417D625537A1329');
$this->addSql('DROP INDEX IDX_B417D625537A1329 ON time_spent');
$this->addSql('ALTER TABLE time_spent DROP message_id');
}
}
}
2 changes: 1 addition & 1 deletion src/Controller/Tickets/MessagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function create(

$minutesSpent = $form->has('timeSpent') ? $form->get('timeSpent')->getData() : 0;
if ($minutesSpent > 0) {
$ticketTimeAccounting->accountTime($ticket, $minutesSpent);
$ticketTimeAccounting->accountTime($minutesSpent, $ticket, $message);
}

$type = $form->has('type') ? $form->get('type')->getData() : 'normal';
Expand Down
16 changes: 16 additions & 0 deletions src/Entity/TimeSpent.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ class TimeSpent implements EntityInterface, MonitorableEntityInterface, UidEntit
#[ORM\JoinColumn(onDelete: 'SET NULL')]
private ?Contract $contract = null;

#[ORM\ManyToOne]
#[ORM\JoinColumn(onDelete: 'SET NULL')]
private ?Message $message = null;

public function getTicket(): ?Ticket
{
return $this->ticket;
Expand Down Expand Up @@ -124,4 +128,16 @@ public function getTimelineType(): string
{
return 'time_spent';
}

public function getMessage(): ?Message
{
return $this->message;
}

public function setMessage(?Message $message): static
{
$this->message = $message;

return $this;
}
}
10 changes: 8 additions & 2 deletions src/Service/TicketTimeAccounting.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@ public function __construct(
*
* @param positive-int $minutes
*/
public function accountTime(Entity\Ticket $ticket, int $minutes): void
{
public function accountTime(
int $minutes,
Entity\Ticket $ticket,
?Entity\Message $message = null,
): void {
$contract = $ticket->getOngoingContract();

if (!$contract) {
$timeSpent = new Entity\TimeSpent();
$timeSpent->setTicket($ticket);
$timeSpent->setTime($minutes);
$timeSpent->setRealTime($minutes);
$timeSpent->setMessage($message);

$this->timeSpentRepository->save($timeSpent, true);

Expand All @@ -42,6 +46,7 @@ public function accountTime(Entity\Ticket $ticket, int $minutes): void

$timeSpent = $this->contractTimeAccounting->accountTime($contract, $minutes);
$timeSpent->setTicket($ticket);
$timeSpent->setMessage($message);

$this->timeSpentRepository->save($timeSpent, true);

Expand All @@ -54,6 +59,7 @@ public function accountTime(Entity\Ticket $ticket, int $minutes): void
$timeSpent->setTicket($ticket);
$timeSpent->setTime($remainingUnaccountedTime);
$timeSpent->setRealTime($remainingUnaccountedTime);
$timeSpent->setMessage($message);

$this->timeSpentRepository->save($timeSpent, true);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Controller/Tickets/MessagesControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,11 @@ public function testPostCreateAcceptsTimeSpent(): void
],
]);

$message = Factory\MessageFactory::last();
$timeSpent = Factory\TimeSpentFactory::first();
$this->assertSame(20, $timeSpent->getTime());
$this->assertSame($ticket->getId(), $timeSpent->getTicket()->getId());
$this->assertSame($message->getId(), $timeSpent->getMessage()->getId());
}

public function testPostCanAssociateTimeSpentToContract(): void
Expand Down
Loading