authorize('addTeamMember', $organization); // TODO: refactor after owner refactoring $this->validate($organization, $email, $role); $newOrganizationMember = User::query() ->where('email', $email) ->where('is_placeholder', '=', false) ->firstOrFail(); AddingTeamMember::dispatch($organization, $newOrganizationMember); DB::transaction(function () use ($organization, $newOrganizationMember, $role) { $organization->users()->attach( $newOrganizationMember, ['role' => $role] ); if ($role === Role::Owner->value) { app(UserService::class)->changeOwnership($organization, $newOrganizationMember); } }); TeamMemberAdded::dispatch($organization, $newOrganizationMember); } /** * Validate the add member operation. */ protected function validate(Organization $organization, string $email, ?string $role): void { Validator::make([ 'email' => $email, 'role' => $role, ], $this->rules())->after( $this->ensureUserIsNotAlreadyOnTeam($organization, $email) )->validateWithBag('addTeamMember'); } /** * Get the validation rules for adding a team member. * * @return array> */ protected function rules(): array { return array_filter([ 'email' => [ 'required', 'email', (new ExistsEloquent(User::class, 'email', function (Builder $builder) { return $builder->where('is_placeholder', '=', false); }))->withMessage(__('We were unable to find a registered user with this email address.')), ], 'role' => [ 'required', 'string', Rule::in([ Role::Owner->value, Role::Admin->value, Role::Manager->value, Role::Employee->value, ]), ], ]); } /** * Ensure that the user is not already on the team. */ protected function ensureUserIsNotAlreadyOnTeam(Organization $team, string $email): Closure { return function ($validator) use ($team, $email) { $validator->errors()->addIf( $team->hasRealUserWithEmail($email), 'email', __('This user already belongs to the team.') ); }; } }