diff --git a/src/components/campaign/opsSync/opsEstimateOutbox.ts b/src/components/campaign/opsSync/opsEstimateOutbox.ts index efc47909..554bc83d 100644 --- a/src/components/campaign/opsSync/opsEstimateOutbox.ts +++ b/src/components/campaign/opsSync/opsEstimateOutbox.ts @@ -19,7 +19,7 @@ import { applyOpsVotePledgeEstimateWrite, patchOpsVotePledgeEstimateOptimistic, readOpsVotePledge, -} from '@/components/campaign/opsSync/opsMirrorClient' +} from '@/components/campaign/opsSync/opsVotePledgeMirror' import { opsOutboxKey, type OpsOutboxKey } from '@/lib/campaignOps/opsContract' import { isOpsEstimateConflictMessage, diff --git a/src/components/campaign/opsSync/opsMirrorClient.ts b/src/components/campaign/opsSync/opsMirrorClient.ts index 713debe0..8a6fe238 100644 --- a/src/components/campaign/opsSync/opsMirrorClient.ts +++ b/src/components/campaign/opsSync/opsMirrorClient.ts @@ -8,10 +8,7 @@ import { type OpsMirrorPersistenceMode, type OpsMirrorStore, } from '@/components/campaign/opsSync/opsMirrorPersistence' -import { - mapVotePledgeWriteToOps, - type VotePledgeEstimateWriteResult, -} from '@/lib/campaignOps/mapVotePledgeWriteToOps' +import { votePledgesCollection } from '@/components/campaign/opsSync/opsVotePledgeMirror' import { createEmptyOpsSnapshot, type OpsActivity, @@ -24,12 +21,10 @@ import { type OpsOutboxKey, type OpsSnapshot, type OpsStateDeputy, - type OpsVotePledge, } from '@/lib/campaignOps/opsContract' import { mergeOpsSnapshot } from '@/lib/campaignOps/opsMerge' import { OPS_MIRROR_SCHEMA_VERSION } from '@/lib/campaignOps/opsMirrorVersion' import type { OpsSyncState } from '@/lib/campaignOps/opsSyncMeta' -import type { VoteEstimateScenarioViewModel } from '@/lib/voteEstimate' export const OPS_SYNC_PATH = '/campanha/api/ops-sync' @@ -43,7 +38,7 @@ const createRowCollection = (id: string) => export const municipalitiesCollection = createRowCollection('ops-municipalities') const leadershipsCollection = createRowCollection('ops-leaderships') -export const votePledgesCollection = createRowCollection('ops-vote-pledges') +export { votePledgesCollection } from '@/components/campaign/opsSync/opsVotePledgeMirror' const activitiesCollection = createRowCollection('ops-activities') const stateDeputiesCollection = createRowCollection('ops-state-deputies') const organizationsCollection = createRowCollection('ops-organizations') @@ -261,51 +256,3 @@ export const syncOpsMirror = async (options: SyncOpsMirrorOptions = {}): Promise } } } - -export const readOpsVotePledge = (pledgeId: number): OpsVotePledge | undefined => - votePledgesCollection.get(pledgeId) - -export const subscribeOpsVotePledge = ( - pledgeId: number, - onStoreChange: () => void, -): (() => void) => { - let previous = votePledgesCollection.get(pledgeId) - const subscription = votePledgesCollection.subscribeChanges(() => { - const next = votePledgesCollection.get(pledgeId) - if (next === previous) return - previous = next - onStoreChange() - }) - return () => { - subscription.unsubscribe() - } -} - -/** Optimistic estimate patch while the outbox row is pending. */ -export const patchOpsVotePledgeEstimateOptimistic = ( - pledgeId: number, - estimatedVotes: VoteEstimateScenarioViewModel, - estimateNote: string | null, -): void => { - if (!votePledgesCollection.has(pledgeId)) return - votePledgesCollection.update(pledgeId, (draft) => { - draft.estimatedVotes = estimatedVotes - draft.estimateNote = estimateNote - }) -} - -/** Authoritative patch after a successful server write (OH7). */ -export const applyOpsVotePledgeEstimateWrite = (doc: VotePledgeEstimateWriteResult): void => { - const mapped = mapVotePledgeWriteToOps(doc) - if (votePledgesCollection.has(mapped.id)) { - votePledgesCollection.update(mapped.id, (draft) => { - draft.estimatedVotes = mapped.estimatedVotes - draft.estimateNote = mapped.estimateNote - draft.estimatedAt = mapped.estimatedAt - draft.estimatedBy = mapped.estimatedBy - draft.updatedAt = mapped.updatedAt - }) - return - } - votePledgesCollection.insert(mapped) -} diff --git a/src/components/campaign/opsSync/opsVotePledgeMirror.ts b/src/components/campaign/opsSync/opsVotePledgeMirror.ts new file mode 100644 index 00000000..51e29937 --- /dev/null +++ b/src/components/campaign/opsSync/opsVotePledgeMirror.ts @@ -0,0 +1,65 @@ +'use client' + +import { createCollection, localOnlyCollectionOptions } from '@tanstack/db' + +import { + mapVotePledgeWriteToOps, + type VotePledgeEstimateWriteResult, +} from '@/lib/campaignOps/mapVotePledgeWriteToOps' +import type { OpsVotePledge } from '@/lib/campaignOps/opsContract' +import type { VoteEstimateScenarioViewModel } from '@/lib/voteEstimate' + +export const votePledgesCollection = createCollection( + localOnlyCollectionOptions({ + id: 'ops-vote-pledges', + getKey: (row) => row.id, + }), +) + +export const readOpsVotePledge = (pledgeId: number): OpsVotePledge | undefined => + votePledgesCollection.get(pledgeId) + +export const subscribeOpsVotePledge = ( + pledgeId: number, + onStoreChange: () => void, +): (() => void) => { + let previous = votePledgesCollection.get(pledgeId) + const subscription = votePledgesCollection.subscribeChanges(() => { + const next = votePledgesCollection.get(pledgeId) + if (next === previous) return + previous = next + onStoreChange() + }) + return () => { + subscription.unsubscribe() + } +} + +/** Optimistic estimate patch while the outbox row is pending. */ +export const patchOpsVotePledgeEstimateOptimistic = ( + pledgeId: number, + estimatedVotes: VoteEstimateScenarioViewModel, + estimateNote: string | null, +): void => { + if (!votePledgesCollection.has(pledgeId)) return + votePledgesCollection.update(pledgeId, (draft) => { + draft.estimatedVotes = estimatedVotes + draft.estimateNote = estimateNote + }) +} + +/** Authoritative patch after a successful server write (OH7). */ +export const applyOpsVotePledgeEstimateWrite = (doc: VotePledgeEstimateWriteResult): void => { + const mapped = mapVotePledgeWriteToOps(doc) + if (votePledgesCollection.has(mapped.id)) { + votePledgesCollection.update(mapped.id, (draft) => { + draft.estimatedVotes = mapped.estimatedVotes + draft.estimateNote = mapped.estimateNote + draft.estimatedAt = mapped.estimatedAt + draft.estimatedBy = mapped.estimatedBy + draft.updatedAt = mapped.updatedAt + }) + return + } + votePledgesCollection.insert(mapped) +} diff --git a/src/components/campaign/votePledge/PledgeEstimateForm.tsx b/src/components/campaign/votePledge/PledgeEstimateForm.tsx index 84a177dd..936af6f8 100644 --- a/src/components/campaign/votePledge/PledgeEstimateForm.tsx +++ b/src/components/campaign/votePledge/PledgeEstimateForm.tsx @@ -23,7 +23,7 @@ import type { OpsEstimateSyncStatus } from '@/components/campaign/opsSync/opsEst import { readOpsVotePledge, subscribeOpsVotePledge, -} from '@/components/campaign/opsSync/opsMirrorClient' +} from '@/components/campaign/opsSync/opsVotePledgeMirror' import { CampaignFormActionMessage } from '@/components/campaign/shared/CampaignFormActionMessage' import { VoteEstimateScenarioInputs } from '@/components/campaign/votePledge/VoteEstimateScenarioInputs' import { Badge } from '@/components/ui/Badge' diff --git a/tests/unit/opsMirrorClient.unit.spec.ts b/tests/unit/opsMirrorClient.unit.spec.ts index 0f8cd29c..52f820ec 100644 --- a/tests/unit/opsMirrorClient.unit.spec.ts +++ b/tests/unit/opsMirrorClient.unit.spec.ts @@ -3,18 +3,20 @@ import { afterEach, describe, expect, it, vi } from 'vitest' import { applyOpsMirrorSnapshot, - applyOpsVotePledgeEstimateWrite, bootstrapOpsMirror, getOpsMirrorLastSyncedAt, getOpsMirrorPersistenceMode, municipalitiesCollection, OPS_SYNC_PATH, - patchOpsVotePledgeEstimateOptimistic, readOpsMirrorSnapshot, resetOpsMirrorClientForTests, syncOpsMirror, votePledgesCollection, } from '@/components/campaign/opsSync/opsMirrorClient' +import { + applyOpsVotePledgeEstimateWrite, + patchOpsVotePledgeEstimateOptimistic, +} from '@/components/campaign/opsSync/opsVotePledgeMirror' import { createMemoryOpsMirrorStore } from '@/components/campaign/opsSync/opsMirrorPersistence' import { formatOpsSyncRelative,