add: full multi-tenancy control
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
namespace Webkul\Contact\Repositories;
|
||||
|
||||
use Illuminate\Container\Container;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Webkul\Attribute\Repositories\AttributeRepository;
|
||||
use Webkul\Attribute\Repositories\AttributeValueRepository;
|
||||
use Webkul\Contact\Contracts\Organization;
|
||||
use Webkul\Core\Eloquent\Repository;
|
||||
|
||||
class OrganizationRepository extends Repository
|
||||
{
|
||||
/**
|
||||
* Create a new repository instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(
|
||||
protected AttributeRepository $attributeRepository,
|
||||
protected AttributeValueRepository $attributeValueRepository,
|
||||
Container $container
|
||||
) {
|
||||
parent::__construct($container);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify model class name.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function model()
|
||||
{
|
||||
return Organization::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create.
|
||||
*
|
||||
* @return \Webkul\Contact\Contracts\Organization
|
||||
*/
|
||||
public function create(array $data)
|
||||
{
|
||||
if (isset($data['user_id'])) {
|
||||
$data['user_id'] = $data['user_id'] ?: null;
|
||||
}
|
||||
|
||||
$organization = parent::create($data);
|
||||
|
||||
$this->attributeValueRepository->save(array_merge($data, [
|
||||
'entity_id' => $organization->id,
|
||||
]));
|
||||
|
||||
return $organization;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update.
|
||||
*
|
||||
* @param int $id
|
||||
* @param array $attribute
|
||||
* @return \Webkul\Contact\Contracts\Organization
|
||||
*/
|
||||
public function update(array $data, $id, $attributes = [])
|
||||
{
|
||||
if (isset($data['user_id'])) {
|
||||
$data['user_id'] = $data['user_id'] ?: null;
|
||||
}
|
||||
|
||||
$organization = parent::update($data, $id);
|
||||
|
||||
/**
|
||||
* If attributes are provided then only save the provided attributes and return.
|
||||
*/
|
||||
if (! empty($attributes)) {
|
||||
$conditions = ['entity_type' => $data['entity_type']];
|
||||
|
||||
if (isset($data['quick_add'])) {
|
||||
$conditions['quick_add'] = 1;
|
||||
}
|
||||
|
||||
$attributes = $this->attributeRepository->where($conditions)
|
||||
->whereIn('code', $attributes)
|
||||
->get();
|
||||
|
||||
$this->attributeValueRepository->save(array_merge($data, [
|
||||
'entity_id' => $organization->id,
|
||||
]), $attributes);
|
||||
|
||||
return $organization;
|
||||
}
|
||||
|
||||
$this->attributeValueRepository->save(array_merge($data, [
|
||||
'entity_id' => $organization->id,
|
||||
]));
|
||||
|
||||
return $organization;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete organization and it's persons.
|
||||
*
|
||||
* @param int $id
|
||||
* @return @void
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
$organization = $this->findOrFail($id);
|
||||
|
||||
DB::transaction(function () use ($organization, $id) {
|
||||
$this->attributeValueRepository->deleteWhere([
|
||||
'entity_id' => $id,
|
||||
'entity_type' => 'organizations',
|
||||
]);
|
||||
|
||||
$organization->delete();
|
||||
});
|
||||
}
|
||||
}
|
||||
184
packages/Webkul/Contact/src/Repositories/PersonRepository.php
Normal file
184
packages/Webkul/Contact/src/Repositories/PersonRepository.php
Normal file
@@ -0,0 +1,184 @@
|
||||
<?php
|
||||
|
||||
namespace Webkul\Contact\Repositories;
|
||||
|
||||
use Illuminate\Container\Container;
|
||||
use Webkul\Attribute\Repositories\AttributeRepository;
|
||||
use Webkul\Attribute\Repositories\AttributeValueRepository;
|
||||
use Webkul\Contact\Contracts\Person;
|
||||
use Webkul\Core\Eloquent\Repository;
|
||||
|
||||
class PersonRepository extends Repository
|
||||
{
|
||||
/**
|
||||
* Searchable fields.
|
||||
*/
|
||||
protected $fieldSearchable = [
|
||||
'name',
|
||||
'emails',
|
||||
'contact_numbers',
|
||||
'organization_id',
|
||||
'job_title',
|
||||
'organization.name',
|
||||
'user_id',
|
||||
'user.name',
|
||||
];
|
||||
|
||||
/**
|
||||
* Create a new repository instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(
|
||||
protected AttributeRepository $attributeRepository,
|
||||
protected AttributeValueRepository $attributeValueRepository,
|
||||
protected OrganizationRepository $organizationRepository,
|
||||
Container $container
|
||||
) {
|
||||
parent::__construct($container);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify model class name.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function model()
|
||||
{
|
||||
return Person::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create.
|
||||
*
|
||||
* @return \Webkul\Contact\Contracts\Person
|
||||
*/
|
||||
public function create(array $data)
|
||||
{
|
||||
$data = $this->sanitizeRequestedPersonData($data);
|
||||
|
||||
if (! empty($data['organization_name'])) {
|
||||
$organization = $this->fetchOrCreateOrganizationByName($data['organization_name']);
|
||||
|
||||
$data['organization_id'] = $organization->id;
|
||||
}
|
||||
|
||||
if (isset($data['user_id'])) {
|
||||
$data['user_id'] = $data['user_id'] ?: null;
|
||||
}
|
||||
|
||||
$person = parent::create($data);
|
||||
|
||||
$this->attributeValueRepository->save(array_merge($data, [
|
||||
'entity_id' => $person->id,
|
||||
]));
|
||||
|
||||
return $person;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update.
|
||||
*
|
||||
* @return \Webkul\Contact\Contracts\Person
|
||||
*/
|
||||
public function update(array $data, $id, $attributes = [])
|
||||
{
|
||||
$data = $this->sanitizeRequestedPersonData($data);
|
||||
|
||||
$data['user_id'] = empty($data['user_id']) ? null : $data['user_id'];
|
||||
|
||||
if (! empty($data['organization_name'])) {
|
||||
$organization = $this->fetchOrCreateOrganizationByName($data['organization_name']);
|
||||
|
||||
$data['organization_id'] = $organization->id;
|
||||
|
||||
unset($data['organization_name']);
|
||||
}
|
||||
|
||||
$person = parent::update($data, $id);
|
||||
|
||||
/**
|
||||
* If attributes are provided then only save the provided attributes and return.
|
||||
*/
|
||||
if (! empty($attributes)) {
|
||||
$conditions = ['entity_type' => $data['entity_type']];
|
||||
|
||||
if (isset($data['quick_add'])) {
|
||||
$conditions['quick_add'] = 1;
|
||||
}
|
||||
|
||||
$attributes = $this->attributeRepository->where($conditions)
|
||||
->whereIn('code', $attributes)
|
||||
->get();
|
||||
|
||||
$this->attributeValueRepository->save(array_merge($data, [
|
||||
'entity_id' => $person->id,
|
||||
]), $attributes);
|
||||
|
||||
return $person;
|
||||
}
|
||||
|
||||
$this->attributeValueRepository->save(array_merge($data, [
|
||||
'entity_id' => $person->id,
|
||||
]));
|
||||
|
||||
return $person;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves customers count based on date.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getCustomerCount($startDate, $endDate)
|
||||
{
|
||||
return $this
|
||||
->whereBetween('created_at', [$startDate, $endDate])
|
||||
->get()
|
||||
->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch or create an organization.
|
||||
*/
|
||||
public function fetchOrCreateOrganizationByName(string $organizationName)
|
||||
{
|
||||
$organization = $this->organizationRepository->findOneWhere([
|
||||
'name' => $organizationName,
|
||||
]);
|
||||
|
||||
return $organization ?: $this->organizationRepository->create([
|
||||
'entity_type' => 'organizations',
|
||||
'name' => $organizationName,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize requested person data and return the clean array.
|
||||
*/
|
||||
private function sanitizeRequestedPersonData(array $data): array
|
||||
{
|
||||
if (
|
||||
array_key_exists('organization_id', $data)
|
||||
&& empty($data['organization_id'])
|
||||
) {
|
||||
$data['organization_id'] = null;
|
||||
}
|
||||
|
||||
$uniqueIdParts = array_filter([
|
||||
$data['user_id'] ?? null,
|
||||
$data['organization_id'] ?? null,
|
||||
$data['emails'][0]['value'] ?? null,
|
||||
]);
|
||||
|
||||
$data['unique_id'] = implode('|', $uniqueIdParts);
|
||||
|
||||
if (isset($data['contact_numbers'])) {
|
||||
$data['contact_numbers'] = collect($data['contact_numbers'])->filter(fn ($number) => ! is_null($number['value']))->toArray();
|
||||
|
||||
$data['unique_id'] .= '|'.$data['contact_numbers'][0]['value'];
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user