CORS implementation (#3065)

* fix regression in company name

* HasOneThrough for company user

* Validation rules for contact email addresses

* Force a blank contact if no contacts passed in client
This commit is contained in:
David Bomba 2019-11-13 22:32:53 +11:00 committed by GitHub
parent 0354b58f82
commit e1fa1186d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 45 additions and 15 deletions

View File

@ -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"),

View File

@ -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';
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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
*

View File

@ -66,6 +66,19 @@ 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();
}
}

View File

@ -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);
}
}