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:
Antoine Le Gonidec 2024-07-08 12:44:40 +02:00
parent 509058ec46
commit aa9c1624a1
Signed by: vv221
GPG key ID: 636B78F91CEB80D8
2 changed files with 27 additions and 0 deletions

View file

@ -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();
});
}
/**

View file

@ -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();
}
}
}