From c68aefb847b09daa0697de7684d3451e2e68ce1e Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Thu, 09 Jul 2026 23:10:23 +0000
Subject: [PATCH] =Refactor of Integrations.php to be a bit more useful with a suite of methods for create, update, delete back and forths for integrations where sharing is enabled. Also considering a single instance with a connect method to connect as the site (no user) vs connecting as an individual user - rather than recreating instances for every user.

---
 inc/integrations/Helcim.php |   63 +++++++++++++------------------
 1 files changed, 26 insertions(+), 37 deletions(-)

diff --git a/inc/integrations/Helcim.php b/inc/integrations/Helcim.php
index 7f2556d..f5dc8a5 100644
--- a/inc/integrations/Helcim.php
+++ b/inc/integrations/Helcim.php
@@ -2,6 +2,7 @@
 namespace JVBase\integrations;
 
 use Exception;
+use JVBase\meta\Meta;
 use WP_Error;
 use WP_Post;
 use JVBase\ui\Checkout;
@@ -25,6 +26,7 @@
  */
 class Helcim extends Integrations
 {
+	protected static string $syncCustomer = 'helcim_sync_customer';
 	protected string $service_name = 'helcim';
 	protected string|array $apiBase = 'https://api.helcim.com/v2';
 
@@ -711,9 +713,6 @@
 			return $desc;
 		}, 10, 2);
 
-		// Register queue operation types with IntegrationExecutor
-		$this->registerQueueTypes();
-
 		// Register webhook endpoint (handled by parent)
 		$this->registerWebhookEndpoint();
 	}
@@ -760,38 +759,16 @@
 	}
 
 
-	protected function registerQueueTypes(): void
+	protected function registerAdditionalQueueTypes(IntegrationExecutor $executor): void
 	{
 		$queue    = JVB()->queue();
-		$executor = new IntegrationExecutor();
 
-		$queue->registry()->register('helcim_sync_to', new TypeConfig(
-			executor:  $executor,
-			chunkKey:  'items',
-			chunkSize: 10,
-			maxRetries: 3
-		));
-
-		$queue->registry()->register('helcim_sync_from', new TypeConfig(
-			executor:  $executor,
-			chunkKey:  'items',
-			chunkSize: 10,
-			maxRetries: 3
-		));
-
-		$queue->registry()->register('helcim_delete_from', new TypeConfig(
-			executor:  $executor,
-			chunkKey:  'external_ids',
-			chunkSize: 20,
-			maxRetries: 2
-		));
-
-		$queue->registry()->register('helcim_import', new TypeConfig(
+		$queue->registry()->register(self::$import, new TypeConfig(
 			executor:   $executor,
 			maxRetries: 3
 		));
 
-		$queue->registry()->register('helcim_sync_customer', new TypeConfig(
+		$queue->registry()->register(self::$syncCustomer, new TypeConfig(
 			executor:   $executor,
 			maxRetries: 2
 		));
@@ -1028,22 +1005,34 @@
 
 	protected function handleTheSavePost(int $postID, \WP_Post $post, bool $update, array $settings): void
 	{
-		$fields = $this->getSyncFields($postID, 'post', ['share_to_helcim', 'schedule_helcim']);
 
-		if (empty($fields['share_to_helcim'])) {
-			return;
-		}
-
-		// Uses IntegrationExecutor via TypeRegistry instead of FilteredExecutor
-		$this->queueOperation('sync_to', [
+		error_log('==== [Helcim]::handleTheSavePost ====');
+		$this->queueOperation(self::$syncTo, [
 			'items'   => [$postID],
-			'user_id' => $this->userID,
+			'user'		=> user_can($post->post_author, 'manage_options') ? null : $post->post_author
 		], [
 			'priority' => 'high',
 			'delay'    => 30,
 		]);
+		Meta::forPost($postID)->set('_'.$this->service_name.'_sync_status', 'queued');
+	}
+	/**
+	 * Handle post deletion
+	 */
+	public function handleDeletePost(int $postID): void
+	{
+		$item_id = Meta::forPost($postID)->get("_{$this->service_name}_item_id");
 
-		update_post_meta($postID, BASE . '_helcim_sync_status', 'queued');
+		if (empty($item_id)) {
+			return;
+		}
+		$this->queueOperation(self::$deleteFrom, [
+			'external_ids' => [$item_id],
+			'post_id'      => $postID,
+		], [
+			'priority' => 'high',
+		]);
+
 	}
 
 	protected function handleImportFromHelcim(): array

--
Gitblit v1.10.0