Aimeos Autofill plugin conflict with jsonapi service update

Help for integrating the Laravel package
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
kdim95
Advanced
Posts: 205
Joined: 26 Aug 2022, 12:17

Aimeos Autofill plugin conflict with jsonapi service update

Post by kdim95 » 25 Sep 2023, 10:13

Laravel framework version: 10.16.1
Aimeos Laravel version: 2023.04.*
PHP Version: 8.2.8
Environment: Linux

Hello,

There is a conflict between the built-in autofill plugin and the jsonapi.
If a service is updated (changed) through the jsonapi, the autofill plugin prevents the service from being changed.
I think this is because the service gets removed before getting replaced with a different service, causing it to be re-added by the autofill plugin.

Can you look into this?

Thanks!

User avatar
aimeos
Administrator
Posts: 7894
Joined: 01 Jan 1970, 00:00

Re: Aimeos Autofill plugin conflict with jsonapi service update

Post by aimeos » 26 Sep 2023, 16:35

We've made a little change in dev-master and 2023.07.x-dev to be able to replace existing service options even if the Autofill plugin is active. Can you give it a try?
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

kdim95
Advanced
Posts: 205
Joined: 26 Aug 2022, 12:17

Re: Aimeos Autofill plugin conflict with jsonapi service update

Post by kdim95 » 26 Sep 2023, 21:20

I can't update from 2023.04.*, because it breaks changes I've done in the backend.
You've been changing the backend structure to add Vue JS support.
It will take some time before I understand how you've changed the backend before I can update it and implement my changes, and I'm working on other things right now.
Can you tell me what exactly you changed to make it work?

User avatar
aimeos
Administrator
Posts: 7894
Joined: 01 Jan 1970, 00:00

Re: Aimeos Autofill plugin conflict with jsonapi service update

Post by aimeos » 27 Sep 2023, 06:50

Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

kdim95
Advanced
Posts: 205
Joined: 26 Aug 2022, 12:17

Re: Aimeos Autofill plugin conflict with jsonapi service update

Post by kdim95 » 27 Sep 2023, 09:06

Yes, that seems to work.

https://aimeos.org/docs/2023.x/config/c ... et/#name_4

Code: Select all

'client' => [
	'jsonapi' => [
		'basket' => [
			'service' => [
				'name' => 'StandardCustom',
			]
		]
	],
],

Code: Select all

<?php

namespace Aimeos\Client\JsonApi\Basket\Service;

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

class StandardCustom extends Standard
{
    private \Aimeos\Controller\Frontend\Basket\Iface $controller;

    /**
	 * Initializes the client
	 *
	 * @param \Aimeos\MShop\ContextIface $context MShop context object
	 */
	public function __construct( \Aimeos\MShop\ContextIface $context )
	{
		parent::__construct( $context );

		$this->controller = \Aimeos\Controller\Frontend::create( $this->context(), 'basket' );
	}

    /**
	 * Updates the resource or the resource list partitially
	 *
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
	 */
	public function patch( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
	{
		$view = $this->view();

		try
		{
			$this->clearCache();
			$this->controller->setType( $view->param( 'id', 'default' ) );

			$body = (string) $request->getBody();

			if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) {
				throw new \Aimeos\Client\JsonApi\Exception( 'Invalid JSON in body', 400 );
			}

			if( !is_array( $payload->data ) ) {
				$payload->data = [$payload->data];
			}

			$cntl = \Aimeos\Controller\Frontend::create( $this->context(), 'service' );

			if( $type = $view->param( 'relatedid' ) ) {
				$this->controller->deleteService( $type );
			}

			foreach( $payload->data as $pos => $entry )
			{
				if( !isset( $entry->id ) ) {
					throw new \Aimeos\Client\JsonApi\Exception( 'Service type (ID) is missing', 400 );
				}

				if( !isset( $entry->attributes ) ) {
					throw new \Aimeos\Client\JsonApi\Exception( 'Service attributes are missing', 400 );
				}

				if( !isset( $entry->attributes->{'service.id'} ) ) {
					throw new \Aimeos\Client\JsonApi\Exception( 'Service ID in attributes is missing', 400 );
				}

				$item = $cntl->uses( ['media', 'price', 'text'] )->get( $entry->attributes->{'service.id'} );
				unset( $entry->attributes->{'service.id'} );

				$this->controller->addService( $item, (array) $entry->attributes, $pos );
			}


			$view->item = $this->controller->get();
			$status = 200;
		}
		catch( \Aimeos\MShop\Plugin\Provider\Exception $e )
		{
			$status = 409;
			$errors = $this->translatePluginErrorCodes( $e->getErrorCodes() );
			$view->errors = $this->getErrorDetails( $e, 'mshop' ) + $errors;
		}
		catch( \Aimeos\MShop\Exception $e )
		{
			$status = 404;
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
		}
		catch( \Exception $e )
		{
			$status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500;
			$view->errors = $this->getErrorDetails( $e );
		}

		return $this->render( $response, $view, $status );
	}
}

kdim95
Advanced
Posts: 205
Joined: 26 Aug 2022, 12:17

Re: Aimeos Autofill plugin conflict with jsonapi service update

Post by kdim95 » 19 Oct 2023, 11:49

There is another problem.
Now the notifications get called multiple times for the same service.
'addService.after' and 'addService.before' get called twice when the service is updated.
The first time notification occurs, is when the service is added with its default values by the AutoFill plugin.
The second time is with my updated values of the service.
That makes it impossible to track changes in the service through my plugin, because every time it's the default value.

kdim95
Advanced
Posts: 205
Joined: 26 Aug 2022, 12:17

Re: Aimeos Autofill plugin conflict with jsonapi service update

Post by kdim95 » 19 Oct 2023, 13:51

I found a solution.

I added a bool parameter to every method that sends notifications in this class:
Aimeos\MShop\Order\Item\Base

The AutoFill plugin should set that bool parameter to false when calling deleteService(), addService(), etc.

User avatar
aimeos
Administrator
Posts: 7894
Joined: 01 Jan 1970, 00:00

Re: Aimeos Autofill plugin conflict with jsonapi service update

Post by aimeos » 23 Oct 2023, 08:05

kdim95 wrote: 19 Oct 2023, 11:49 Now the notifications get called multiple times for the same service.
'addService.after' and 'addService.before' get called twice when the service is updated.
This is a side effect of the event system which calls the plugins whenever the basket is modified.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply