Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Pull - Update metadata for Coupons and Product after being notified. #2338

Merged
merged 8 commits into from
Mar 28, 2024
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
26 changes: 20 additions & 6 deletions src/Coupon/CouponHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Automattic\WooCommerce\GoogleListingsAndAds\Value\ChannelVisibility;
use Automattic\WooCommerce\GoogleListingsAndAds\Value\NotificationStatus;
use Automattic\WooCommerce\GoogleListingsAndAds\Value\SyncStatus;
use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\Notifications\HelperNotificationInterface;
use WC_Coupon;
defined( 'ABSPATH' ) || exit();

Expand All @@ -18,7 +19,7 @@
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Coupon
*/
class CouponHelper implements Service {
class CouponHelper implements Service, HelperNotificationInterface {

use PluginHelper;

Expand Down Expand Up @@ -57,6 +58,19 @@
$this->merchant_center = $merchant_center;
}

/**
* Mark the item as notified.
*
* @param WC_Coupon $coupon
*
* @return void
*/
public function mark_as_notified( $coupon ): void {
$this->meta_handler->update_synced_at( $coupon, time() );
$this->meta_handler->update_sync_status( $coupon, SyncStatus::SYNCED );
$this->update_empty_visibility( $coupon );

Check warning on line 71 in src/Coupon/CouponHelper.php

View check run for this annotation

Codecov / codecov/patch

src/Coupon/CouponHelper.php#L68-L71

Added lines #L68 - L71 were not covered by tests
}

/**
* Mark a coupon as synced. This function accepts nullable $google_id,
* which guarantees version compatibility for Alpha, Beta and stable verison promtoion APIs.
Expand Down Expand Up @@ -92,7 +106,7 @@
*
* @param WC_Coupon $coupon
*/
public function mark_as_unsynced( WC_Coupon $coupon ) {
public function mark_as_unsynced( $coupon ): void {
$this->meta_handler->delete_synced_at( $coupon );
$this->meta_handler->update_sync_status( $coupon, SyncStatus::NOT_SYNCED );
$this->meta_handler->delete_google_ids( $coupon );
Expand Down Expand Up @@ -375,7 +389,7 @@
* @param WC_Coupon $coupon
* @param string $status
*/
public function set_notification_status( WC_Coupon $coupon, $status ): void {
public function set_notification_status( $coupon, $status ): void {
$this->meta_handler->update_notification_status( $coupon, $status );
}

Expand All @@ -387,7 +401,7 @@
*
* @return bool
*/
public function should_trigger_create_notification( WC_Coupon $coupon ): bool {
public function should_trigger_create_notification( $coupon ): bool {
return $this->is_ready_to_notify( $coupon ) && ! $this->has_notified_creation( $coupon );
}

Expand All @@ -399,7 +413,7 @@
*
* @return bool
*/
public function should_trigger_update_notification( WC_Coupon $coupon ): bool {
public function should_trigger_update_notification( $coupon ): bool {
return $this->is_ready_to_notify( $coupon ) && $this->has_notified_creation( $coupon );
}

Expand All @@ -411,7 +425,7 @@
*
* @return bool
*/
public function should_trigger_delete_notification( WC_Coupon $coupon ): bool {
public function should_trigger_delete_notification( $coupon ): bool {
return ! $this->is_ready_to_notify( $coupon ) && $this->has_notified_creation( $coupon );
}
}
22 changes: 11 additions & 11 deletions src/Jobs/Notifications/AbstractItemNotificationJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@

namespace Automattic\WooCommerce\GoogleListingsAndAds\Jobs\Notifications;

use Automattic\WooCommerce\GoogleListingsAndAds\Coupon\CouponHelper;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\ProductHelper;
use Automattic\WooCommerce\GoogleListingsAndAds\Value\NotificationStatus;
use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\Notifications\HelperNotificationInterface;

defined( 'ABSPATH' ) || exit;

Expand Down Expand Up @@ -38,7 +37,7 @@ protected function process_items( array $args ): void {

if ( $this->can_process( $item, $topic ) && $this->notifications_service->notify( $topic, $item ) ) {
$this->set_status( $item, $this->get_after_notification_status( $topic ) );
$this->maybe_mark_as_unsynced( $topic, $item );
$this->handle_notified( $topic, $item );
}
}

Expand Down Expand Up @@ -91,18 +90,19 @@ protected function can_process( int $item_id, string $topic ): bool {
}

/**
* If there is a valid Item ID and topic is a deletion topic. Mark the item as unsynced.
* Handle the item after the notification.
*
* @param string $topic
* @param int $item
*/
protected function maybe_mark_as_unsynced( string $topic, int $item ): void {
if ( ! str_contains( $topic, '.delete' ) ) {
return;
protected function handle_notified( string $topic, int $item ): void {
if ( str_contains( $topic, '.delete' ) ) {
$this->get_helper()->mark_as_unsynced( $this->get_item( $item ) );
}

$item = $this->get_item( $item );
$this->get_helper()->mark_as_unsynced( $item );
if ( str_contains( $topic, '.create' ) ) {
$this->get_helper()->mark_as_notified( $this->get_item( $item ) );
}
}

/**
Expand All @@ -116,7 +116,7 @@ abstract protected function get_item( int $item_id );
/**
* Get the helper
*
* @return CouponHelper|ProductHelper
* @return HelperNotificationInterface
*/
abstract public function get_helper();
abstract public function get_helper(): HelperNotificationInterface;
}
15 changes: 8 additions & 7 deletions src/Jobs/Notifications/CouponNotificationJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Automattic\WooCommerce\GoogleListingsAndAds\API\WP\NotificationsService;
use Automattic\WooCommerce\GoogleListingsAndAds\Coupon\CouponHelper;
use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\ActionSchedulerJobMonitor;
use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\Notifications\HelperNotificationInterface;

defined( 'ABSPATH' ) || exit;

Expand All @@ -27,16 +28,16 @@ class CouponNotificationJob extends AbstractItemNotificationJob {
/**
* Notifications Jobs constructor.
*
* @param ActionSchedulerInterface $action_scheduler
* @param ActionSchedulerJobMonitor $monitor
* @param NotificationsService $notifications_service
* @param CouponHelper $coupon_helper
* @param ActionSchedulerInterface $action_scheduler
* @param ActionSchedulerJobMonitor $monitor
* @param NotificationsService $notifications_service
* @param HelperNotificationInterface $coupon_helper
*/
public function __construct(
ActionSchedulerInterface $action_scheduler,
ActionSchedulerJobMonitor $monitor,
NotificationsService $notifications_service,
CouponHelper $coupon_helper
HelperNotificationInterface $coupon_helper
) {
$this->notifications_service = $notifications_service;
$this->helper = $coupon_helper;
Expand All @@ -56,9 +57,9 @@ protected function get_item( int $item_id ) {
/**
* Get the Coupon Helper
*
* @return CouponHelper
* @return HelperNotificationInterface
*/
public function get_helper() {
public function get_helper(): HelperNotificationInterface {
return $this->helper;
}

Expand Down
71 changes: 71 additions & 0 deletions src/Jobs/Notifications/HelperNotificationInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
declare( strict_types=1 );

namespace Automattic\WooCommerce\GoogleListingsAndAds\Jobs\Notifications;

defined( 'ABSPATH' ) || exit;

Check warning on line 6 in src/Jobs/Notifications/HelperNotificationInterface.php

View check run for this annotation

Codecov / codecov/patch

src/Jobs/Notifications/HelperNotificationInterface.php#L6

Added line #L6 was not covered by tests

/**
* Interface HelperNotificationInterface
*
* @since x.x.x
* @package Automattic\WooCommerce\GoogleListingsAndAds\Jobs\Notifications
*/
interface HelperNotificationInterface {

/**
* Checks if the item can be processed based on the topic.
*
* @param WC_Product|WC_Coupon $item
*
* @return bool
*/
public function should_trigger_create_notification( $item ): bool;


/**
* Indicates if the item ready for sending a delete Notification.
*
* @param WC_Product|WC_Coupon $item
*
* @return bool
*/
public function should_trigger_delete_notification( $item ): bool;

/**
* Indicates if the item ready for sending an update Notification.
*
* @param WC_Product|WC_Coupon $item
*
* @return bool
*/
public function should_trigger_update_notification( $item ): bool;

/**
* Marks the item as unsynced.
*
* @param WC_Product|WC_Coupon $item
*
* @return void
*/
public function mark_as_unsynced( $item ): void;

/**
* Set the notification status for an item.
*
* @param WC_Product|WC_Coupon $item
* @param string $status
*
* @return void
*/
public function set_notification_status( $item, $status ): void;

/**
* Marks the item as notified.
*
* @param WC_Product|WC_Coupon $item
*
* @return void
*/
public function mark_as_notified( $item ): void;
}
13 changes: 7 additions & 6 deletions src/Jobs/Notifications/ProductNotificationJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Automattic\WooCommerce\GoogleListingsAndAds\API\WP\NotificationsService;
use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\ActionSchedulerJobMonitor;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\ProductHelper;
use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\Notifications\HelperNotificationInterface;

defined( 'ABSPATH' ) || exit;

Expand All @@ -27,16 +28,16 @@ class ProductNotificationJob extends AbstractItemNotificationJob {
/**
* Notifications Jobs constructor.
*
* @param ActionSchedulerInterface $action_scheduler
* @param ActionSchedulerJobMonitor $monitor
* @param NotificationsService $notifications_service
* @param ProductHelper $helper
* @param ActionSchedulerInterface $action_scheduler
* @param ActionSchedulerJobMonitor $monitor
* @param NotificationsService $notifications_service
* @param HelperNotificationInterface $helper
*/
public function __construct(
ActionSchedulerInterface $action_scheduler,
ActionSchedulerJobMonitor $monitor,
NotificationsService $notifications_service,
ProductHelper $helper
HelperNotificationInterface $helper
) {
$this->notifications_service = $notifications_service;
$this->helper = $helper;
Expand All @@ -58,7 +59,7 @@ protected function get_item( int $item_id ) {
*
* @return ProductHelper
*/
public function get_helper() {
public function get_helper(): HelperNotificationInterface {
return $this->helper;
}

Expand Down
38 changes: 32 additions & 6 deletions src/Product/ProductHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Automattic\WooCommerce\GoogleListingsAndAds\Value\MCStatus;
use Automattic\WooCommerce\GoogleListingsAndAds\Value\SyncStatus;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Google\Service\ShoppingContent\Product as GoogleProduct;
use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\Notifications\HelperNotificationInterface;
use WC_Product;
use WC_Product_Variation;
use WP_Post;
Expand All @@ -25,7 +26,7 @@
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Product
*/
class ProductHelper implements Service {
class ProductHelper implements Service, HelperNotificationInterface {

use PluginHelper;

Expand Down Expand Up @@ -57,6 +58,31 @@
$this->target_audience = $target_audience;
}

/**
* Mark the item as notified.
*
* @param WC_Product $product
*
* @return void
*/
public function mark_as_notified( $product ): void {
$this->meta_handler->delete_failed_delete_attempts( $product );
$this->meta_handler->update_synced_at( $product, time() );
$this->meta_handler->update_sync_status( $product, SyncStatus::SYNCED );
$this->update_empty_visibility( $product );

Check warning on line 72 in src/Product/ProductHelper.php

View check run for this annotation

Codecov / codecov/patch

src/Product/ProductHelper.php#L68-L72

Added lines #L68 - L72 were not covered by tests

// mark the parent product as synced if it's a variation
if ( $product instanceof WC_Product_Variation ) {

Check warning on line 75 in src/Product/ProductHelper.php

View check run for this annotation

Codecov / codecov/patch

src/Product/ProductHelper.php#L75

Added line #L75 was not covered by tests
try {
$parent_product = $this->get_wc_product( $product->get_parent_id() );
} catch ( InvalidValue $exception ) {
return;

Check warning on line 79 in src/Product/ProductHelper.php

View check run for this annotation

Codecov / codecov/patch

src/Product/ProductHelper.php#L77-L79

Added lines #L77 - L79 were not covered by tests
}

$this->mark_as_notified( $parent_product );

Check warning on line 82 in src/Product/ProductHelper.php

View check run for this annotation

Codecov / codecov/patch

src/Product/ProductHelper.php#L82

Added line #L82 was not covered by tests
}
}

/**
* Mark a product as synced in the local database.
* This function also handles the following cleanup tasks:
Expand Down Expand Up @@ -103,7 +129,7 @@
/**
* @param WC_Product $product
*/
public function mark_as_unsynced( WC_Product $product ) {
public function mark_as_unsynced( $product ): void {
$this->meta_handler->delete_synced_at( $product );
if ( ! $this->is_sync_ready( $product ) ) {
$this->meta_handler->delete_sync_status( $product );
Expand Down Expand Up @@ -390,7 +416,7 @@
*
* @return bool
*/
public function should_trigger_create_notification( WC_Product $product ): bool {
public function should_trigger_create_notification( $product ): bool {
return $this->is_ready_to_notify( $product ) && ! $this->has_notified_creation( $product );
}

Expand All @@ -402,7 +428,7 @@
*
* @return bool
*/
public function should_trigger_update_notification( WC_Product $product ): bool {
public function should_trigger_update_notification( $product ): bool {
return $this->is_ready_to_notify( $product ) && $this->has_notified_creation( $product );
}

Expand All @@ -414,7 +440,7 @@
*
* @return bool
*/
public function should_trigger_delete_notification( WC_Product $product ): bool {
public function should_trigger_delete_notification( $product ): bool {
return ! $this->is_ready_to_notify( $product ) && $this->has_notified_creation( $product );
}

Expand Down Expand Up @@ -447,7 +473,7 @@
* @param WC_Product $product
* @param string $status
*/
public function set_notification_status( WC_Product $product, $status ): void {
public function set_notification_status( $product, $status ): void {
$this->meta_handler->update_notification_status( $product, $status );
}

Expand Down
Loading
Loading