Skip to content

Commit

Permalink
make new NotificationService (not tested)
Browse files Browse the repository at this point in the history
  • Loading branch information
temaotl committed Jul 12, 2024
1 parent 87250fe commit 7eac6ef
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 13 deletions.
13 changes: 7 additions & 6 deletions app/Http/Controllers/EntityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use App\Jobs\GitAddToCategory;
use App\Jobs\GitAddToEdugain;
use App\Jobs\GitAddToHfd;
use App\Jobs\GitAddToRs;
use App\Jobs\GitDeleteFromCategory;
use App\Jobs\GitDeleteFromEdugain;
use App\Jobs\GitDeleteFromHfd;
Expand Down Expand Up @@ -271,7 +270,7 @@ public function update(Request $request, Entity $entity)
->with('status', __('entities.not_changed'));
}

// Make update with job
// TODO notification
/* Bus::chain([
new GitUpdateEntity($entity, Auth::user()),
function () use ($entity) {
Expand Down Expand Up @@ -474,15 +473,16 @@ function () use ($entity) {
$status = $entity->rs ? 'rs' : 'no_rs';
$color = $entity->rs ? 'green' : 'red';

if ($entity->rs) {
// TODO notification
/* if ($entity->rs) {
GitAddToRs::dispatch($entity, Auth::user());
Notification::send($entity->operators, new EntityAddedToRs($entity));
Notification::send(User::activeAdmins()->select('id', 'email')->get(), new EntityAddedToRs($entity));
} else {
GitDeleteFromRs::dispatch($entity, Auth::user());
Notification::send($entity->operators, new EntityDeletedFromRs($entity));
Notification::send(User::activeAdmins()->select('id', 'email')->get(), new EntityDeletedFromRs($entity));
}
}*/

return redirect()
->back()
Expand Down Expand Up @@ -542,7 +542,8 @@ function () use ($entity, $category) {
$status = $entity->hfd ? 'hfd' : 'no_hfd';
$color = $entity->hfd ? 'red' : 'green';

if ($entity->hfd) {
//TODO change HfD status
/* if ($entity->hfd) {
GitAddToHfd::dispatch($entity, Auth::user());
Notification::send($entity->operators, new EntityAddedToHfd($entity));
Notification::send(User::activeAdmins()->select('id', 'email')->get(), new EntityAddedToHfd($entity));
Expand All @@ -551,7 +552,7 @@ function () use ($entity, $category) {
Mail::to(config('mail.ra.address'))->send(new NewIdentityProvider($entity));
Notification::send($entity->operators, new EntityDeletedFromHfd($entity));
Notification::send(User::activeAdmins()->select('id', 'email')->get(), new EntityDeletedFromHfd($entity));
}
}*/

return redirect()
->route('entities.show', $entity)
Expand Down
12 changes: 12 additions & 0 deletions app/Jobs/FolderAddEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use App\Models\Entity;
use App\Models\Federation;
use App\Models\Membership;
use App\Notifications\EntityStateChanged;
use App\Notifications\EntityUpdated;
use App\Services\EntityService;
use App\Services\NotificationService;
use App\Traits\HandlesJobsFailuresTrait;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand Down Expand Up @@ -66,6 +70,14 @@ public function handle(): void
try {
$lock->block(120);
EntityFacade::saveMetadataToFederationFolder($this->entity->id, $fedId->federation_id);


/* if ($this->entity->wasRecentlyCreated) {
NotificationService::sendEntityNotification($this->entity,EntityUpdated::class);
} elseif ($this->entity->wasChanged('deleted_at') && is_null($this->entity->deleted_at)) {
NotificationService::sendEntityNotification($this->entity,EntityStateChanged::class);
}*/

RunMdaScript::dispatch($federation, $lock->owner());
} catch (Exception $e) {
Log::error($e->getMessage());
Expand Down
11 changes: 11 additions & 0 deletions app/Jobs/FolderDeleteEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

use App\Facades\EntityFacade;
use App\Models\Entity;
use App\Notifications\EntityDeletedFromHfd;
use App\Notifications\EntityStateChanged;
use App\Services\EntityService;
use App\Services\NotificationService;
use App\Traits\HandlesJobsFailuresTrait;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand Down Expand Up @@ -53,6 +57,13 @@ public function handle(): void
try {
$lock->block(120);
EntityFacade::deleteEntityMetadataFromFolder($entity->file, $federation->xml_id);

NotificationService::sendEntityNotification($entity,EntityStateChanged::class);
if ($entity->hfd) {
NotificationService::sendEntityNotification($entity,EntityDeletedFromHfd::class);
}


RunMdaScript::dispatch($federation, $lock->owner());
} catch (Exception $e) {
Log::error($e->getMessage());
Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/GitAddToRs.php → app/Jobs/Old_GitAddToRs.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Illuminate\Support\Facades\Storage;
use Throwable;

class GitAddToRs implements ShouldQueue
class Old_GitAddToRs implements ShouldQueue
{
use Dispatchable, GitTrait, InteractsWithQueue, Queueable, SerializesModels;

Expand Down
19 changes: 15 additions & 4 deletions app/Listeners/SendUpdatedEntityToSaveJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

use App\Events\UpdateEntity;
use App\Jobs\FolderAddEntity;
use App\Models\User;
use App\Notifications\EntityUpdated;
use App\Services\NotificationService;
use Illuminate\Support\Facades\Notification;

class SendUpdatedEntityToSaveJob
{
Expand All @@ -21,12 +25,19 @@ public function __construct()
public function handle(UpdateEntity $event): void
{

$ent = $event->entity;
$entity = $event->entity;

if ($ent->wasChanged('xml_file') ||
($ent->wasChanged('approved') && $ent->approved == 1)
) {


if ($entity->wasChanged('xml_file') ||
($entity->wasChanged('approved') && $entity->approved == 1)
)
{
FolderAddEntity::dispatch($event->entity);
}
elseif ($entity->approved == 1)
{
NotificationService::sendEntityNotification($entity,EntityUpdated::class);
}
}
}
4 changes: 2 additions & 2 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ public function boot()
Model::preventLazyLoading();
}

RateLimiter::for('mda-run-limit', function (RunMdaScript $job) {
/* RateLimiter::for('mda-run-limit', function (RunMdaScript $job) {
$diskName = config('storageCfg.name');
$pathToDirectory = Storage::disk($diskName)->path($job->federation->name);
$lockKey = 'directory-'.md5($pathToDirectory).'-lock';
return Limit::perMinute(1)->by($lockKey);
});
});*/

}
}
16 changes: 16 additions & 0 deletions app/Services/NotificationService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace App\Services;
use App\Models\Entity;
use App\Models\User;
use Illuminate\Support\Facades\Notification;


class NotificationService{

public static function sendEntityNotification(Entity $entity,$notification){
$admins = User::activeAdmins()->select('id', 'email')->get();
Notification::sendNow($entity->operators, new $notification($entity));
Notification::sendNow($admins, new $notification($entity));
}

}

0 comments on commit 7eac6ef

Please sign in to comment.