refactor: replace custom CRM with Monica fork
Some checks failed
Build & Push Monica Image to Gitea Registry / build-and-push (push) Failing after 9s

This commit is contained in:
Kroonk
2026-05-22 16:19:55 +02:00
parent 38cc4c09ca
commit a213a1dba0
2215 changed files with 242567 additions and 6015 deletions

View File

@@ -0,0 +1,46 @@
<?php
namespace App\Services\Account\Settings;
use App\Services\BaseService;
use App\Models\Account\Account;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\QueryException;
class ArchiveAllContacts extends BaseService
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules()
{
return [
'account_id' => 'required|integer|exists:accounts,id',
];
}
/**
* Archive all the contacts in the account.
*
* This method is used by a user who wants to downgrade his plan.
*
* @param array $data
* @return bool
*/
public function execute(array $data): bool
{
$this->validate($data);
try {
DB::table('contacts')
->where('account_id', $data['account_id'])
->update(['is_active' => 0]);
} catch (QueryException $e) {
return false;
}
return true;
}
}

View File

@@ -0,0 +1,86 @@
<?php
namespace App\Services\Account\Settings;
use App\Services\BaseService;
use App\Models\Account\Account;
use App\Exceptions\StripeException;
class DestroyAccount extends BaseService
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules()
{
return [
'account_id' => 'required|integer|exists:accounts,id',
];
}
/**
* Completely delete an account.
*
* @param array $data
* @return void
*
* @throws StripeException
*/
public function execute(array $data): void
{
$this->validate($data);
$account = Account::find($data['account_id']);
$this->destroyDocuments($account);
$this->destroyPhotos($account);
$this->cancelStripe($account);
$account->delete();
}
/**
* Destroy the documents.
*
* @param Account $account
* @return void
*/
private function destroyDocuments(Account $account)
{
app(DestroyAllDocuments::class)->execute([
'account_id' => $account->id,
]);
}
/**
* Destroy the photos.
*
* @param Account $account
* @return void
*/
private function destroyPhotos(Account $account)
{
app(DestroyAllPhotos::class)->execute([
'account_id' => $account->id,
]);
}
/**
* Cancel Stripe subscription.
*
* @param Account $account
* @return void
*
* @throws StripeException
*/
private function cancelStripe(Account $account)
{
if ($account->isSubscribed() && ! $account->has_access_to_paid_version_for_free) {
$account->subscriptionCancel();
}
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace App\Services\Account\Settings;
use App\Services\BaseService;
use App\Models\Contact\Document;
class DestroyAllDocuments extends BaseService
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules()
{
return [
'account_id' => 'required|integer|exists:accounts,id',
];
}
/**
* Destroy all documents in an account.
*
* @param array $data
* @return bool
*/
public function execute(array $data): bool
{
$this->validate($data);
$documents = Document::where('account_id', $data['account_id'])
->get();
foreach ($documents as $document) {
$document->delete();
}
return true;
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace App\Services\Account\Settings;
use App\Models\Account\Photo;
use App\Services\BaseService;
class DestroyAllPhotos extends BaseService
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules()
{
return [
'account_id' => 'required|integer|exists:accounts,id',
];
}
/**
* Destroy all photos in an account.
*
* @param array $data
* @return bool
*/
public function execute(array $data): bool
{
$this->validate($data);
$photos = Photo::where('account_id', $data['account_id'])
->get();
foreach ($photos as $photo) {
$photo->delete();
}
return true;
}
}

View File

@@ -0,0 +1,94 @@
<?php
namespace App\Services\Account\Settings;
use App\Models\User\User;
use Illuminate\Support\Str;
use App\Services\BaseService;
use function Safe\json_encode;
use App\Models\Account\Account;
use Illuminate\Support\Facades\Storage;
use App\ExportResources\Account\Account as AccountResource;
class JsonExportAccount extends BaseService
{
/** @var string */
protected $tempFileName;
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules()
{
return [
'account_id' => 'required|integer|exists:accounts,id',
'user_id' => 'required|integer|exists:users,id',
];
}
/**
* Export account as Json.
*
* @param array $data
* @return string
*/
public function execute(array $data): string
{
$this->validate($data);
$user = User::findOrFail($data['user_id']);
$this->tempFileName = 'temp/'.Str::random(40).'.json';
$this->writeExport($data, $user);
return $this->tempFileName;
}
/**
* Export data in temp file.
*
* @param array $data
* @param User $user
*/
private function writeExport(array $data, User $user)
{
$result = [];
$result['version'] = '1.0-preview.1';
$result['app_version'] = config('monica.app_version');
$result['export_date'] = now();
$result['url'] = config('app.url');
$result['exported_by'] = $user->uuid;
$result['account'] = $this->exportAccount($data);
$this->writeToTempFile(json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_IGNORE | JSON_UNESCAPED_SLASHES));
}
/**
* Write to a temp file.
*
* @return void
*/
private function writeToTempFile(string $sql)
{
Storage::disk('local')
->append($this->tempFileName, $sql);
}
/**
* Export the Account table.
*
* @param array $data
* @return mixed
*/
private function exportAccount(array $data)
{
$account = Account::find($data['account_id']);
$exporter = new AccountResource($account);
return $exporter->resolve();
}
}

View File

@@ -0,0 +1,180 @@
<?php
namespace App\Services\Account\Settings;
use App\Services\BaseService;
use App\Models\Account\Account;
use App\Services\QueuableService;
use App\Services\DispatchableService;
use App\Services\Contact\Contact\DestroyContact;
class ResetAccount extends BaseService implements QueuableService
{
use DispatchableService;
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules()
{
return [
'account_id' => 'required|integer|exists:accounts,id',
];
}
/**
* Reset the account.
*
* @param array $data
* @return void
*/
public function handle(array $data): void
{
$this->validate($data);
$account = Account::find($data['account_id']);
$this->destroyCompanies($account);
$this->destroyDays($account);
$this->destroyPlaces($account);
$this->destroyDocuments($account);
$this->destroyPhotos($account);
$this->destroyJournalEntries($account);
$this->destroyImportJobs($account);
$this->destroyContacts($account);
}
/**
* Destroy the companies.
*
* @param Account $account
* @return void
*/
private function destroyCompanies(Account $account)
{
$companies = $account->companies;
foreach ($companies as $company) {
$company->delete();
}
}
/**
* Destroy the days.
*
* @param Account $account
* @return void
*/
private function destroyDays(Account $account)
{
$days = $account->days;
foreach ($days as $day) {
$day->delete();
}
}
/**
* Destroy the places.
*
* @param Account $account
* @return void
*/
private function destroyPlaces(Account $account)
{
$places = $account->places;
foreach ($places as $place) {
$place->delete();
}
}
/**
* Destroy the documents.
*
* @param Account $account
* @return void
*/
private function destroyDocuments(Account $account)
{
app(DestroyAllDocuments::class)->execute([
'account_id' => $account->id,
]);
}
/**
* Destroy the photos.
*
* @param Account $account
* @return void
*/
private function destroyPhotos(Account $account)
{
app(DestroyAllPhotos::class)->execute([
'account_id' => $account->id,
]);
}
/**
* Destroy the journal entries associated with all the contacts of this
* account.
*
* @param Account $account
* @return void
*/
private function destroyJournalEntries(Account $account)
{
$entries = $account->entries;
foreach ($entries as $entry) {
$entry->delete();
}
$activities = $account->activities;
foreach ($activities as $activity) {
$entries = $activity->journalEntries;
foreach ($entries as $entry) {
$entry->delete();
}
$activity->delete();
}
}
/**
* Destroy the import jobs.
*
* @param Account $account
* @return void
*/
private function destroyImportJobs(Account $account)
{
$importjobs = $account->importjobs;
foreach ($importjobs as $importjob) {
$importjob->delete();
}
}
/**
* Destroy all the contacts associated with this account.
*
* @param Account $account
* @return void
*/
private function destroyContacts(Account $account)
{
$contacts = $account->contacts;
foreach ($contacts as $contact) {
DestroyContact::dispatchSync([
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
'force_delete' => true,
]);
}
}
}

File diff suppressed because it is too large Load Diff