On initial install, link existing Customers to Mailboxes
The link is set only for Customers that are linked to exactly one Mailbox, through the Conversations they are linked to.
This commit is contained in:
parent
509058ec46
commit
aa9c1624a1
2 changed files with 27 additions and 0 deletions
|
@ -30,6 +30,11 @@ class AddMailboxIdColumnToCustomersTable extends Migration {
|
|||
// On mailbox deletion, delete all customer entries that are linked to it.
|
||||
->onDelete('cascade');
|
||||
});
|
||||
// Link Customers to Mailboxes, through their Conversation information.
|
||||
$customers = Customer::all();
|
||||
$customers->each(function ($customer) {
|
||||
$customer->linkToMailboxThroughConversations();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -82,4 +82,26 @@ class Customer extends BaseCustomer {
|
|||
->whereIn('mailbox_id', $mailboxes)
|
||||
->first($columns);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Mailboxes this Customer is linked to through Conversations.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function mailboxesThroughConversations() {
|
||||
return $this
|
||||
->conversations
|
||||
->pluck('mailbox');
|
||||
}
|
||||
|
||||
/**
|
||||
* If this Customer is linked to a single Mailbox through Conversations, link it to it.
|
||||
*/
|
||||
public function linkToMailboxThroughConversations() {
|
||||
$mailboxes = $this->mailboxesThroughConversations();
|
||||
if ( $mailboxes->count() == 1 ) {
|
||||
$this->mailbox_id = $mailboxes->first()->id;
|
||||
$this->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue