teqo-backup/src/components/campaign/opsSync/opsEstimateOutbox.ts
Cursor Agent 0630995bb2
OH11 — SW static cache-first, logout campaignOps wipe, offline e2e
- Service worker: cache-first for same-origin /_next/static/ (before RSC guard)
- Logout: clearCampaignOpsStorage (abort sync → outbox → persistence) then Cache API
- E2E: full offline journey in campaignOpsOffline (write→reload→online→sync)
- Docs: CHANGELOG + ops-hibrido spec; test:e2e:ops-hybrid script

Closes #173

Co-authored-by: Francisco Solla <fsolla@users.noreply.github.com>
2026-08-02 02:31:58 +00:00

200 lines
6.8 KiB
TypeScript

'use client'
import { createCollection, localOnlyCollectionOptions } from '@tanstack/db'
import {
IndexedDBAdapter,
NonRetriableError,
startOfflineExecutor,
type OfflineExecutor,
} from '@tanstack/offline-transactions'
import { estimateVotesCas } from '@/app/(campaign)/campanha/actions/votePledge'
import {
collapseEstimateOutboxByPledge,
toOpsEstimateOutboxRow,
type EnqueueEstimateVotesInput,
type OpsEstimateOutboxRow,
} from '@/components/campaign/opsSync/opsEstimateOutboxModel'
import {
applyOpsVotePledgeEstimateWrite,
patchOpsVotePledgeEstimateOptimistic,
readOpsVotePledge,
} from '@/components/campaign/opsSync/opsVotePledgeMirror'
import { opsOutboxKey, type OpsOutboxKey } from '@/lib/campaignOps/opsContract'
import {
isOpsEstimateConflictMessage,
parseOpsEstimateConflictServerEstimatedAt,
} from '@/lib/schemas/votePledge'
const ESTIMATE_MUTATION_FN = 'estimateVotes'
const opsEstimateOutboxCollection = createCollection(
localOnlyCollectionOptions<OpsEstimateOutboxRow, number>({
id: 'ops-estimate-outbox',
getKey: (row) => row.pledgeId,
}),
)
let executorSingleton: OfflineExecutor | null = null
let initPromise: Promise<OfflineExecutor> | null = null
const createEstimateOfflineExecutor = (): OfflineExecutor =>
startOfflineExecutor({
collections: { estimateOutbox: opsEstimateOutboxCollection },
storage: new IndexedDBAdapter('teqo-ops-estimate', 'outbox'),
beforeRetry: collapseEstimateOutboxByPledge,
mutationFns: {
[ESTIMATE_MUTATION_FN]: async ({ transaction }) => {
const mutation = transaction.mutations[0]
if (!mutation) {
throw new NonRetriableError('Mutação de estimativa vazia.')
}
const row = mutation.modified as OpsEstimateOutboxRow
try {
const updated = await estimateVotesCas({
pledge: row.pledgeId,
estimatedVotes: row.estimatedVotes,
estimateNote: row.estimateNote,
baseEstimatedAt: row.baseEstimatedAt,
})
applyOpsVotePledgeEstimateWrite(updated)
opsEstimateOutboxCollection.utils.acceptMutations(transaction)
if (opsEstimateOutboxCollection.has(row.pledgeId)) {
opsEstimateOutboxCollection.update(row.pledgeId, (draft) => {
draft.status = 'synced'
})
opsEstimateOutboxCollection.delete(row.pledgeId)
}
} catch (error) {
const message = error instanceof Error ? error.message : String(error)
if (isOpsEstimateConflictMessage(message)) {
const serverEstimatedAt = parseOpsEstimateConflictServerEstimatedAt(message)
if (opsEstimateOutboxCollection.has(row.pledgeId)) {
opsEstimateOutboxCollection.update(row.pledgeId, (draft) => {
draft.status = 'conflict'
draft.serverEstimatedAt = serverEstimatedAt
draft.errorMessage = message
})
}
throw new NonRetriableError(message)
}
if (opsEstimateOutboxCollection.has(row.pledgeId)) {
opsEstimateOutboxCollection.update(row.pledgeId, (draft) => {
draft.status = 'error'
draft.errorMessage = message
})
}
throw error instanceof Error ? error : new Error(message)
}
},
},
})
export const getOpsEstimateOfflineExecutor = async (): Promise<OfflineExecutor> => {
if (executorSingleton) {
await executorSingleton.waitForInit()
return executorSingleton
}
if (!initPromise) {
initPromise = (async () => {
const executor = createEstimateOfflineExecutor()
await executor.waitForInit()
executorSingleton = executor
return executor
})()
}
return initPromise
}
export const enqueueEstimateVotes = async (input: EnqueueEstimateVotesInput): Promise<void> => {
const executor = await getOpsEstimateOfflineExecutor()
const run = executor.createOfflineAction<EnqueueEstimateVotesInput>({
mutationFnName: ESTIMATE_MUTATION_FN,
onMutate: (variables) => {
const mirrorRow = readOpsVotePledge(variables.pledge)
const baseEstimatedAt =
variables.baseEstimatedAt !== undefined
? variables.baseEstimatedAt
: (mirrorRow?.estimatedAt ?? null)
const next = toOpsEstimateOutboxRow({ ...variables, baseEstimatedAt }, 'pending')
patchOpsVotePledgeEstimateOptimistic(next.pledgeId, next.estimatedVotes, next.estimateNote)
if (opsEstimateOutboxCollection.has(next.pledgeId)) {
opsEstimateOutboxCollection.update(next.pledgeId, (draft) => {
draft.estimatedVotes = next.estimatedVotes
draft.estimateNote = next.estimateNote
draft.baseEstimatedAt = next.baseEstimatedAt
draft.status = 'pending'
draft.serverEstimatedAt = undefined
draft.errorMessage = undefined
})
} else {
opsEstimateOutboxCollection.insert(next)
}
},
})
// Fire-and-forget: awaiting isPersisted would hang while offline (retriable
// failures stay queued). UI follows collection status via subscribeChanges.
run(input)
}
export const subscribeOpsEstimateOutboxRow = (
pledgeId: number,
onStoreChange: () => void,
): (() => void) => {
let previous = opsEstimateOutboxCollection.get(pledgeId)
const subscription = opsEstimateOutboxCollection.subscribeChanges(() => {
const next = opsEstimateOutboxCollection.get(pledgeId)
if (next === previous) return
previous = next
onStoreChange()
})
return () => {
subscription.unsubscribe()
}
}
export const readOpsEstimateOutboxRow = (pledgeId: number): OpsEstimateOutboxRow | undefined =>
opsEstimateOutboxCollection.get(pledgeId)
export const discardOpsEstimateOutboxRow = (pledgeId: number): void => {
if (opsEstimateOutboxCollection.has(pledgeId)) {
opsEstimateOutboxCollection.delete(pledgeId)
}
}
/** Pending/conflict pledge ids as merge outbox keys (OH5 — protect local rows). */
export const collectOpsEstimateOutboxKeys = (): Set<OpsOutboxKey> => {
const keys = new Set<OpsOutboxKey>()
for (const row of opsEstimateOutboxCollection.toArray) {
if (row.status === 'pending' || row.status === 'conflict') {
keys.add(opsOutboxKey('votePledges', row.pledgeId))
}
}
return keys
}
/** Logout wipe — outbox storage + in-memory rows (OH11). */
export const clearOpsEstimateOutboxForLogout = async (): Promise<void> => {
if (executorSingleton) {
try {
await executorSingleton.clearOutbox()
executorSingleton.dispose()
} catch {
// Best effort — private mode / torn-down storage.
}
executorSingleton = null
initPromise = null
}
for (const row of opsEstimateOutboxCollection.toArray) {
if (opsEstimateOutboxCollection.has(row.pledgeId)) {
opsEstimateOutboxCollection.delete(row.pledgeId)
}
}
}