Skip to content
This repository has been archived by the owner on Feb 13, 2023. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into release/v0.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rprzedzik committed Jan 16, 2020
2 parents 1d08862 + 84d49fc commit 7e15f93
Show file tree
Hide file tree
Showing 7 changed files with 324 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/**
* Copyright © Bold Brand Commerce Sp. z o.o. All rights reserved.
* See LICENSE.txt for license details.
*/

declare(strict_types = 1);

namespace Ergonode\Notification\Application\Controller\Api;

use Ergonode\Account\Infrastructure\Provider\AuthenticatedUserProviderInterface;
use Ergonode\Api\Application\Response\SuccessResponse;
use Ergonode\Notification\Domain\Query\NotificationQueryInterface;
use Swagger\Annotations as SWG;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

/**
* @Route("/profile/notifications/check", methods={"GET"})
*/
class NotificationCheckAction
{
/**
* @var NotificationQueryInterface
*/
private $query;

/**
* @var AuthenticatedUserProviderInterface
*/
private $userProvider;

/**
* @param NotificationQueryInterface $query
* @param AuthenticatedUserProviderInterface $userProvider
*/
public function __construct(NotificationQueryInterface $query, AuthenticatedUserProviderInterface $userProvider)
{
$this->query = $query;
$this->userProvider = $userProvider;
}

/**
* @SWG\Tag(name="Profile")
* @SWG\Parameter(
* name="language",
* in="path",
* type="string",
* required=true,
* default="EN",
* description="Language Code",
* )
* @SWG\Response(
* response=200,
* description="Returns notification information for current user",
* )
*
* @return Response
*
* @throws \Exception
*/
public function __invoke(): Response
{
$user = $this->userProvider->provide();
$result = $this->query->check($user->getId());

return new SuccessResponse($result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

/**
* Copyright © Bold Brand Commerce Sp. z o.o. All rights reserved.
* See LICENSE.txt for license details.
*/

declare(strict_types = 1);

namespace Ergonode\Notification\Application\Controller\Api;

use Ergonode\Account\Infrastructure\Provider\AuthenticatedUserProviderInterface;
use Ergonode\Api\Application\Response\AcceptedResponse;
use Ergonode\Api\Application\Response\SuccessResponse;
use Ergonode\EventSourcing\Infrastructure\Bus\CommandBusInterface;
use Ergonode\Notification\Domain\Command\MarkNotificationCommand;
use Ramsey\Uuid\Uuid;
use Swagger\Annotations as SWG;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

/**
* @Route("/profile/notifications/{notification}/mark", methods={"POST"})
*/
class NotificationMarkUpdateAction
{
/**
* @var AuthenticatedUserProviderInterface
*/
private $userProvider;

/**
* @var CommandBusInterface
*/
private $commandBud;

/**
* @param AuthenticatedUserProviderInterface $userProvider
* @param CommandBusInterface $commandBud
*/
public function __construct(AuthenticatedUserProviderInterface $userProvider, CommandBusInterface $commandBud)
{
$this->userProvider = $userProvider;
$this->commandBud = $commandBud;
}

/**
* @SWG\Tag(name="Profile")
* @SWG\Parameter(
* name="language",
* in="path",
* type="string",
* required=true,
* default="EN",
* description="Language Code",
* )
* @SWG\Parameter(
* name="notification",
* in="path",
* type="string",
* description="Notification id",
* )
* @SWG\Response(
* response=200,
* description="Returns notifications",
* )
*
* @param string $notification
*
* @return Response
*
* @throws \Exception
*/
public function __invoke(string $notification): Response
{
$user = $this->userProvider->provide();
$command = new MarkNotificationCommand(Uuid::fromString($notification), $user->getId(), new \DateTime());

$this->commandBud->dispatch($command);

return new AcceptedResponse();
}
}
70 changes: 70 additions & 0 deletions module/notification/src/Domain/Command/MarkNotificationCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/**
* Copyright © Bold Brand Commerce Sp. z o.o. All rights reserved.
* See LICENSE.txt for license details.
*/

declare(strict_types = 1);

namespace Ergonode\Notification\Domain\Command;

use Ergonode\Account\Domain\Entity\UserId;
use Ergonode\EventSourcing\Infrastructure\DomainCommandInterface;
use Ramsey\Uuid\Uuid;

/**
*/
class MarkNotificationCommand implements DomainCommandInterface
{
/**
* @var Uuid
*/
private $notificationId;

/**
* @var UserId
*/
private $userId;

/**
* @var \DateTime
*/
private $readAt;

/**
* @param Uuid $notificationId
* @param UserId $userId
* @param \DateTime $readAt
*/
public function __construct(Uuid $notificationId, UserId $userId, \DateTime $readAt)
{
$this->notificationId = $notificationId;
$this->userId = $userId;
$this->readAt = $readAt;
}

/**
* @return Uuid
*/
public function getNotificationId(): Uuid
{
return $this->notificationId;
}

/**
* @return UserId
*/
public function getUserId(): UserId
{
return $this->userId;
}

/**
* @return \DateTime
*/
public function getReadAt(): \DateTime
{
return $this->readAt;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Ergonode\Account\Domain\Entity\UserId;
use Ergonode\Core\Domain\ValueObject\Language;
use Ergonode\Grid\DataSetInterface;
use Ramsey\Uuid\Uuid;

/**
*/
Expand All @@ -23,4 +24,20 @@ interface NotificationQueryInterface
* @return DataSetInterface
*/
public function getDataSet(UserId $id, Language $language): DataSetInterface;

/**
* @param UserId $id
*
* @return array
*/
public function check(UserId $id): array;

/**
* @param Uuid $id
* @param UserId $userId
* @param \DateTime $readAt
*
* @throws \Doctrine\DBAL\DBALException
*/
public function mark(Uuid $id, UserId $userId, \DateTime $readAt): void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Copyright © Bold Brand Commerce Sp. z o.o. All rights reserved.
* See LICENSE.txt for license details.
*/

declare(strict_types = 1);

namespace Ergonode\Notification\Infrastructure\Handler;

use Ergonode\Notification\Domain\Command\MarkNotificationCommand;
use Ergonode\Notification\Domain\Query\NotificationQueryInterface;
use Ergonode\Notification\Infrastructure\Sender\NotificationMarker;

/**
*/
class MarkNotificationCommandHandler
{
/**
* @var NotificationQueryInterface
*/
private $query;

/**
* @param NotificationQueryInterface $query
*/
public function __construct(NotificationQueryInterface $query)
{
$this->query = $query;
}

/**
* @param MarkNotificationCommand $command
*
* @throws \Exception
*/
public function __invoke(MarkNotificationCommand $command)
{
$this->query->mark($command->getNotificationId(), $command->getUserId(), $command->getReadAt());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
namespace Ergonode\Notification\Infrastructure\Sender;

use Ergonode\Account\Domain\Entity\UserId;
use Ergonode\Notification\Domain\Entity\Notification;
use Ergonode\Notification\Domain\NotificationInterface;
use Webmozart\Assert\Assert;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Ergonode\Notification\Domain\Query\NotificationQueryInterface;
use Ergonode\Grid\DataSetInterface;
use Ergonode\Grid\DbalDataSet;
use Ramsey\Uuid\Uuid;

/**
*/
Expand Down Expand Up @@ -64,6 +65,48 @@ public function getDataSet(UserId $id, Language $language): DataSetInterface
return new DbalDataSet($result);
}

/**
* @param UserId $id
*
* @return array
*/
public function check(UserId $id): array
{
$query = $this->connection->createQueryBuilder();
$result = $query->select('count(*) as count')
->from(self::USER_NOTIFICATION_TABLE)
->andWhere($query->expr()->eq('recipient_id', ':id'))
->andWhere($query->expr()->isNull('read_at'))
->setParameter(':id', $id->getValue())
->execute()
->fetch(\PDO::FETCH_COLUMN);

return [
'unread' => $result ?: 0,
];
}

/**
* @param Uuid $id
* @param UserId $userId
* @param \DateTime $readAt
*
* @throws \Doctrine\DBAL\DBALException
*/
public function mark(Uuid $id, UserId $userId, \DateTime $readAt): void
{
$this->connection->update(
'users_notification',
[
'read_at' => $readAt->format('Y-m-d'),
],
[
'recipient_id' => $userId->getValue(),
'notification_id' => $id->toString(),
]
);
}

/**
* @return QueryBuilder
*/
Expand Down

0 comments on commit 7e15f93

Please sign in to comment.