1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 16:31:33 +02:00

Force at least 1 blank contact per client (#3319)

This commit is contained in:
David Bomba 2020-02-12 21:03:17 +11:00 committed by GitHub
parent 5649c039c1
commit 3a76d8bc34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 22 deletions

View File

@ -28,6 +28,6 @@ class SelfUpdateController extends BaseController
$res = $updater->update();
return response()->json($res);
return response()->json(['message'=>$res], 200);
}
}

View File

@ -11,6 +11,7 @@
namespace App\Repositories;
use App\Factory\ClientContactFactory;
use App\Models\Client;
use App\Models\ClientContact;
use Illuminate\Support\Str;
@ -20,16 +21,15 @@ use Illuminate\Support\Str;
*/
class ClientContactRepository extends BaseRepository
{
public function save($contacts, Client $client) : void
public function save(array $data, Client $client) : void
{
/* Convert array to collection */
$contacts = collect($contacts);
if(isset($data['contacts']))
$contacts = collect($data['contacts']);
else
$contacts = collect();
/* Get array of IDs which have been removed from the contacts array and soft delete each contact */
collect($client->contacts->pluck('id'))->diff($contacts->pluck('id'))->each(function ($contact) {
//ClientContact::find($contact)->delete();
ClientContact::destroy($contact);
});
@ -43,36 +43,34 @@ class ClientContactRepository extends BaseRepository
//loop and update/create contacts
$contacts->each(function ($contact) use ($client) {
$update_contact = null;
//$update_contact = null;
if (isset($contact['id'])) {
$update_contact = ClientContact::find($contact['id']);
}
if (!$update_contact) {
$update_contact = new ClientContact;
$update_contact = ClientContactFactory::create($client->company_id, $client->user_id);
$update_contact->client_id = $client->id;
$update_contact->company_id = $client->company_id;
$update_contact->user_id = $client->user_id;
$update_contact->contact_key = Str::random(40);
}
$update_contact->fill($contact);
$update_contact->save();
});
//always made sure we have one blank contact to maintain state
if ($contacts->count() == 0) {
$new_contact = new ClientContact;
$new_contact = ClientContactFactory::create($client->company_id, $client->user_id);
$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

@ -54,6 +54,8 @@ class ClientRepository extends BaseRepository
* @param \App\Models\Client $client The client
*
* @return Client|\App\Models\Client|null Client Object
*
* @todo Write tests to make sure that custom client numbers work as expected.
*/
public function save(array $data, Client $client) : ?Client
{
@ -64,15 +66,11 @@ class ClientRepository extends BaseRepository
if ($client->id_number == "" || !$client->id_number) {
$client->id_number = $this->getNextClientNumber($client);
} //todo write tests for this and make sure that custom client numbers also works as expected from here
}
$client->save();
// \Log::error($client);
if (isset($data['contacts'])) {
$contacts = $this->contact_repo->save($data['contacts'], $client);
}
$this->contact_repo->save($data, $client);
if (empty($data['name'])) {
$data['name'] = $client->present()->name();