diff --git a/api/v1/invitations/InvitationController.php b/api/v1/invitations/InvitationController.php index 0d1ea50d1db..a6d1c9811e2 100644 --- a/api/v1/invitations/InvitationController.php +++ b/api/v1/invitations/InvitationController.php @@ -30,11 +30,8 @@ use PKP\invitation\core\Invitation; use PKP\invitation\core\ReceiveInvitationController; use PKP\invitation\core\traits\HasMailable; -use PKP\invitation\invitations\userRoleAssignment\resources\UserRoleAssignmentInviteResource; -use PKP\invitation\invitations\userRoleAssignment\rules\EmailMustNotExistRule; use PKP\invitation\invitations\userRoleAssignment\rules\UserMustExistRule; use PKP\invitation\models\InvitationModel; -use PKP\security\authorization\UserRolesRequiredPolicy; use PKP\security\Role; use PKP\validation\ValidatorFactory; use Validator; @@ -45,18 +42,24 @@ class InvitationController extends PKPBaseController public const PARAM_ID = 'invitationId'; public const PARAM_KEY = 'key'; - public $notNeedAPIHandler = [ - 'getMany', + public $actionsInvite = [ + 'get', + 'populate', + 'invite', 'getMailable', - 'cancel', ]; - public $noParamRequired = [ - 'getMany', + public $actionsReceive = [ + 'receive', + 'finalize', + 'refine', + 'decline', + 'cancel', ]; public $requiresType = [ 'add', + 'getMany', ]; public $requiresOnlyId = [ @@ -124,7 +127,7 @@ public function getGroupRoutes(): void ]), ])->group(function () { - Route::get('', $this->getMany(...)) + Route::get('{type}', $this->getMany(...)) ->name('invitation.getMany'); // Get By Id Methods @@ -207,45 +210,39 @@ public function authorize(PKPRequest $request, array &$args, array $roleAssignme $invitation = Repo::invitation()->getByIdAndKey($invitationId, $invitationKey); } - if ($actionName == 'getMany') { - $this->addPolicy(new UserRolesRequiredPolicy($request), true); - } else { - if (!isset($invitation)) { - throw new Exception('Invitation could not be created'); - } - - $this->invitation = $invitation; + if (!isset($invitation)) { + throw new Exception('Invitation could not be created'); + } - if (!in_array($actionName, $this->notNeedAPIHandler)) { - if (!$this->invitation instanceof IApiHandleable) { - throw new Exception('This invitation does not support API handling'); - } + $this->invitation = $invitation; - if (in_array($actionName, $this->requiresOnlyId) && $this->invitation->getStatus() != InvitationStatus::INITIALIZED) { - throw new Exception('This action is not allowed'); - } + if (!$this->invitation instanceof IApiHandleable) { + throw new Exception('This invitation does not support API handling'); + } - if (in_array($actionName, $this->requiresIdAndKey) && $this->invitation->getStatus() != InvitationStatus::PENDING) { - throw new Exception('This action is not allowed'); - } + if (in_array($actionName, $this->actionsInvite) && $this->invitation->getStatus() != InvitationStatus::INITIALIZED) { + throw new Exception('This action is not allowed'); + } - $this->createInvitationHandler = $invitation->getCreateInvitationController($this->invitation); - $this->receiveInvitationHandler = $invitation->getReceiveInvitationController($this->invitation); + if (in_array($actionName, $this->actionsReceive) && $this->invitation->getStatus() != InvitationStatus::PENDING) { + throw new Exception('This action is not allowed'); + } - if (!isset($this->createInvitationHandler) || !isset($this->receiveInvitationHandler)) { - throw new Exception('This invitation should have defined its API handling code'); - } + $this->createInvitationHandler = $invitation->getCreateInvitationController($this->invitation); + $this->receiveInvitationHandler = $invitation->getReceiveInvitationController($this->invitation); - $this->selectedHandler = $this->getHandlerForAction($actionName); + if (!isset($this->createInvitationHandler) || !isset($this->receiveInvitationHandler)) { + throw new Exception('This invitation should have defined its API handling code'); + } - if (!method_exists($this->selectedHandler, $actionName)) { - throw new Exception("The handler does not support the method: {$actionName}"); - } + $this->selectedHandler = $this->getHandlerForAction($actionName); - $this->selectedHandler->authorize($this, $request, $args, $roleAssignments); - } + if (!method_exists($this->selectedHandler, $actionName)) { + throw new Exception("The handler does not support the method: {$actionName}"); } + $this->selectedHandler->authorize($this, $request, $args, $roleAssignments); + return parent::authorize($request, $args, $roleAssignments); } @@ -269,7 +266,6 @@ public function add(Request $illuminateRequest): JsonResponse 'nullable', 'required_without:userId', 'email', - new EmailMustNotExistRule($payload['inviteeEmail']), ] ]; @@ -338,63 +334,51 @@ public function getMany(Request $illuminateRequest): JsonResponse $context = $illuminateRequest->attributes->get('context'); /** @var \PKP\context\Context $context */ $invitationType = $this->getParameter(self::PARAM_TYPE); - $count = $illuminateRequest->query('count', 10); // default count to 10 if not provided - $offset = $illuminateRequest->query('offset', 0); // default offset to 0 if not provided - + // Build the common query $query = InvitationModel::query() - ->when($invitationType, function ($query, $invitationType) { - return $query->byType($invitationType); - }) - ->when($context, function ($query, $context) { - return $query->byContextId($context->getId()); - }) + ->when($invitationType, fn($query) => $query->byType($invitationType)) + ->when($context, fn($query) => $query->byContextId($context->getId())) ->stillActive(); - - $maxCount = $query->count(); - $invitations = $query->skip($offset) - ->take($count) - ->get(); - - $finalCollection = $invitations->map(function ($invitation) { - $specificInvitation = Repo::invitation()->getById($invitation->id); - return $specificInvitation; - }); + // Delegate to the specific handler for additional logic + $specificData = $this->selectedHandler->getMany($illuminateRequest, $query); return response()->json([ - 'itemsMax' => $maxCount, - 'items' => (UserRoleAssignmentInviteResource::collection($finalCollection)), + 'itemsMax' => $query->count(), + 'items' => $specificData, ], Response::HTTP_OK); } - public function getMailable(Request $illuminateRequest): JsonResponse + public function cancel(Request $illuminateRequest): JsonResponse { - if (in_array(HasMailable::class, class_uses($this->invitation))) { - $mailable = $this->invitation->getMailable(); - + if (!$this->invitation->isPending()) { return response()->json([ - 'mailable' => $mailable, - ], Response::HTTP_OK); + 'error' => __('invitation.api.error.invitationCantBeCanceled'), + ], Response::HTTP_BAD_REQUEST); } - return response()->json([ - 'error' => __('invitation.api.error.invitationTypeNotHasMailable'), - ], Response::HTTP_BAD_REQUEST); + try { + return $this->selectedHandler->cancel(); + } catch (\Exception $e) { + return response()->json([], Response::HTTP_INTERNAL_SERVER_ERROR); + } } - public function cancel(Request $illuminateRequest): JsonResponse + public function getMailable(Request $illuminateRequest): JsonResponse { - if (!$this->invitation->isPending()) { + // Ensure the invitation supports mailables + if (!in_array(HasMailable::class, class_uses($this->invitation))) { return response()->json([ - 'error' => __('invitation.api.error.invitationCantBeCanceled'), + 'error' => __('invitation.api.error.invitationTypeNotHasMailable'), ], Response::HTTP_BAD_REQUEST); } - $this->invitation->updateStatus(InvitationStatus::CANCELLED); - - return response()->json( - (new UserRoleAssignmentInviteResource($this->invitation))->toArray($illuminateRequest), - Response::HTTP_OK - ); + try { + return $this->selectedHandler->getMailable(); + } catch (\Exception $e) { + return response()->json([ + 'error' => $e->getMessage(), + ], Response::HTTP_INTERNAL_SERVER_ERROR); + } } } diff --git a/classes/invitation/core/CreateInvitationController.php b/classes/invitation/core/CreateInvitationController.php index b712877a014..7f760b12166 100644 --- a/classes/invitation/core/CreateInvitationController.php +++ b/classes/invitation/core/CreateInvitationController.php @@ -14,12 +14,13 @@ namespace PKP\invitation\core; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Routing\Controller; +use Illuminate\Support\Collection; use PKP\core\PKPBaseController; use PKP\core\PKPRequest; -use PKP\invitation\core\Invitation; abstract class CreateInvitationController extends Controller { @@ -30,4 +31,8 @@ abstract public function add(Request $illuminateRequest): JsonResponse; abstract public function populate(Request $illuminateRequest): JsonResponse; abstract public function invite(Request $illuminateRequest): JsonResponse; abstract public function get(Request $illuminateRequest): JsonResponse; + + abstract public function cancel(): JsonResponse; + abstract public function getMailable(): JsonResponse; + abstract public function getMany(Request $illuminateRequest, Builder $query): Collection; } diff --git a/classes/invitation/core/Invitation.php b/classes/invitation/core/Invitation.php index 3057196afdc..9be98b1d3af 100644 --- a/classes/invitation/core/Invitation.php +++ b/classes/invitation/core/Invitation.php @@ -236,11 +236,7 @@ public function updatePayload(?ValidationContext $validationContext = null): ?bo } } - // Update the payload attribute on the invitation model - $this->invitationModel->setAttribute('payload', $currentPayloadArray); - - // Save the updated invitation model to the database - return $this->invitationModel->save(); + return $this->invitationModel->update(['payload' => $currentPayloadArray]); } public function getNotAccessibleBeforeInvite(): array @@ -351,6 +347,15 @@ public function getExistingUser(): ?User return Repo::user()->get($this->invitationModel->userId); } + public function getExistingUserByEmail(): ?User + { + if (!isset($this->invitationModel->email)) { + return null; + } + + return Repo::user()->getByEmail($this->invitationModel->email); + } + public function getContext(): ?Context { if (!isset($this->invitationModel->contextId)) { @@ -480,10 +485,10 @@ protected function array_diff_assoc_recursive($array1, $array2) return $difference; } - public function updateStatus(InvitationStatus $status): void + public function updateStatus(InvitationStatus $status): bool { $this->invitationModel->status = $status; - $this->invitationModel->save(); + return $this->invitationModel->save(); } public function isPending(): bool diff --git a/classes/invitation/invitations/userRoleAssignment/UserRoleAssignmentInvite.php b/classes/invitation/invitations/userRoleAssignment/UserRoleAssignmentInvite.php index e3dafeb0e5d..91fc0ce9f0f 100644 --- a/classes/invitation/invitations/userRoleAssignment/UserRoleAssignmentInvite.php +++ b/classes/invitation/invitations/userRoleAssignment/UserRoleAssignmentInvite.php @@ -35,6 +35,7 @@ use PKP\invitation\invitations\userRoleAssignment\rules\UserMustExistRule; use PKP\mail\mailables\UserRoleAssignmentInvitationNotify; use PKP\security\Validation; +use PKP\user\User; class UserRoleAssignmentInvite extends Invitation implements IApiHandleable { @@ -166,6 +167,11 @@ public function getValidationRules(ValidationContext $validationContext = Valida $this->getPayload()->userGroupsToAdd ); $invitationValidationRules[Invitation::VALIDATION_RULE_GENERIC][] = new UserMustExistRule($this->getUserId()); + } + + if ( + $validationContext === ValidationContext::VALIDATION_CONTEXT_FINALIZE + ) { $invitationValidationRules[Invitation::VALIDATION_RULE_GENERIC][] = new EmailMustNotExistRule($this->getEmail()); } @@ -207,5 +213,25 @@ public function updatePayload(?ValidationContext $validationContext = null): ?bo // Call the parent updatePayload method to continue the normal update process return parent::updatePayload($validationContext); } - + + public function changeInvitationUserIdUsingUserEmail(): ?bool + { + $invitationUserByEmail = $this->getExistingUserByEmail(); + + if (!isset($this->invitationModel->userId) && isset($invitationUserByEmail)) { + $this->invitationModel->userId = $invitationUserByEmail->getId(); + $this->invitationModel->email = null; + + $result = $this->invitationModel->save(); + + if ($result) { + $this->getPayload()->shouldUseInviteData = true; + + return $this->updatePayload(); + } + } + + return null; + } + } diff --git a/classes/invitation/invitations/userRoleAssignment/handlers/api/UserRoleAssignmentCreateController.php b/classes/invitation/invitations/userRoleAssignment/handlers/api/UserRoleAssignmentCreateController.php index d9e6a1b3a36..833c65f9e6c 100644 --- a/classes/invitation/invitations/userRoleAssignment/handlers/api/UserRoleAssignmentCreateController.php +++ b/classes/invitation/invitations/userRoleAssignment/handlers/api/UserRoleAssignmentCreateController.php @@ -13,14 +13,18 @@ namespace PKP\invitation\invitations\userRoleAssignment\handlers\api; +use APP\facades\Repo; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Http\Response; +use Illuminate\Support\Collection; use PKP\core\PKPBaseController; use PKP\core\PKPRequest; use PKP\invitation\core\CreateInvitationController; +use PKP\invitation\core\enums\InvitationStatus; use PKP\invitation\core\enums\ValidationContext; -use PKP\invitation\core\Invitation; +use PKP\invitation\invitations\userRoleAssignment\resources\UserRoleAssignmentInviteManagerDataResource; use PKP\invitation\invitations\userRoleAssignment\resources\UserRoleAssignmentInviteResource; use PKP\invitation\invitations\userRoleAssignment\UserRoleAssignmentInvite; use PKP\security\authorization\ContextAccessPolicy; @@ -110,6 +114,8 @@ public function invite(Request $illuminateRequest): JsonResponse $this->invitation->getPayload()->sendEmailAddress = $existingUser->getEmail(); } + $this->invitation->getPayload()->inviteStagePayload = $this->invitation->getPayload(); + $this->invitation->updatePayload(); if (!$this->invitation->validate([], ValidationContext::VALIDATION_CONTEXT_INVITE)) { @@ -131,4 +137,47 @@ public function invite(Request $illuminateRequest): JsonResponse Response::HTTP_OK ); } + + public function getMany(Request $illuminateRequest, Builder $query): Collection + { + $count = $illuminateRequest->query('count', 10); // Default count + $offset = $illuminateRequest->query('offset', 0); // Default offset + + // Apply pagination and retrieve results + $invitations = $query + ->skip($offset) + ->take($count) + ->get(); + + $finalCollection = $invitations->map(function ($invitation) { + $specificInvitation = Repo::invitation()->getById($invitation->id); + return $specificInvitation; + }); + + return UserRoleAssignmentInviteManagerDataResource::collection($finalCollection)->collect(); + } + + public function cancel(): JsonResponse + { + $result = $this->invitation->updateStatus(InvitationStatus::CANCELLED); + + if (!$result) { + return response()->json([], Response::HTTP_CONFLICT); + } + + return response()->json([], Response::HTTP_OK); + } + + public function getMailable(): JsonResponse + { + $mailable = $this->invitation->getMailable(); + + if (!isset($mailable)) { + return response()->json([], Response::HTTP_NOT_FOUND); + } + + return response()->json([ + 'mailable' => $mailable, + ], Response::HTTP_OK); + } } diff --git a/classes/invitation/invitations/userRoleAssignment/handlers/api/UserRoleAssignmentReceiveController.php b/classes/invitation/invitations/userRoleAssignment/handlers/api/UserRoleAssignmentReceiveController.php index fc8506357bb..277f42a742f 100644 --- a/classes/invitation/invitations/userRoleAssignment/handlers/api/UserRoleAssignmentReceiveController.php +++ b/classes/invitation/invitations/userRoleAssignment/handlers/api/UserRoleAssignmentReceiveController.php @@ -23,7 +23,6 @@ use PKP\core\PKPBaseController; use PKP\invitation\core\enums\InvitationStatus; use PKP\invitation\core\enums\ValidationContext; -use PKP\invitation\core\Invitation; use PKP\invitation\core\ReceiveInvitationController; use PKP\invitation\invitations\userRoleAssignment\helpers\UserGroupHelper; use PKP\invitation\invitations\userRoleAssignment\resources\UserRoleAssignmentInviteResource; @@ -45,6 +44,8 @@ public function __construct(public UserRoleAssignmentInvite $invitation) */ public function authorize(PKPBaseController $controller, PKPRequest $request, array &$args, array $roleAssignments): bool { + $this->invitation->changeInvitationUserIdUsingUserEmail(); + $user = $this->invitation->getExistingUser(); if (!isset($user)) { $controller->addPolicy(new AnonymousUserPolicy($request)); diff --git a/classes/invitation/invitations/userRoleAssignment/payload/UserRoleAssignmentInvitePayload.php b/classes/invitation/invitations/userRoleAssignment/payload/UserRoleAssignmentInvitePayload.php index 88718e7bcb5..81c8e2d2cc0 100644 --- a/classes/invitation/invitations/userRoleAssignment/payload/UserRoleAssignmentInvitePayload.php +++ b/classes/invitation/invitations/userRoleAssignment/payload/UserRoleAssignmentInvitePayload.php @@ -21,7 +21,6 @@ use PKP\invitation\invitations\userRoleAssignment\rules\AddUserGroupRule; use PKP\invitation\invitations\userRoleAssignment\rules\AllowedKeysRule; use PKP\invitation\invitations\userRoleAssignment\rules\NotNullIfPresent; -use PKP\invitation\invitations\userRoleAssignment\rules\PrimaryLocaleRequired; use PKP\invitation\invitations\userRoleAssignment\rules\ProhibitedIncludingNull; use PKP\invitation\invitations\userRoleAssignment\rules\UserGroupExistsRule; use PKP\invitation\invitations\userRoleAssignment\rules\UsernameExistsRule; @@ -42,6 +41,8 @@ public function __construct( public ?array $userGroupsToAdd = null, public ?bool $passwordHashed = null, public ?string $sendEmailAddress = null, + public ?array $inviteStagePayload = null, + public ?bool $shouldUseInviteData = null, ) { parent::__construct(get_object_vars($this)); @@ -161,6 +162,9 @@ public function getValidationRules(UserRoleAssignmentInvite $invitation, Validat Rule::when(in_array($validationContext, [ValidationContext::VALIDATION_CONTEXT_INVITE, ValidationContext::VALIDATION_CONTEXT_FINALIZE]), ['nullable']), 'orcid' ], + 'shouldUseInviteData' => [ + 'prohibited', + ], ]; return $validationRules; diff --git a/classes/invitation/invitations/userRoleAssignment/resources/BaseUserRoleAssignmentInviteResource.php b/classes/invitation/invitations/userRoleAssignment/resources/BaseUserRoleAssignmentInviteResource.php new file mode 100644 index 00000000000..b88ce4cece9 --- /dev/null +++ b/classes/invitation/invitations/userRoleAssignment/resources/BaseUserRoleAssignmentInviteResource.php @@ -0,0 +1,93 @@ +invitationModel->toArray(); + + return $invitationData; + } + + /** + * Transform the userGroupsToAdd to include related UserGroup data. + * + * @param array|null $userGroups + * @return array + */ + protected function transformUserGroups(?array $userGroups) + { + return collect($userGroups)->map(function ($userGroup) { + $userGroupModel = Repo::userGroup()->get($userGroup['userGroupId']); + + return [ + 'userGroupId' => $userGroup['userGroupId'], + 'userGroupName' => $userGroupModel->getName(null), + 'masthead' => $userGroup['masthead'], + 'dateStart' => $userGroup['dateStart'], + 'dateEnd' => $userGroup['dateEnd'], + ]; + })->toArray(); + } + + /** + * Transform the userGroupsToAdd to include related UserGroup data. + * + * @param array|null $userGroups + * @return array + */ + protected function transformUser(?User $user): ?array + { + if (!isset($user)) { + return null; + } + + return [ + 'email' => $user->getEmail(), + 'fullName' => $user->getFullName(), + 'familyName' => $user->getFamilyName(null), + 'givenName' => $user->getGivenName(null), + 'country' => $user->getCountry(), + 'affiliation' => $user->getAffiliation(null), + 'orcid' => $user->getOrcid() + ]; + } + + protected function createNewUserFromPayload(UserRoleAssignmentInvitePayload $payload): User + { + $newUser = new User(); + + $newUser->setAffiliation($payload->affiliation, null); + $newUser->setFamilyName($payload->familyName, null); + $newUser->setGivenName($payload->givenName, null); + $newUser->setCountry($payload->userCountry); + $newUser->setUsername($payload->username); + $newUser->setEmail($payload->sendEmailAddress); + + return $newUser; + } +} \ No newline at end of file diff --git a/classes/invitation/invitations/userRoleAssignment/resources/UserRoleAssignmentInviteManagerDataResource.php b/classes/invitation/invitations/userRoleAssignment/resources/UserRoleAssignmentInviteManagerDataResource.php new file mode 100644 index 00000000000..a467ea3d3b6 --- /dev/null +++ b/classes/invitation/invitations/userRoleAssignment/resources/UserRoleAssignmentInviteManagerDataResource.php @@ -0,0 +1,63 @@ +getPayload(); + + $existingUser = null; + $newUser = null; + + if ($this->getPayload()->shouldUseInviteData) { + $payload = UserRoleAssignmentInvitePayload::fromArray($this->getPayload()->inviteStagePayload); + + $newUser = $this->createNewUserFromPayload($payload); + } else { + $existingUser = $this->getExistingUser(); + + if (!isset($existingUser)) { + $newUser = $this->createNewUserFromPayload($payload); + } + } + + // Return specific fields from the UserRoleAssignmentInvite + return array_merge($baseData, [ + 'orcid' => $payload->userOrcid, + 'givenName' => $payload->givenName, + 'familyName' => $payload->familyName, + 'affiliation' => $payload->affiliation, + 'country' => $payload->userCountry, + 'emailSubject' => $payload->emailSubject, + 'emailBody' => $payload->emailBody, + 'userGroupsToAdd' => $this->transformUserGroups($payload->userGroupsToAdd), + 'username' => $payload->username, + 'sendEmailAddress' => $payload->sendEmailAddress, + 'existingUser' => $this->transformUser($existingUser), + 'newUser' => $this->transformUser($newUser), + ]); + } +} \ No newline at end of file diff --git a/classes/invitation/invitations/userRoleAssignment/resources/UserRoleAssignmentInviteResource.php b/classes/invitation/invitations/userRoleAssignment/resources/UserRoleAssignmentInviteResource.php index 71b0f2e1ec8..ce1bb1b45e8 100644 --- a/classes/invitation/invitations/userRoleAssignment/resources/UserRoleAssignmentInviteResource.php +++ b/classes/invitation/invitations/userRoleAssignment/resources/UserRoleAssignmentInviteResource.php @@ -14,13 +14,10 @@ namespace PKP\invitation\invitations\userRoleAssignment\resources; -use APP\facades\Repo; use Illuminate\Http\Request; -use Illuminate\Http\Resources\Json\JsonResource; use PKP\user\User; -use PKP\userGroup\UserGroup; -class UserRoleAssignmentInviteResource extends JsonResource +class UserRoleAssignmentInviteResource extends BaseUserRoleAssignmentInviteResource { /** * Transform the resource into an array. @@ -28,7 +25,7 @@ class UserRoleAssignmentInviteResource extends JsonResource public function toArray(Request $request) { // Get all attributes of the invitationModel as an array - $invitationData = $this->invitationModel->toArray(); + $baseData = parent::toArray($request); $existingUser = $this->getExistingUser(); $newUser = null; @@ -39,18 +36,18 @@ public function toArray(Request $request) $newUser->setAffiliation($this->getPayload()->affiliation, null); $newUser->setFamilyName($this->getPayload()->familyName, null); $newUser->setGivenName($this->getPayload()->givenName, null); - $newUser->setCountry($this->getPayload()->country); + $newUser->setCountry($this->getPayload()->userCountry); $newUser->setUsername($this->getPayload()->username); $newUser->setEmail($this->getPayload()->sendEmailAddress); } // Return specific fields from the UserRoleAssignmentInvite - return array_merge($invitationData, [ - 'orcid' => $this->getPayload()->orcid, + return array_merge($baseData, [ + 'orcid' => $this->getPayload()->userOrcid, 'givenName' => $this->getPayload()->givenName, 'familyName' => $this->getPayload()->familyName, 'affiliation' => $this->getPayload()->affiliation, - 'country' => $this->getPayload()->country, + 'country' => $this->getPayload()->userCountry, 'emailSubject' => $this->getPayload()->emailSubject, 'emailBody' => $this->getPayload()->emailBody, 'userGroupsToAdd' => $this->transformUserGroups($this->getPayload()->userGroupsToAdd), @@ -60,48 +57,4 @@ public function toArray(Request $request) 'newUser' => $this->transformUser($newUser), ]); } - - /** - * Transform the userGroupsToAdd to include related UserGroup data. - * - * @param array|null $userGroups - * @return array - */ - protected function transformUserGroups(?array $userGroups) - { - return collect($userGroups)->map(function ($userGroup) { - $userGroupModel = UserGroup::find($userGroup['userGroupId']); - - return [ - 'userGroupId' => $userGroup['userGroupId'], - 'userGroupName' => $userGroupModel ? $userGroupModel->getLocalizedData('name') : null, - 'masthead' => $userGroup['masthead'], - 'dateStart' => $userGroup['dateStart'], - 'dateEnd' => $userGroup['dateEnd'], - ]; - })->toArray(); - } - - /** - * Transform the userGroupsToAdd to include related UserGroup data. - * - * @param array|null $userGroups - * @return array - */ - protected function transformUser(?User $user): ?array - { - if (!isset($user)) { - return null; - } - - return [ - 'email' => $user->getEmail(), - 'fullName' => $user->getFullName(), - 'familyName' => $user->getFamilyName(null), - 'givenName' => $user->getGivenName(null), - 'country' => $user->getCountry(), - 'affiliation' => $user->getAffiliation(null), - 'orcid' => $user->getOrcid() - ]; - } } \ No newline at end of file