feat(Reminder process): add 4 reminder phases for membership (Retzien)
All checks were successful
Deploy Roxane to Preprod / deploy (push) Successful in 1m21s
All checks were successful
Deploy Roxane to Preprod / deploy (push) Successful in 1m21s
This commit is contained in:
@@ -9,9 +9,13 @@ use Illuminate\Support\Facades\Http;
|
||||
class DolibarrService
|
||||
{
|
||||
protected string $baseUrl;
|
||||
|
||||
protected string $htaccessUrl;
|
||||
|
||||
protected string $username;
|
||||
|
||||
protected string $password;
|
||||
|
||||
protected string $apiKey;
|
||||
|
||||
public function __construct()
|
||||
@@ -20,7 +24,7 @@ class DolibarrService
|
||||
$this->htaccessUrl = config('services.dolibarr.htaccess_url');
|
||||
$this->username = config('services.dolibarr.username');
|
||||
$this->password = config('services.dolibarr.password');
|
||||
$this->apiKey = config('services.dolibarr.api_key');
|
||||
$this->apiKey = config('services.dolibarr.api_key');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -30,21 +34,22 @@ class DolibarrService
|
||||
{
|
||||
return Http::withBasicAuth($this->username, $this->password)
|
||||
->withHeaders([
|
||||
'Accept' => 'application/json',
|
||||
'DOLAPIKEY' => $this->apiKey,
|
||||
'Accept' => 'application/json',
|
||||
'DOLAPIKEY' => $this->apiKey,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all members
|
||||
*
|
||||
* @throws ConnectionException
|
||||
*/
|
||||
public function getAllMembers(int $limit = 400, string $sortField = 't.rowid', string $sortOrder = 'ASC'): array
|
||||
{
|
||||
$response = $this->client()->get($this->baseUrl . '/members', [
|
||||
$response = $this->client()->get($this->baseUrl.'/members', [
|
||||
'sortfield' => $sortField,
|
||||
'sortorder' => $sortOrder,
|
||||
'limit' => $limit,
|
||||
'limit' => $limit,
|
||||
]);
|
||||
|
||||
return $response->json();
|
||||
@@ -52,11 +57,12 @@ class DolibarrService
|
||||
|
||||
/**
|
||||
* Get member subscriptions
|
||||
*
|
||||
* @throws ConnectionException
|
||||
*/
|
||||
public function getMemberSubscriptions(int|string $id): array
|
||||
{
|
||||
$response = $this->client()->get($this->baseUrl . '/members/'. $id . '/subscriptions');
|
||||
$response = $this->client()->get($this->baseUrl.'/members/'.$id.'/subscriptions');
|
||||
|
||||
return $response->json();
|
||||
}
|
||||
@@ -64,19 +70,18 @@ class DolibarrService
|
||||
/**
|
||||
* Update a member with custom data
|
||||
*
|
||||
* @param int|string $id The Dolibarr member ID (rowid)
|
||||
* @param array $data Array of attributes to update (e.g. ['email' => 'new@email.com', 'array_options' => ['options_custom' => 'val']])
|
||||
* @param int|string $id The Dolibarr member ID (rowid)
|
||||
* @param array $data Array of attributes to update (e.g. ['email' => 'new@email.com', 'array_options' => ['options_custom' => 'val']])
|
||||
*
|
||||
* @throws ConnectionException
|
||||
*/
|
||||
public function updateMember(int|string $id, array $data): bool
|
||||
{
|
||||
$response = $this->client()->put(
|
||||
$this->baseUrl . '/members/' . $id,
|
||||
$this->baseUrl.'/members/'.$id,
|
||||
$data
|
||||
);
|
||||
|
||||
return $response->successful();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -18,9 +18,9 @@ class ISPConfigMailService extends ISPConfigService
|
||||
public function getAllMailDomains(): array
|
||||
{
|
||||
return Cache::remember(
|
||||
"ispconfig.mail.domains.all",
|
||||
'ispconfig.mail.domains.all',
|
||||
config('services.ispconfig.cache_ttl'),
|
||||
fn() => $this->call('mail_domain_get', ['primary_id' => -1])
|
||||
fn () => $this->call('mail_domain_get', ['primary_id' => -1])
|
||||
);
|
||||
}
|
||||
|
||||
@@ -30,9 +30,9 @@ class ISPConfigMailService extends ISPConfigService
|
||||
public function getAllMailUsers(): array
|
||||
{
|
||||
return Cache::remember(
|
||||
"ispconfig.mail.users.all",
|
||||
'ispconfig.mail.users.all',
|
||||
config('services.ispconfig.cache_ttl'),
|
||||
fn() => $this->call('mail_user_get', ['primary_id' => -1])
|
||||
fn () => $this->call('mail_user_get', ['primary_id' => -1])
|
||||
);
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ class ISPConfigMailService extends ISPConfigService
|
||||
$allUsers = $this->getAllMailUsers();
|
||||
|
||||
return collect($allUsers)->filter(function ($user) use ($domain) {
|
||||
return str_ends_with($user['email'], '@' . $domain);
|
||||
return str_ends_with($user['email'], '@'.$domain);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ class ISPConfigMailService extends ISPConfigService
|
||||
|
||||
$user = collect($allUsers)->firstWhere('email', $email);
|
||||
|
||||
if (!$user) {
|
||||
if (! $user) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -97,7 +97,7 @@ class ISPConfigMailService extends ISPConfigService
|
||||
$user = collect($this->getAllMailUsers())
|
||||
->firstWhere('email', $email);
|
||||
|
||||
if (!$user) {
|
||||
if (! $user) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -105,10 +105,10 @@ class ISPConfigMailService extends ISPConfigService
|
||||
|
||||
// On récupère l'enregistrement COMPLET (OBLIGATOIRE)
|
||||
$mailUserRecord = $this->call('mail_user_get', [
|
||||
$mailuserId
|
||||
$mailuserId,
|
||||
]);
|
||||
|
||||
if (!is_array($mailUserRecord)) {
|
||||
if (! is_array($mailUserRecord)) {
|
||||
throw new \RuntimeException('mail_user_get did not return array');
|
||||
}
|
||||
|
||||
@@ -121,11 +121,9 @@ class ISPConfigMailService extends ISPConfigService
|
||||
$result = $this->call('mail_user_update', [
|
||||
0, // client_id (ADMIN)
|
||||
$mailuserId, // primary_id
|
||||
$mailUserRecord // FULL RECORD
|
||||
$mailUserRecord, // FULL RECORD
|
||||
]);
|
||||
|
||||
return (bool) $result;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -4,15 +4,18 @@ namespace App\Services\ISPConfig;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use SoapClient;
|
||||
use SoapFault;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class ISPConfigService
|
||||
{
|
||||
protected ?SoapClient $client = null;
|
||||
|
||||
protected ?string $sessionId = null;
|
||||
|
||||
protected array $config;
|
||||
|
||||
protected string $serverType;
|
||||
|
||||
/**
|
||||
@@ -23,7 +26,7 @@ class ISPConfigService
|
||||
$this->serverType = $serverType;
|
||||
$this->config = config("services.ispconfig.servers.{$serverType}");
|
||||
|
||||
if (!$this->config) {
|
||||
if (! $this->config) {
|
||||
throw new Exception("ISPConfig server configuration not found for: {$serverType}");
|
||||
}
|
||||
}
|
||||
@@ -44,8 +47,8 @@ class ISPConfigService
|
||||
'ssl' => [
|
||||
'verify_peer' => false,
|
||||
'verify_peer_name' => false,
|
||||
]
|
||||
])
|
||||
],
|
||||
]),
|
||||
]);
|
||||
|
||||
$this->sessionId = $this->client->login(
|
||||
@@ -53,17 +56,17 @@ class ISPConfigService
|
||||
$this->config['password']
|
||||
);
|
||||
|
||||
Log::info("ISPConfig connected", [
|
||||
Log::info('ISPConfig connected', [
|
||||
'server' => $this->serverType,
|
||||
'session_id' => $this->sessionId
|
||||
'session_id' => $this->sessionId,
|
||||
]);
|
||||
|
||||
} catch (SoapFault $e) {
|
||||
Log::error("ISPConfig connection failed", [
|
||||
Log::error('ISPConfig connection failed', [
|
||||
'server' => $this->serverType,
|
||||
'error' => $e->getMessage()
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
throw new Exception("Failed to connect to ISPConfig: " . $e->getMessage());
|
||||
throw new Exception('Failed to connect to ISPConfig: '.$e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,11 +75,11 @@ class ISPConfigService
|
||||
if ($this->client && $this->sessionId) {
|
||||
try {
|
||||
$this->client->logout($this->sessionId);
|
||||
Log::info("ISPConfig disconnected", ['server' => $this->serverType]);
|
||||
Log::info('ISPConfig disconnected', ['server' => $this->serverType]);
|
||||
} catch (SoapFault $e) {
|
||||
Log::warning("ISPConfig logout failed", [
|
||||
Log::warning('ISPConfig logout failed', [
|
||||
'server' => $this->serverType,
|
||||
'error' => $e->getMessage()
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -97,21 +100,21 @@ class ISPConfigService
|
||||
|
||||
$result = $this->client->__soapCall($method, $params);
|
||||
|
||||
Log::debug("ISPConfig API call", [
|
||||
Log::debug('ISPConfig API call', [
|
||||
'method' => $method,
|
||||
'server' => $this->serverType,
|
||||
'success' => true
|
||||
'success' => true,
|
||||
]);
|
||||
|
||||
return $result;
|
||||
|
||||
} catch (SoapFault $e) {
|
||||
Log::error("ISPConfig API call failed", [
|
||||
Log::error('ISPConfig API call failed', [
|
||||
'method' => $method,
|
||||
'server' => $this->serverType,
|
||||
'error' => $e->getMessage()
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
throw new Exception("ISPConfig API call failed: " . $e->getMessage());
|
||||
throw new Exception('ISPConfig API call failed: '.$e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,9 +129,9 @@ class ISPConfigService
|
||||
public function getAllClients(): array
|
||||
{
|
||||
return Cache::remember(
|
||||
"ispconfig.mail.clients.all",
|
||||
'ispconfig.mail.clients.all',
|
||||
config('services.ispconfig.cache_ttl'),
|
||||
fn() => $this->call('client_get_all')
|
||||
fn () => $this->call('client_get_all')
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
152
app/Services/MemberRenewalService.php
Normal file
152
app/Services/MemberRenewalService.php
Normal file
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Member;
|
||||
use App\Models\MemberRenewalReminder;
|
||||
use App\Notifications\MemberAccountDeactivatedNotification;
|
||||
use App\Notifications\MemberRenewalReminder2Notification;
|
||||
use App\Notifications\MemberRenewalReminder3Notification;
|
||||
use App\Notifications\MemberRenewalReminderNotification;
|
||||
use App\Services\Dolibarr\DolibarrService;
|
||||
|
||||
class MemberRenewalService
|
||||
{
|
||||
public function __construct(
|
||||
protected DolibarrService $dolibarr,
|
||||
protected MemberService $memberService
|
||||
) {}
|
||||
|
||||
public function sendReminder1(Member $member, int $year): bool
|
||||
{
|
||||
$reminder = MemberRenewalReminder::firstOrCreate(
|
||||
['member_id' => $member->id, 'renewal_year' => $year],
|
||||
);
|
||||
|
||||
if ($reminder->reminder_1_sent) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->hasActiveMembershipForYear($member, $year)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $this->shouldSendRenewalForYear($member, $year)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$member->notify(new MemberRenewalReminderNotification($member));
|
||||
|
||||
$reminder->markAsReminder1Sent();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function sendReminder2(Member $member, int $year): bool
|
||||
{
|
||||
$reminder = MemberRenewalReminder::firstOrCreate(
|
||||
['member_id' => $member->id, 'renewal_year' => $year],
|
||||
);
|
||||
|
||||
if ($reminder->reminder_2_sent) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->hasActiveMembershipForYear($member, $year)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $this->shouldSendRenewalForYear($member, $year)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$member->notify(new MemberRenewalReminder2Notification($member));
|
||||
|
||||
$reminder->markAsReminder2Sent();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function sendReminder3(Member $member, int $year): bool
|
||||
{
|
||||
$reminder = MemberRenewalReminder::firstOrCreate(
|
||||
['member_id' => $member->id, 'renewal_year' => $year],
|
||||
);
|
||||
|
||||
if ($reminder->reminder_3_sent) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->hasActiveMembershipForYear($member, $year)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $this->shouldSendRenewalForYear($member, $year)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$member->notify(new MemberRenewalReminder3Notification($member));
|
||||
|
||||
$reminder->markAsReminder3Sent();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function deactivateMember(Member $member, int $year): bool
|
||||
{
|
||||
$reminder = MemberRenewalReminder::firstOrCreate(
|
||||
['member_id' => $member->id, 'renewal_year' => $year],
|
||||
);
|
||||
|
||||
if ($reminder->deactivation_sent) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->hasActiveMembershipForYear($member, $year)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->memberService->deactivateMember($member);
|
||||
|
||||
$member->notify(new MemberAccountDeactivatedNotification($member));
|
||||
|
||||
$reminder->markAsDeactivated();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function hasActiveMembershipForYear(Member $member, int $year): bool
|
||||
{
|
||||
$yearStart = "{$year}-01-01";
|
||||
$yearEnd = "{$year}-12-31";
|
||||
|
||||
return $member->memberships()
|
||||
->where('status', 'active')
|
||||
->where(function ($query) use ($yearStart, $yearEnd) {
|
||||
$query->where(function ($q) use ($yearStart, $yearEnd) {
|
||||
$q->where('start_date', '<=', $yearEnd)
|
||||
->where(function ($q2) use ($yearStart) {
|
||||
$q2->whereNull('end_date')
|
||||
->orWhere('end_date', '>=', $yearStart);
|
||||
});
|
||||
});
|
||||
})
|
||||
->exists();
|
||||
}
|
||||
|
||||
protected function shouldSendRenewalForYear(Member $member, int $year): bool
|
||||
{
|
||||
$currentMembership = $member->memberships()
|
||||
->where('status', 'active')
|
||||
->orderBy('end_date', 'desc')
|
||||
->first();
|
||||
|
||||
if (! $currentMembership || ! $currentMembership->end_date) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$expirationYear = (int) date('Y', strtotime($currentMembership->end_date));
|
||||
|
||||
return $expirationYear === ($year - 1);
|
||||
}
|
||||
}
|
||||
@@ -22,31 +22,34 @@ class NextcloudService
|
||||
'OCS-APIRequest' => 'true',
|
||||
'Accept' => 'application/json',
|
||||
])
|
||||
->baseUrl(config('services.nextcloud.url') . '/ocs/v1.php');
|
||||
->baseUrl(config('services.nextcloud.url').'/ocs/v1.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable user by email
|
||||
*
|
||||
* @throws ConnectionException
|
||||
*/
|
||||
public function disableUserByEmail(string $email): void
|
||||
{
|
||||
$userId = $this->findUserIdByEmail($email);
|
||||
|
||||
if (!$userId) {
|
||||
Log::warning("Utilisateur Nextcloud introuvable", ['email' => $email]);
|
||||
if (! $userId) {
|
||||
Log::warning('Utilisateur Nextcloud introuvable', ['email' => $email]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$response = $this->http->put("/cloud/users/{$userId}/disable");
|
||||
|
||||
if (!$response->successful()) {
|
||||
if (! $response->successful()) {
|
||||
throw new \RuntimeException("Erreur désactivation Nextcloud {$userId}");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Desable user by id
|
||||
*
|
||||
* @throws ConnectionException
|
||||
*/
|
||||
public function disableUserById(string $userId): void
|
||||
@@ -60,7 +63,7 @@ class NextcloudService
|
||||
protected function findUserIdByEmail(string $email): ?string
|
||||
{
|
||||
return Cache::remember(
|
||||
'nextcloud.user_id.' . md5($email),
|
||||
'nextcloud.user_id.'.md5($email),
|
||||
now()->addDays(7),
|
||||
function () use ($email) {
|
||||
return $this->resolveUserIdByEmail($email);
|
||||
@@ -110,6 +113,4 @@ class NextcloudService
|
||||
->get("/cloud/users/{$userId}")
|
||||
->json('ocs.data') ?? [];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user