refactor: move backend in apps/backend

This commit is contained in:
Giovanni committed 2026-10-03 18:35:36 +02:00
1 parent cf529278bd
commit e2b471312c
178 files changed
+210 -193

No files matched your search

+13
View File
@@ -0,0 +1,13 @@
<?php
namespace apps\backend\app\Services;
use Carbon\Carbon;
class FormatDate
{
public static function formatDate($date){
return Carbon::parse($date)->format('d/m/Y, H:i');
}
}
+25
View File
@@ -0,0 +1,25 @@
<?php
namespace apps\backend\app\Services;
class GetRole
{
public static function getRole($role)
{
$role = (int)$role;
return match ($role) {
1 => "Président",
2 => "Gérant",
3 => "Bénévole",
default => "Utilisateur",
};
}
public static function GetAllRoles() {
return [
"Président",
"Gérant",
"Bénévole"
];
}
}
+143
View File
@@ -0,0 +1,143 @@
<?php
namespace apps\backend\app\Services;
use apps\backend\app\Models\Event;
use apps\backend\app\Models\User;
use Illuminate\Support\Facades\Cache;
use Typesense\Client;
use function App\Services\config;
use function App\Services\report;
class TypesenseService
{
protected Client $client;
public function __construct()
{
$this->client = new Client([
'api_key' => config('app.typesense_api_key'),
'nodes' => [
[
'host' => config('app.typesense_connection'),
'port' => '8108',
'protocol' => 'http',
],
],
]);
}
private function ensureCollectionsExist(): void
{
static $checked = false;
if ($checked) {
return;
}
try {
$collections = $this->client->collections->retrieve();
$names = array_column($collections, 'name');
if (!in_array('events', $names)) {
$this->client->collections->create([
'name' => 'events',
'fields' => [
['name' => 'name', 'type' => 'string'],
['name' => 'description', 'type' => 'string'],
],
]);
}
if (!in_array('users', $names)) {
$this->client->collections->create([
'name' => 'users',
'fields' => [
['name' => 'name', 'type' => 'string'],
['name' => 'lastname', 'type' => 'string'],
['name' => 'validate', 'type' => 'bool'],
],
]);
}
$checked = true;
} catch (\Throwable $e) {
report($e);
}
}
public function upsertEvent(Event $event): void
{
$this->ensureCollectionsExist();
try {
$this->client->collections['events']
->documents
->upsert([
'id' => (string) $event->id,
'name' => $event->name,
'description' => $event->description,
]);
} catch (\Throwable $e) {
report($e);
}
}
public function deleteEvent(int $eventId): void
{
$this->ensureCollectionsExist();
try {
$this->client->collections['events']
->documents[(string) $eventId]
->delete();
} catch (\Throwable $e) {
report($e);
}
}
public function upsertUser(User $user): void
{
$this->ensureCollectionsExist();
if (!$user->validate) {
try {
$this->client->collections['users']
->documents[(string) $user->id]
->retrieve();
$this->deleteUser($user->id);
} catch (\Throwable $e) {
}
return;
}
try {
$this->client->collections['users']
->documents
->upsert([
'id' => (string) $user->id,
'name' => $user->name,
'lastname' => $user->lastname,
'validate' => true,
]);
} catch (\Throwable $e) {
report($e);
}
}
public function deleteUser(int $userId): void
{
$this->ensureCollectionsExist();
try {
$this->client->collections['users']
->documents[(string) $userId]
->delete();
} catch (\Throwable $e) {
report($e);
}
}
}
+31
View File
@@ -0,0 +1,31 @@
<?php
namespace apps\backend\app\Services;
use apps\backend\app\Models\User;
use Illuminate\Support\Facades\Log;
class UserService
{
public static function getData(int $id) : array {
$user = User::findOrFail($id);
return [
"id" => $user->id,
"name" => $user->name,
"lastname" => $user->lastname,
"email" => $user->email,
"role" => GetRole::getRole($user->role),
"isAdmin" => ($user->role === 1 || $user->role === 2),
"phone" => $user->phone,
"created_at" => $user->created_at,
"updated_at" => $user->updated_at,
"verfied_at" => $user->verfied_at,
"validate" => $user->validate,
"tasks" => $user->tasks,
"profile_photo_path" => $user->profile_photo_path,
"email_notifications" => $user->email_notifications,
"web_notifications" => $user->web_notifications,
];
}
}