fix(Handle Expired members script)

This commit is contained in:
2026-01-18 21:51:57 +01:00
parent 6a78b7fb68
commit 363bb2e635
4 changed files with 80 additions and 20 deletions

View File

@@ -8,10 +8,11 @@ use App\Services\Nextcloud\NextcloudService;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Http\Client\ConnectionException; use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Symfony\Component\Console\Command\Command as CommandAlias;
class HandleExpiredMembersDolibarr extends Command class HandleExpiredMembersDolibarr extends Command
{ {
protected $signature = 'members:cleanup-expired'; protected $signature = 'members:cleanup-expired {--dry-run}';
protected $description = 'Résilie les adhérents expirés et désactive leurs services'; protected $description = 'Résilie les adhérents expirés et désactive leurs services';
public function __construct( public function __construct(
@@ -27,19 +28,36 @@ class HandleExpiredMembersDolibarr extends Command
*/ */
public function handle(): int public function handle(): int
{ {
$dryRun = $this->option('dry-run');
$this->info(
$dryRun
? 'DRY-RUN activé aucune modification ne sera effectuée'
: 'Mode réel activé'
);
$this->info('Récupération des adhérents Dolibarr'); $this->info('Récupération des adhérents Dolibarr');
$members = collect($this->dolibarr->getAllMembers()); $members = collect($this->dolibarr->getAllMembers());
$today = now()->startOfDay();
$expiredMembers = $members->filter(fn ($m) => $m['status'] === 'expired'); $expiredMembers = $members->filter(function ($member) use ($today) {
if (($member['statut'] ?? null) != 1) {
return false;
}
dd($expiredMembers); if (empty($member['datefin'])) {
return false;
}
return \Carbon\Carbon::parse($member['datefin'])->lt($today);
});
$this->info("{$expiredMembers->count()} adhérent(s) expiré(s)"); $this->info("{$expiredMembers->count()} adhérent(s) expiré(s)");
foreach ($expiredMembers as $member) { foreach ($expiredMembers as $member) {
try { try {
$this->processMember($member); $this->processMember($member, $dryRun);
} catch (\Throwable $e) { } catch (\Throwable $e) {
Log::error('Erreur traitement adhérent', [ Log::error('Erreur traitement adhérent', [
'member_id' => $member['id'], 'member_id' => $member['id'],
@@ -48,31 +66,46 @@ class HandleExpiredMembersDolibarr extends Command
} }
} }
$this->info('Traitement terminé'); $this->info(
return Command::SUCCESS; $dryRun
? 'DRY-RUN terminé aucune action effectuée'
: 'Traitement terminé'
);
return CommandAlias::SUCCESS;
} }
protected function processMember(array $member): void /**
* @throws ConnectionException
*/
protected function processMember(array $member, bool $dryRun): void
{ {
$email = $member['email'] ?? null; $email = $this->extractRetzienEmail($member['email'] ?? null);
$this->info("{$member['id']} - {$email}"); $this->line("{$member['id']} - {$email}");
// 1. Résiliation Dolibarr // Résiliation Dolibarr
$this->dolibarr->setMemberStatus($member['id'], 'resilie'); if ($dryRun) {
$this->info("[DRY-RUN] Résiliation Dolibarr");
// 2. Désactivation mail ISPConfig } else {
if ($email) { $this->dolibarr->setMemberStatus($member['id'], '0');
$this->disableMailAccount($email);
} }
// 3. Désactivation Nextcloud // Désactivation mail
if ($email) { if ($email) {
$this->nextcloud->disableUserByEmail($email); $this->disableMailAccount($email, $dryRun);
}
// Désactivation Nextcloud
if ($email) {
if ($dryRun) {
$this->info("[DRY-RUN] Désactivation Nextcloud");
} else {
$this->nextcloud->disableUserByEmail($email);
}
} }
} }
protected function disableMailAccount(string $email): void protected function disableMailAccount(string $email, bool $dryRun): void
{ {
$details = $this->mailService->getMailUserDetails($email); $details = $this->mailService->getMailUserDetails($email);
@@ -81,6 +114,11 @@ class HandleExpiredMembersDolibarr extends Command
return; return;
} }
if ($dryRun) {
$this->info("[DRY-RUN] Mail désactivé");
return;
}
$this->mailService->updateMailUser($email, [ $this->mailService->updateMailUser($email, [
'postfix' => 'n', 'postfix' => 'n',
'disablesmtp' => 'y', 'disablesmtp' => 'y',
@@ -91,4 +129,15 @@ class HandleExpiredMembersDolibarr extends Command
$this->info("Mail désactivé"); $this->info("Mail désactivé");
} }
protected function extractRetzienEmail(?string $emails): ?string
{
if (!$emails) {
return null;
}
return collect(explode(';', $emails))
->map(fn (string $email): string => trim($email))
->filter(fn (string $email): bool => str_contains($email, '@retzien.fr'))
->first();
}
} }

View File

@@ -61,7 +61,7 @@ class DolibarrService
return $response->json(); return $response->json();
} }
public function setMemberStatus(int|string $id, string $status): bool public function setMemberStatus(int|string $id, int|string $status): bool
{ {
$response = $this->client()->put( $response = $this->client()->put(
$this->baseUrl . '/members/' . $id, $this->baseUrl . '/members/' . $id,

View File

@@ -2,6 +2,7 @@
namespace App\Services\Nextcloud; namespace App\Services\Nextcloud;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\PendingRequest; use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
@@ -25,6 +26,7 @@ class NextcloudService
/** /**
* Désactive un utilisateur Nextcloud à partir de son email * Désactive un utilisateur Nextcloud à partir de son email
* @throws ConnectionException
*/ */
public function disableUserByEmail(string $email): void public function disableUserByEmail(string $email): void
{ {
@@ -44,6 +46,7 @@ class NextcloudService
/** /**
* Trouve le userId Nextcloud à partir de lemail * Trouve le userId Nextcloud à partir de lemail
* @throws ConnectionException
*/ */
protected function findUserIdByEmail(string $email): ?string protected function findUserIdByEmail(string $email): ?string
{ {

View File

@@ -19,7 +19,7 @@ Route::middleware(['auth', 'verified'])->group(function () {
// TEST // TEST
Route::get('/test/sync-ispconfig', function () { /*Route::get('/test/sync-ispconfig', function () {
if (!app()->isLocal()) { if (!app()->isLocal()) {
abort(403); abort(403);
@@ -37,6 +37,14 @@ Route::get('/test/isp-mails', function() {
$ispService = new \App\Services\ISPConfig\ISPConfigMailService; $ispService = new \App\Services\ISPConfig\ISPConfigMailService;
return $ispService->getAllMailDomains(); return $ispService->getAllMailDomains();
});*/
Route::get('/test-dolibarr', function () {
$dolibarrService = new \App\Services\Dolibarr\DolibarrService();
$members = $dolibarrService->getAllMembers();
dd($members);
}); });
require __DIR__.'/settings.php'; require __DIR__.'/settings.php';