Skip to content

Commit

Permalink
added: existing order re-indexing to migration (#517)
Browse files Browse the repository at this point in the history
  • Loading branch information
bhupeshappfoster authored Sep 6, 2024
1 parent 185150d commit 49c5885
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/controllers/FilesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function actionCreateExportZip()
return $this->asFailure($this->getErrorMessage(implode('\n', $errors)));
}

Craft::$app->getElements()->saveElement($order, true, true, true);
Craft::$app->getElements()->saveElement($order);
$transaction->commit();

return $this->asSuccess(null, ['translatedFiles' => $zipDest]);
Expand Down
10 changes: 5 additions & 5 deletions src/controllers/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ public function actionSaveOrder()
$order->wordCount = array_sum($wordCounts);

// Manual Translation will make orders 'in progress' status after creation
$success = Craft::$app->getElements()->saveElement($order, true, true, true);
$success = Craft::$app->getElements()->saveElement($order);

if (!$success) {
Translations::$plugin->logHelper->log('[' . __METHOD__ . '] Couldn’t save the order', Constants::LOG_LEVEL_ERROR);
Expand Down Expand Up @@ -617,7 +617,7 @@ public function actionSaveOrder()
$order->status = Constants::ORDER_STATUS_NEW;
$order->dateOrdered = new DateTime();

$success = Craft::$app->getElements()->saveElement($order, true, true, true);
$success = Craft::$app->getElements()->saveElement($order);

if (! $success) {
Translations::$plugin->logHelper->log('[' . __METHOD__ . '] Couldn’t save the order', Constants::LOG_LEVEL_ERROR);
Expand Down Expand Up @@ -1185,7 +1185,7 @@ public function actionUpdateOrderFilesSource()
}
}

Craft::$app->getElements()->saveElement($order, true, true, false);
Craft::$app->getElements()->saveElement($order);
$transaction->commit();
} catch (\Exception $e) {
$transaction->rollBack();
Expand Down Expand Up @@ -1229,7 +1229,7 @@ public function actionAcceptQuote()

$order->status = Constants::ORDER_STATUS_NEW;
$order->logActivity("Order quote accepted");
Craft::$app->getElements()->saveElement($order, true, true, false);
Craft::$app->getElements()->saveElement($order);

return $this->asSuccess($this->getSuccessMessage("Quote approve request sent"));
}
Expand Down Expand Up @@ -1266,7 +1266,7 @@ public function actionDeclineQuote()

$order->status = Constants::ORDER_STATUS_GETTING_QUOTE;
$order->logActivity("Order quote declined");
Craft::$app->getElements()->saveElement($order, true, true, false);
Craft::$app->getElements()->saveElement($order);

return $this->asSuccess($this->getSuccessMessage("Quote decline request sent"));
}
Expand Down
41 changes: 41 additions & 0 deletions src/migrations/m240829_054957_drop_commerce_draft_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace acclaro\translations\migrations;

use Craft;
use craft\db\Migration;
use acclaro\translations\elements\Order;

/**
* m240829_054957_drop_commerce_draft_table migration.
Expand All @@ -20,6 +22,45 @@ public function safeUp(): bool

echo "Done dropping translations_commercedrafts table...\n";

echo "Re-indexing all existing orders...\n";

$batchSize = 100;
$offset = 0;
$totalProcessed = 0;

while (true) {
$orders = Order::find()
->limit($batchSize)
->offset($offset)
->all();

if (empty($orders)) {
break;
}

foreach ($orders as $order) {
try {
$order = Craft::$app->getElements()->getElementById($order->id, null, $order->sourceSite);
if ($order) {
$order->resaving = true;
if (!Craft::$app->getElements()->saveElement($order)) {
Craft::error('Failed to save order ID: ' . $order->id, __METHOD__);
}
}
} catch (\Throwable $e) {
Craft::error('Error re-saving order ID: ' . $order->id . '. Error: ' . $e->getMessage(), __METHOD__);
throw $e;
}
}

$totalProcessed += count($orders);
$offset += $batchSize;

echo "Processed $totalProcessed orders so far...\n";
}

echo "Done re-indexing all orders.\n";

return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/repository/DraftRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public function applyTranslationDraft($fileId, $file='', $draft='')
$canonical = $draft->getCanonical();
$draft->setFieldValues($canonical->getFieldValues());
// Let's try saving the element prior to applying draft
if (!Craft::$app->getElements()->saveElement($draft, true, true, true)) {
if (!Craft::$app->getElements()->saveElement($draft)) {
throw new InvalidElementException($draft);
}

Expand Down

0 comments on commit 49c5885

Please sign in to comment.