Add the ability to create new Customers with an already used e-mail

A same e-mail can only be used for several Customers as long as they are not linked to the same Mailbox.
This commit is contained in:
Antoine Le Gonidec 2024-07-09 14:00:52 +02:00
parent 62b4f49d4d
commit 9af30eef63
Signed by: vv221
GPG key ID: 636B78F91CEB80D8
2 changed files with 35 additions and 2 deletions

31
src/Email.php Normal file
View file

@ -0,0 +1,31 @@
<?php
/*
SPDX-License-Identifier: AGPL-3.0-only
SPDX-FileCopyrightText: © 2024 Millions Missing FRANCE <info@millionsmissing.fr>
*/
namespace MillionsMissingFrance\FreescoutRestrictedCustomers;
use Illuminate\Database\Eloquent\Model;
use Watson\Rememberable\Rememberable;
use App\Email as BaseEmail;
class Email extends BaseEmail {
/**
* Check if an Email already exists for a given Mailbox.
*
* @param Mailbox $mailbox
* @param string $email
*
* @return bool
*/
public static function alreadyExistsInMailbox($mailbox, $email) {
$emails_count = self
::whereHas('customer', function($query) use($mailbox) {
$query->where('mailbox_id', '=', $mailbox->id);
})
->where('email', $email)
->count();
return ( $emails_count != 0 );
}
}

View file

@ -7,7 +7,6 @@
namespace MillionsMissingFrance\FreescoutRestrictedCustomers\Http\Controllers; namespace MillionsMissingFrance\FreescoutRestrictedCustomers\Http\Controllers;
use App\Conversation; use App\Conversation;
use App\Email;
use Modules\Crm\Entities\CustomerField; use Modules\Crm\Entities\CustomerField;
use Modules\Crm\Entities\CustomerCustomerField; use Modules\Crm\Entities\CustomerCustomerField;
use Validator; use Validator;
@ -16,6 +15,8 @@ use Illuminate\Http\Response;
use Illuminate\Routing\Controller; use Illuminate\Routing\Controller;
use Modules\Crm\Http\Controllers\CrmController as BaseCrmController; use Modules\Crm\Http\Controllers\CrmController as BaseCrmController;
use MillionsMissingFrance\FreescoutRestrictedCustomers\Customer; use MillionsMissingFrance\FreescoutRestrictedCustomers\Customer;
use MillionsMissingFrance\FreescoutRestrictedCustomers\Email;
use MillionsMissingFrance\FreescoutRestrictedCustomers\Mailbox;
class CrmController extends BaseCrmController { class CrmController extends BaseCrmController {
public function createCustomer(Request $request) { public function createCustomer(Request $request) {
@ -53,10 +54,11 @@ class CrmController extends BaseCrmController {
// Check email uniqueness. // Check email uniqueness.
$fail = false; $fail = false;
$mailbox = Mailbox::find($request->mailbox);
foreach ($request->emails as $i => $email) { foreach ($request->emails as $i => $email) {
$sanitized_email = Email::sanitizeEmail($email); $sanitized_email = Email::sanitizeEmail($email);
if ($sanitized_email) { if ($sanitized_email) {
$email_exists = Email::where('email', $sanitized_email)->first(); $email_exists = Email::alreadyExistsInMailbox($mailbox, $sanitized_email);
if ($email_exists) { if ($email_exists) {
$validator->getMessageBag()->add('emails.'.$i, __('A customer with this email already exists.')); $validator->getMessageBag()->add('emails.'.$i, __('A customer with this email already exists.'));