Skip to content

Commit

Permalink
add two endpoints to check if user donated to reward
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbeig committed May 2, 2024
1 parent 0ef04c8 commit 378d46a
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 4 deletions.
2 changes: 1 addition & 1 deletion config/packages/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ security:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
api:
pattern: ^/(userInfo|userApiToken|userInvests|userInvestedToReward)
pattern: ^/(userInfo|userApiToken|userInvests|userInvestedToReward|userActiveInvestedToReward)
security: true
stateless: true
oauth2: true
Expand Down
9 changes: 9 additions & 0 deletions src/Controller/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace App\Controller;

use App\UseCase\GetUserActiveInvestedRewardUseCase;
use App\UseCase\GetUserApiTokenUseCase;
use App\UseCase\GetUserInfoUseCase;
use App\UseCase\GetUserInvestsUseCase;
Expand Down Expand Up @@ -62,6 +63,14 @@ public function userInvestedToReward(Request $request, int $reward_id, GetUserIn
return $this->json($response);
}

#[Route('/userActiveInvestedToReward/{reward_id}', name: 'userInvestedToReward')]
public function userActiveInvestInReward(int $reward_id, GetUserActiveInvestedRewardUseCase $useCase): Response
{
$response = $useCase->execute($reward_id, $this->getUser()->getUserIdentifier());

return $this->json($response);
}

#[Route('/{_locale}', name: 'home')]
public function index(): Response
{
Expand Down
16 changes: 16 additions & 0 deletions src/Entity/Invest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Entity;

use App\Repository\InvestRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: InvestRepository::class)]
Expand All @@ -22,6 +23,9 @@ class Invest
#[ORM\Column]
private ?int $status = null;

#[ORM\Column(type: Types::DATE_MUTABLE)]
private ?\DateTimeInterface $invested = null;

public function getId(): ?int
{
return $this->id;
Expand Down Expand Up @@ -62,4 +66,16 @@ public function setStatus(int $status): static

return $this;
}

public function getInvested(): ?\DateTimeInterface
{
return $this->invested;
}

public function setInvested(\DateTimeInterface $invested): static
{
$this->invested = $invested;

return $this;
}
}
15 changes: 14 additions & 1 deletion src/Repository/InvestRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,20 @@ public function hasUserInvestedInReward(User $user, int $reward_id): bool
return (bool) $this->createQueryBuilder('i')
->select('i')
->innerJoin(InvestReward::class, 'ir', 'WITH', 'ir.invest = i.id')
->where('i.userId = :user_id AND ir.reward = :reward_id')
->where('i.userId = :user_id AND ir.reward = :reward_id and i.status = 1')
->setParameter(':user_id', $user->getId())
->setParameter(':reward_id', $reward_id)
->setMaxResults(1)
->getQuery()
->getOneOrNullResult();
}

public function hasUserInvestedInRewardLastMonth(User $user, int $reward_id): bool
{
return (bool) $this->createQueryBuilder('i')
->select('i')
->innerJoin(InvestReward::class, 'ir', 'WITH', 'ir.invest = i.id')
->where('i.status = 1 and i.userId = :user_id AND ir.reward = :reward_id AND i.invested >= DATE_SUB(CURRENT_DATE(), 1, \'MONTH\')')
->setParameter(':user_id', $user->getId())
->setParameter(':reward_id', $reward_id)
->setMaxResults(1)
Expand Down
37 changes: 37 additions & 0 deletions src/UseCase/GetUserActiveInvestedRewardUseCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/*
* This file is part of the Goteo Package.
*
* (c) Platoniq y Fundación Goteo <[email protected]>
*
* For the full copyright and license information, please view the README.md
* and LICENSE files that was distributed with this source code.
*/

namespace App\UseCase;

use App\Repository\InvestRepository;
use App\Repository\UserRepository;

class GetUserActiveInvestedRewardUseCase
{
private UserRepository $userRepository;
private InvestRepository $investRepository;

public function __construct(
UserRepository $userRepository,
InvestRepository $investRepository
) {
$this->userRepository = $userRepository;
$this->investRepository = $investRepository;
}

public function execute(
int $rewardId,
string $userId,
): bool {
$user = $this->userRepository->findById($userId);

return $this->investRepository->hasUserInvestedInRewardLastMonth($user, $rewardId);
}
}
4 changes: 2 additions & 2 deletions src/UseCase/GetUserInvestedToRewardUseCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public function __construct(
}

public function execute(
int $reward_id,
int $rewardId,
string $userId,
): bool {
$user = $this->userRepository->findById($userId);

return $this->investRepository->hasUserInvestedInReward($user, $reward_id);
return $this->investRepository->hasUserInvestedInReward($user, $rewardId);
}
}

0 comments on commit 378d46a

Please sign in to comment.