teqo-backup/src/components/campaign/opsSync/opsMunicipalityOutboxModel.ts
Cursor Agent daeff53bf4
OH10: mirror CAS base, advisors conflict toast, pledgeId path
Prefer municipalitiesCollection updatedAt for list/update outbox CAS,
restore advisors conflict Manter/Usar toast, and pass pledgeId for O(1)
declareVotes mirror/outbox keys. Drop unused membership Cas export and
expectedVotes baseUpdatedAt.

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

120 lines
3.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import type { OfflineTransaction } from '@tanstack/offline-transactions'
import type { EngagementLevel } from '@/lib/engagementLevel'
import type { PoliticalTrendStatusValue } from '@/lib/schemas/municipality'
import type {
MunicipalitySignalType,
MunicipalityUpdateKind,
} from '@/lib/schemas/municipalityUpdate'
export type OpsMunicipalityWriteSyncStatus = 'pending' | 'synced' | 'conflict' | 'error'
export type OpsDeclareVotesOutboxRow = {
id: string
municipalityId: number
leadershipId: number
/** Mirror pledge id when known — avoids O(n) lookup in collectOutboxKeys. */
pledgeId?: number
declaredVotes: number
baseUpdatedAt?: string | null
status: OpsMunicipalityWriteSyncStatus
serverUpdatedAt?: string | null
errorMessage?: string
}
export type OpsMunicipalityUpdateOutboxRow = {
id: string
municipalityId: number
kind: MunicipalityUpdateKind
worked?: string
failed?: string
needs?: string
body?: string
activeVolunteers?: number
newSupports?: number
signalType?: MunicipalitySignalType
baseUpdatedAt?: string | null
status: OpsMunicipalityWriteSyncStatus
serverUpdatedAt?: string | null
errorMessage?: string
}
export type OpsPoliticalTrendOutboxRow = {
municipalityId: number
status: PoliticalTrendStatusValue | null
note: string | null
baseUpdatedAt?: string | null
statusSync: OpsMunicipalityWriteSyncStatus
serverUpdatedAt?: string | null
errorMessage?: string
}
export type OpsEngagementLevelOutboxRow = {
municipalityId: number
level: EngagementLevel
note: string
reversalSignals: string
triangulatedShock: boolean
override: boolean
baseUpdatedAt?: string | null
status: OpsMunicipalityWriteSyncStatus
serverUpdatedAt?: string | null
errorMessage?: string
}
export type OpsAdvisorsOutboxRow = {
municipalityId: number
advisors: number[]
baseUpdatedAt?: string | null
status: OpsMunicipalityWriteSyncStatus
serverUpdatedAt?: string | null
errorMessage?: string
}
/** Collapse declare-votes retries to the newest per leadership×municipality. */
export const collapseDeclareVotesOutbox = (
transactions: OfflineTransaction[],
): OfflineTransaction[] => collapseByMetadataKey(transactions, 'declareKey')
/** Feed creates are append-only — keep every distinct client id. */
export const collapseMunicipalityUpdateOutbox = (
transactions: OfflineTransaction[],
): OfflineTransaction[] => collapseByMetadataKey(transactions, 'clientId')
export const collapsePoliticalTrendOutbox = (
transactions: OfflineTransaction[],
): OfflineTransaction[] => collapseByMetadataKey(transactions, 'municipalityId')
export const collapseEngagementLevelOutbox = (
transactions: OfflineTransaction[],
): OfflineTransaction[] => collapseByMetadataKey(transactions, 'municipalityId')
export const collapseAdvisorsOutbox = (
transactions: OfflineTransaction[],
): OfflineTransaction[] => collapseByMetadataKey(transactions, 'municipalityId')
const collapseByMetadataKey = (
transactions: OfflineTransaction[],
metadataKey: string,
): OfflineTransaction[] => {
const latestByKey = new Map<string, OfflineTransaction>()
for (const transaction of transactions) {
const raw = transaction.metadata?.[metadataKey]
const key =
typeof raw === 'number' || typeof raw === 'string'
? String(raw)
: String(transaction.mutations[0]?.key ?? transaction.id)
const current = latestByKey.get(key)
if (!current || transaction.createdAt.getTime() >= current.createdAt.getTime()) {
latestByKey.set(key, transaction)
}
}
return [...latestByKey.values()].sort(
(left, right) => left.createdAt.getTime() - right.createdAt.getTime(),
)
}
export const declareVotesOutboxKey = (leadershipId: number, municipalityId: number): string =>
`${leadershipId}:${municipalityId}`