Skip to content

Commit

Permalink
feat: WIP delete insurance product if insurance config is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin-Freoua-Alma committed Sep 27, 2024
1 parent e9307ba commit 219c93f
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 8 deletions.
22 changes: 18 additions & 4 deletions alma/controllers/admin/AdminAlmaInsuranceConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

use Alma\PrestaShop\Builders\Admin\InsuranceHelperBuilder;
use Alma\PrestaShop\Builders\Services\InsuranceProductServiceBuilder;
use Alma\PrestaShop\Exceptions\InsuranceProductException;
use Alma\PrestaShop\Helpers\Admin\AdminInsuranceHelper;
use Alma\PrestaShop\Helpers\ConfigurationHelper;
use Alma\PrestaShop\Helpers\ConstantsHelper;
Expand Down Expand Up @@ -96,19 +97,32 @@ public function ajaxProcessSaveConfigInsurance()
{
try {
$config = Tools::getValue('config');
$savedInsuranceStatus = $config[AdminInsuranceHelper::$fieldsDbInsuranceToIframeParamNames[ConstantsHelper::ALMA_ACTIVATE_INSURANCE]];

$this->insuranceHelper->saveConfigInsurance($config);
$this->insuranceProductService->handleInsuranceProductState(
$config[AdminInsuranceHelper::$fieldsDbInsuranceToIframeParamNames[ConstantsHelper::ALMA_ACTIVATE_INSURANCE]]
);
$this->insuranceProductService->handleInsuranceProductState($savedInsuranceStatus);

$this->insuranceProductService->removeInsuranceProductsNotOrdered();
if ($savedInsuranceStatus === 'false') {
$this->insuranceProductService->removeInsuranceProductsNotOrdered();
}

$this->ajaxRenderAndExit(json_encode([
'success' => true,
'message' => $this->module->l('Your configuration has been saved', 'AdminAlmaInsuranceConfiguration'),
])
);
} catch (InsuranceProductException $e) {
Logger::instance()->error('Error insurance product during change configuration: ' . $e->getMessage());
$this->ajaxRenderAndExit(json_encode([
'error' => [
'msg' => sprintf(
$this->module->l('Error insurance product during change configuration: %1$s', 'AdminAlmaInsuranceConfiguration'),
$e->getMessage()
),
'code' => $e->getCode(),
],
])
);
} catch (\Exception $e) {
Logger::instance()->error('Error creating Alma configuration insurance: ' . $e->getMessage());
$this->ajaxRenderAndExit(json_encode([
Expand Down
10 changes: 8 additions & 2 deletions alma/lib/Services/InsuranceProductService.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,8 @@ public function handleInsuranceProductState($state)
/**
* @return false|void
*
* @throws \PrestaShopDatabaseException
* @throws InsuranceProductException
* @throws \PrestaShopDatabaseException
*/
public function removeInsuranceProductsNotOrdered()
{
Expand All @@ -470,8 +470,14 @@ public function removeInsuranceProductsNotOrdered()
try {
$this->cartService->deleteProductByCartId($insuranceProductId, $cartId['id_cart']);
} catch (CartException $e) {
throw new InsuranceProductException("[Alma] Error while removing product id : {$insuranceProductId} in cart id : {$cartId}");
throw new InsuranceProductException("[Alma] Error while removing product id : {$insuranceProductId} in cart id : {$cartId['id_cart']}");
}
}
try {
$arrayCartIds = array_column($cartIds, 'id_cart');
$this->almaInsuranceProductRepository->deleteAssociationsByCartIds(implode(', ', $arrayCartIds));
} catch (\PrestaShopDatabaseException $e) {
throw new InsuranceProductException(sprintf('[Alma]: Error while removing association with insurance product. Cart ids : %s', json_encode($cartIds)), 0, $e);
}
}
}
1 change: 0 additions & 1 deletion alma/tests/Unit/Services/CartServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ public function testDeleteProductWithoutCartId()

/**
* @throws CartException
* @ignore Test return warning TODO: Need to improve
*/
public function testDeleteProductWithProductIdAndCartId()
{
Expand Down
70 changes: 69 additions & 1 deletion alma/tests/Unit/Services/InsuranceProductServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

namespace Alma\PrestaShop\Tests\Unit\Services;

use Alma\PrestaShop\Exceptions\CartException;
use Alma\PrestaShop\Exceptions\InsuranceProductException;
use Alma\PrestaShop\Factories\CartFactory;
use Alma\PrestaShop\Factories\ContextFactory;
Expand Down Expand Up @@ -497,8 +498,75 @@ public function testRemoveInsuranceProductsNotOrderedThrowExceptionByDeleteProdu
)
->willReturnOnConsecutiveCalls(
true,
$this->throwException(new InsuranceProductException("[Alma] Error while removing product id : {$this->insuranceProductMock->id} from cart id : {$this->cartMock2->id}"))
$this->throwException(new CartException("Product id and cart id are required. ProductId: {$this->insuranceProductMock->id}, cartId: {$this->cartMock2->id}"))
);

$this->expectException(InsuranceProductException::class);
$this->insuranceProductService->removeInsuranceProductsNotOrdered();
}

/**
* @throws InsuranceProductException
*/
public function testRemoveInsuranceProductsNotOrderedThrowExceptionIfDeleteAssociationsByCartIdsError()
{
$this->almaInsuranceProductRepository->expects($this->once())
->method('getCartsNotOrdered')
->willReturn([
['id_cart' => $this->cartMock1->id],
['id_cart' => $this->cartMock2->id],
]);
$this->productRepositoryMock->expects($this->once())
->method('getProductIdByReference')
->with(ConstantsHelper::ALMA_INSURANCE_PRODUCT_REFERENCE)
->willReturn($this->insuranceProductMock->id);
$this->cartServiceMock->expects($this->exactly(2))
->method('deleteProductByCartId')
->withConsecutive(
[$this->insuranceProductMock->id, $this->cartMock1->id],
[$this->insuranceProductMock->id, $this->cartMock2->id]
)
->willReturnOnConsecutiveCalls(true, true);

$this->almaInsuranceProductRepository->expects($this->once())
->method('deleteAssociationsByCartIds')
->with("{$this->cartMock1->id}, {$this->cartMock2->id}")
->willThrowException(new \PrestaShopDatabaseException());

$this->expectException(InsuranceProductException::class);
$this->insuranceProductService->removeInsuranceProductsNotOrdered();
}

/**
* @throws InsuranceProductException
* @throws \PrestaShopDatabaseException
*/
public function testRemoveInsuranceProductsNotOrderedWithRightData()
{
$this->almaInsuranceProductRepository->expects($this->once())
->method('getCartsNotOrdered')
->willReturn([
['id_cart' => $this->cartMock1->id],
['id_cart' => $this->cartMock2->id],
['id_cart' => '34'],
]);
$this->productRepositoryMock->expects($this->once())
->method('getProductIdByReference')
->with(ConstantsHelper::ALMA_INSURANCE_PRODUCT_REFERENCE)
->willReturn($this->insuranceProductMock->id);
$this->cartServiceMock->expects($this->exactly(3))
->method('deleteProductByCartId')
->withConsecutive(
[$this->insuranceProductMock->id, $this->cartMock1->id],
[$this->insuranceProductMock->id, $this->cartMock2->id],
[$this->insuranceProductMock->id, '34']
)
->willReturnOnConsecutiveCalls(true, true, false);

$this->almaInsuranceProductRepository->expects($this->once())
->method('deleteAssociationsByCartIds')
->with("{$this->cartMock1->id}, {$this->cartMock2->id}, 34");

$this->insuranceProductService->removeInsuranceProductsNotOrdered();
}

Expand Down

0 comments on commit 219c93f

Please sign in to comment.