Compare commits

..

No commits in common. "f07ecd9bdff42e9764c7e5d496a6d71eb3bcab27" and "62b4f49d4d394ab6dc0a4947158dc039e664456a" have entirely different histories.

7 changed files with 8 additions and 177 deletions

View file

@ -1,7 +1,3 @@
0.5.0
* Allow multiple Customers to use the same email, as long as they are linked to distinct Mailboxes.
0.4.0
* Display all Customers when using an admin account.

View file

@ -19,7 +19,7 @@ You have been warned.
### Install the package with composer
```
composer require "millions-missing-france/freescout-restricted-customers" "0.5.0"
composer require "millions-missing-france/freescout-restricted-customers" "0.4.0"
```
### Edit the application routes
@ -110,6 +110,8 @@ should be replaced with:
Route::group(['middleware' => ['web', 'auth', 'roles'], 'roles' => ['user', 'admin'], 'prefix' => \Helper::getSubdirectory(), 'namespace' => 'MillionsMissingFrance\FreescoutRestrictedCustomers\Http\Controllers'], function()
{
Route::get('/customers/new', '\MillionsMissingFrance\FreescoutRestrictedCustomers\Http\Controllers\CrmController@createCustomer')->name('freescout-restricted-customers.create_customer');
// The Crm module initialization will crash if no route named "crm.create_customer" is set.
Route::get('/customers/new', '\MillionsMissingFrance\FreescoutRestrictedCustomers\Http\Controllers\CrmController@createCustomer')->name('crm.create_customer');
Route::post('/customers/new', '\MillionsMissingFrance\FreescoutRestrictedCustomers\Http\Controllers\CrmController@createCustomerSave');
Route::get('/crm/ajax-html/{action}/{param?}', ['uses' => '\MillionsMissingFrance\FreescoutRestrictedCustomers\Http\Controllers\CrmController@ajaxHtml'])->name('crm.ajax_html');
Route::get('/customers/fields/ajax-search', ['uses' => '\MillionsMissingFrance\FreescoutRestrictedCustomers\Http\Controllers\CrmController@ajaxSearch', 'laroute' => true])->name('crm.ajax_search');
@ -123,21 +125,7 @@ Route::group(['middleware' => ['web', 'auth', 'roles'], 'roles' => ['admin'], 'p
});
```
#### Modules/Crm/Providers/CrmServiceProvider.php
At line 173, this route call:
```php
$html = __('Customers').' <a href="#" data-trigger="modal" data-modal-title="'.__('Add Customer').'" data-modal-size="lg" data-modal-no-footer="true" data-modal-body=\'<iframe src="'.route('crm.create_customer', ['x_embed' => 1]).'" frameborder="0" class="modal-iframe"></iframe>\' class="btn btn-bordered btn-xs" style="position:relative;top:-1px;margin-left:4px;"><i class="glyphicon glyphicon-plus" title="'.__('Add Customer').'" data-toggle="tooltip"></i></a>';
```
should be replaced with:
```php
$html = __('Customers').' <a href="#" data-trigger="modal" data-modal-title="'.__('Add Customer').'" data-modal-size="lg" data-modal-no-footer="true" data-modal-body=\'<iframe src="'.route('freescout-restricted-customers.create_customer', ['x_embed' => 1]).'" frameborder="0" class="modal-iframe"></iframe>\' class="btn btn-bordered btn-xs" style="position:relative;top:-1px;margin-left:4px;"><i class="glyphicon glyphicon-plus" title="'.__('Add Customer').'" data-toggle="tooltip"></i></a>';
```
### Update the database schema
#### Update the database schema
```
php artisan migrate

View file

@ -1,7 +1,7 @@
{
"name": "millions-missing-france/freescout-restricted-customers",
"description": "Freescout restricted customers - Restrict access to Freescout customers to specific mailboxes",
"version": "0.5.0",
"version": "0.4.0",
"type": "library",
"license": ["AGPL-3.0-only"],
"authors": [

View file

@ -1,30 +0,0 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class DropUnicityFromCustomerEmails extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::table('emails', function (Blueprint $table) {
$table->dropUnique(['email']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::table('emails', function (Blueprint $table) {
// TODO: Remove all duplicated e-mails.
$table->unique('email');
});
}
}

View file

@ -6,6 +6,7 @@
namespace MillionsMissingFrance\FreescoutRestrictedCustomers;
use App\Email;
use App\CustomerChannel;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Hash;
@ -104,80 +105,4 @@ class Customer extends BaseCustomer {
$this->save();
}
}
/**
* Set customer emails.
*
* @param array $emails
*/
public function syncEmails($emails) {
if (is_array($emails)) {
$deleted_emails = [];
foreach ($this->emails as $email) {
foreach ($emails as $email_address) {
if (Email::sanitizeEmail($email->email) == Email::sanitizeEmail($email_address)) {
continue 2;
}
}
$deleted_emails[] = $email;
}
$mailbox = $this->mailbox;
foreach ($emails as $email_address) {
$email_address = Email::sanitizeEmail($email_address);
if (!$email_address) {
continue;
}
$email = Email
::whereInMailbox($mailbox)
->where('email', $email_address)
->first();
$new_emails = [];
if ($email) {
// Assign email to current customer
if ($email->customer_id != $this->id) {
$email->customer()->associate($this);
$email->save();
}
} else {
$new_emails[] = new Email(['email' => $email_address]);
}
if ($new_emails) {
$this->emails()->saveMany($new_emails);
}
}
foreach ($deleted_emails as $email) {
if (Conversation::where('customer_email', $email->email)->exists()) {
// Create customers for deleted emails
// if there is a conversation with 'customer_email'.
$customer = new self();
$customer->save();
$email->customer()->associate($customer);
$email->save();
} else {
// Simply delete an email.
$email->delete();
}
}
}
}
/**
* Add new email to customer.
*/
public function addEmail($email_address, $check_if_exists = false) {
// Check if email already exists and belongs to another customer.
if ($check_if_exists) {
$mailbox = $this->mailbox;
$email = Email
::whereInMailbox($mailbox)
->where('email', $email_address)
->first();
if ($email && !empty($email->customer_id)) {
return false;
}
}
$new_email = new Email(['email' => $email_address]);
$this->emails()->save($new_email);
}
}

View file

@ -1,46 +0,0 @@
<?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 );
}
/**
* Return a query limited to a specific Mailbox
*
* @param Mailbox $mailbox
*
* @return QueryBuilder
*/
public static function whereInMailbox($mailbox) {
$query = self
::whereHas('customer', function($query) use($mailbox) {
$query->where('mailbox_id', '=', $mailbox->id);
});
return $query;
}
}

View file

@ -7,6 +7,7 @@
namespace MillionsMissingFrance\FreescoutRestrictedCustomers\Http\Controllers;
use App\Conversation;
use App\Email;
use Modules\Crm\Entities\CustomerField;
use Modules\Crm\Entities\CustomerCustomerField;
use Validator;
@ -15,8 +16,6 @@ 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) {
@ -54,11 +53,10 @@ 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::alreadyExistsInMailbox($mailbox, $sanitized_email);
$email_exists = Email::where('email', $sanitized_email)->first();
if ($email_exists) {
$validator->getMessageBag()->add('emails.'.$i, __('A customer with this email already exists.'));