fix(Handle Expired members script)
This commit is contained in:
@@ -8,10 +8,11 @@ use App\Services\Nextcloud\NextcloudService;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Http\Client\ConnectionException;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Symfony\Component\Console\Command\Command as CommandAlias;
|
||||
|
||||
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';
|
||||
|
||||
public function __construct(
|
||||
@@ -27,19 +28,36 @@ class HandleExpiredMembersDolibarr extends Command
|
||||
*/
|
||||
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');
|
||||
|
||||
$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)");
|
||||
|
||||
foreach ($expiredMembers as $member) {
|
||||
try {
|
||||
$this->processMember($member);
|
||||
$this->processMember($member, $dryRun);
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('Erreur traitement adhérent', [
|
||||
'member_id' => $member['id'],
|
||||
@@ -48,31 +66,46 @@ class HandleExpiredMembersDolibarr extends Command
|
||||
}
|
||||
}
|
||||
|
||||
$this->info('Traitement terminé');
|
||||
return Command::SUCCESS;
|
||||
$this->info(
|
||||
$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
|
||||
$this->dolibarr->setMemberStatus($member['id'], 'resilie');
|
||||
|
||||
// 2. Désactivation mail ISPConfig
|
||||
if ($email) {
|
||||
$this->disableMailAccount($email);
|
||||
// Résiliation Dolibarr
|
||||
if ($dryRun) {
|
||||
$this->info("[DRY-RUN] Résiliation Dolibarr");
|
||||
} else {
|
||||
$this->dolibarr->setMemberStatus($member['id'], '0');
|
||||
}
|
||||
|
||||
// 3. Désactivation Nextcloud
|
||||
// Désactivation mail
|
||||
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);
|
||||
|
||||
@@ -81,6 +114,11 @@ class HandleExpiredMembersDolibarr extends Command
|
||||
return;
|
||||
}
|
||||
|
||||
if ($dryRun) {
|
||||
$this->info("[DRY-RUN] Mail désactivé");
|
||||
return;
|
||||
}
|
||||
|
||||
$this->mailService->updateMailUser($email, [
|
||||
'postfix' => 'n',
|
||||
'disablesmtp' => 'y',
|
||||
@@ -91,4 +129,15 @@ class HandleExpiredMembersDolibarr extends Command
|
||||
$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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ class DolibarrService
|
||||
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(
|
||||
$this->baseUrl . '/members/' . $id,
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Services\Nextcloud;
|
||||
|
||||
use Illuminate\Http\Client\ConnectionException;
|
||||
use Illuminate\Http\Client\PendingRequest;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
@@ -25,6 +26,7 @@ class NextcloudService
|
||||
|
||||
/**
|
||||
* Désactive un utilisateur Nextcloud à partir de son email
|
||||
* @throws ConnectionException
|
||||
*/
|
||||
public function disableUserByEmail(string $email): void
|
||||
{
|
||||
@@ -44,6 +46,7 @@ class NextcloudService
|
||||
|
||||
/**
|
||||
* Trouve le userId Nextcloud à partir de l’email
|
||||
* @throws ConnectionException
|
||||
*/
|
||||
protected function findUserIdByEmail(string $email): ?string
|
||||
{
|
||||
|
||||
@@ -19,7 +19,7 @@ Route::middleware(['auth', 'verified'])->group(function () {
|
||||
|
||||
// TEST
|
||||
|
||||
Route::get('/test/sync-ispconfig', function () {
|
||||
/*Route::get('/test/sync-ispconfig', function () {
|
||||
|
||||
if (!app()->isLocal()) {
|
||||
abort(403);
|
||||
@@ -37,6 +37,14 @@ Route::get('/test/isp-mails', function() {
|
||||
$ispService = new \App\Services\ISPConfig\ISPConfigMailService;
|
||||
|
||||
return $ispService->getAllMailDomains();
|
||||
});*/
|
||||
|
||||
Route::get('/test-dolibarr', function () {
|
||||
$dolibarrService = new \App\Services\Dolibarr\DolibarrService();
|
||||
|
||||
$members = $dolibarrService->getAllMembers();
|
||||
|
||||
dd($members);
|
||||
});
|
||||
|
||||
require __DIR__.'/settings.php';
|
||||
|
||||
Reference in New Issue
Block a user