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:
parent
62b4f49d4d
commit
9af30eef63
2 changed files with 35 additions and 2 deletions
31
src/Email.php
Normal file
31
src/Email.php
Normal 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 );
|
||||
}
|
||||
}
|
|
@ -7,7 +7,6 @@
|
|||
namespace MillionsMissingFrance\FreescoutRestrictedCustomers\Http\Controllers;
|
||||
|
||||
use App\Conversation;
|
||||
use App\Email;
|
||||
use Modules\Crm\Entities\CustomerField;
|
||||
use Modules\Crm\Entities\CustomerCustomerField;
|
||||
use Validator;
|
||||
|
@ -16,6 +15,8 @@ use Illuminate\Http\Response;
|
|||
use Illuminate\Routing\Controller;
|
||||
use Modules\Crm\Http\Controllers\CrmController as BaseCrmController;
|
||||
use MillionsMissingFrance\FreescoutRestrictedCustomers\Customer;
|
||||
use MillionsMissingFrance\FreescoutRestrictedCustomers\Email;
|
||||
use MillionsMissingFrance\FreescoutRestrictedCustomers\Mailbox;
|
||||
|
||||
class CrmController extends BaseCrmController {
|
||||
public function createCustomer(Request $request) {
|
||||
|
@ -53,10 +54,11 @@ class CrmController extends BaseCrmController {
|
|||
|
||||
// Check email uniqueness.
|
||||
$fail = false;
|
||||
$mailbox = Mailbox::find($request->mailbox);
|
||||
foreach ($request->emails as $i => $email) {
|
||||
$sanitized_email = Email::sanitizeEmail($email);
|
||||
if ($sanitized_email) {
|
||||
$email_exists = Email::where('email', $sanitized_email)->first();
|
||||
$email_exists = Email::alreadyExistsInMailbox($mailbox, $sanitized_email);
|
||||
|
||||
if ($email_exists) {
|
||||
$validator->getMessageBag()->add('emails.'.$i, __('A customer with this email already exists.'));
|
||||
|
|
Loading…
Reference in a new issue