Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Backend/Controllers/CampaignEditAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Okay\Entities\FeaturesEntity;
use Okay\Entities\FeaturesValuesEntity;
use Okay\Modules\Sviat\Promo\Entities\PromoCampaignEntity;
use Okay\Modules\Sviat\Promo\Entities\PromoFeedLinkEntity;
use Okay\Modules\Sviat\Promo\Entities\PromoRewardLineEntity;
use Okay\Modules\Sviat\Promo\Entities\PromoScopeEntity;
use Okay\Modules\Sviat\Promo\Entities\PromoQuantityTierEntity;
Expand Down Expand Up @@ -195,7 +196,12 @@ public function fetch(
$campaignRepository->saveFeedLinks(
(int) $promo->id,
(int) ($promo->feed_enabled ?? 0),
$payload->postFeedIds()
$payload->postFeedIds(),
PromoFeedLinkEntity::hiddenTypes(
feeds: class_exists(\Okay\Modules\OkayCMS\Feeds\Entities\FeedsEntity::class),
gm: class_exists(\Okay\Modules\OkayCMS\GoogleMerchant\Entities\GoogleMerchantFeedsEntity::class),
merchantApi: class_exists(\Okay\Modules\Sviat\MerchantApi\Services\CatalogItemBuilder::class),
),
);

$promoObjects = $scopeRows->find(['promo_id' => $promo->id]);
Expand Down
11 changes: 11 additions & 0 deletions Entities/PromoFeedLinkEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ class PromoFeedLinkEntity extends Entity
/** Значення ENUM-колонки feed_type: install() і оновлення беруть саме цей список. */
public const TYPES = [self::TYPE_FEEDS, self::TYPE_GM, self::TYPE_MERCHANT_API];

/**
* Цілі, модулів яких у магазині немає: перемикача у формі акції для них немає, тож збереження
* акції не мусить чіпати їхні прив'язки.
*
* @return list<string>
*/
public static function hiddenTypes(bool $feeds, bool $gm, bool $merchantApi): array
{
return array_keys(array_filter([self::TYPE_FEEDS => !$feeds, self::TYPE_GM => !$gm, self::TYPE_MERCHANT_API => !$merchantApi]));
}

protected static $fields = [
'promo_id',
'feed_type',
Expand Down
5 changes: 4 additions & 1 deletion Helpers/CampaignRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,12 @@ public function getLinkedFeedIds(int $promoId): array
* Спочатку видаляє старі, потім записує нові.
*
* @param array<string, int[]> $feedIds Наприклад: ['feeds' => [1], 'gm' => [2, 3]]
* @param list<string> $hiddenTypes цілі без перемикача у формі (модуля немає): їхні прив'язки лишаються як були
*/
public function saveFeedLinks(int $promoId, int $feedEnabled, array $feedIds): void
public function saveFeedLinks(int $promoId, int $feedEnabled, array $feedIds, array $hiddenTypes = []): void
{
$kept = array_intersect_key($this->feedLinks->getLinkedFeedIds($promoId), array_flip($hiddenTypes));
$feedIds = $kept + array_diff_key($feedIds, $kept);
$this->feedLinks->deleteByPromoId($promoId);

if (!$feedEnabled) {
Expand Down
2 changes: 1 addition & 1 deletion Init/module.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"Okay": "4.5.3",
"version": "2.0.0",
"version": "2.0.1",
"moduleName": "Promo: акції та знижки",
"vendor": {
"email": "devsviat@proton.me",
Expand Down
25 changes: 25 additions & 0 deletions tests/PromoMerchantApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,31 @@ public function testMerchantApiLinkIsSaved(): void
$this->assertContains(['promo_id' => 9, 'feed_type' => 'gm', 'feed_id' => 2], $added);
}

/**
* Ціль, модуля якої зараз немає (вимкнений GoogleMerchant, MerchantApi на стоку), не має перемикача у формі.
* Збереження акції не мусить стирати її прив'язки: інакше після повернення модуля акція зникла б із фіда.
*/
public function testLinksOfHiddenTargetsSurviveSave(): void
{
$added = [];
$links = $this->createStub(PromoFeedLinkEntity::class);
$links->method('getLinkedFeedIds')->willReturn(['merchant_api' => [1], 'gm' => [2, 3], 'feeds' => [4]]);
$links->method('add')->willReturnCallback(static function ($row) use (&$added) {
$added[] = $row['feed_type'] . ':' . $row['feed_id'];

return 1;
});
$entityFactory = $this->createStub(EntityFactory::class);
$entityFactory->method('get')->willReturnCallback(fn($class) => $class === PromoFeedLinkEntity::class ? $links : $this->createStub($class));

new CampaignRepository($entityFactory, $this->createStub(Config::class), $this->createStub(Image::class))
->saveFeedLinks(9, 1, ['feeds' => [5]], hiddenTypes: ['merchant_api', 'gm']);

sort($added);
$this->assertSame(['feeds:5', 'gm:2', 'gm:3', 'merchant_api:1'], $added);
$this->assertSame(['merchant_api'], PromoFeedLinkEntity::hiddenTypes(feeds: true, gm: true, merchantApi: false));
}

/**
* Колонка feed_type — ENUM: значення поза списком нестрогий MariaDB мовчки записав би порожнім рядком.
* Встановлення й оновлення мають брати той самий список, і в ньому має бути merchant_api.
Expand Down
Loading