authorize('addCompanyEmployee', $company); $this->validate($company, $email, $role); InvitingCompanyEmployee::dispatch($company, $email, $role); $invitation = $company->companyInvitations()->create([ 'email' => $email, 'role' => $role, ]); Mail::to($email)->send(new CompanyInvitation($invitation)); } /** * Validate the invite employee operation. */ protected function validate(Company $company, string $email, ?string $role): void { Validator::make([ 'email' => $email, 'role' => $role, ], $this->rules($company), [ 'email.unique' => __('filament-companies::default.errors.employee_already_invited'), ])->after( $this->ensureUserIsNotAlreadyOnCompany($company, $email) )->validateWithBag('addCompanyEmployee'); } /** * Get the validation rules for inviting a company employee. * * @return array */ protected function rules(Company $company): array { return array_filter([ 'email' => [ 'required', 'email', Rule::unique('company_invitations')->where(static function (Builder $query) use ($company) { $query->where('company_id', $company->id); }), ], 'role' => FilamentCompanies::hasRoles() ? ['required', 'string', new Role] : null, ]); } /** * Ensure that the employee is not already on the company. */ protected function ensureUserIsNotAlreadyOnCompany(Company $company, string $email): Closure { return static function ($validator) use ($company, $email) { $validator->errors()->addIf( $company->hasUserWithEmail($email), 'email', __('filament-companies::default.errors.employee_already_belongs_to_company') ); }; } }