This repository has been archived by the owner on Feb 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bugfix(product) fix audit information generation for product (#1820)
- Loading branch information
1 parent
51770a2
commit 7a536d5
Showing
37 changed files
with
345 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
/** | ||
* Copyright © Bold Brand Commerce Sp. z o.o. All rights reserved. | ||
* See LICENSE.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ergonode\Migration; | ||
|
||
use Doctrine\DBAL\Schema\Schema; | ||
use Ergonode\Product\Domain\Event\ProductValueChangedEvent; | ||
use Ergonode\Product\Domain\Event\ProductValueAddedEvent; | ||
|
||
/** | ||
* Auto-generated Ergonode Migration Class: | ||
*/ | ||
final class Version20211219110000 extends AbstractErgonodeMigration | ||
{ | ||
/** | ||
* @throws \Exception | ||
*/ | ||
public function up(Schema $schema): void | ||
{ | ||
$this->addSql(' | ||
CREATE TABLE IF NOT EXISTS audit ( | ||
id uuid NOT NULL, | ||
created_at TIMESTAMP WITH TIME ZONE NOT NULL, | ||
created_by UUID DEFAULT NULL, | ||
edited_at TIMESTAMP WITH TIME ZONE NOT NULL, | ||
edited_by UUID DEFAULT NULL, | ||
PRIMARY KEY(id) | ||
) | ||
'); | ||
|
||
$this->updateAudit(); | ||
$eventId = $this->getEventId(ProductValueAddedEvent::class); | ||
$this->removeEvents($eventId, 'esa_created_at'); | ||
$this->removeEvents($eventId, 'esa_created_by'); | ||
$this->removeEvents($eventId, 'esa_edited_at'); | ||
$this->removeEvents($eventId, 'esa_edited_by'); | ||
$eventId = $this->getEventId(ProductValueChangedEvent::class); | ||
$this->removeEvents($eventId, 'esa_edited_at'); | ||
$this->removeEvents($eventId, 'esa_edited_by'); | ||
|
||
$this->removeValues('esa_created_at'); | ||
$this->removeValues('esa_created_by'); | ||
$this->removeValues('esa_edited_at'); | ||
$this->removeValues('esa_edited_by'); | ||
|
||
$this->addSql('ALTER TABLE product DROP column created_at'); | ||
$this->addSql('ALTER TABLE product DROP column updated_at'); | ||
|
||
$this->addSql('DELETE FROM event_store_snapshot WHERE aggregate_id in (SELECT id FROM product)'); | ||
} | ||
|
||
private function getEventId(string $event): string | ||
{ | ||
return $this->connection->executeQuery( | ||
'SELECT id FROM event_store_event WHERE event_class = :class', | ||
[ | ||
':class' => $event, | ||
] | ||
)->fetchOne(); | ||
} | ||
|
||
private function removeEvents(string $eventId, string $code): void | ||
{ | ||
$this->connection->executeQuery( | ||
'DELETE FROM event_store WHERE event_id = ? and payload::TEXT ilike \'%'.$code.'%\'', | ||
[$eventId] | ||
); | ||
} | ||
|
||
private function removeValues(string $code): void | ||
{ | ||
$attributeId = $this->connection->executeQuery( | ||
'SELECT id FROM attribute WHERE code = :code', | ||
[ | ||
':code' => $code, | ||
] | ||
)->fetchOne(); | ||
|
||
if ($attributeId) { | ||
$this->addSql( | ||
'DELETE FROM value_translation WHERE value_id IN | ||
(SELECT value_id FROM product_value WHERE attribute_id = ?)', | ||
[$attributeId] | ||
); | ||
|
||
$this->addSql( | ||
'DELETE FROM product_value WHERE attribute_id = ?', | ||
[$attributeId] | ||
); | ||
} | ||
} | ||
|
||
private function updateAudit(): void | ||
{ | ||
$this->addSql('INSERT INTO audit (id, created_at, edited_at) | ||
SELECT id, created_at, updated_at FROM product'); | ||
|
||
$this->addSql('UPDATE audit | ||
SET created_by = t.recorded_by | ||
FROM (SELECT aggregate_id, recorded_by FROM event_store | ||
WHERE id IN (SELECT min(id) FROM event_store group by aggregate_id)) AS t | ||
WHERE audit.id = t.aggregate_id'); | ||
|
||
$this->addSql('UPDATE audit | ||
SET edited_by = t.recorded_by | ||
FROM (SELECT aggregate_id, recorded_by FROM event_store | ||
WHERE id IN (SELECT max(id) FROM event_store group by aggregate_id)) AS t | ||
WHERE audit.id = t.aggregate_id'); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
module/product/src/Application/Handler/AbstractAuditEventHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
/** | ||
* Copyright © Bold Brand Commerce Sp. z o.o. All rights reserved. | ||
* See LICENSE.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ergonode\Product\Application\Handler; | ||
|
||
use Ergonode\SharedKernel\Domain\Aggregate\UserId; | ||
use Ergonode\SharedKernel\Domain\User\UserInterface; | ||
use Doctrine\DBAL\Connection; | ||
use Symfony\Component\Security\Core\Security; | ||
|
||
abstract class AbstractAuditEventHandler | ||
{ | ||
protected Connection $connection; | ||
|
||
protected Security $security; | ||
|
||
public function __construct(Connection $connection, Security $security) | ||
{ | ||
$this->connection = $connection; | ||
$this->security = $security; | ||
} | ||
|
||
protected function getUser(): ?UserId | ||
{ | ||
$user = $this->security->getUser(); | ||
|
||
if ($user instanceof UserInterface) { | ||
return $user->getId(); | ||
} | ||
|
||
return null; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
module/product/src/Application/Handler/AuditProductCreatedEventHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
/** | ||
* Copyright © Bold Brand Commerce Sp. z o.o. All rights reserved. | ||
* See LICENSE.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ergonode\Product\Application\Handler; | ||
|
||
use Ergonode\Product\Application\Event\ProductCreatedEvent; | ||
use Doctrine\DBAL\Types\Types; | ||
|
||
class AuditProductCreatedEventHandler extends AbstractAuditEventHandler | ||
{ | ||
private const TABLE = 'audit'; | ||
|
||
public function __invoke(ProductCreatedEvent $event): void | ||
{ | ||
$date = new \DateTime(); | ||
$user = $this->getUser(); | ||
|
||
$this->connection->insert( | ||
self::TABLE, | ||
[ | ||
'id' => $event->getProduct()->getId()->getValue(), | ||
'created_at' => $date, | ||
'edited_at' => $date, | ||
'created_by' => $user, | ||
'edited_by' => $user, | ||
], | ||
[ | ||
'created_at' => Types::DATETIMETZ_MUTABLE, | ||
'edited_at' => Types::DATETIMETZ_MUTABLE, | ||
], | ||
); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
module/product/src/Application/Handler/AuditProductDeletedEventHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
/** | ||
* Copyright © Bold Brand Commerce Sp. z o.o. All rights reserved. | ||
* See LICENSE.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ergonode\Product\Application\Handler; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Ergonode\Product\Application\Event\ProductDeletedEvent; | ||
|
||
class AuditProductDeletedEventHandler | ||
{ | ||
private const TABLE = 'audit'; | ||
|
||
private Connection $connection; | ||
|
||
public function __construct(Connection $connection) | ||
{ | ||
$this->connection = $connection; | ||
} | ||
|
||
public function __invoke(ProductDeletedEvent $event): void | ||
{ | ||
$this->connection->delete( | ||
self::TABLE, | ||
[ | ||
'id' => $event->getProduct()->getId()->getValue(), | ||
] | ||
); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
module/product/src/Application/Handler/AuditProductUpdatedEventHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
/** | ||
* Copyright © Bold Brand Commerce Sp. z o.o. All rights reserved. | ||
* See LICENSE.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ergonode\Product\Application\Handler; | ||
|
||
use Doctrine\DBAL\Types\Types; | ||
use Ergonode\Product\Application\Event\ProductUpdatedEvent; | ||
|
||
class AuditProductUpdatedEventHandler extends AbstractAuditEventHandler | ||
{ | ||
private const TABLE = 'audit'; | ||
|
||
public function __invoke(ProductUpdatedEvent $event): void | ||
{ | ||
$createdAt = new \DateTime(); | ||
$createdBy = $this->getUser(); | ||
|
||
$this->connection->update( | ||
self::TABLE, | ||
[ | ||
'edited_at' => $createdAt, | ||
'edited_by' => $createdBy, | ||
], | ||
[ | ||
'id' => $event->getProduct()->getId()->getValue(), | ||
], | ||
[ | ||
'edited_at' => Types::DATETIMETZ_MUTABLE, | ||
], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.