143 lines
4.5 KiB
TypeScript
143 lines
4.5 KiB
TypeScript
import type { ChannelSyncSummary } from "./types.ts";
|
|||
|
|
|
||
|
|
export const STORE_SYNC_POLL_MS = 1_500;
|
||
|
|
export const STORE_SYNC_POLL_TIMEOUT_MS = 5 * 60_000;
|
||
|
|
|
||
|
|
export type StoreSyncKind = "products" | "orders" | "reviews";
|
||
|
|
export type StoreSyncPhase = "queued" | "running" | "completed";
|
||
|
|
|
||
|
|
export type StoreSyncSnapshot = {
|
||
|
|
pending: boolean;
|
||
|
|
syncedAt: string | null;
|
||
|
|
status: string;
|
||
|
|
error: string;
|
||
|
|
summary: ChannelSyncSummary | null;
|
||
|
|
};
|
||
|
|
|
||
|
|
type SyncConfigFields = {
|
||
|
|
pending_sync?: boolean;
|
||
|
|
pending_orders_sync?: boolean;
|
||
|
|
pending_reviews_sync?: boolean;
|
||
|
|
last_synced_at?: string | null;
|
||
|
|
last_orders_synced_at?: string | null;
|
||
|
|
last_reviews_synced_at?: string | null;
|
||
|
|
last_sync_status?: string | null;
|
||
|
|
last_orders_sync_status?: string | null;
|
||
|
|
last_reviews_sync_status?: string | null;
|
||
|
|
last_sync_error?: string | null;
|
||
|
|
last_orders_sync_error?: string | null;
|
||
|
|
last_reviews_sync_error?: string | null;
|
||
|
|
last_sync_summary?: ChannelSyncSummary | null;
|
||
|
|
};
|
||
|
|
|
||
|
|
export function readStoreSyncSnapshot(
|
||
|
|
config: SyncConfigFields,
|
||
|
|
kind: StoreSyncKind
|
||
|
|
): StoreSyncSnapshot {
|
||
|
|
if (kind === "orders") {
|
||
|
|
return {
|
||
|
|
pending: Boolean(config.pending_orders_sync),
|
||
|
|
syncedAt: config.last_orders_synced_at ?? null,
|
||
|
|
status: String(config.last_orders_sync_status ?? ""),
|
||
|
|
error: String(config.last_orders_sync_error ?? ""),
|
||
|
|
summary: null
|
||
|
|
};
|
||
|
|
}
|
||
|
|
if (kind === "reviews") {
|
||
|
|
return {
|
||
|
|
pending: Boolean(config.pending_reviews_sync),
|
||
|
|
syncedAt: config.last_reviews_synced_at ?? null,
|
||
|
|
status: String(config.last_reviews_sync_status ?? ""),
|
||
|
|
error: String(config.last_reviews_sync_error ?? ""),
|
||
|
|
summary: null
|
||
|
|
};
|
||
|
|
}
|
||
|
|
return {
|
||
|
|
pending: Boolean(config.pending_sync),
|
||
|
|
syncedAt: config.last_synced_at ?? null,
|
||
|
|
status: String(config.last_sync_status ?? ""),
|
||
|
|
error: String(config.last_sync_error ?? ""),
|
||
|
|
summary: config.last_sync_summary ?? null
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Derive live phase from pending flag + whether synced_at advanced past baseline. */
|
||
|
|
export function storeSyncPhase(
|
||
|
|
snap: StoreSyncSnapshot,
|
||
|
|
baselineSyncedAt: string | null
|
||
|
|
): StoreSyncPhase {
|
||
|
|
if (snap.pending) return "queued";
|
||
|
|
if (snap.syncedAt && snap.syncedAt !== baselineSyncedAt) return "completed";
|
||
|
|
return "running";
|
||
|
|
}
|
||
|
|
|
||
|
|
export function isFailedStoreSyncStatus(status: string | null | undefined): boolean {
|
||
|
|
const s = (status ?? "").toLowerCase().trim();
|
||
|
|
return s === "failed" || s === "error";
|
||
|
|
}
|
||
|
|
|
||
|
|
export function isRetryableStoreSyncStatus(status: string | null | undefined): boolean {
|
||
|
|
const s = (status ?? "").toLowerCase().trim();
|
||
|
|
return s === "failed" || s === "error" || s === "partial";
|
||
|
|
}
|
||
|
|
|
||
|
|
function sleep(ms: number, signal: AbortSignal): Promise<void> {
|
||
|
|
if (signal.aborted) {
|
||
|
|
return Promise.reject(new DOMException("Aborted", "AbortError"));
|
||
|
|
}
|
||
|
|
return new Promise((resolve, reject) => {
|
||
|
|
const timer = setTimeout(() => {
|
||
|
|
signal.removeEventListener("abort", onAbort);
|
||
|
|
resolve();
|
||
|
|
}, ms);
|
||
|
|
const onAbort = () => {
|
||
|
|
clearTimeout(timer);
|
||
|
|
reject(new DOMException("Aborted", "AbortError"));
|
||
|
|
};
|
||
|
|
signal.addEventListener("abort", onAbort, { once: true });
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
export type PollStoreSyncResult<T> = {
|
||
|
|
config: T;
|
||
|
|
phase: StoreSyncPhase;
|
||
|
|
snap: StoreSyncSnapshot;
|
||
|
|
timedOut: boolean;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Poll GET store config until the sync kind leaves queued/running (synced_at advances)
|
||
|
|
* or timeout. Mirrors feeds pollFeedSyncJob against existing config status fields.
|
||
|
|
*/
|
||
|
|
export async function pollStoreSyncConfig<T extends SyncConfigFields>(opts: {
|
||
|
|
kind: StoreSyncKind;
|
||
|
|
baselineSyncedAt: string | null;
|
||
|
|
signal: AbortSignal;
|
||
|
|
/** Injected GET config fetch (keeps this module free of $lib for node:test). */
|
||
|
|
fetchConfig: (signal: AbortSignal) => Promise<T>;
|
||
|
|
onTick?: (config: T, phase: StoreSyncPhase, snap: StoreSyncSnapshot) => void;
|
||
|
|
intervalMs?: number;
|
||
|
|
timeoutMs?: number;
|
||
|
|
}): Promise<PollStoreSyncResult<T>> {
|
||
|
|
const intervalMs = opts.intervalMs ?? STORE_SYNC_POLL_MS;
|
||
|
|
const timeoutMs = opts.timeoutMs ?? STORE_SYNC_POLL_TIMEOUT_MS;
|
||
|
|
const started = Date.now();
|
||
|
|
|
||
|
|
let config = await opts.fetchConfig(opts.signal);
|
||
|
|
let snap = readStoreSyncSnapshot(config, opts.kind);
|
||
|
|
let phase = storeSyncPhase(snap, opts.baselineSyncedAt);
|
||
|
|
opts.onTick?.(config, phase, snap);
|
||
|
|
|
||
|
|
while (phase !== "completed") {
|
||
|
|
if (Date.now() - started >= timeoutMs) {
|
||
|
|
return { config, phase, snap, timedOut: true };
|
||
|
|
}
|
||
|
|
await sleep(intervalMs, opts.signal);
|
||
|
|
config = await opts.fetchConfig(opts.signal);
|
||
|
|
snap = readStoreSyncSnapshot(config, opts.kind);
|
||
|
|
phase = storeSyncPhase(snap, opts.baselineSyncedAt);
|
||
|
|
opts.onTick?.(config, phase, snap);
|
||
|
|
}
|
||
|
|
|
||
|
|
return { config, phase, snap, timedOut: false };
|
||
|
|
}
|