diff --git a/app/Http/Controllers/SelfUpdateController.php b/app/Http/Controllers/SelfUpdateController.php index 08d632043782..969f6434f34e 100644 --- a/app/Http/Controllers/SelfUpdateController.php +++ b/app/Http/Controllers/SelfUpdateController.php @@ -28,6 +28,6 @@ class SelfUpdateController extends BaseController $res = $updater->update(); - return response()->json($res); + return response()->json(['message'=>$res], 200); } } diff --git a/app/Repositories/ClientContactRepository.php b/app/Repositories/ClientContactRepository.php index 997f66dad7bb..893ad675c90f 100644 --- a/app/Repositories/ClientContactRepository.php +++ b/app/Repositories/ClientContactRepository.php @@ -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(); } + } + + } diff --git a/app/Repositories/ClientRepository.php b/app/Repositories/ClientRepository.php index 4b2b22ea6232..8d058c9a3896 100644 --- a/app/Repositories/ClientRepository.php +++ b/app/Repositories/ClientRepository.php @@ -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();