🚀 مُصادَق: الإطلاق الأولي للنظام المتكامل
This commit is contained in:
62
app/Modules/Companies/CompanyController.php
Normal file
62
app/Modules/Companies/CompanyController.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Companies;
|
||||
|
||||
use App\Core\{Request, Response};
|
||||
use App\Modules\Companies\{CompanyModel, CompanyService};
|
||||
use Throwable;
|
||||
|
||||
final class CompanyController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly CompanyModel $companyModel,
|
||||
private readonly CompanyService $companyService
|
||||
) {}
|
||||
|
||||
public function list(Request $request): void
|
||||
{
|
||||
$companies = $this->companyModel->findByTenant($request->tenantId);
|
||||
Response::json([
|
||||
'success' => true,
|
||||
'data' => $companies
|
||||
]);
|
||||
}
|
||||
|
||||
public function create(Request $request): void
|
||||
{
|
||||
$data = $request->getBody();
|
||||
$data['tenant_id'] = $request->tenantId;
|
||||
|
||||
try {
|
||||
$companyId = $this->companyService->createCompany($data);
|
||||
Response::json([
|
||||
'success' => true,
|
||||
'data' => ['id' => $companyId],
|
||||
'message' => 'تم إضافة الشركة بنجاح'
|
||||
], 201);
|
||||
} catch (Throwable $e) {
|
||||
Response::error('فشل إضافة الشركة', 'CREATE_FAILED', 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function updateJoFotara(Request $request, string $id): void
|
||||
{
|
||||
$data = [
|
||||
'jofotara_client_id' => $request->input('client_id'),
|
||||
'jofotara_secret_key' => $request->input('secret_key'),
|
||||
'is_jofotara_linked' => 1
|
||||
];
|
||||
|
||||
try {
|
||||
$this->companyService->createCompany(array_merge($data, ['id' => $id])); // Reuses encryption logic
|
||||
Response::json([
|
||||
'success' => true,
|
||||
'message' => 'تم تحديث بيانات جو-فواتير بنجاح'
|
||||
]);
|
||||
} catch (Throwable $e) {
|
||||
Response::error('فشل تحديث البيانات', 'UPDATE_FAILED', 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
19
app/Modules/Companies/CompanyModel.php
Normal file
19
app/Modules/Companies/CompanyModel.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Companies;
|
||||
|
||||
use App\Models\BaseModel;
|
||||
|
||||
final class CompanyModel extends BaseModel
|
||||
{
|
||||
protected string $table = 'companies';
|
||||
|
||||
public function findByTenant(string $tenantId): array
|
||||
{
|
||||
$stmt = $this->db()->prepare("SELECT * FROM {$this->table} WHERE tenant_id = ? AND deleted_at IS NULL");
|
||||
$stmt->execute([$tenantId]);
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
}
|
||||
43
app/Modules/Companies/CompanyService.php
Normal file
43
app/Modules/Companies/CompanyService.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Companies;
|
||||
|
||||
use App\Services\Security\EncryptionService;
|
||||
use App\Modules\Companies\CompanyModel;
|
||||
|
||||
final class CompanyService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly CompanyModel $companyModel,
|
||||
private readonly EncryptionService $encryption
|
||||
) {}
|
||||
|
||||
public function createCompany(array $data): string
|
||||
{
|
||||
// Encrypt sensitive JoFotara credentials
|
||||
if (isset($data['jofotara_client_id'])) {
|
||||
$data['jofotara_client_id_encrypted'] = $this->encryption->encrypt($data['jofotara_client_id']);
|
||||
unset($data['jofotara_client_id']);
|
||||
}
|
||||
|
||||
if (isset($data['jofotara_secret_key'])) {
|
||||
$data['jofotara_secret_key_encrypted'] = $this->encryption->encrypt($data['jofotara_secret_key']);
|
||||
unset($data['jofotara_secret_key']);
|
||||
}
|
||||
|
||||
return (string)$this->companyModel->create($data);
|
||||
}
|
||||
|
||||
public function getJoFotaraCredentials(string $companyId): array
|
||||
{
|
||||
$company = $this->companyModel->find($companyId);
|
||||
if (!$company) return [];
|
||||
|
||||
return [
|
||||
'clientId' => $company['jofotara_client_id_encrypted'] ? $this->encryption->decrypt($company['jofotara_client_id_encrypted']) : null,
|
||||
'secretKey' => $company['jofotara_secret_key_encrypted'] ? $this->encryption->decrypt($company['jofotara_secret_key_encrypted']) : null,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user