Ensure at least one contact has send_email set

This commit is contained in:
David Bomba 2021-11-15 21:46:58 +11:00
parent 02cd79651e
commit 750f05ad8b
5 changed files with 54 additions and 5 deletions

View File

@ -195,6 +195,8 @@ class BaseModel extends Model
// Remove any runs of periods (thanks falstro!)
$formatted_number = mb_ereg_replace("([\.]{2,})", '', $formatted_number);
$formatted_number = str_replace(" ", "", $formatted_number);
return $formatted_number;
}

View File

@ -22,7 +22,9 @@ use Illuminate\Support\Str;
*/
class ClientContactRepository extends BaseRepository
{
public $is_primary;
private bool $is_primary = true;
private bool $set_send_email_on_contact = false;
public function save(array $data, Client $client) : void
{
@ -36,13 +38,20 @@ class ClientContactRepository extends BaseRepository
ClientContact::destroy($contact);
});
$this->is_primary = true;
/* Ensure send_email always exists in at least one contact */
if(!$contacts->contains('send_email', true))
$this->set_send_email_on_contact = true;
/* Set first record to primary - always */
$contacts = $contacts->sortByDesc('is_primary')->map(function ($contact) {
$contact['is_primary'] = $this->is_primary;
$this->is_primary = false;
if($this->set_send_email_on_contact){
$contact['send_email'] = true;
$this->set_send_email_on_contact = false;
}
return $contact;
});

View File

@ -129,7 +129,7 @@ class PaymentRepository extends BaseRepository {
//todo optimize this into a single query
foreach ($data['invoices'] as $paid_invoice) {
$invoice = Invoice::whereId($paid_invoice['invoice_id'])->first();
$invoice = Invoice::withTrashed()->whereId($paid_invoice['invoice_id'])->first();
if ($invoice) {
$invoice = $invoice->service()

View File

@ -25,8 +25,8 @@
@foreach($multiple_contacts as $contact)
<a data-turbolinks="false"
href="{{ route('client.switch_company', $contact->hashed_id) }}"
class="block px-4 py-2 text-sm leading-5 text-gray-700 hover:bg-gray-100 hover:text-gray-900 focus:outline-none focus:bg-gray-100 focus:text-gray-900">{{ $contact->company->present()->name() }}
- {{ $contact->client->present()->name()}}</a>
class="block px-4 py-2 text-sm leading-5 text-gray-700 hover:bg-gray-100 hover:text-gray-900 focus:outline-none focus:bg-gray-100 focus:text-gray-900">{{ $contact->client->present()->name()}} - {{ $contact->company->present()->name() }}
</a>
@endforeach
</div>
</div>

View File

@ -10,6 +10,7 @@
*/
namespace Tests\Unit;
use App\Factory\ClientContactFactory;
use App\Factory\InvoiceItemFactory;
use App\Utils\Traits\UserSessionAttributes;
use Illuminate\Support\Facades\Session;
@ -77,4 +78,41 @@ class CollectionMergingTest extends TestCase
$this->assertTrue(collect($items)->contains('type_id', 3));
}
public function testClientContactSendEmailExists()
{
$new_collection = collect();
$cc = ClientContactFactory::create(1,1);
$cc->send_email = true;
$new_collection->push($cc);
$cc_false = ClientContactFactory::create(1,1);
$cc_false->send_email = false;
$new_collection->push($cc_false);
$this->assertTrue($new_collection->contains('send_email', true));
}
public function testClientContactSendEmailDoesNotExists()
{
$new_collection = collect();
$cc = ClientContactFactory::create(1,1);
$cc->send_email = false;
$new_collection->push($cc);
$cc_false = ClientContactFactory::create(1,1);
$cc_false->send_email = false;
$new_collection->push($cc_false);
$this->assertFalse($new_collection->contains('send_email', true));
}
}