feat&fix(add Listmonk service and sync part 1, fix association email on member sync)

This commit is contained in:
2026-04-07 16:52:18 +02:00
parent 703a75a11a
commit 6754d8684a
14 changed files with 298 additions and 240 deletions

1
.gitignore vendored
View File

@@ -1,4 +1,5 @@
*.log *.log
TODO.txt
.DS_Store .DS_Store
.env .env
.env.backup .env.backup

View File

@@ -23,8 +23,7 @@ class HandleExpiredMembersDolibarr extends Command
protected ISPConfigMailService $mailService, protected ISPConfigMailService $mailService,
protected NextcloudService $nextcloud, protected NextcloudService $nextcloud,
protected MemberService $memberService protected MemberService $memberService
) ) {
{
parent::__construct(); parent::__construct();
} }
@@ -65,11 +64,12 @@ class HandleExpiredMembersDolibarr extends Command
if ($emailFilter) { if ($emailFilter) {
$expiredMembers = $expiredMembers->filter(function ($member) use ($emailFilter) { $expiredMembers = $expiredMembers->filter(function ($member) use ($emailFilter) {
return $this->extractRetzienEmail($member['email'] ?? null) === $emailFilter; return Member::extractRetzienEmail($member['email'] ?? '') === $emailFilter;
}); });
if ($expiredMembers->isEmpty()) { if ($expiredMembers->isEmpty()) {
$this->warn("Aucun adhérent expiré trouvé pour {$emailFilter}"); $this->warn("Aucun adhérent expiré trouvé pour {$emailFilter}");
return CommandAlias::SUCCESS; return CommandAlias::SUCCESS;
} }
} }
@@ -102,13 +102,13 @@ class HandleExpiredMembersDolibarr extends Command
*/ */
protected function processMember(array $member, bool $dryRun): void protected function processMember(array $member, bool $dryRun): void
{ {
$email = $this->extractRetzienEmail($member['email'] ?? null); $email = Member::extractRetzienEmail($member['email'] ?? '');
$this->line("{$member['id']} - {$email}"); $this->line("{$member['id']} - {$email}");
// Résiliation Dolibarr // Résiliation Dolibarr
if ($dryRun) { if ($dryRun) {
$this->info("[DRY-RUN] Résiliation Dolibarr"); $this->info('[DRY-RUN] Résiliation Dolibarr');
} else { } else {
$this->dolibarr->updateMember($member['id'], [ $this->dolibarr->updateMember($member['id'], [
'statut' => 0, 'statut' => 0,
@@ -129,7 +129,7 @@ class HandleExpiredMembersDolibarr extends Command
// Désactivation Nextcloud // Désactivation Nextcloud
if ($email) { if ($email) {
if ($dryRun) { if ($dryRun) {
$this->info("[DRY-RUN] Désactivation Nextcloud"); $this->info('[DRY-RUN] Désactivation Nextcloud');
} else { } else {
$this->nextcloud->disableUserByEmail($email); $this->nextcloud->disableUserByEmail($email);
} }
@@ -145,11 +145,13 @@ class HandleExpiredMembersDolibarr extends Command
if (! $details) { if (! $details) {
$this->warn("Boîte mail inexistante : {$email}"); $this->warn("Boîte mail inexistante : {$email}");
return; return;
} }
if ($dryRun) { if ($dryRun) {
$this->info("[DRY-RUN] Mail désactivé ({$email})"); $this->info("[DRY-RUN] Mail désactivé ({$email})");
return; return;
} }
@@ -166,16 +168,4 @@ class HandleExpiredMembersDolibarr extends Command
$this->info("Mail désactivé : {$email}"); $this->info("Mail désactivé : {$email}");
} }
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

@@ -68,7 +68,7 @@ class SyncDolibarrMembers extends Command
'lastname' => $member['lastname'], 'lastname' => $member['lastname'],
'firstname' => $member['firstname'], 'firstname' => $member['firstname'],
'email' => $member['email'] ?: null, 'email' => $member['email'] ?: null,
'retzien_email' => '', 'retzien_email' => Member::extractRetzienEmail($member['email'] ?? ''),
'company' => $member['societe'], 'company' => $member['societe'],
'website_url' => $member['url'], 'website_url' => $member['url'],
'address' => $member['address'], 'address' => $member['address'],

View File

@@ -7,18 +7,20 @@ use App\Models\IspconfigMember;
use App\Models\Member; use App\Models\Member;
use App\Services\ISPConfig\ISPConfigMailService; use App\Services\ISPConfig\ISPConfigMailService;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use function Laravel\Prompts\progress; use function Laravel\Prompts\progress;
class SyncISPConfigMailMembers extends Command class SyncISPConfigMailMembers extends Command
{ {
protected $signature = 'sync:ispconfig-mail-members'; protected $signature = 'sync:ispconfig-mail-members';
protected $description = 'Synchronise les services MAIL ISPConfig des membres - Email Retzien'; protected $description = 'Synchronise les services MAIL ISPConfig des membres - Email Retzien';
public function handle(): void public function handle(): void
{ {
$this->info('Synchronisation ISPConfig MAIL'); $this->info('Synchronisation ISPConfig MAIL');
$ispMail = new ISPConfigMailService(); $ispMail = new ISPConfigMailService;
// Récupération de tous les mail users // Récupération de tous les mail users
$mailUsers = collect($ispMail->getAllMailUsers()); $mailUsers = collect($ispMail->getAllMailUsers());
@@ -35,18 +37,14 @@ class SyncISPConfigMailMembers extends Command
$synced = 0; $synced = 0;
// Parcours des membres // Parcours des membres
Member::whereNotNull('email')->chunk(100, function ($members) use ( Member::whereNotNull('retzien_email')->where('retzien_email', '!=', '')->chunk(100, function ($members) use (
$progressBar, $progressBar,
$emailToMailUserId, $emailToMailUserId,
$ispMail, $ispMail,
&$synced &$synced
) { ) {
foreach ($members as $member) { foreach ($members as $member) {
$emails = array_map('trim', explode(';', $member->email)); $retzienEmail = strtolower($member->retzien_email);
$retzienEmail = collect($emails)
->map(fn ($e) => strtolower($e))
->first(fn ($e) => str_ends_with($e, '@retzien.fr'));
if (! $retzienEmail) { if (! $retzienEmail) {
continue; continue;
@@ -56,6 +54,7 @@ class SyncISPConfigMailMembers extends Command
if (! $mailUserId) { if (! $mailUserId) {
$this->warn("Aucun mail user ISPConfig pour {$retzienEmail}"); $this->warn("Aucun mail user ISPConfig pour {$retzienEmail}");
continue; continue;
} }

View File

@@ -0,0 +1,119 @@
<?php
namespace App\Console\Commands;
use App\Models\ListmonkMember;
use App\Models\Member;
use App\Services\ListMonk\ListMonkService;
use Illuminate\Console\Command;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\Log;
use Symfony\Component\Console\Command\Command as CommandAlias;
use function Laravel\Prompts\progress;
class SyncListmonkMembers extends Command
{
protected $signature = 'listmonk:sync-members
{--dry-run : Run without writing to the database}
{--member= : Sync a single member by member_id}';
protected $description = 'Sync Listmonk user accounts with members';
public function __construct(
protected ListMonkService $listmonk
) {
parent::__construct();
}
/**
* @throws ConnectionException
*/
public function handle(): int
{
$dryRun = $this->option('dry-run');
$memberFilter = $this->option('member');
$this->info(
$dryRun
? 'DRY-RUN enabled'
: 'Syncing Listmonk → Members'
);
$members = Member::query()
->when($memberFilter, fn ($q) => $q->where('id', $memberFilter))
->get()
->filter(fn (Member $m) => ! empty($m->retzien_email))
->keyBy(fn (Member $m) => strtolower($m->retzien_email));
if ($members->isEmpty()) {
$this->warn('No members to sync');
return CommandAlias::SUCCESS;
}
$this->info("{$members->count()} members to sync");
$listmonkUsers = $this->listmonk->getUsers();
dd($listmonkUsers);
$this->info(count($listmonkUsers).' Listmonk users found');
$progress = null;
if (! $dryRun) {
$progress = progress(
label: 'Syncing members',
steps: $members->count()
);
$progress->start();
}
$synced = 0;
foreach ($listmonkUsers as $user) {
try {
$email = strtolower($user['email'] ?? '');
if (! $email || ! $members->has($email)) {
continue;
}
$member = $members[$email];
if ($dryRun) {
$this->line("[DRY-RUN] {$member->id} ({$email}) ← Listmonk user #{$user['id']}");
} else {
ListmonkMember::query()->updateOrCreate(
['member_id' => $member->id],
[
'listmonk_user_id' => $user['id'],
'data' => $user,
]
);
$progress?->advance();
}
$synced++;
} catch (\Throwable $e) {
Log::error('Listmonk sync error', [
'user' => $user['id'] ?? null,
'error' => $e->getMessage(),
]);
$progress?->advance();
}
}
if ($progress) {
$progress->finish();
$this->newLine();
}
$this->info("Sync complete — {$synced} accounts linked");
return CommandAlias::SUCCESS;
}
}

View File

@@ -9,6 +9,7 @@ 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; use Symfony\Component\Console\Command\Command as CommandAlias;
use function Laravel\Prompts\progress; use function Laravel\Prompts\progress;
class SyncNextcloudMembers extends Command class SyncNextcloudMembers extends Command
@@ -40,14 +41,15 @@ class SyncNextcloudMembers extends Command
); );
$members = Member::query() $members = Member::query()
->where('email', 'like', '%@retzien.fr%') ->whereNotNull('retzien_email')
->where('retzien_email', '!=', '')
->when($memberFilter, fn ($q) => $q->where('id', $memberFilter)) ->when($memberFilter, fn ($q) => $q->where('id', $memberFilter))
->get() ->get()
->filter(fn (Member $m) => !empty($m->retzien_email))
->keyBy(fn (Member $m) => strtolower($m->retzien_email)); ->keyBy(fn (Member $m) => strtolower($m->retzien_email));
if ($members->isEmpty()) { if ($members->isEmpty()) {
$this->warn('Aucun membre à synchroniser'); $this->warn('Aucun membre à synchroniser');
return CommandAlias::SUCCESS; return CommandAlias::SUCCESS;
} }

View File

@@ -24,6 +24,7 @@ class Synchronisations extends Page
'ispconfig_mail', 'ispconfig_mail',
'ispconfig_web', 'ispconfig_web',
'nextcloud', 'nextcloud',
'listmonk',
'services', 'services',
]; ];
@@ -154,6 +155,17 @@ class Synchronisations extends Page
}); });
} }
public function syncListmonkAction(): Action
{
return Action::make('syncListmonk')
->requiresConfirmation()
->modalHeading(__('synchronisations.sections.listmonk.modal_heading'))
->modalDescription(__('synchronisations.sections.listmonk.modal_description'))
->modalSubmitActionLabel(__('synchronisations.action.submit'))
->disabled(fn () => in_array($this->getCommandStatus('listmonk')['status'], ['pending', 'running']))
->action(fn () => $this->enqueueCommand('listmonk', 'listmonk:sync-members'));
}
public function syncServicesAction(): Action public function syncServicesAction(): Action
{ {
return Action::make('syncServices') return Action::make('syncServices')

View File

@@ -0,0 +1,40 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* @property int $id
* @property int $member_id
* @property int|null $listmonk_user_id
* @property array<array-key, mixed>|null $data
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \App\Models\Member $member
*
* @mixin \Eloquent
*/
class ListmonkMember extends Model
{
protected $table = 'listmonks_members';
protected $fillable = [
'member_id',
'listmonk_user_id',
'data',
];
protected function casts(): array
{
return [
'data' => 'array',
];
}
public function member(): BelongsTo
{
return $this->belongsTo(Member::class);
}
}

View File

@@ -113,18 +113,19 @@ class Member extends Model
return __("members.fields.$attribute"); return __("members.fields.$attribute");
} }
public static function extractRetzienEmail(string $rawEmails): ?string
{
return collect(explode(';', $rawEmails))
->map(fn (string $email) => trim($email))
->filter(fn (string $email) => str_ends_with($email, '@retzien.fr'))
->first();
}
public function getFullNameAttribute(): string public function getFullNameAttribute(): string
{ {
return "{$this->firstname} {$this->lastname}"; return "{$this->firstname} {$this->lastname}";
} }
public function getRetzienEmailAttribute(): ?string
{
$emails = explode(';', $this->email);
return collect($emails)->filter(fn ($email) => str_contains($email, '@retzien.fr'))->first();
}
public function user(): BelongsTo public function user(): BelongsTo
{ {
return $this->belongsTo(User::class); return $this->belongsTo(User::class);

View File

@@ -17,15 +17,27 @@ class ListMonkService
config('services.listmonk.password') config('services.listmonk.password')
) )
->withHeaders(['Accept' => 'application/json']) ->withHeaders(['Accept' => 'application/json'])
->baseUrl(config('services.listmonk.base_url').'/api'); ->baseUrl(config('services.listmonk.base_url'));
} }
// ------------------------------------------------------------------------- /**
// Lists * Retrieve all Listmonk user accounts.
// ------------------------------------------------------------------------- *
* @return array<array-key, mixed>
*
* @throws ConnectionException
*/
public function getUsers(): array
{
return $this->http
->get('/users')
->json('data') ?? [];
}
/** /**
* Retrieve all mailing lists. * Retrieve all mailing lists with their subscriber counts.
*
* @return array<array-key, mixed>
* *
* @throws ConnectionException * @throws ConnectionException
*/ */
@@ -35,188 +47,4 @@ class ListMonkService
->get('/lists', ['per_page' => 'all']) ->get('/lists', ['per_page' => 'all'])
->json('data.results') ?? []; ->json('data.results') ?? [];
} }
/**
* Retrieve a single list by its ID.
*
* @throws ConnectionException
*/
public function getList(int $listId): ?array
{
$response = $this->http->get("/lists/{$listId}");
if (! $response->successful()) {
return null;
}
return $response->json('data');
}
// -------------------------------------------------------------------------
// Subscribers
// -------------------------------------------------------------------------
/**
* Retrieve subscribers with optional pagination.
*
* @throws ConnectionException
*/
public function getSubscribers(int $page = 1, int $perPage = 100): array
{
return $this->http
->get('/subscribers', [
'page' => $page,
'per_page' => $perPage,
])
->json('data.results') ?? [];
}
/**
* Retrieve a single subscriber by their Listmonk ID.
*
* @throws ConnectionException
*/
public function getSubscriber(int $subscriberId): ?array
{
$response = $this->http->get("/subscribers/{$subscriberId}");
if (! $response->successful()) {
return null;
}
return $response->json('data');
}
/**
* Find a subscriber by their email address.
*
* @throws ConnectionException
*/
public function getSubscriberByEmail(string $email): ?array
{
$results = $this->http
->get('/subscribers', ['query' => "subscribers.email = '{$email}'"])
->json('data.results') ?? [];
return $results[0] ?? null;
}
/**
* Create a new subscriber and enrol them in the given lists.
*
* @param array<int> $listIds IDs of the lists to subscribe to.
* @param array<string, mixed> $attribs Custom attributes (e.g. language preference).
*
* @throws ConnectionException
*/
public function createSubscriber(
string $email,
string $name,
array $listIds = [],
array $attribs = [],
string $status = 'enabled',
): ?array {
$response = $this->http->post('/subscribers', [
'email' => $email,
'name' => $name,
'status' => $status,
'lists' => $listIds,
'attribs' => $attribs,
]);
if (! $response->successful()) {
return null;
}
return $response->json('data');
}
/**
* Update an existing subscriber's information.
*
* @param array<int> $listIds
* @param array<string, mixed> $attribs
*
* @throws ConnectionException
*/
public function updateSubscriber(
int $subscriberId,
string $email,
string $name,
array $listIds = [],
array $attribs = [],
string $status = 'enabled',
): bool {
$response = $this->http->put("/subscribers/{$subscriberId}", [
'email' => $email,
'name' => $name,
'status' => $status,
'lists' => $listIds,
'attribs' => $attribs,
]);
return $response->successful();
}
/**
* Subscribe or unsubscribe a set of subscribers from lists.
*
* @param array<int> $subscriberIds
* @param array<int> $listIds
* @param string $action subscribe | unsubscribe
* @param string $status confirmed | unconfirmed
*
* @throws ConnectionException
*/
public function updateSubscriberLists(
array $subscriberIds,
array $listIds,
string $action = 'subscribe',
string $status = 'confirmed',
): bool {
$response = $this->http->put('/subscribers/lists', [
'ids' => $subscriberIds,
'action' => $action,
'status' => $status,
'list_ids' => $listIds,
]);
return $response->successful();
}
/**
* Add a subscriber to the blocklist.
*
* @throws ConnectionException
*/
public function blocklistSubscriber(int $subscriberId): bool
{
return $this->http
->put("/subscribers/{$subscriberId}/blocklist")
->successful();
}
/**
* Permanently delete a subscriber.
*
* @throws ConnectionException
*/
public function deleteSubscriber(int $subscriberId): bool
{
return $this->http
->delete("/subscribers/{$subscriberId}")
->successful();
}
/**
* Send an opt-in confirmation email to a subscriber.
*
* @throws ConnectionException
*/
public function sendOptin(int $subscriberId): bool
{
return $this->http
->post("/subscribers/{$subscriberId}/optin")
->successful();
}
} }

View File

@@ -0,0 +1,30 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('listmonks_members', function (Blueprint $table) {
$table->id();
$table->foreignId('member_id')->constrained('members')->onDelete('NO ACTION');
$table->unsignedInteger('listmonk_user_id')->nullable();
$table->json('data')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('listmonks_members');
}
};

View File

@@ -46,6 +46,13 @@ return [
'dry_run_label' => 'Simulation mode (dry-run)', 'dry_run_label' => 'Simulation mode (dry-run)',
'dry_run_helper' => 'Simulates the operation without making any changes.', 'dry_run_helper' => 'Simulates the operation without making any changes.',
], ],
'listmonk' => [
'heading' => 'Listmonk',
'description' => 'Link members to their Listmonk subscriber accounts (@retzien.fr).',
'action_label' => 'Listmonk',
'modal_heading' => 'Listmonk Synchronisation',
'modal_description' => 'Link members to their Listmonk subscriber accounts using their @retzien.fr address.',
],
'services' => [ 'services' => [
'heading' => 'Member Services', 'heading' => 'Member Services',
'description' => 'Synchronise services associated with active members.', 'description' => 'Synchronise services associated with active members.',

View File

@@ -46,6 +46,13 @@ return [
'dry_run_label' => 'Mode simulation (dry-run)', 'dry_run_label' => 'Mode simulation (dry-run)',
'dry_run_helper' => 'Simule l\'opération sans effectuer de modifications.', 'dry_run_helper' => 'Simule l\'opération sans effectuer de modifications.',
], ],
'listmonk' => [
'heading' => 'Listmonk',
'description' => 'Lie les membres à leurs comptes abonnés Listmonk (@retzien.fr).',
'action_label' => 'Listmonk',
'modal_heading' => 'Synchronisation Listmonk',
'modal_description' => 'Lie les membres à leurs comptes abonnés Listmonk en se basant sur leur adresse @retzien.fr.',
],
'services' => [ 'services' => [
'heading' => 'Services membres', 'heading' => 'Services membres',
'description' => 'Synchronise les services associés aux membres actifs.', 'description' => 'Synchronise les services associés aux membres actifs.',

View File

@@ -10,6 +10,7 @@
$ispMail = $this->getCommandStatus('ispconfig_mail'); $ispMail = $this->getCommandStatus('ispconfig_mail');
$ispWeb = $this->getCommandStatus('ispconfig_web'); $ispWeb = $this->getCommandStatus('ispconfig_web');
$nextcloud = $this->getCommandStatus('nextcloud'); $nextcloud = $this->getCommandStatus('nextcloud');
$listmonk = $this->getCommandStatus('listmonk');
$services = $this->getCommandStatus('services'); $services = $this->getCommandStatus('services');
@endphp @endphp
@@ -118,6 +119,27 @@
</div> </div>
</x-filament::section> </x-filament::section>
<x-filament::section>
<x-slot name="heading">
@include('filament.pages.partials.sync-heading', [
'label' => __('synchronisations.sections.listmonk.heading'),
'status' => $listmonk,
])
</x-slot>
<div class="space-y-3">
<p class="text-sm text-gray-500 dark:text-gray-400">
{{ __('synchronisations.sections.listmonk.description') }}
</p>
@include('filament.pages.partials.sync-status', ['status' => $listmonk])
<x-filament::button
wire:click="mountAction('syncListmonk')"
:disabled="in_array($listmonk['status'], ['pending', 'running'])"
>
{{ __('synchronisations.action.submit') }}
</x-filament::button>
</div>
</x-filament::section>
<x-filament::section> <x-filament::section>
<x-slot name="heading"> <x-slot name="heading">
@include('filament.pages.partials.sync-heading', [ @include('filament.pages.partials.sync-heading', [