refactor: replace custom CRM with Monica fork
Some checks failed
Build & Push Monica Image to Gitea Registry / build-and-push (push) Failing after 9s
Some checks failed
Build & Push Monica Image to Gitea Registry / build-and-push (push) Failing after 9s
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Auth\Population;
|
||||
|
||||
use App\Services\BaseService;
|
||||
use App\Models\Account\Account;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Database\QueryException;
|
||||
use App\Models\Contact\ContactFieldType;
|
||||
|
||||
/**
|
||||
* Populate the contact field types table for a given account.
|
||||
* This is typically done when a new account is created.
|
||||
*/
|
||||
class PopulateContactFieldTypesTable extends BaseService
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the service.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'account_id' => 'required|integer|exists:accounts,id',
|
||||
'migrate_existing_data' => 'required|boolean',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the service.
|
||||
*
|
||||
* @param array $data
|
||||
* @return bool
|
||||
*/
|
||||
public function execute(array $data): bool
|
||||
{
|
||||
$this->createEntries($data);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create contact field type entries.
|
||||
*
|
||||
* @param array $data
|
||||
* @return void
|
||||
*/
|
||||
private function createEntries($data)
|
||||
{
|
||||
$defaultContactFieldTypes = $this->getDefaultContactFieldTypes($data);
|
||||
|
||||
foreach ($defaultContactFieldTypes as $defaultContactFieldType) {
|
||||
$this->createEntry($defaultContactFieldType, $data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default contact field types.
|
||||
*
|
||||
* @param array $data
|
||||
* @return Collection
|
||||
*
|
||||
* @throws QueryException if the query does not run for some reasons.
|
||||
*/
|
||||
private function getDefaultContactFieldTypes($data)
|
||||
{
|
||||
if ($data['migrate_existing_data'] == 1) {
|
||||
$defaultContactFieldTypes = DB::table('default_contact_field_types')
|
||||
->get();
|
||||
} else {
|
||||
$defaultContactFieldTypes = DB::table('default_contact_field_types')
|
||||
->where('migrated', 0)
|
||||
->get();
|
||||
}
|
||||
|
||||
return $defaultContactFieldTypes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an entry in the life event category table.
|
||||
*
|
||||
* @param object $defaultContactFieldType
|
||||
* @param array $data
|
||||
* @return void
|
||||
*/
|
||||
private function createEntry($defaultContactFieldType, $data)
|
||||
{
|
||||
ContactFieldType::create([
|
||||
'account_id' => $data['account_id'],
|
||||
'name' => $defaultContactFieldType->name,
|
||||
'fontawesome_icon' => (is_null($defaultContactFieldType->fontawesome_icon) ? null : $defaultContactFieldType->fontawesome_icon),
|
||||
'protocol' => (is_null($defaultContactFieldType->protocol) ? null : $defaultContactFieldType->protocol),
|
||||
'delible' => $defaultContactFieldType->delible,
|
||||
'type' => (is_null($defaultContactFieldType->type) ? null : $defaultContactFieldType->type),
|
||||
]);
|
||||
}
|
||||
}
|
||||
170
app/Services/Auth/Population/PopulateLifeEventsTable.php
Normal file
170
app/Services/Auth/Population/PopulateLifeEventsTable.php
Normal file
@@ -0,0 +1,170 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Auth\Population;
|
||||
|
||||
use App\Services\BaseService;
|
||||
use App\Models\Account\Account;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use App\Models\Contact\LifeEventType;
|
||||
use Illuminate\Database\QueryException;
|
||||
use App\Models\Contact\LifeEventCategory;
|
||||
|
||||
/**
|
||||
* Populate life event types and life event categories for a given account.
|
||||
* This is typically done when a new account is created.
|
||||
*/
|
||||
class PopulateLifeEventsTable extends BaseService
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the service.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'account_id' => 'required|integer|exists:accounts,id',
|
||||
'migrate_existing_data' => 'required|boolean',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* The data needed for the query to be executed.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $data;
|
||||
|
||||
/**
|
||||
* Execute the service.
|
||||
*
|
||||
* @param array $givenData
|
||||
* @return bool
|
||||
*/
|
||||
public function execute(array $givenData): bool
|
||||
{
|
||||
$this->data = $givenData;
|
||||
|
||||
if (! $this->validate($this->data)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$locale = $this->getLocaleOfAccount($this->data['account_id']);
|
||||
if (is_null($locale)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->createEntries($locale);
|
||||
|
||||
$this->markTableAsMigrated();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the locale associated with the account.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
private function getLocaleOfAccount($accountId)
|
||||
{
|
||||
// get the account
|
||||
$account = Account::findOrFail($accountId);
|
||||
|
||||
return $account->getFirstLocale();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create life event category and life event type entries.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function createEntries($locale)
|
||||
{
|
||||
App::setLocale($locale);
|
||||
|
||||
$defaultLifeEventCategories = $this->getDefaultLifeEventCategories();
|
||||
|
||||
foreach ($defaultLifeEventCategories as $defaultLifeEventCategory) {
|
||||
$lifeEventCategory = $this->feedLifeEventCategory($defaultLifeEventCategory);
|
||||
|
||||
$defaultLifeEventTypes = DB::table('default_life_event_types')
|
||||
->where('default_life_event_category_id', $defaultLifeEventCategory->id)
|
||||
->get();
|
||||
|
||||
foreach ($defaultLifeEventTypes as $defaultLifeEventType) {
|
||||
$this->feedLifeEventType($defaultLifeEventType, $lifeEventCategory);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default life event categories.
|
||||
*
|
||||
* @return Collection
|
||||
*
|
||||
* @throws QueryException if the query does not run for some reasons.
|
||||
*/
|
||||
private function getDefaultLifeEventCategories()
|
||||
{
|
||||
if ($this->data['migrate_existing_data'] == 1) {
|
||||
$defaultLifeEventCategories = DB::table('default_life_event_categories')
|
||||
->get();
|
||||
} else {
|
||||
$defaultLifeEventCategories = DB::table('default_life_event_categories')
|
||||
->where('migrated', 0)
|
||||
->get();
|
||||
}
|
||||
|
||||
return $defaultLifeEventCategories;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an entry in the life event category table.
|
||||
*
|
||||
* @param object $defaultLifeEventCategory
|
||||
* @return LifeEventCategory
|
||||
*/
|
||||
private function feedLifeEventCategory($defaultLifeEventCategory): LifeEventCategory
|
||||
{
|
||||
return LifeEventCategory::create([
|
||||
'account_id' => $this->data['account_id'],
|
||||
'name' => trans('settings.personalization_life_event_category_'.$defaultLifeEventCategory->translation_key),
|
||||
'core_monica_data' => true,
|
||||
'default_life_event_category_key' => $defaultLifeEventCategory->translation_key,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an entry in the life event type table.
|
||||
*
|
||||
* @param object $defaultLifeEventType
|
||||
* @return void
|
||||
*/
|
||||
private function feedLifeEventType($defaultLifeEventType, $lifeEventCategory)
|
||||
{
|
||||
LifeEventType::create([
|
||||
'account_id' => $this->data['account_id'],
|
||||
'life_event_category_id' => $lifeEventCategory->id,
|
||||
'core_monica_data' => true,
|
||||
'specific_information_structure' => $defaultLifeEventType->specific_information_structure,
|
||||
'default_life_event_type_key' => $defaultLifeEventType->translation_key,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark the table as migrated.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function markTableAsMigrated()
|
||||
{
|
||||
DB::table('default_life_event_categories')
|
||||
->update(['migrated' => 1]);
|
||||
|
||||
DB::table('default_life_event_types')
|
||||
->update(['migrated' => 1]);
|
||||
}
|
||||
}
|
||||
107
app/Services/Auth/Population/PopulateModulesTable.php
Normal file
107
app/Services/Auth/Population/PopulateModulesTable.php
Normal file
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Auth\Population;
|
||||
|
||||
use App\Models\User\Module;
|
||||
use App\Services\BaseService;
|
||||
use App\Models\Account\Account;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Database\QueryException;
|
||||
|
||||
/**
|
||||
* Populate the modules table for a given account.
|
||||
*/
|
||||
class PopulateModulesTable extends BaseService
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the service.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'account_id' => 'required|integer|exists:accounts,id',
|
||||
'migrate_existing_data' => 'required|boolean',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* The data needed for the query to be executed.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $data;
|
||||
|
||||
/**
|
||||
* Execute the service.
|
||||
*
|
||||
* @param array $givenData
|
||||
* @return bool
|
||||
*/
|
||||
public function execute(array $givenData): bool
|
||||
{
|
||||
$this->data = $givenData;
|
||||
|
||||
if (! $this->validate($this->data)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->createEntries();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create modules entries.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function createEntries()
|
||||
{
|
||||
$defaultModules = $this->getDefaultModules();
|
||||
|
||||
foreach ($defaultModules as $defaultModule) {
|
||||
$this->feedModule($defaultModule);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default modules.
|
||||
*
|
||||
* @return Collection
|
||||
*
|
||||
* @throws QueryException if the query does not run for some reasons.
|
||||
*/
|
||||
private function getDefaultModules()
|
||||
{
|
||||
if ($this->data['migrate_existing_data'] == 1) {
|
||||
$defaultModules = DB::table('default_contact_modules')
|
||||
->get();
|
||||
} else {
|
||||
$defaultModules = DB::table('default_contact_modules')
|
||||
->where('migrated', 0)
|
||||
->get();
|
||||
}
|
||||
|
||||
return $defaultModules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an entry in the module table.
|
||||
*
|
||||
* @param object $defaultModule
|
||||
* @return void
|
||||
*/
|
||||
private function feedModule($defaultModule)
|
||||
{
|
||||
Module::create([
|
||||
'account_id' => $this->data['account_id'],
|
||||
'key' => $defaultModule->key,
|
||||
'translation_key' => $defaultModule->translation_key,
|
||||
'delible' => $defaultModule->delible,
|
||||
'active' => $defaultModule->active,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user