diff --git a/src/core/quote-outcomes.mjs b/src/core/quote-outcomes.mjs index e5e4519..b0cb78b 100644 --- a/src/core/quote-outcomes.mjs +++ b/src/core/quote-outcomes.mjs @@ -56,12 +56,15 @@ export function deriveQuoteOutcomeRecords({ const expectedDelta = expectedDeltasByQuote.get(submission.quote_id) || null; if (!expectedDelta) continue; + const submittedTs = timestampValue(submission.submitted_at); + const matches = inventoryDeltas.filter((movement) => ( movementMatchesExpectedDelta({ movement, expectedDelta, - submittedAt: submission.submitted_at, attributionWindowMs, + submittedTs, + movementTs: movement.observed_ts, }) )); if (matches.length) candidatesByQuote.set(submission.quote_id, matches); @@ -152,6 +155,7 @@ export function deriveInventoryDeltas({ inventorySnapshots = [], activeAssetIds deltas.push({ movement_id: `${previous.observed_at}->${current.observed_at}`, observed_at: current.observed_at, + observed_ts: timestampValue(current.observed_at), previous_observed_at: previous.observed_at, inventory_id: current.inventory_id, previous_inventory_id: previous.inventory_id, @@ -343,21 +347,19 @@ function baseOutcomeRecord({ function movementMatchesExpectedDelta({ movement, expectedDelta, - submittedAt, + submittedTs, + movementTs, attributionWindowMs, }) { - const submittedTs = timestampValue(submittedAt); - const movementTs = timestampValue(movement.observed_at); if (!Number.isFinite(submittedTs) || !Number.isFinite(movementTs)) return false; if (movementTs < submittedTs) return false; if (movementTs - submittedTs > attributionWindowMs) return false; for (const [assetId, expected] of Object.entries(expectedDelta)) { - if (safeBigInt(movement.delta_units?.[assetId]) !== expected) return false; - } - for (const [assetId, actual] of Object.entries(movement.delta_units || {})) { - if (Object.prototype.hasOwnProperty.call(expectedDelta, assetId)) continue; - if (safeBigInt(actual) !== 0n) return false; + const actual = safeBigInt(movement.delta_units?.[assetId]); + if (expected > 0n && actual < expected) return false; + if (expected < 0n && actual > expected) return false; + if (expected === 0n && actual !== 0n) return false; } return true; diff --git a/src/lib/postgres.mjs b/src/lib/postgres.mjs index 55b896c..c5baa21 100644 --- a/src/lib/postgres.mjs +++ b/src/lib/postgres.mjs @@ -81,6 +81,7 @@ const REFRESHABLE_INTENT_REQUEST_OUTCOME_STATUSES = [ export function createPostgresPool({ connectionString }) { return new Pool({ connectionString, + max: 30, }); }