2024-07-03 12:02:11 +00:00
|
|
|
<?php
|
|
|
|
/*
|
2024-07-06 16:56:09 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
2024-07-03 12:02:11 +00:00
|
|
|
SPDX-FileCopyrightText: © 2024 Millions Missing FRANCE <info@millionsmissing.fr>
|
|
|
|
*/
|
|
|
|
|
2024-07-10 15:26:43 +00:00
|
|
|
namespace Modules\MMFRestrictedCustomers\Entities;
|
2024-07-03 12:02:11 +00:00
|
|
|
|
|
|
|
use App\Customer as BaseCustomer;
|
|
|
|
|
|
|
|
class Customer extends BaseCustomer {
|
|
|
|
/**
|
|
|
|
* Attributes fillable using fill() method.
|
|
|
|
*
|
|
|
|
* @var [type]
|
|
|
|
*/
|
|
|
|
protected $fillable = [
|
|
|
|
// Default list, imported from BaseCustomer.
|
|
|
|
'first_name',
|
|
|
|
'last_name',
|
|
|
|
'company',
|
|
|
|
'job_title',
|
|
|
|
'address',
|
|
|
|
'city',
|
|
|
|
'state',
|
|
|
|
'zip',
|
|
|
|
'country',
|
|
|
|
'photo_url',
|
|
|
|
'age',
|
|
|
|
'gender',
|
|
|
|
'notes',
|
|
|
|
'channel',
|
|
|
|
'channel_id',
|
|
|
|
'social_profiles',
|
|
|
|
// Addition specific to this package.
|
|
|
|
'mailbox_id',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Mailbox that is allowed to access this Customer information.
|
|
|
|
*/
|
|
|
|
public function mailbox() {
|
|
|
|
return $this->belongsTo(Mailbox::class);
|
|
|
|
}
|
2024-07-05 12:18:54 +00:00
|
|
|
|
2024-07-05 15:46:34 +00:00
|
|
|
/**
|
|
|
|
* Only return a Customer instance if it is available to the current User.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @param array $columns
|
|
|
|
* @return mixed|static
|
|
|
|
*/
|
|
|
|
public static function find($id, $columns = ['*']) {
|
|
|
|
// Get the list of Mailboxes the current User has access to.
|
|
|
|
$user = auth()->user();
|
|
|
|
$mailboxes = $user->mailboxesIdsCanView();
|
|
|
|
|
|
|
|
$customer = self
|
|
|
|
::where('id', '=', $id)
|
|
|
|
->whereIn('mailbox_id', $mailboxes)
|
|
|
|
->first($columns);
|
|
|
|
}
|
2024-07-08 10:44:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Mailboxes this Customer is linked to through Conversations.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function mailboxesThroughConversations() {
|
|
|
|
return $this
|
|
|
|
->conversations
|
2024-07-08 11:43:26 +00:00
|
|
|
->pluck('mailbox')
|
|
|
|
->unique();
|
2024-07-08 10:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
|
|
|
}
|
2024-07-09 12:38:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
2024-07-03 12:02:11 +00:00
|
|
|
}
|