Fix media sync with arcadia on update only if linked, check before link that is not already linked.
This commit is contained in:
parent
e7f5a7e6cd
commit
ba2b9dcde2
5 changed files with 27 additions and 18 deletions
|
|
@ -634,6 +634,12 @@ class MediaController extends Controller
|
|||
|
||||
$user->authorizeRoles([config('roles.const.ADMIN')]);
|
||||
|
||||
if ($media->arcadia_id) {
|
||||
throw ValidationException::withMessages([
|
||||
'media' => trans('Ce média est déjà lié à arcadia')
|
||||
]);
|
||||
}
|
||||
|
||||
MediaLinkWithArcadia::dispatch($user, $media);
|
||||
|
||||
return response()->json(true);
|
||||
|
|
|
|||
|
|
@ -2,10 +2,8 @@
|
|||
|
||||
namespace App\Listeners;
|
||||
|
||||
use App\Enums\EventHistoryType;
|
||||
use App\Events\MediaLinkWithArcadia;
|
||||
use App\Lib\ArcadiaLinkUtilities;
|
||||
use App\Models\Historique;
|
||||
|
||||
class CreateIntoArcadia
|
||||
{
|
||||
|
|
@ -18,19 +16,11 @@ class CreateIntoArcadia
|
|||
*/
|
||||
public function handle(MediaLinkWithArcadia $event)
|
||||
{
|
||||
$user = $event->getUser();
|
||||
$media = $event->getMedia();
|
||||
|
||||
$historique = new Historique();
|
||||
$historique->type = EventHistoryType::changed;
|
||||
$historique->description = trans('Ajout de la liaison Arcadia');
|
||||
$historique->media()->associate($media);
|
||||
$historique->user()->associate($user);
|
||||
|
||||
// * Link To arcadia
|
||||
if (!ArcadiaLinkUtilities::create($media)) {
|
||||
throw new \RuntimeException('Failed to link media with Arcadia');
|
||||
}
|
||||
$historique->saveOrFail();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ use App\Enums\EventHistoryType;
|
|||
use App\Events\MediaChangedLabel;
|
||||
use App\Events\MediaCreated;
|
||||
use App\Events\MediaDestroyed;
|
||||
use App\Events\MediaLinkWithArcadia;
|
||||
use App\Events\MediaMoved;
|
||||
use App\Events\MediaUpdated;
|
||||
use App\Models\Historique;
|
||||
|
|
@ -15,11 +16,11 @@ class HistorizeMediaEvent
|
|||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param \App\Events\MediaCreated $event
|
||||
* @param \App\Events\MediaCreated|\App\Events\MediaLinkWithArcadia $event
|
||||
* @return void
|
||||
* @throws \RuntimeException If saving historic to database failed.
|
||||
*/
|
||||
public function handle(MediaCreated $event)
|
||||
public function handle(MediaCreated | MediaLinkWithArcadia $event)
|
||||
{
|
||||
$historique = new Historique();
|
||||
$type = EventHistoryType::created;
|
||||
|
|
@ -48,6 +49,10 @@ class HistorizeMediaEvent
|
|||
\optional($event->getNewLabel())->libelle ?? trans('aucune')
|
||||
);
|
||||
}
|
||||
if ($event instanceof MediaLinkWithArcadia) {
|
||||
$type = EventHistoryType::changed;
|
||||
$description = trans('Ajout de la liaison Arcadia');
|
||||
}
|
||||
|
||||
$historique->user()->associate($event->getUser());
|
||||
$historique->media()->associate($event->getMedia());
|
||||
|
|
|
|||
|
|
@ -33,18 +33,20 @@ class SyncWithArcadia
|
|||
|
||||
switch (true) {
|
||||
case $event instanceof MediaUpdated:
|
||||
// ! Le média n'est pas un brouillon.
|
||||
if ($event->getMedia()->statut !== MediaStatut::brouillon) {
|
||||
// ! Le média n'est pas un brouillon et est lié à arcadia
|
||||
if (
|
||||
$event->getMedia()->statut !== MediaStatut::brouillon and
|
||||
$event->getMedia()->arcadia_id
|
||||
) {
|
||||
$succeed = ArcadiaLinkUtilities::update($event->getMedia());
|
||||
} else {
|
||||
$succeed = true;
|
||||
}
|
||||
break;
|
||||
case $event instanceof MediaDestroyed:
|
||||
$succeed = ArcadiaLinkUtilities::delete($event->getMedia());
|
||||
break;
|
||||
case $event instanceof MediaCreated:
|
||||
$succeed = ArcadiaLinkUtilities::create($event->getMedia());
|
||||
// ! Le média est lié à arcadia
|
||||
$succeed = $event->getMedia()->arcadia_id ?
|
||||
ArcadiaLinkUtilities::delete($event->getMedia()) : true;
|
||||
break;
|
||||
default:
|
||||
throw new \ErrorException('Unhandled event ' . \get_class($event));
|
||||
|
|
|
|||
|
|
@ -6,9 +6,11 @@ use App\Events\MediaChangedLabel;
|
|||
use App\Events\MediaCreated;
|
||||
use App\Events\MediaUpdated;
|
||||
use App\Events\MediaDestroyed;
|
||||
use App\Events\MediaLinkWithArcadia;
|
||||
use App\Events\MediaMoved;
|
||||
use App\Events\MediaSearched;
|
||||
use App\Listeners\CleanUserOldSearches;
|
||||
use App\Listeners\CreateIntoArcadia;
|
||||
use App\Listeners\SyncWithArcadia;
|
||||
use App\Listeners\HistorizeMediaEvent;
|
||||
use App\Listeners\SaveUserSearch;
|
||||
|
|
@ -27,6 +29,10 @@ class EventServiceProvider extends ServiceProvider
|
|||
Registered::class => [
|
||||
SendEmailVerificationNotification::class,
|
||||
],
|
||||
MediaLinkWithArcadia::class => [
|
||||
CreateIntoArcadia::class,
|
||||
HistorizeMediaEvent::class
|
||||
],
|
||||
MediaSearched::class => [
|
||||
SaveUserSearch::class,
|
||||
CleanUserOldSearches::class
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue