diff --git a/app/api/module/Api/src/Domain/CommandHandler/User/RegisterConsultantAndOperator.php b/app/api/module/Api/src/Domain/CommandHandler/User/RegisterConsultantAndOperator.php index b7c10ea88e..8f0959e8f0 100644 --- a/app/api/module/Api/src/Domain/CommandHandler/User/RegisterConsultantAndOperator.php +++ b/app/api/module/Api/src/Domain/CommandHandler/User/RegisterConsultantAndOperator.php @@ -6,8 +6,10 @@ namespace Dvsa\Olcs\Api\Domain\CommandHandler\User; +use Doctrine\Common\Collections\ArrayCollection; 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\Transfer\Command\CommandInterface; use Dvsa\Olcs\Transfer\Command\User\RegisterUserSelfserve as RegisterUserSelfServeCommand; @@ -29,6 +31,10 @@ public function handleCommand(CommandInterface $command) $this->result->merge($this->handleSideEffect(RegisterUserSelfServeCommand::create($consultantDetails))); + // Get the new consultant user entity and set the correct role. + $consultantUser = $this->getRepo()->fetchById($this->result->getId('user')); + $consultantUser->setRoles(new ArrayCollection([RoleEntity::ROLE_OPERATOR_TC])); + $this->getRepo()->save($consultantUser); return $this->result; } } diff --git a/app/api/test/module/Api/src/Domain/CommandHandler/User/RegisterConsultantAndOperatorTest.php b/app/api/test/module/Api/src/Domain/CommandHandler/User/RegisterConsultantAndOperatorTest.php index 450100a18c..155a5c33ed 100644 --- a/app/api/test/module/Api/src/Domain/CommandHandler/User/RegisterConsultantAndOperatorTest.php +++ b/app/api/test/module/Api/src/Domain/CommandHandler/User/RegisterConsultantAndOperatorTest.php @@ -60,6 +60,16 @@ public function testHandleCommand() $consultantResult = new Result(); $consultantResult->addId('user', 101)->addMessage('User created successfully'); + $consultant = m::mock(UserEntity::class)->makePartial(); + $this->repoMap['User']->shouldReceive('fetchById')->with(101)->andReturn($consultant); + + $consultant->shouldReceive('setRoles') + ->with(m::type(\Doctrine\Common\Collections\ArrayCollection::class)) + ->once() + ->andReturnSelf(); + + $this->repoMap['User']->shouldReceive('save')->with($consultant); + $this->expectedSideEffect( RegisterUserSelfServeCommand::class, $consultantDetails,