Skip to content

Commit

Permalink
feat: welcome page for external users with terms and conditions VOL-5664
Browse files Browse the repository at this point in the history
  • Loading branch information
ilindsay committed Nov 15, 2024
1 parent 14665d4 commit 423ac3d
Show file tree
Hide file tree
Showing 23 changed files with 680 additions and 16 deletions.
1 change: 1 addition & 0 deletions app/api/module/Api/config/command-map.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@
TransferCommand\User\DeletePartner::class => CommandHandler\User\DeletePartner::class,
TransferCommand\User\UpdateUserLastLoginAt::class => CommandHandler\User\UpdateUserLastLoginAt::class,
TransferCommand\User\RegisterConsultantAndOperator::class => CommandHandler\User\RegisterConsultantAndOperator::class,
TransferCommand\User\AgreeTerms::class => CommandHandler\User\AgreeTerms::class,

// Transfer - Team
TransferCommand\Team\CreateTeam::class => CommandHandler\Team\CreateTeam::class,
Expand Down
1 change: 1 addition & 0 deletions app/api/module/Api/config/validation-map/user.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@
CommandHandler\User\UpdateUserSelfserve::class => CanManageUser::class,
CommandHandler\User\UpdateUserSelfserveFactory::class => CanManageUser::class,
CommandHandler\User\UpdateUserLastLoginAt::class => NotIsAnonymousUser::class,
CommandHandler\User\AgreeTerms::class => IsExternalUser::class,
];
46 changes: 46 additions & 0 deletions app/api/module/Api/src/Domain/CommandHandler/User/AgreeTerms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Dvsa\Olcs\Api\Domain\CommandHandler\User;

use Dvsa\Olcs\Api\Domain\AuthAwareInterface;
use Dvsa\Olcs\Api\Domain\AuthAwareTrait;
use Dvsa\Olcs\Api\Domain\CacheAwareInterface;
use Dvsa\Olcs\Api\Domain\CacheAwareTrait;
use Dvsa\Olcs\Api\Domain\CommandHandler\AbstractCommandHandler;
use Dvsa\Olcs\Api\Domain\Repository\User as UserRepository;
use Dvsa\Olcs\Api\Entity\User\User;
use Dvsa\Olcs\Transfer\Command\CommandInterface;

final class AgreeTerms extends AbstractCommandHandler implements AuthAwareInterface, CacheAwareInterface
{
use AuthAwareTrait;
use CacheAwareTrait;

public const SUCCESS_MSG = 'Terms and conditions accepted';

protected $repoServiceName = 'User';

public function handleCommand(CommandInterface $command)
{
$userId = $this->getCurrentUser()->getId();

/**
* @var UserRepository $repo
* @var User $user
*/
$repo = $this->getRepo();

$user = $repo->fetchById($userId);
$user->agreeTermsAndConditions();
$repo->save($user);

$this->clearUserCaches([$userId]);

$this->result->addId('User', $userId);
$this->result->addMessage(self::SUCCESS_MSG);

return $this->result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Dvsa\Olcs\Api\Domain\CommandHandler\AbstractUserCommandHandler;
use Dvsa\Olcs\Api\Domain\CommandHandler\TransactionedInterface;
use Dvsa\Olcs\Api\Entity\User\Role as RoleEntity;
use Dvsa\Olcs\Api\Entity\User\User as UserEntity;
use Dvsa\Olcs\Transfer\Command\CommandInterface;
use Dvsa\Olcs\Transfer\Command\User\RegisterUserSelfserve as RegisterUserSelfServeCommand;

Expand All @@ -20,8 +21,12 @@ final class RegisterConsultantAndOperator extends AbstractUserCommandHandler imp

public function handleCommand(CommandInterface $command)
{
//because this is being created by a transport consultant the operator admin will need to agree terms
$operatorDetails = $command->getOperatorDetails();
$operatorDetails['createdByConsultant'] = true;

// Register the operator first, a new Org will be created.
$this->result->merge($this->handleSideEffect(RegisterUserSelfServeCommand::create($command->getOperatorDetails())));
$this->result->merge($this->handleSideEffect(RegisterUserSelfServeCommand::create($operatorDetails)));

// Get the newly created user entity
$user = $this->getRepo()->fetchById($this->result->getId('user'));
Expand All @@ -32,10 +37,19 @@ public function handleCommand(CommandInterface $command)

$this->result->merge($this->handleSideEffect(RegisterUserSelfServeCommand::create($consultantDetails)));

// Get the new consultant user entity and set the correct role.
/**
* Get the new consultant user entity and set the correct role
*
* @var UserEntity $consultantUser
* @var RoleEntity $operatorTcRole
*/
$consultantUser = $this->getRepo()->fetchById($this->result->getId('user'));
$operatorTcRole = $this->getRepo('Role')->fetchByRole(RoleEntity::ROLE_OPERATOR_TC);
$consultantUser->setRoles(new ArrayCollection([$operatorTcRole]));

//the consultant has already accepted terms and conditions
$consultantUser->agreeTermsAndConditions();

$this->getRepo()->save($consultantUser);
return $this->result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public function handleCommand(CommandInterface $command)
)
);

//if the user wasn't created by a transport consultant
if ($command->getCreatedByConsultant() !== true) {
$user->agreeTermsAndConditions();
}

$result = new Result();

$this->getRepo()->save($user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public function handleQuery(QueryInterface $query)
$result = $this->result(
$user,
[
'termsAgreed',
'team',
'transportManager',
'partnerContactDetails',
Expand Down
33 changes: 33 additions & 0 deletions app/api/module/Api/src/Entity/User/AbstractUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,15 @@ abstract class AbstractUser implements BundleSerializableInterface, JsonSerializ
*/
protected $translateToWelsh = 0;

/**
* Terms agreed
*
* @var boolean
*
* @ORM\Column(type="boolean", name="terms_agreed", nullable=false, options={"default": 0})
*/
protected $termsAgreed = 0;

/**
* Transport manager
*
Expand Down Expand Up @@ -303,6 +312,30 @@ public function getAccountDisabled()
return $this->accountDisabled;
}

/**
* Set the terms agreed
*
* @param boolean $termsAgreed new value being set
*
* @return User
*/
public function setTermsAgreed($termsAgreed)
{
$this->termsAgreed = $termsAgreed;

return $this;
}

/**
* Get the terms agreed
*
* @return boolean
*/
public function getTermsAgreed()
{
return $this->termsAgreed;
}

/**
* Set the contact details
*
Expand Down
12 changes: 12 additions & 0 deletions app/api/module/Api/src/Entity/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -765,4 +765,16 @@ public function canResetPassword(): bool
//disabled users can't reset
return !$this->isDisabled();
}

public function agreeTermsAndConditions(): User
{
$this->termsAgreed = true;

return $this;
}

public function hasAgreedTermsAndConditions(): bool
{
return $this->termsAgreed;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Dvsa\OlcsTest\Api\Domain\CommandHandler\User;

use Dvsa\Olcs\Api\Domain\Repository\User as UserRepository;
use Dvsa\Olcs\Api\Entity\User\User as UserEntity;
use Dvsa\Olcs\Transfer\Command\User\AgreeTerms as Cmd;
use Dvsa\Olcs\Transfer\Service\CacheEncryption;
use Dvsa\OlcsTest\Api\Domain\CommandHandler\AbstractCommandHandlerTestCase;
use Dvsa\Olcs\Api\Domain\CommandHandler\User\AgreeTerms;
use LmcRbacMvc\Service\AuthorizationService;
use Mockery as m;

class AgreeTermsTest extends AbstractCommandHandlerTestCase
{
public function setUp(): void
{
$this->sut = new AgreeTerms();
$this->mockRepo('User', UserRepository::class);

$this->mockedSmServices = [
AuthorizationService::class => m::mock(AuthorizationService::class),
CacheEncryption::class => m::mock(CacheEncryption::class),
];

parent::setUp();
}

public function testHandleCommand()
{
$userId = 999;

$command = Cmd::create([]);

$loggedInUser = m::mock(UserEntity::class);
$loggedInUser->expects('getId')->andReturn($userId);

$this->mockedSmServices[AuthorizationService::class]->expects('getIdentity->getUser')
->andReturn($loggedInUser);

$this->mockedSmServices[CacheEncryption::class]->expects('removeCustomItems')
->with(CacheEncryption::USER_ACCOUNT_IDENTIFIER, [$userId]);

$userFromRepo = m::mock(UserEntity::class);
$userFromRepo->expects('agreeTermsAndConditions')->withNoArgs();

$this->repoMap['User']->expects('fetchById')->with($userId)->andReturn($userFromRepo);
$this->repoMap['User']->expects('save')->with($userFromRepo);

$expectedResult = [
'id' => [
'User' => $userId,
],
'messages' => [
0 => AgreeTerms::SUCCESS_MSG
],
];

$this->assertEquals($expectedResult, $this->sut->handleCommand($command)->toArray());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Dvsa\OlcsTest\Api\Domain\CommandHandler\User;

use Dvsa\Olcs\Api\Domain\CommandHandler\User\RegisterConsultantAndOperator;
Expand All @@ -26,9 +28,10 @@ public function setUp(): void
parent::setUp();
}

public function testHandleCommand()
public function testHandleCommand(): void
{
$operatorDetails = ['organisationName' => 'Operator Org',];
$operatorDetails = ['organisationName' => 'Operator Org'];
$operatorModifiedDetails = ['organisationName' => 'Operator Org', 'createdByConsultant' => true];

$command = RegisterConsultantAndOperatorCommand::create(
[
Expand All @@ -41,7 +44,7 @@ public function testHandleCommand()

$this->expectedSideEffect(
RegisterUserSelfServeCommand::class,
$operatorDetails,
$operatorModifiedDetails,
$operatorResult
);

Expand Down Expand Up @@ -70,6 +73,8 @@ public function testHandleCommand()
->once()
->andReturnSelf();

$consultant->expects('agreeTermsAndConditions')->withNoArgs();

$this->repoMap['User']->shouldReceive('save')->with($consultant);

$mockRole = m::mock(Role::class);
Expand Down
Loading

0 comments on commit 423ac3d

Please sign in to comment.