diff --git a/app/Http/Controllers/OpenAPI/CompanyUserSchema.php b/app/Http/Controllers/OpenAPI/CompanyUserSchema.php index 84c385c27ad4..7b208941c5aa 100644 --- a/app/Http/Controllers/OpenAPI/CompanyUserSchema.php +++ b/app/Http/Controllers/OpenAPI/CompanyUserSchema.php @@ -6,6 +6,7 @@ * @OA\Property(property="permissions", type="string", example="[create_invoice]", description="The company user permissions"), * @OA\Property(property="settings", type="object", example="The local shop", description="The company name"), * @OA\Property(property="is_owner", type="boolean", example=true, description="Determines whether the user owns this company"), + * @OA\Property(property="is_admin", type="boolean", example=true, description="Determines whether the user is the admin of this company"), * @OA\Property(property="is_locked", type="boolean", example=true, description="Determines whether the users access to this company has been locked"), * @OA\Property(property="updated_at", type="integer", example="1231232312321", description="The last time the record was modified"), * @OA\Property(property="deleted_at", type="integer", example="12312312321", description="Timestamp when the user was archived"), diff --git a/app/Http/Requests/Client/StoreClientRequest.php b/app/Http/Requests/Client/StoreClientRequest.php index 0837825cdfad..133f0a017f60 100644 --- a/app/Http/Requests/Client/StoreClientRequest.php +++ b/app/Http/Requests/Client/StoreClientRequest.php @@ -44,6 +44,7 @@ class StoreClientRequest extends Request //$rules['name'] = 'required|min:1'; $rules['id_number'] = 'unique:clients,id_number,' . $this->id . ',id,company_id,' . $this->company_id; $rules['settings'] = new ValidSettingsRule(); + $rules['contacts.*.email'] = 'distinct'; $contacts = request('contacts'); @@ -51,11 +52,8 @@ class StoreClientRequest extends Request { for ($i = 0; $i < count($contacts); $i++) { - // $rules['contacts.' . $i . '.email'] = Rule::unique('client_contacts','email')->where(function ($query) { - // return $query->where('company_id', $this->company_id); - // }); - //$rules['contacts.' . $i . '.email'] = 'nullable|email|unique:client_contacts,email,NULL,' . isset($contacts[$i]['id']).',company_id,'.$this->company_id; - $rules['contacts.' . $i . '.email'] = 'nullable|email|unique:client_contacts,email,client_id,'.$this->id; + + //$rules['contacts.' . $i . '.email'] = 'nullable|email|distinct'; } } diff --git a/app/Http/Requests/Client/UpdateClientRequest.php b/app/Http/Requests/Client/UpdateClientRequest.php index 8190c9ffc469..882e907dc1f1 100644 --- a/app/Http/Requests/Client/UpdateClientRequest.php +++ b/app/Http/Requests/Client/UpdateClientRequest.php @@ -45,18 +45,16 @@ class UpdateClientRequest extends Request //$rules['id_number'] = 'unique:clients,id_number,,id,company_id,' . auth()->user()->company()->id; $rules['id_number'] = 'unique:clients,id_number,' . $this->id . ',id,company_id,' . $this->company_id; $rules['settings'] = new ValidSettingsRule(); - - -// $rules['settings'] = 'json'; + $rules['contacts.*.email'] = 'distinct'; $contacts = request('contacts'); if(is_array($contacts)) { - for ($i = 0; $i < count($contacts); $i++) { - $rules['contacts.' . $i . '.email'] = 'nullable|email|unique:client_contacts,email,' . isset($contacts[$i]['id'].',company_id,'.$this->company_id); - //$rules['contacts.' . $i . '.email'] = 'nullable|email'; - } + // for ($i = 0; $i < count($contacts); $i++) { + // // $rules['contacts.' . $i . '.email'] = 'nullable|email|unique:client_contacts,email,' . isset($contacts[$i]['id'].',company_id,'.$this->company_id); + // //$rules['contacts.' . $i . '.email'] = 'nullable|email'; + // } } return $rules; diff --git a/app/Jobs/Company/CreateCompany.php b/app/Jobs/Company/CreateCompany.php index d6e17430a57d..b07d5f79292d 100644 --- a/app/Jobs/Company/CreateCompany.php +++ b/app/Jobs/Company/CreateCompany.php @@ -51,9 +51,7 @@ class CreateCompany { $settings = CompanySettings::defaults(); - $default_company_name = isset($this->request['first_name']) ? $this->request['first_name'] : '' . ' ' . isset($this->request['last_name']) ? $this->request['last_name'] : ''; - - $settings->name = isset($this->request['name']) ? $this->request['name'] : $default_company_name; + $settings->name = isset($this->request['name']) ? $this->request['name'] : ''; $company = new Company(); $company->account_id = $this->account->id; diff --git a/app/Models/User.php b/app/Models/User.php index 73ab78d7f0fe..f744055d8081 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -202,6 +202,19 @@ class User extends Authenticatable implements MustVerifyEmail } + public function company_user() + { + return $this->hasOneThrough(CompanyUser::class, CompanyToken::class, + 'user_id', // Foreign key on CompanyToken table... + 'company_id', // Foreign key on CompanyUser table... + 'id', // Local key on suppliers table... + 'company_id' // Local key on CompanyToken table... + ); + + // return $this->user_companies->where('company_id', $this->companyId()); + } + + /** * Returns the currently set company id for the user * diff --git a/app/Repositories/ClientContactRepository.php b/app/Repositories/ClientContactRepository.php index aaa64c270a68..e149969fb62e 100644 --- a/app/Repositories/ClientContactRepository.php +++ b/app/Repositories/ClientContactRepository.php @@ -66,7 +66,20 @@ class ClientContactRepository extends BaseRepository $update_contact->save(); }); + //always made sure we have one blank contact to maintain state + if($contacts->count() == 0) + { + $new_contact = new ClientContact; + $new_contact->client_id = $client->id; + $new_contact->company_id = $client->company_id; + $new_contact->user_id = $client->user_id; + $new_contact->contact_key = Str::random(40); + $new_contact->is_primary = true; + $new_contact->save(); + + } + } } \ No newline at end of file diff --git a/app/Transformers/UserTransformer.php b/app/Transformers/UserTransformer.php index 7030529a1947..e17260ab3af4 100644 --- a/app/Transformers/UserTransformer.php +++ b/app/Transformers/UserTransformer.php @@ -40,6 +40,7 @@ class UserTransformer extends EntityTransformer protected $availableIncludes = [ 'companies', 'company_users', + 'company_user' ]; @@ -95,4 +96,12 @@ class UserTransformer extends EntityTransformer return $this->includeCollection($user->user_companies, $transformer, CompanyUser::class); } + + public function includeCompanyUser(User $user) + { + $transformer = new CompanyUserTransformer($this->serializer); + + return $this->includeItem($user->company_user, $transformer, CompanyUser::class); + + } }