From b6f9fd9e2994eb3a6d7a288cbc8371b468bc0e2c Mon Sep 17 00:00:00 2001 From: Bennani Soufiane Date: Mon, 11 Nov 2024 14:28:50 +0100 Subject: [PATCH] - implement data update operations for sync-status route --- Data/CustomersAddressesData.php | 72 ++--- Data/ProductImagesData.php | 142 ++++----- Data/ProductsVariationsData.php | 294 +++++++++--------- Model/ShopimindSyncStatus.php | 45 ++- PassiveSynchronization/SyncCustomers.php | 41 ++- .../SyncCustomersAddresses.php | 45 ++- .../SyncCustomersGroups.php | 43 ++- .../SyncNewsletterSubscribers.php | 42 ++- PassiveSynchronization/SyncOrderStatus.php | 42 ++- PassiveSynchronization/SyncOrders.php | 41 ++- PassiveSynchronization/SyncProducts.php | 46 ++- .../SyncProductsCategories.php | 42 ++- PassiveSynchronization/SyncProductsImages.php | 47 ++- .../SyncProductsManufacturers.php | 41 ++- .../SyncProductsVariations.php | 45 ++- PassiveSynchronization/SyncVouchers.php | 41 ++- lib/Utils.php | 27 -- 17 files changed, 745 insertions(+), 351 deletions(-) diff --git a/Data/CustomersAddressesData.php b/Data/CustomersAddressesData.php index 3b997df..cfb496a 100644 --- a/Data/CustomersAddressesData.php +++ b/Data/CustomersAddressesData.php @@ -1,35 +1,37 @@ -findOneById( $address->getCountryId() ); - - $data = [ - "address_id" => intval( $address->getId() ), - "first_name" => $address->getFirstname() ?? '', - "last_name" => $address->getLastname() ?? '', - "primary_phone" => $address->getPhone() ?? null, - "secondary_phone" => $address->getCellphone() ?? null, - "company" => $address->getCompany() ?? null, - "address_line_1" => $address->getAddress1() ?? '', - "address_line_2" => $address->getAddress2() ?? '', - "postal_code" => $address->getZipcode() ?? '', - "city" => $address->getCity() ?? '', - "country" => !empty( $country ) ? $country->getTitle() : '', - "is_active" => true - ]; - - return $data; - } -} +findOneById( $address->getCountryId() ); + + $data = [ + "address_id" => intval( $address->getId() ), + "first_name" => $address->getFirstname() ?? '', + "last_name" => $address->getLastname() ?? '', + "primary_phone" => $address->getPhone() ?? null, + "secondary_phone" => $address->getCellphone() ?? null, + "company" => $address->getCompany() ?? null, + "address_line_1" => $address->getAddress1() ?? '', + "address_line_2" => $address->getAddress2() ?? '', + "postal_code" => $address->getZipcode() ?? '', + "city" => $address->getCity() ?? '', + "country" => !empty( $country ) ? $country->getTitle() : '', + "is_active" => true, + "created_at" => $address->getCreatedAt()->format('Y-m-d\TH:i:s.u\Z'), + 'updated_at' => $address->getUpdatedAt()->format('Y-m-d\TH:i:s.u\Z'), + ]; + + return $data; + } +} diff --git a/Data/ProductImagesData.php b/Data/ProductImagesData.php index 72f5f88..4469785 100644 --- a/Data/ProductImagesData.php +++ b/Data/ProductImagesData.php @@ -1,70 +1,72 @@ - strval( $productImage->getId() ), - 'variation_id' => null, - 'lang' => $lang->getCode(), - 'url' => $rootUrl, - 'is_default' => false, - ]; - } else if ( $action == 'update') { - $url = ''; - - try { - $imgSourcePath = $productImage->getUploadDir().DS.$productImage->getFile(); - - $productImageEvent = new ImageEvent(); - $productImageEvent->setSourceFilepath($imgSourcePath)->setCacheSubdirectory('product_image'); - - $dispatcher->dispatch($productImageEvent, TheliaEvents::IMAGE_PROCESS); - $url = $productImageEvent->getFileUrl(); - } catch (\Throwable $th) { - $url = null; - } - - - $productSaleElementsProductImages = $productImage->getProductSaleElementsProductImages()->getFirst(); - - $data = [ - 'image_id' => strval( $productImage->getId() ), - 'variation_id' => $productSaleElementsProductImages ? intval( $productSaleElementsProductImages->getProductSaleElementsId() ) : null, - 'lang' => $lang->getCode(), - 'url' => $url, - 'is_default' => ( $productImage->getPosition() == 1 ) ? true : false, - ]; - } - - return $data; - } -} + strval( $productImage->getId() ), + 'variation_id' => null, + 'lang' => $lang->getCode(), + 'url' => $rootUrl, + 'is_default' => false, + ]; + } else if ( $action == 'update') { + $url = ''; + + try { + $imgSourcePath = $productImage->getUploadDir().DS.$productImage->getFile(); + + $productImageEvent = new ImageEvent(); + $productImageEvent->setSourceFilepath($imgSourcePath)->setCacheSubdirectory('product_image'); + + $dispatcher->dispatch($productImageEvent, TheliaEvents::IMAGE_PROCESS); + $url = $productImageEvent->getFileUrl(); + } catch (\Throwable $th) { + $url = null; + } + + + $productSaleElementsProductImages = $productImage->getProductSaleElementsProductImages()->getFirst(); + + $data = [ + 'image_id' => strval( $productImage->getId() ), + 'variation_id' => $productSaleElementsProductImages ? intval( $productSaleElementsProductImages->getProductSaleElementsId() ) : null, + 'lang' => $lang->getCode(), + 'url' => $url, + 'is_default' => ( $productImage->getPosition() == 1 ) ? true : false, + 'created_at' => $productImage->getCreatedAt()->format('Y-m-d\TH:i:s.u\Z'), + 'updated_at' => $productImage->getUpdatedAt()->format('Y-m-d\TH:i:s.u\Z') + ]; + } + + return $data; + } +} diff --git a/Data/ProductsVariationsData.php b/Data/ProductsVariationsData.php index c9756be..81ce625 100644 --- a/Data/ProductsVariationsData.php +++ b/Data/ProductsVariationsData.php @@ -1,146 +1,148 @@ -findOneByByDefault(true)->getLocale(); - - $attribute = $productVariation->getAttributeCombinations(); - $title = self::getTitle( $attribute, $locale ); - $productTitle = $productVariation->getProduct()->getTranslation( $locale )->getTitle(); - $defaultProductTitle = $productVariation->getProduct()->getTranslation( LangQuery::create()->findOneByByDefault(true)->getLocale() )->getTitle(); - - $productVariationTitle = ''; - - if ( !empty( $title ) ) { - $productVariationTitle = $title; - } else if ( !empty( $productTitle ) ) { - $productVariationTitle = $productTitle; - } else { - $productVariationTitle = $defaultTitle; - } - - return [ - "variation_id" => intval( $productVariation->getId() ), - "lang" => substr( $locale , 0, 2 ), - "name" => $productVariationTitle ? $productVariationTitle : $defaultProductTitle, - "reference" => $productVariation->getRef(), - "ean13" => ( !empty( $productVariation->getEanCode() ) ) ? $productVariation->getEanCode() : null, - "link" => $productVariation->getProduct()->getUrl( $locale ), - "image_link" => self::getDefaultImage( $productVariation->getId(), $productVariation->getProduct()->getId(), $dispatcher ), - "price" => self::getPrice( $productVariation->getId() ), - "price_discount" => self::getPromoPrice( $productVariation->getId() ), - "quantity_remaining" => $productVariation->getQuantity() ?? 0, - "is_default" => ( bool ) $productVariation->getIsDefault() - ]; - } - - /** - * Retrieves the name for a product variation. - * - * @param $attribute - */ - public static function getTitle( $attribute, $locale ){ - $title = ""; - foreach ($attribute as $item) { - $attribute = AttributeAvI18nQuery::create()->filterByLocale( $locale )->findOneById( $item->getAttributeAvId() ); - if ($attribute) { - $title .= $attribute->getTitle(). " "; - } - } - return trim( $title ); - } - - /** - * Retrieves the price for a product variation. - * - * @param int $productSalesElementId The ID of the product sales element. - */ - public static function getPrice( int $productSalesElementId ){ - $productSalesElement = ProductPriceQuery::create()->findOneByProductSaleElementsId( $productSalesElementId ); - if ( $productSalesElement ) { - $productPrice = $productSalesElement->getPrice(); - return Utils::formatNumber( $productPrice ); - } - return 0; - } - - /** - * Retrieves the promotional price for a product variation identified by its ID. - * - * @param int $productSalesElementId The ID of the product variation. - */ - public static function getPromoPrice( int $productSalesElementId ){ - $productSalesElement = ProductPriceQuery::create()->findOneByProductSaleElementsId( $productSalesElementId ); - if ( $productSalesElement ) { - $productPromoPrice = $productSalesElement->getPromoPrice(); - return Utils::formatNumber( $productPromoPrice ); - } - } - - /** - * Retrieves default images of productVariation - * - * @param int $productSaleElementId - * @param int $productId - * @param EventDispatcherInterface $dispatcher - * - */ - public static function getDefaultImage( int $productSaleElementId, int $idProduct, EventDispatcherInterface $dispatcher ){ - $productSaleElementsProductImage = ProductSaleElementsProductImageQuery::create() - ->filterByProductSaleElementsId( $productSaleElementId ) - ->findOne(); - - $idParam = !empty( $productSaleElementsProductImage ) ? $productSaleElementsProductImage->getProductSaleElementsId() : $idProduct; - - $defaultImage = ProductImageQuery::create() - ->filterByProductId( $idParam ) - ->filterByPosition(1) - ->findOne(); - - if ( !empty( $defaultImage ) ) { - try { - $imagePath = ConfigQuery::read('images_library_path') . DIRECTORY_SEPARATOR . $defaultImage->getFile(); - - $imgSourcePath = $imagePath; - - $productImageEvent = new ImageEvent(); - $productImageEvent->setSourceFilepath($imgSourcePath)->setCacheSubdirectory('product_image'); - - $dispatcher->dispatch($productImageEvent, TheliaEvents::IMAGE_PROCESS); - $url = $productImageEvent->getFileUrl(); - - return $url; - } catch (\Throwable $th) { - //throw $th; - } - } - - return null; - } -} +findOneByByDefault(true)->getLocale(); + + $attribute = $productVariation->getAttributeCombinations(); + $title = self::getTitle( $attribute, $locale ); + $productTitle = $productVariation->getProduct()->getTranslation( $locale )->getTitle(); + $defaultProductTitle = $productVariation->getProduct()->getTranslation( LangQuery::create()->findOneByByDefault(true)->getLocale() )->getTitle(); + + $productVariationTitle = ''; + + if ( !empty( $title ) ) { + $productVariationTitle = $title; + } else if ( !empty( $productTitle ) ) { + $productVariationTitle = $productTitle; + } else { + $productVariationTitle = $defaultTitle; + } + + return [ + "variation_id" => intval( $productVariation->getId() ), + "lang" => substr( $locale , 0, 2 ), + "name" => $productVariationTitle ? $productVariationTitle : $defaultProductTitle, + "reference" => $productVariation->getRef(), + "ean13" => ( !empty( $productVariation->getEanCode() ) ) ? $productVariation->getEanCode() : null, + "link" => $productVariation->getProduct()->getUrl( $locale ), + "image_link" => self::getDefaultImage( $productVariation->getId(), $productVariation->getProduct()->getId(), $dispatcher ), + "price" => self::getPrice( $productVariation->getId() ), + "price_discount" => self::getPromoPrice( $productVariation->getId() ), + "quantity_remaining" => $productVariation->getQuantity() ?? 0, + "is_default" => ( bool ) $productVariation->getIsDefault(), + "created_at" => $productVariation->getCreatedAt()->format('Y-m-d\TH:i:s.u\Z'), + "updated_at" => $productVariation->getUpdatedAt()->format('Y-m-d\TH:i:s.u\Z'), + ]; + } + + /** + * Retrieves the name for a product variation. + * + * @param $attribute + */ + public static function getTitle( $attribute, $locale ){ + $title = ""; + foreach ($attribute as $item) { + $attribute = AttributeAvI18nQuery::create()->filterByLocale( $locale )->findOneById( $item->getAttributeAvId() ); + if ($attribute) { + $title .= $attribute->getTitle(). " "; + } + } + return trim( $title ); + } + + /** + * Retrieves the price for a product variation. + * + * @param int $productSalesElementId The ID of the product sales element. + */ + public static function getPrice( int $productSalesElementId ){ + $productSalesElement = ProductPriceQuery::create()->findOneByProductSaleElementsId( $productSalesElementId ); + if ( $productSalesElement ) { + $productPrice = $productSalesElement->getPrice(); + return Utils::formatNumber( $productPrice ); + } + return 0; + } + + /** + * Retrieves the promotional price for a product variation identified by its ID. + * + * @param int $productSalesElementId The ID of the product variation. + */ + public static function getPromoPrice( int $productSalesElementId ){ + $productSalesElement = ProductPriceQuery::create()->findOneByProductSaleElementsId( $productSalesElementId ); + if ( $productSalesElement ) { + $productPromoPrice = $productSalesElement->getPromoPrice(); + return Utils::formatNumber( $productPromoPrice ); + } + } + + /** + * Retrieves default images of productVariation + * + * @param int $productSaleElementId + * @param int $productId + * @param EventDispatcherInterface $dispatcher + * + */ + public static function getDefaultImage( int $productSaleElementId, int $idProduct, EventDispatcherInterface $dispatcher ){ + $productSaleElementsProductImage = ProductSaleElementsProductImageQuery::create() + ->filterByProductSaleElementsId( $productSaleElementId ) + ->findOne(); + + $idParam = !empty( $productSaleElementsProductImage ) ? $productSaleElementsProductImage->getProductSaleElementsId() : $idProduct; + + $defaultImage = ProductImageQuery::create() + ->filterByProductId( $idParam ) + ->filterByPosition(1) + ->findOne(); + + if ( !empty( $defaultImage ) ) { + try { + $imagePath = ConfigQuery::read('images_library_path') . DIRECTORY_SEPARATOR . $defaultImage->getFile(); + + $imgSourcePath = $imagePath; + + $productImageEvent = new ImageEvent(); + $productImageEvent->setSourceFilepath($imgSourcePath)->setCacheSubdirectory('product_image'); + + $dispatcher->dispatch($productImageEvent, TheliaEvents::IMAGE_PROCESS); + $url = $productImageEvent->getFileUrl(); + + return $url; + } catch (\Throwable $th) { + //throw $th; + } + } + + return null; + } +} diff --git a/Model/ShopimindSyncStatus.php b/Model/ShopimindSyncStatus.php index cb6c086..b4d15b7 100644 --- a/Model/ShopimindSyncStatus.php +++ b/Model/ShopimindSyncStatus.php @@ -48,10 +48,12 @@ public static function updateShopimindSyncStatus( $idShopAskSyncs, $currentDataT if ( !empty( $syncStatus ) ) { $syncStatus->setCurrentDataType( $currentDataType ); }else { + $currentDateTime = new \DateTime('now', new \DateTimeZone('UTC')); + $syncStatus = new ShopimindSyncStatus(); $syncStatus->setId( $idShopAskSyncs ); $syncStatus->setCurrentDataType( $currentDataType ); - $syncStatus->setFirstCall( new \DateTime('now') ); + $syncStatus->setFirstCall( $currentDateTime ); $syncStatus->setGlobalState( 'in_progress' ); $syncStatus->setStatuses( self::generateObjectStatuses() ); } @@ -74,13 +76,40 @@ private static function generateObjectStatuses() 'total_objects_count' => 0, 'sent_successful_count' => 0, 'sent_failed_count' => 0, - 'last_update' => null + 'last_sync_update' => null, + 'last_object_update' => null, ]; } return $objectStatuses; } + /** + * Updates the count details in the status of a specific object type for synchronization + * The update depends on the value of $respons. + * + * @param $idShopAskSyncs + * @param $objectType + * @param $response + * @param $count + * @return void + */ + public static function updateObjectStatusesCount( $idShopAskSyncs, $objectType, $response, $count ) + { + $objectStatusDetails = ShopimindSyncStatus::getObjectStatus( $idShopAskSyncs, $objectType ); + if ( isset( $response['statusCode'] ) && $response['statusCode'] == 200 ) { + $objectStatus = [ + "sent_successful_count" => $objectStatusDetails['sent_successful_count'] + $count, + ]; + }else { + $objectStatus = [ + "sent_failed_count" => $objectStatusDetails['sent_failed_count'] + $count, + ]; + } + + ShopimindSyncStatus::updateObjectStatuses( $idShopAskSyncs, $objectType, $objectStatus ); + } + /** * Update object statuses * @@ -90,8 +119,8 @@ private static function generateObjectStatuses() */ public static function updateObjectStatuses( $idShopAskSyncs, $objectType, $newData ) { - if ( !in_array( $objectType, self::OBJECT_TYPES ) ) return; //TODO - if ( empty( $newData ) || !is_array( $newData ) ) return; //TODO + if ( !in_array( $objectType, self::OBJECT_TYPES ) ) return; + if ( empty( $newData ) || !is_array( $newData ) ) return; $syncStatus = ShopimindSyncStatusQuery::create()->findOneById( $idShopAskSyncs ); if ( !empty( $syncStatus ) ) { @@ -114,8 +143,12 @@ public static function updateObjectStatuses( $idShopAskSyncs, $objectType, $newD $newObjectStatuses[$objectType]['sent_failed_count'] = $newData['sent_failed_count']; } - $currentDateTime = new \DateTime(); - $newObjectStatuses[$objectType]['last_update'] = $currentDateTime->format('Y-m-d\TH:i:s.u\Z'); + if ( isset( $newData['last_object_update'] ) && !empty( $newData['last_object_update'] ) ) { + $newObjectStatuses[$objectType]['last_object_update'] = $newData['last_object_update']; + } + + $currentDateTime = new \DateTime('now', new \DateTimeZone('UTC')); + $newObjectStatuses[$objectType]['last_sync_update'] = $currentDateTime->format('Y-m-d\TH:i:s.u\Z'); $syncStatus->setStatuses( $newObjectStatuses ); diff --git a/PassiveSynchronization/SyncCustomers.php b/PassiveSynchronization/SyncCustomers.php index 11dd72c..58a1e9e 100644 --- a/PassiveSynchronization/SyncCustomers.php +++ b/PassiveSynchronization/SyncCustomers.php @@ -47,6 +47,12 @@ public static function processSyncCustomers( $lastUpdate, $ids, $requestedBy, $i if ( !empty( $idShopAskSyncs ) ) { ShopimindSyncStatus::updateShopimindSyncStatus( $idShopAskSyncs, 'customers' ); + $objectStatus = ShopimindSyncStatus::getObjectStatus( $idShopAskSyncs, 'customers' ); + $oldCount = !empty( $objectStatus ) ? $objectStatus['total_objects_count'] : 0; + if( $oldCount > 0 ){ + $count = $oldCount; + } + $objectStatus = [ "status" => "in_progress", "total_objects_count" => $count, @@ -121,16 +127,34 @@ public static function syncCustomers( Request $request ) do { if ( empty( $lastUpdate ) ) { if ( empty( $customerIds ) ) { - $customers = CustomerQuery::create()->offset( $offset )->limit( $limit )->find(); + $customers = CustomerQuery::create() + ->orderByUpdatedAt() + ->offset( $offset ) + ->limit( $limit ) + ->find(); }else { - $customers = CustomerQuery::create()->filterById( $customerIds )->offset( $offset )->limit( $limit )->find(); + $customers = CustomerQuery::create() + ->orderByUpdatedAt() + ->filterById( $customerIds ) + ->offset( $offset ) + ->limit( $limit ) + ->find(); } } else { $lastUpdate = trim( $lastUpdate, '"\''); if ( empty( $customerIds ) ) { - $customers = CustomerQuery::create()->offset( $offset )->limit( $limit )->filterByUpdatedAt( $lastUpdate, '>=' ); + $customers = CustomerQuery::create() + ->orderByUpdatedAt() + ->offset( $offset ) + ->limit( $limit ) + ->filterByUpdatedAt( $lastUpdate, '>=' ); }else { - $customers = CustomerQuery::create()->filterById( $customerIds )->offset( $offset )->limit( $limit )->filterByUpdatedAt( $lastUpdate, '>=' ); + $customers = CustomerQuery::create() + ->orderByUpdatedAt() + ->filterById( $customerIds ) + ->offset( $offset ) + ->limit( $limit ) + ->filterByUpdatedAt( $lastUpdate, '>=' ); } } @@ -150,7 +174,14 @@ public static function syncCustomers( Request $request ) $response = SpmCustomers::bulkSave( Utils::getAuth( $requestHeaders ), $data ); if ( !empty( $idShopAskSyncs ) ) { - Utils::updateObjectStatusesCount( $idShopAskSyncs, 'customers', $response, count( $data ) ); + ShopimindSyncStatus::updateObjectStatusesCount( $idShopAskSyncs, 'customers', $response, count( $data ) ); + + $lastObject = end( $data ); + $lastObjectUpdate = $lastObject['updated_at']; + $objectStatus = [ + "last_object_update" => $lastObjectUpdate, + ]; + ShopimindSyncStatus::updateObjectStatuses( $idShopAskSyncs, 'customers', $objectStatus ); } Utils::handleResponse( $response ); diff --git a/PassiveSynchronization/SyncCustomersAddresses.php b/PassiveSynchronization/SyncCustomersAddresses.php index a052dae..d112470 100644 --- a/PassiveSynchronization/SyncCustomersAddresses.php +++ b/PassiveSynchronization/SyncCustomersAddresses.php @@ -45,6 +45,12 @@ public static function processSyncCustomersAddresses( $lastUpdate, $ids, $reques if ( !empty( $idShopAskSyncs ) ) { ShopimindSyncStatus::updateShopimindSyncStatus( $idShopAskSyncs, 'customers_addresses' ); + $objectStatus = ShopimindSyncStatus::getObjectStatus( $idShopAskSyncs, 'customers_addresses' ); + $oldCount = !empty( $objectStatus ) ? $objectStatus['total_objects_count'] : 0; + if( $oldCount > 0 ){ + $count = $oldCount; + } + $objectStatus = [ "status" => "in_progress", "total_objects_count" => $count, @@ -119,16 +125,38 @@ public static function syncCustomersAddresses( Request $request ) do { if ( empty( $lastUpdate ) ) { if ( empty( $customerAddressesIds ) ) { - $customersAddresses = AddressQuery::create()->offset( $offset )->limit( $limit )->orderBy('customer_id')->find(); + $customersAddresses = AddressQuery::create() + ->orderByUpdatedAt() + ->offset( $offset ) + ->limit( $limit ) + ->orderBy('customer_id') + ->find(); }else { - $customersAddresses = AddressQuery::create()->filterById( $customerAddressesIds )->offset( $offset )->limit( $limit )->orderBy('customer_id')->find(); + $customersAddresses = AddressQuery::create() + ->orderByUpdatedAt() + ->filterById( $customerAddressesIds ) + ->offset( $offset ) + ->limit( $limit ) + ->orderBy('customer_id') + ->find(); } } else { $lastUpdate = trim( $lastUpdate, '"\''); if ( empty( $customerAddressesIds ) ) { - $customersAddresses = AddressQuery::create()->offset( $offset )->limit( $limit )->orderBy('customer_id')->filterByUpdatedAt( $lastUpdate, '>=' ); + $customersAddresses = AddressQuery::create() + ->orderByUpdatedAt() + ->offset( $offset ) + ->limit( $limit ) + ->orderBy('customer_id') + ->filterByUpdatedAt( $lastUpdate, '>=' ); }else { - $customersAddresses = AddressQuery::create()->filterById( $customerAddressesIds )->offset( $offset )->limit( $limit )->orderBy('customer_id')->filterByUpdatedAt( $lastUpdate, '>=' ); + $customersAddresses = AddressQuery::create() + ->orderByUpdatedAt() + ->filterById( $customerAddressesIds ) + ->offset( $offset ) + ->limit( $limit ) + ->orderBy('customer_id') + ->filterByUpdatedAt( $lastUpdate, '>=' ); } } @@ -150,7 +178,14 @@ public static function syncCustomersAddresses( Request $request ) $response = SpmCustomersAddresses::bulkSave( Utils::getAuth( $requestHeaders ), $customerId, $value ); if ( !empty( $idShopAskSyncs ) ) { - Utils::updateObjectStatusesCount( $idShopAskSyncs, 'customers_addresses', $response, count( $value ) ); + ShopimindSyncStatus::updateObjectStatusesCount( $idShopAskSyncs, 'customers_addresses', $response, count( $value ) ); + + $lastObject = end( $value ); + $lastObjectUpdate = $lastObject['updated_at']; + $objectStatus = [ + "last_object_update" => $lastObjectUpdate, + ]; + ShopimindSyncStatus::updateObjectStatuses( $idShopAskSyncs, 'customers_addresses', $objectStatus ); } Utils::handleResponse( $response ); diff --git a/PassiveSynchronization/SyncCustomersGroups.php b/PassiveSynchronization/SyncCustomersGroups.php index be8bc60..66455cb 100644 --- a/PassiveSynchronization/SyncCustomersGroups.php +++ b/PassiveSynchronization/SyncCustomersGroups.php @@ -48,7 +48,13 @@ public static function processSyncCustomersGroups( $lastUpdate, $ids, $requested if ( !empty( $idShopAskSyncs ) ) { ShopimindSyncStatus::updateShopimindSyncStatus( $idShopAskSyncs, 'customers_groups' ); - + + $objectStatus = ShopimindSyncStatus::getObjectStatus( $idShopAskSyncs, 'customers_groups' ); + $oldCount = !empty( $objectStatus ) ? $objectStatus['total_objects_count'] : 0; + if( $oldCount > 0 ){ + $count = $oldCount; + } + $objectStatus = [ "status" => "in_progress", "total_objects_count" => $count, @@ -126,16 +132,34 @@ public static function syncCustomersGroups( Request $request ) do { if ( empty( $lastUpdate ) ) { if ( empty( $customersGroupsIds ) ) { - $customersGroups = CustomerFamilyQuery::create()->offset( $offset )->limit( $limit )->find(); + $customersGroups = CustomerFamilyQuery::create() + ->orderByUpdatedAt() + ->offset( $offset ) + ->limit( $limit ) + ->find(); }else { - $customersGroups = CustomerFamilyQuery::create()->filterById( $customersGroupsIds )->offset( $offset )->limit( $limit )->find(); + $customersGroups = CustomerFamilyQuery::create() + ->orderByUpdatedAt() + ->filterById( $customersGroupsIds ) + ->offset( $offset ) + ->limit( $limit ) + ->find(); } } else { $lastUpdate = trim( $lastUpdate, '"\''); if ( empty( $customersGroupsIds ) ) { - $customersGroups = CustomerFamilyQuery::create()->offset( $offset )->limit( $limit )->filterByUpdatedAt( $lastUpdate, '>=' ); + $customersGroups = CustomerFamilyQuery::create() + ->orderByUpdatedAt() + ->offset( $offset ) + ->limit( $limit ) + ->filterByUpdatedAt( $lastUpdate, '>=' ); }else { - $customersGroups = CustomerFamilyQuery::create()->filterById( $customersGroupsIds )->offset( $offset )->limit( $limit )->filterByUpdatedAt( $lastUpdate, '>=' ); + $customersGroups = CustomerFamilyQuery::create() + ->orderByUpdatedAt() + ->filterById( $customersGroupsIds ) + ->offset( $offset ) + ->limit( $limit ) + ->filterByUpdatedAt( $lastUpdate, '>=' ); } } @@ -161,7 +185,14 @@ public static function syncCustomersGroups( Request $request ) $response = SpmCustomersGroups::bulkSave( Utils::getAuth( $requestHeaders ), $data ); if ( !empty( $idShopAskSyncs ) ) { - Utils::updateObjectStatusesCount( $idShopAskSyncs, 'customers_groups', $response, count( $data ) ); + ShopimindSyncStatus::updateObjectStatusesCount( $idShopAskSyncs, 'customers_groups', $response, count( $data ) ); + + $lastObject = end( $data ); + $lastObjectUpdate = $lastObject['updated_at']; + $objectStatus = [ + "last_object_update" => $lastObjectUpdate, + ]; + ShopimindSyncStatus::updateObjectStatuses( $idShopAskSyncs, 'customers_groups', $objectStatus ); } Utils::handleResponse( $response ); diff --git a/PassiveSynchronization/SyncNewsletterSubscribers.php b/PassiveSynchronization/SyncNewsletterSubscribers.php index 093c132..e50db73 100644 --- a/PassiveSynchronization/SyncNewsletterSubscribers.php +++ b/PassiveSynchronization/SyncNewsletterSubscribers.php @@ -45,6 +45,12 @@ public static function processSyncNewsletterSubscribers( $lastUpdate, $ids, $req if ( !empty( $idShopAskSyncs ) ) { ShopimindSyncStatus::updateShopimindSyncStatus( $idShopAskSyncs, 'newsletter_subscribers' ); + $objectStatus = ShopimindSyncStatus::getObjectStatus( $idShopAskSyncs, 'newsletter_subscribers' ); + $oldCount = !empty( $objectStatus ) ? $objectStatus['total_objects_count'] : 0; + if( $oldCount > 0 ){ + $count = $oldCount; + } + $objectStatus = [ "status" => "in_progress", "total_objects_count" => $count, @@ -119,16 +125,34 @@ public static function syncNewsletterSubscribers( Request $request ) do { if ( empty( $lastUpdate ) ) { if ( empty( $newsletterSubscriberIds ) ) { - $newsletters = NewsletterQuery::create()->offset( $offset )->limit( $limit )->find(); + $newsletters = NewsletterQuery::create() + ->orderByUpdatedAt() + ->offset( $offset ) + ->limit( $limit ) + ->find(); }else { - $newsletters = NewsletterQuery::create()->filterById( $newsletterSubscriberIds )->offset( $offset )->limit( $limit )->find(); + $newsletters = NewsletterQuery::create() + ->orderByUpdatedAt() + ->filterById( $newsletterSubscriberIds ) + ->offset( $offset ) + ->limit( $limit ) + ->find(); } } else { $lastUpdate = trim( $lastUpdate, '"\''); if ( empty( $newsletterSubscriberIds ) ) { - $newsletters = NewsletterQuery::create()->offset( $offset )->limit( $limit )->filterByUpdatedAt( $lastUpdate, '>=' ); + $newsletters = NewsletterQuery::create() + ->orderByUpdatedAt() + ->offset( $offset ) + ->limit( $limit ) + ->filterByUpdatedAt( $lastUpdate, '>=' ); }else { - $newsletters = NewsletterQuery::create()->offset( $offset )->filterById( $newsletterSubscriberIds )->limit( $limit )->filterByUpdatedAt( $lastUpdate, '>=' ); + $newsletters = NewsletterQuery::create() + ->orderByUpdatedAt() + ->offset( $offset ) + ->filterById( $newsletterSubscriberIds ) + ->limit( $limit ) + ->filterByUpdatedAt( $lastUpdate, '>=' ); } } @@ -148,14 +172,20 @@ public static function syncNewsletterSubscribers( Request $request ) $response = SpmNewsletterSubscribers::bulkSave( Utils::getAuth( $requestHeaders ), $data ); if ( !empty( $idShopAskSyncs ) ) { - Utils::updateObjectStatusesCount( $idShopAskSyncs, 'newsletter_subscribers', $response, count( $data ) ); + ShopimindSyncStatus::updateObjectStatusesCount( $idShopAskSyncs, 'newsletter_subscribers', $response, count( $data ) ); + + $lastObject = end( $data ); + $lastObjectUpdate = $lastObject['updated_at']; + $objectStatus = [ + "last_object_update" => $lastObjectUpdate, + ]; + ShopimindSyncStatus::updateObjectStatuses( $idShopAskSyncs, 'newsletter_subscribers', $objectStatus ); } Utils::handleResponse( $response ); Utils::log( 'newsletterSubscribers' , 'passive synchronization', json_encode( $response ) ); } - } while ( $hasMore ); } catch (\Throwable $th) { diff --git a/PassiveSynchronization/SyncOrderStatus.php b/PassiveSynchronization/SyncOrderStatus.php index d5b5ce0..da505b7 100644 --- a/PassiveSynchronization/SyncOrderStatus.php +++ b/PassiveSynchronization/SyncOrderStatus.php @@ -49,6 +49,12 @@ public static function processSyncOrderStatus( $lastUpdate, $ids, $requestedBy, if ( !empty( $idShopAskSyncs ) ) { ShopimindSyncStatus::updateShopimindSyncStatus( $idShopAskSyncs, 'orders_statuses' ); + $objectStatus = ShopimindSyncStatus::getObjectStatus( $idShopAskSyncs, 'orders_statuses' ); + $oldCount = !empty( $objectStatus ) ? $objectStatus['total_objects_count'] : 0; + if( $oldCount > 0 ){ + $count = $oldCount; + } + $objectStatus = [ "status" => "in_progress", "total_objects_count" => $count, @@ -126,16 +132,34 @@ public function syncOrderStatus( Request $request ) do { if ( empty( $lastUpdate ) ) { if ( empty( $orderStatuesesIds ) ) { - $ordersStatuses = OrderStatusQuery::create()->offset( $offset )->limit( $limit )->find(); + $ordersStatuses = OrderStatusQuery::create() + ->orderByUpdatedAt() + ->offset( $offset ) + ->limit( $limit ) + ->find(); }else { - $ordersStatuses = OrderStatusQuery::create()->filterById( $orderStatuesesIds )->offset( $offset )->limit( $limit )->find(); + $ordersStatuses = OrderStatusQuery::create() + ->orderByUpdatedAt() + ->filterById( $orderStatuesesIds ) + ->offset( $offset ) + ->limit( $limit ) + ->find(); } } else { $lastUpdate = trim( $lastUpdate, '"\''); if ( empty( $orderStatuesesIds ) ) { - $ordersStatuses = OrderStatusQuery::create()->offset( $offset )->limit( $limit )->filterByUpdatedAt( $lastUpdate, '>=' ); + $ordersStatuses = OrderStatusQuery::create() + ->orderByUpdatedAt() + ->offset( $offset ) + ->limit( $limit ) + ->filterByUpdatedAt( $lastUpdate, '>=' ); }else { - $ordersStatuses = OrderStatusQuery::create()->filterById( $orderStatuesesIds )->offset( $offset )->limit( $limit )->filterByUpdatedAt( $lastUpdate, '>=' ); + $ordersStatuses = OrderStatusQuery::create() + ->orderByUpdatedAt() + ->filterById( $orderStatuesesIds ) + ->offset( $offset ) + ->limit( $limit ) + ->filterByUpdatedAt( $lastUpdate, '>=' ); } } @@ -162,14 +186,20 @@ public function syncOrderStatus( Request $request ) $response = SpmOrdersStatus::bulkSave( Utils::getAuth( $requestHeaders ), $data ); if ( !empty( $idShopAskSyncs ) ) { - Utils::updateObjectStatusesCount( $idShopAskSyncs, 'orders_statuses', $response, count( $data ) ); + ShopimindSyncStatus::updateObjectStatusesCount( $idShopAskSyncs, 'orders_statuses', $response, count( $data ) ); + + $lastObject = end( $data ); + $lastObjectUpdate = $lastObject['updated_at']; + $objectStatus = [ + "last_object_update" => $lastObjectUpdate, + ]; + ShopimindSyncStatus::updateObjectStatuses( $idShopAskSyncs, 'orders_statuses', $objectStatus ); } Utils::handleResponse( $response ); Utils::log( 'orderStatuses' , 'passive synchronization', json_encode( $response ) ); } - } while ( $hasMore ); } catch (\Throwable $th) { diff --git a/PassiveSynchronization/SyncOrders.php b/PassiveSynchronization/SyncOrders.php index 6e77e82..8e62677 100644 --- a/PassiveSynchronization/SyncOrders.php +++ b/PassiveSynchronization/SyncOrders.php @@ -45,6 +45,12 @@ public static function processSyncOrders( $lastUpdate, $ids, $requestedBy, $idSh if ( !empty( $idShopAskSyncs ) ) { ShopimindSyncStatus::updateShopimindSyncStatus( $idShopAskSyncs, 'orders' ); + $objectStatus = ShopimindSyncStatus::getObjectStatus( $idShopAskSyncs, 'orders' ); + $oldCount = !empty( $objectStatus ) ? $objectStatus['total_objects_count'] : 0; + if( $oldCount > 0 ){ + $count = $oldCount; + } + $objectStatus = [ "status" => "in_progress", "total_objects_count" => $count, @@ -119,16 +125,34 @@ public static function syncOrders( Request $request ) do { if ( empty( $lastUpdate ) ) { if ( empty( $ordersIds ) ) { - $orders = OrderQuery::create()->offset( $offset )->limit( $limit )->find(); + $orders = OrderQuery::create() + ->orderByUpdatedAt() + ->offset( $offset ) + ->limit( $limit ) + ->find(); }else { - $orders = OrderQuery::create()->filterById( $ordersIds )->offset( $offset )->limit( $limit )->find(); + $orders = OrderQuery::create() + ->orderByUpdatedAt() + ->filterById( $ordersIds ) + ->offset( $offset ) + ->limit( $limit ) + ->find(); } } else { $lastUpdate = trim( $lastUpdate, '"\''); if ( empty( $ordersIds ) ) { - $orders = OrderQuery::create()->offset( $offset )->limit( $limit )->filterByUpdatedAt( $lastUpdate, '>=' ); + $orders = OrderQuery::create() + ->orderByUpdatedAt() + ->offset( $offset ) + ->limit( $limit ) + ->filterByUpdatedAt( $lastUpdate, '>=' ); }else { - $orders = OrderQuery::create()->filterById( $ordersIds )->offset( $offset )->limit( $limit )->filterByUpdatedAt( $lastUpdate, '>=' ); + $orders = OrderQuery::create() + ->orderByUpdatedAt() + ->filterById( $ordersIds ) + ->offset( $offset ) + ->limit( $limit ) + ->filterByUpdatedAt( $lastUpdate, '>=' ); } } @@ -148,7 +172,14 @@ public static function syncOrders( Request $request ) $response = SpmOrders::bulkSave( Utils::getAuth( $requestHeaders ), $data ); if ( !empty( $idShopAskSyncs ) ) { - Utils::updateObjectStatusesCount( $idShopAskSyncs, 'orders', $response, count( $data ) ); + ShopimindSyncStatus::updateObjectStatusesCount( $idShopAskSyncs, 'orders', $response, count( $data ) ); + + $lastObject = end( $data ); + $lastObjectUpdate = $lastObject['updated_at']; + $objectStatus = [ + "last_object_update" => $lastObjectUpdate, + ]; + ShopimindSyncStatus::updateObjectStatuses( $idShopAskSyncs, 'orders', $objectStatus ); } Utils::handleResponse( $response ); diff --git a/PassiveSynchronization/SyncProducts.php b/PassiveSynchronization/SyncProducts.php index 7678779..f6dbf17 100644 --- a/PassiveSynchronization/SyncProducts.php +++ b/PassiveSynchronization/SyncProducts.php @@ -50,10 +50,16 @@ public static function processSyncProducts( $lastUpdate, $ids, $requestedBy, $id if ( !empty( $idShopAskSyncs ) ) { ShopimindSyncStatus::updateShopimindSyncStatus( $idShopAskSyncs, 'products' ); - + + $objectStatus = ShopimindSyncStatus::getObjectStatus( $idShopAskSyncs, 'products' ); + $oldCount = !empty( $objectStatus ) ? $objectStatus['total_objects_count'] : 0; + if( $oldCount > 0 ){ + $count = $oldCount; + } + $objectStatus = [ "status" => "in_progress", - "total_objects_count" => $count, + "total_objects_count" => $count + $oldCount, ]; ShopimindSyncStatus::updateObjectStatuses( $idShopAskSyncs, 'products', $objectStatus ); } @@ -128,16 +134,34 @@ public static function syncProducts( Request $request, EventDispatcherInterface do { if ( empty( $lastUpdate ) ) { if ( empty( $productsIds ) ) { - $products = ProductQuery::create()->offset( $offset )->limit( $limit )->find(); + $products = ProductQuery::create() + ->orderByUpdatedAt() + ->offset( $offset ) + ->limit( $limit ) + ->find(); }else { - $products = ProductQuery::create()->filterById( $productsIds )->offset( $offset )->limit( $limit )->find(); + $products = ProductQuery::create() + ->orderByUpdatedAt() + ->filterById( $productsIds ) + ->offset( $offset ) + ->limit( $limit ) + ->find(); } } else { $lastUpdate = trim( $lastUpdate, '"\''); if ( empty( $productsIds ) ) { - $products = ProductQuery::create()->offset( $offset )->limit( $limit )->filterByUpdatedAt( $lastUpdate, '>=' ); + $products = ProductQuery::create() + ->orderByUpdatedAt() + ->offset( $offset ) + ->limit( $limit ) + ->filterByUpdatedAt( $lastUpdate, '>=' ); }else { - $products = ProductQuery::create()->filterById( $productsIds )->offset( $offset )->limit( $limit )->filterByUpdatedAt( $lastUpdate, '>=' ); + $products = ProductQuery::create() + ->orderByUpdatedAt() + ->filterById( $productsIds ) + ->offset( $offset ) + ->limit( $limit ) + ->filterByUpdatedAt( $lastUpdate, '>=' ); } } @@ -164,13 +188,21 @@ public static function syncProducts( Request $request, EventDispatcherInterface $response = SpmProducts::bulkSave( Utils::getAuth( $requestHeaders ), $data ); if ( !empty( $idShopAskSyncs ) ) { - Utils::updateObjectStatusesCount( $idShopAskSyncs, 'products', $response, count( $data ) ); + ShopimindSyncStatus::updateObjectStatusesCount( $idShopAskSyncs, 'products', $response, count( $data ) ); + + $lastObject = end( $data ); + $lastObjectUpdate = $lastObject['updated_at']; + $objectStatus = [ + "last_object_update" => $lastObjectUpdate, + ]; + ShopimindSyncStatus::updateObjectStatuses( $idShopAskSyncs, 'products', $objectStatus ); } Utils::handleResponse( $response ); Utils::log( 'products' , 'passive synchronization' , json_encode( $response ) ); } + } while ( $hasMore ); } catch (\Throwable $th) { diff --git a/PassiveSynchronization/SyncProductsCategories.php b/PassiveSynchronization/SyncProductsCategories.php index b22deab..b88696c 100644 --- a/PassiveSynchronization/SyncProductsCategories.php +++ b/PassiveSynchronization/SyncProductsCategories.php @@ -49,6 +49,12 @@ public static function processSyncProductsCategories( $lastUpdate, $ids, $reques if ( !empty( $idShopAskSyncs ) ) { ShopimindSyncStatus::updateShopimindSyncStatus( $idShopAskSyncs, 'products_categories' ); + $objectStatus = ShopimindSyncStatus::getObjectStatus( $idShopAskSyncs, 'products_categories' ); + $oldCount = !empty( $objectStatus ) ? $objectStatus['total_objects_count'] : 0; + if( $oldCount > 0 ){ + $count = $oldCount; + } + $objectStatus = [ "status" => "in_progress", "total_objects_count" => $count, @@ -126,16 +132,34 @@ public static function syncProductsCategories( Request $request ) do { if ( empty( $lastUpdate ) ) { if ( empty( $productsCategoriesIds ) ) { - $categories = CategoryQuery::create()->offset( $offset )->limit( $limit )->find(); + $categories = CategoryQuery::create() + ->orderByUpdatedAt() + ->offset( $offset ) + ->limit( $limit ) + ->find(); }else { - $categories = CategoryQuery::create()->filterById( $productsCategoriesIds )->offset( $offset )->limit( $limit )->find(); + $categories = CategoryQuery::create() + ->orderByUpdatedAt() + ->filterById( $productsCategoriesIds ) + ->offset( $offset ) + ->limit( $limit ) + ->find(); } } else { $lastUpdate = trim( $lastUpdate, '"\''); if ( empty( $productsCategoriesIds ) ) { - $categories = CategoryQuery::create()->offset( $offset )->limit( $limit )->filterByUpdatedAt( $lastUpdate, '>=' ); + $categories = CategoryQuery::create() + ->orderByUpdatedAt() + ->offset( $offset ) + ->limit( $limit ) + ->filterByUpdatedAt( $lastUpdate, '>=' ); }else { - $categories = CategoryQuery::create()->filterById( $productsCategoriesIds )->offset( $offset )->limit( $limit )->filterByUpdatedAt( $lastUpdate, '>=' ); + $categories = CategoryQuery::create() + ->orderByUpdatedAt() + ->filterById( $productsCategoriesIds ) + ->offset( $offset ) + ->limit( $limit ) + ->filterByUpdatedAt( $lastUpdate, '>=' ); } } @@ -162,14 +186,20 @@ public static function syncProductsCategories( Request $request ) $response = SpmProductsCategories::bulkSave( Utils::getAuth( $requestHeaders ), $data ); if ( !empty( $idShopAskSyncs ) ) { - Utils::updateObjectStatusesCount( $idShopAskSyncs, 'products_categories', $response, count( $data ) ); + ShopimindSyncStatus::updateObjectStatusesCount( $idShopAskSyncs, 'products_categories', $response, count( $data ) ); + + $lastObject = end( $data ); + $lastObjectUpdate = $lastObject['updated_at']; + $objectStatus = [ + "last_object_update" => $lastObjectUpdate, + ]; + ShopimindSyncStatus::updateObjectStatuses( $idShopAskSyncs, 'products_categories', $objectStatus ); } Utils::handleResponse( $response ); Utils::log( 'productCategories' , 'passive synchronization', json_encode( $response ) ); } - } while ( $hasMore ); } catch (\Throwable $th) { diff --git a/PassiveSynchronization/SyncProductsImages.php b/PassiveSynchronization/SyncProductsImages.php index 0a83fee..9439d92 100644 --- a/PassiveSynchronization/SyncProductsImages.php +++ b/PassiveSynchronization/SyncProductsImages.php @@ -51,6 +51,12 @@ public static function processSyncProductsImages( $lastUpdate, $ids, $requestedB if ( !empty( $idShopAskSyncs ) ) { ShopimindSyncStatus::updateShopimindSyncStatus( $idShopAskSyncs, 'products_images' ); + $objectStatus = ShopimindSyncStatus::getObjectStatus( $idShopAskSyncs, 'products_images' ); + $oldCount = !empty( $objectStatus ) ? $objectStatus['total_objects_count'] : 0; + if( $oldCount > 0 ){ + $count = $oldCount; + } + $objectStatus = [ "status" => "in_progress", "total_objects_count" => $count, @@ -127,16 +133,38 @@ public static function syncProductsImages( Request $request, EventDispatcherInte do { if ( empty( $lastUpdate ) ) { if ( empty( $productsImagesIds ) ) { - $productImages = ProductImageQuery::create()->offset( $offset )->limit( $limit )->orderBy('product_id')->find(); + $productImages = ProductImageQuery::create() + ->orderByUpdatedAt() + ->offset( $offset ) + ->limit( $limit ) + ->orderBy('product_id') + ->find(); }else { - $productImages = ProductImageQuery::create()->filterById( $productsImagesIds )->offset( $offset )->limit( $limit )->orderBy('product_id')->find(); + $productImages = ProductImageQuery::create() + ->orderByUpdatedAt() + ->filterById( $productsImagesIds ) + ->offset( $offset ) + ->limit( $limit ) + ->orderBy('product_id') + ->find(); } } else { $lastUpdate = trim( $lastUpdate, '"\''); if ( empty( $productsImagesIds ) ) { - $productImages = ProductImageQuery::create()->offset( $offset )->limit( $limit )->orderBy('product_id')->filterByUpdatedAt( $lastUpdate, '>=' ); + $productImages = ProductImageQuery::create() + ->orderByUpdatedAt() + ->offset( $offset ) + ->limit( $limit ) + ->orderBy('product_id') + ->filterByUpdatedAt( $lastUpdate, '>=' ); }else { - $productImages = ProductImageQuery::create()->filterById( $productsImagesIds )->offset( $offset )->limit( $limit )->orderBy('product_id')->filterByUpdatedAt( $lastUpdate, '>=' ); + $productImages = ProductImageQuery::create() + ->orderByUpdatedAt() + ->filterById( $productsImagesIds ) + ->offset( $offset ) + ->limit( $limit ) + ->orderBy('product_id') + ->filterByUpdatedAt( $lastUpdate, '>=' ); } } @@ -160,12 +188,19 @@ public static function syncProductsImages( Request $request, EventDispatcherInte $response = SpmProductsImages::bulkSave( Utils::getAuth( $requestHeaders ), $productId, $value ); if ( !empty( $idShopAskSyncs ) ) { - Utils::updateObjectStatusesCount( $idShopAskSyncs, 'products_images', $response, count( $value ) ); + ShopimindSyncStatus::updateObjectStatusesCount( $idShopAskSyncs, 'products_images', $response, count( $value ) ); + + $lastObject = end( $value ); + $lastObjectUpdate = $lastObject['updated_at']; + $objectStatus = [ + "last_object_update" => $lastObjectUpdate, + ]; + ShopimindSyncStatus::updateObjectStatuses( $idShopAskSyncs, 'products_images', $objectStatus ); } Utils::handleResponse( $response ); - Utils::log( 'productImage' , 'passive synchronization', json_encode( $response ) ); + Utils::log( 'productImage' , 'passive synchronization', json_encode( $value ) ); } } } while ( $hasMore ); diff --git a/PassiveSynchronization/SyncProductsManufacturers.php b/PassiveSynchronization/SyncProductsManufacturers.php index 08df161..88fabcd 100644 --- a/PassiveSynchronization/SyncProductsManufacturers.php +++ b/PassiveSynchronization/SyncProductsManufacturers.php @@ -45,6 +45,12 @@ public static function processSyncProductsManufacturers( $lastUpdate, $ids, $req if ( !empty( $idShopAskSyncs ) ) { ShopimindSyncStatus::updateShopimindSyncStatus( $idShopAskSyncs, 'products_manufacturers' ); + $objectStatus = ShopimindSyncStatus::getObjectStatus( $idShopAskSyncs, 'products_manufacturers' ); + $oldCount = !empty( $objectStatus ) ? $objectStatus['total_objects_count'] : 0; + if( $oldCount > 0 ){ + $count = $oldCount; + } + $objectStatus = [ "status" => "in_progress", "total_objects_count" => $count, @@ -119,16 +125,34 @@ public static function syncProductsManufacturers( Request $request ) do { if ( empty( $lastUpdate ) ) { if ( empty( $manufacturesIds ) ) { - $productsManufacturers = BrandQuery::create()->offset( $offset )->limit( $limit )->find(); + $productsManufacturers = BrandQuery::create() + ->orderByUpdatedAt() + ->offset( $offset ) + ->limit( $limit ) + ->find(); }else { - $productsManufacturers = BrandQuery::create()->filterById( $manufacturesIds )->offset( $offset )->limit( $limit )->find(); + $productsManufacturers = BrandQuery::create() + ->orderByUpdatedAt() + ->filterById( $manufacturesIds ) + ->offset( $offset ) + ->limit( $limit ) + ->find(); } } else { $lastUpdate = trim( $lastUpdate, '"\''); if ( empty( $manufacturesIds ) ) { - $productsManufacturers = BrandQuery::create()->offset( $offset )->limit( $limit )->filterByUpdatedAt( $lastUpdate, '>=' ); + $productsManufacturers = BrandQuery::create() + ->orderByUpdatedAt() + ->offset( $offset ) + ->limit( $limit ) + ->filterByUpdatedAt( $lastUpdate, '>=' ); }else { - $productsManufacturers = BrandQuery::create()->filterById( $manufacturesIds )->offset( $offset )->limit( $limit )->filterByUpdatedAt( $lastUpdate, '>=' ); + $productsManufacturers = BrandQuery::create() + ->orderByUpdatedAt() + ->filterById( $manufacturesIds ) + ->offset( $offset ) + ->limit( $limit ) + ->filterByUpdatedAt( $lastUpdate, '>=' ); } } @@ -148,7 +172,14 @@ public static function syncProductsManufacturers( Request $request ) $response = SpmProductsManufacturers::bulkSave( Utils::getAuth( $requestHeaders ), $data ); if ( !empty( $idShopAskSyncs ) ) { - Utils::updateObjectStatusesCount( $idShopAskSyncs, 'products_manufacturers', $response, count( $data ) ); + ShopimindSyncStatus::updateObjectStatusesCount( $idShopAskSyncs, 'products_manufacturers', $response, count( $data ) ); + + $lastObject = end( $data ); + $lastObjectUpdate = $lastObject['updated_at']; + $objectStatus = [ + "last_object_update" => $lastObjectUpdate, + ]; + ShopimindSyncStatus::updateObjectStatuses( $idShopAskSyncs, 'products_manufacturers', $objectStatus ); } Utils::handleResponse( $response ); diff --git a/PassiveSynchronization/SyncProductsVariations.php b/PassiveSynchronization/SyncProductsVariations.php index d78e8fd..4d7232e 100644 --- a/PassiveSynchronization/SyncProductsVariations.php +++ b/PassiveSynchronization/SyncProductsVariations.php @@ -53,6 +53,12 @@ public static function processSyncProductsVariations ( $lastUpdate, $ids, $reque if ( !empty( $idShopAskSyncs ) ) { ShopimindSyncStatus::updateShopimindSyncStatus( $idShopAskSyncs, 'products_variations' ); + $objectStatus = ShopimindSyncStatus::getObjectStatus( $idShopAskSyncs, 'products_variations' ); + $oldCount = !empty( $objectStatus ) ? $objectStatus['total_objects_count'] : 0; + if( $oldCount > 0 ){ + $count = $oldCount; + } + $objectStatus = [ "status" => "in_progress", "total_objects_count" => $count, @@ -130,16 +136,38 @@ public static function syncProductsVariations( Request $request, EventDispatcher do { if ( empty( $lastUpdate ) ) { if ( empty( $productsVariationsIds ) ) { - $productsVariations = ProductSaleElementsQuery::create()->offset( $offset )->limit( $limit )->orderBy('product_id')->find(); + $productsVariations = ProductSaleElementsQuery::create() + ->orderByUpdatedAt() + ->offset( $offset ) + ->limit( $limit ) + ->orderBy('product_id') + ->find(); }else { - $productsVariations = ProductSaleElementsQuery::create()->filterById( $productsVariationsIds )->offset( $offset )->limit( $limit )->orderBy('product_id')->find(); + $productsVariations = ProductSaleElementsQuery::create() + ->orderByUpdatedAt() + ->filterById( $productsVariationsIds ) + ->offset( $offset ) + ->limit( $limit ) + ->orderBy('product_id') + ->find(); } } else { $lastUpdate = trim( $lastUpdate, '"\''); if ( empty( $productsVariationsIds ) ) { - $productsVariations = ProductSaleElementsQuery::create()->offset( $offset )->limit( $limit )->orderBy('product_id')->filterByUpdatedAt( $lastUpdate, '>=' ); + $productsVariations = ProductSaleElementsQuery::create() + ->orderByUpdatedAt() + ->offset( $offset ) + ->limit( $limit ) + ->orderBy('product_id') + ->filterByUpdatedAt( $lastUpdate, '>=' ); }else { - $productsVariations = ProductSaleElementsQuery::create()->filterById( $productsVariationsIds )->offset( $offset )->limit( $limit )->orderBy('product_id')->filterByUpdatedAt( $lastUpdate, '>=' ); + $productsVariations = ProductSaleElementsQuery::create() + ->orderByUpdatedAt() + ->filterById( $productsVariationsIds ) + ->offset( $offset ) + ->limit( $limit ) + ->orderBy('product_id') + ->filterByUpdatedAt( $lastUpdate, '>=' ); } } @@ -163,7 +191,14 @@ public static function syncProductsVariations( Request $request, EventDispatcher $response = SpmProductsVariations::bulkSave( Utils::getAuth( $requestHeaders ), $productId, $value ); if ( !empty( $idShopAskSyncs ) ) { - Utils::updateObjectStatusesCount( $idShopAskSyncs, 'products_variations', $response, count( $value ) ); + ShopimindSyncStatus::updateObjectStatusesCount( $idShopAskSyncs, 'products_variations', $response, count( $value ) ); + + $lastObject = end( $value ); + $lastObjectUpdate = $lastObject['updated_at']; + $objectStatus = [ + "last_object_update" => $lastObjectUpdate, + ]; + ShopimindSyncStatus::updateObjectStatuses( $idShopAskSyncs, 'products_variations', $objectStatus ); } Utils::handleResponse( $response ); diff --git a/PassiveSynchronization/SyncVouchers.php b/PassiveSynchronization/SyncVouchers.php index ece5172..5d3b1b3 100644 --- a/PassiveSynchronization/SyncVouchers.php +++ b/PassiveSynchronization/SyncVouchers.php @@ -50,6 +50,12 @@ public static function processSyncVouchers( $lastUpdate, $ids, $requestedBy, $id if ( !empty( $idShopAskSyncs ) ) { ShopimindSyncStatus::updateShopimindSyncStatus( $idShopAskSyncs, 'vouchers' ); + $objectStatus = ShopimindSyncStatus::getObjectStatus( $idShopAskSyncs, 'vouchers' ); + $oldCount = !empty( $objectStatus ) ? $objectStatus['total_objects_count'] : 0; + if( $oldCount > 0 ){ + $count = $oldCount; + } + $objectStatus = [ "status" => "in_progress", "total_objects_count" => $count, @@ -127,16 +133,34 @@ public static function syncVouchers( Request $request ) do { if ( empty( $lastUpdate ) ) { if ( empty( $vouchersIds ) ) { - $coupons = CouponQuery::create()->offset( $offset )->limit( $limit )->find(); + $coupons = CouponQuery::create() + ->orderByUpdatedAt() + ->offset( $offset ) + ->limit( $limit ) + ->find(); }else { - $coupons = CouponQuery::create()->filterById( $vouchersIds )->offset( $offset )->limit( $limit )->find(); + $coupons = CouponQuery::create() + ->orderByUpdatedAt() + ->filterById( $vouchersIds ) + ->offset( $offset ) + ->limit( $limit ) + ->find(); } } else { $lastUpdate = trim( $lastUpdate, '"\''); if ( empty( $vouchersIds ) ) { - $coupons = CouponQuery::create()->offset( $offset )->limit( $limit )->filterByUpdatedAt( $lastUpdate, '>=' ); + $coupons = CouponQuery::create() + ->orderByUpdatedAt() + ->offset( $offset ) + ->limit( $limit ) + ->filterByUpdatedAt( $lastUpdate, '>=' ); }else { - $coupons = CouponQuery::create()->filterById( $vouchersIds )->offset( $offset )->limit( $limit )->filterByUpdatedAt( $lastUpdate, '>=' ); + $coupons = CouponQuery::create() + ->orderByUpdatedAt() + ->filterById( $vouchersIds ) + ->offset( $offset ) + ->limit( $limit ) + ->filterByUpdatedAt( $lastUpdate, '>=' ); } } @@ -163,7 +187,14 @@ public static function syncVouchers( Request $request ) $response = SpmVoucher::bulkSave( Utils::getAuth( $requestHeaders ), $data ); if ( !empty( $idShopAskSyncs ) ) { - Utils::updateObjectStatusesCount( $idShopAskSyncs, 'vouchers', $response, count( $data ) ); + ShopimindSyncStatus::updateObjectStatusesCount( $idShopAskSyncs, 'vouchers', $response, count( $data ) ); + + $lastObject = end( $data ); + $lastObjectUpdate = $lastObject['updated_at']; + $objectStatus = [ + "last_object_update" => $lastObjectUpdate, + ]; + ShopimindSyncStatus::updateObjectStatuses( $idShopAskSyncs, 'vouchers', $objectStatus ); } Utils::handleResponse( $response ); diff --git a/lib/Utils.php b/lib/Utils.php index 4ee6958..5b67436 100644 --- a/lib/Utils.php +++ b/lib/Utils.php @@ -9,7 +9,6 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\JsonResponse; use Thelia\Model\ModuleQuery; -use Shopimind\Model\ShopimindSyncStatus; class Utils { @@ -288,30 +287,4 @@ public static function isCustomerFamilyActive() return false; } } - - /** - * Updates the count details in the status of a specific object type for synchronization - * The update depends on the value of $respons. - * - * @param $idShopAskSyncs - * @param $objectType - * @param $response - * @param $count - * @return void - */ - public static function updateObjectStatusesCount( $idShopAskSyncs, $objectType, $response, $count ) - { - $objectStatusDetails = ShopimindSyncStatus::getObjectStatus( $idShopAskSyncs, $objectType ); - if ( isset( $response['statusCode'] ) && $response['statusCode'] == 200 ) { - $objectStatus = [ - "sent_successful_count" => $objectStatusDetails['sent_successful_count'] + $count, - ]; - }else { - $objectStatus = [ - "sent_failed_count" => $objectStatusDetails['sent_failed_count'] + $count, - ]; - } - - ShopimindSyncStatus::updateObjectStatuses( $idShopAskSyncs, $objectType, $objectStatus ); - } }