Recover pending Shopify orders by stable order ID
This commit is contained in:
1 parent
f739106ab9
commit
ba796f0a3f
3 files changed
+52
-5
No files matched your search
@@ -62,6 +62,32 @@ function shopify_delta_reconciliation_cutoff(PDO $pdo): string
|
||||
return ($pendingDate < $stateDate ? $pendingDate : $stateDate)->format(DATE_ATOM);
|
||||
}
|
||||
|
||||
function shopify_delta_load_pending_order_gids(PDO $pdo): array
|
||||
{
|
||||
$stmt = $pdo->query(
|
||||
"SELECT DISTINCT shopify_order_gid
|
||||
FROM shopify_webhook_event
|
||||
WHERE status IN ('received', 'processing', 'failed')
|
||||
AND shopify_order_gid IS NOT NULL
|
||||
ORDER BY shopify_order_gid"
|
||||
);
|
||||
return array_values(array_filter(array_map(
|
||||
static fn (mixed $value): string => trim((string) $value),
|
||||
$stmt->fetchAll(PDO::FETCH_COLUMN)
|
||||
), static fn (string $value): bool => $value !== ''));
|
||||
}
|
||||
|
||||
function shopify_delta_mark_order_events_processed(PDO $pdo, string $orderGid): void
|
||||
{
|
||||
$stmt = $pdo->prepare(
|
||||
"UPDATE shopify_webhook_event
|
||||
SET status = 'processed', processed_at = NOW(), error_code = NULL, error_message = NULL
|
||||
WHERE shopify_order_gid = :shopify_order_gid
|
||||
AND status IN ('received', 'processing', 'failed')"
|
||||
);
|
||||
$stmt->execute([':shopify_order_gid' => $orderGid]);
|
||||
}
|
||||
|
||||
function shopify_delta_store_watermark(PDO $pdo, string $cutoff): void
|
||||
{
|
||||
$stmt = $pdo->prepare(
|
||||
@@ -120,7 +146,7 @@ GQL;
|
||||
return $orders;
|
||||
}
|
||||
|
||||
function run_shopify_delta(array $env, PDO $pdo, string $cutoff, bool $dryRun): array
|
||||
function run_shopify_delta(array $env, PDO $pdo, string $cutoff, bool $dryRun, array $recoveryOrderGids = []): array
|
||||
{
|
||||
$cutoffDate = new DateTimeImmutable($cutoff);
|
||||
$searchQuery = 'created_at:>' . $cutoffDate->format('Y-m-d');
|
||||
@@ -135,14 +161,31 @@ function run_shopify_delta(array $env, PDO $pdo, string $cutoff, bool $dryRun):
|
||||
'clarification_order_gids' => [],
|
||||
];
|
||||
|
||||
$ordersByGid = [];
|
||||
foreach (shopify_delta_load_orders($env, $searchQuery) as $summaryOrder) {
|
||||
$orderGid = trim((string) ($summaryOrder['id'] ?? ''));
|
||||
if ($orderGid !== '') {
|
||||
$ordersByGid[$orderGid] = $summaryOrder;
|
||||
}
|
||||
}
|
||||
foreach ($recoveryOrderGids as $recoveryOrderGid) {
|
||||
if (!isset($ordersByGid[$recoveryOrderGid])) {
|
||||
$ordersByGid[$recoveryOrderGid] = shopify_load_order_by_gid($env, $recoveryOrderGid);
|
||||
}
|
||||
}
|
||||
|
||||
$recoverySet = array_fill_keys($recoveryOrderGids, true);
|
||||
foreach ($ordersByGid as $orderGid => $summaryOrder) {
|
||||
$createdAt = new DateTimeImmutable((string) ($summaryOrder['createdAt'] ?? ''));
|
||||
if ($createdAt <= $cutoffDate) {
|
||||
if (!isset($recoverySet[$orderGid]) && $createdAt <= $cutoffDate) {
|
||||
continue;
|
||||
}
|
||||
$counters['checked_orders']++;
|
||||
if (($summaryOrder['cancelledAt'] ?? null) !== null || strtoupper((string) ($summaryOrder['displayFinancialStatus'] ?? '')) === 'REFUNDED') {
|
||||
$counters['skipped_cancelled_or_refunded']++;
|
||||
if (!$dryRun) {
|
||||
shopify_delta_mark_order_events_processed($pdo, (string) $summaryOrder['id']);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -168,6 +211,9 @@ function run_shopify_delta(array $env, PDO $pdo, string $cutoff, bool $dryRun):
|
||||
$counters['already_imported']++;
|
||||
} else {
|
||||
$counters['handed_off']++;
|
||||
if (!$dryRun) {
|
||||
shopify_delta_mark_order_events_processed($pdo, (string) $summaryOrder['id']);
|
||||
}
|
||||
}
|
||||
} catch (Throwable $error) {
|
||||
$counters['status'] = 'partial_success';
|
||||
|
||||
Reference in new issue
Block a user