OH14 — pick outbox mutation by collection id when mirror rows are registered

This commit is contained in:
Cursor Agent 2026-08-02 04:13:37 +00:00
parent ceb1b3a8af
commit d6d3089f7d
No known key found for this signature in database
2 changed files with 51 additions and 29 deletions

View file

@ -1,6 +1,6 @@
'use client' 'use client'
import { createCollection, localOnlyCollectionOptions } from '@tanstack/db' import { createCollection, localOnlyCollectionOptions, type Transaction } from '@tanstack/db'
import { import {
IndexedDBAdapter, IndexedDBAdapter,
NonRetriableError, NonRetriableError,
@ -43,6 +43,14 @@ const LEADERSHIP_CREATE_FN = 'createLeadership'
const DEMAND_TRANSITION_FN = 'transitionCampaignDemand' const DEMAND_TRANSITION_FN = 'transitionCampaignDemand'
const ACTIVITY_UPDATE_FN = 'updateActivity' const ACTIVITY_UPDATE_FN = 'updateActivity'
const outboxMutationRow = <TRow>(transaction: Transaction, collectionId: string): TRow => {
const mutation = transaction.mutations.find((entry) => entry.collection?.id === collectionId)
if (!mutation) {
throw new NonRetriableError(`Mutação de outbox ausente (${collectionId}).`)
}
return mutation.modified as TRow
}
const leadershipUpdateOutboxCollection = createCollection( const leadershipUpdateOutboxCollection = createCollection(
localOnlyCollectionOptions<OpsLeadershipUpdateOutboxRow, number>({ localOnlyCollectionOptions<OpsLeadershipUpdateOutboxRow, number>({
id: 'ops-leadership-update-outbox', id: 'ops-leadership-update-outbox',
@ -138,9 +146,10 @@ const createDomainOfflineExecutor = (): OfflineExecutor =>
beforeRetry: collapseAllDomainOutbox, beforeRetry: collapseAllDomainOutbox,
mutationFns: { mutationFns: {
[LEADERSHIP_UPDATE_FN]: async ({ transaction }) => { [LEADERSHIP_UPDATE_FN]: async ({ transaction }) => {
const mutation = transaction.mutations[0] const row = outboxMutationRow<OpsLeadershipUpdateOutboxRow>(
if (!mutation) throw new NonRetriableError('Mutação de liderança vazia.') transaction,
const row = mutation.modified as OpsLeadershipUpdateOutboxRow 'ops-leadership-update-outbox',
)
try { try {
const updated = await updateLeadershipInternalCas({ const updated = await updateLeadershipInternalCas({
id: row.leadershipId, id: row.leadershipId,
@ -197,9 +206,10 @@ const createDomainOfflineExecutor = (): OfflineExecutor =>
} }
}, },
[LEADERSHIP_CREATE_FN]: async ({ transaction }) => { [LEADERSHIP_CREATE_FN]: async ({ transaction }) => {
const mutation = transaction.mutations[0] const row = outboxMutationRow<OpsLeadershipCreateOutboxRow>(
if (!mutation) throw new NonRetriableError('Mutação de cadastro vazia.') transaction,
const row = mutation.modified as OpsLeadershipCreateOutboxRow 'ops-leadership-create-outbox',
)
try { try {
await createLeadershipCas({ await createLeadershipCas({
name: row.name, name: row.name,
@ -228,9 +238,10 @@ const createDomainOfflineExecutor = (): OfflineExecutor =>
} }
}, },
[DEMAND_TRANSITION_FN]: async ({ transaction }) => { [DEMAND_TRANSITION_FN]: async ({ transaction }) => {
const mutation = transaction.mutations[0] const row = outboxMutationRow<OpsDemandTransitionOutboxRow>(
if (!mutation) throw new NonRetriableError('Mutação de demanda vazia.') transaction,
const row = mutation.modified as OpsDemandTransitionOutboxRow 'ops-demand-transition-outbox',
)
try { try {
const updated = await transitionCampaignDemandCas({ const updated = await transitionCampaignDemandCas({
id: row.demandId, id: row.demandId,
@ -271,9 +282,10 @@ const createDomainOfflineExecutor = (): OfflineExecutor =>
} }
}, },
[ACTIVITY_UPDATE_FN]: async ({ transaction }) => { [ACTIVITY_UPDATE_FN]: async ({ transaction }) => {
const mutation = transaction.mutations[0] const row = outboxMutationRow<OpsActivityUpdateOutboxRow>(
if (!mutation) throw new NonRetriableError('Mutação de atividade vazia.') transaction,
const row = mutation.modified as OpsActivityUpdateOutboxRow 'ops-activity-update-outbox',
)
try { try {
const updated = await updateActivityCas({ const updated = await updateActivityCas({
id: row.activityId, id: row.activityId,

View file

@ -1,6 +1,6 @@
'use client' 'use client'
import { createCollection, localOnlyCollectionOptions } from '@tanstack/db' import { createCollection, localOnlyCollectionOptions, type Transaction } from '@tanstack/db'
import { import {
IndexedDBAdapter, IndexedDBAdapter,
NonRetriableError, NonRetriableError,
@ -50,6 +50,14 @@ const TREND_MUTATION_FN = 'setPoliticalTrend'
const ENGAGEMENT_MUTATION_FN = 'setEngagementLevel' const ENGAGEMENT_MUTATION_FN = 'setEngagementLevel'
const ADVISORS_MUTATION_FN = 'assignAdvisors' const ADVISORS_MUTATION_FN = 'assignAdvisors'
const outboxMutationRow = <TRow>(transaction: Transaction, collectionId: string): TRow => {
const mutation = transaction.mutations.find((entry) => entry.collection?.id === collectionId)
if (!mutation) {
throw new NonRetriableError(`Mutação de outbox ausente (${collectionId}).`)
}
return mutation.modified as TRow
}
const declareVotesOutboxCollection = createCollection( const declareVotesOutboxCollection = createCollection(
localOnlyCollectionOptions<OpsDeclareVotesOutboxRow, string>({ localOnlyCollectionOptions<OpsDeclareVotesOutboxRow, string>({
id: 'ops-declare-votes-outbox', id: 'ops-declare-votes-outbox',
@ -155,9 +163,10 @@ const createMunicipalityOfflineExecutor = (): OfflineExecutor =>
beforeRetry: collapseAllMunicipalityOutbox, beforeRetry: collapseAllMunicipalityOutbox,
mutationFns: { mutationFns: {
[DECLARE_MUTATION_FN]: async ({ transaction }) => { [DECLARE_MUTATION_FN]: async ({ transaction }) => {
const mutation = transaction.mutations[0] const row = outboxMutationRow<OpsDeclareVotesOutboxRow>(
if (!mutation) throw new NonRetriableError('Mutação de declaração vazia.') transaction,
const row = mutation.modified as OpsDeclareVotesOutboxRow 'ops-declare-votes-outbox',
)
try { try {
const updated = await declareVotesCas({ const updated = await declareVotesCas({
municipality: row.municipalityId, municipality: row.municipalityId,
@ -193,9 +202,10 @@ const createMunicipalityOfflineExecutor = (): OfflineExecutor =>
} }
}, },
[MUNICIPALITY_UPDATE_MUTATION_FN]: async ({ transaction }) => { [MUNICIPALITY_UPDATE_MUTATION_FN]: async ({ transaction }) => {
const mutation = transaction.mutations[0] const row = outboxMutationRow<OpsMunicipalityUpdateOutboxRow>(
if (!mutation) throw new NonRetriableError('Mutação de atualização vazia.') transaction,
const row = mutation.modified as OpsMunicipalityUpdateOutboxRow 'ops-municipality-update-outbox',
)
try { try {
await createMunicipalityUpdateCas({ await createMunicipalityUpdateCas({
municipality: row.municipalityId, municipality: row.municipalityId,
@ -236,9 +246,10 @@ const createMunicipalityOfflineExecutor = (): OfflineExecutor =>
} }
}, },
[TREND_MUTATION_FN]: async ({ transaction }) => { [TREND_MUTATION_FN]: async ({ transaction }) => {
const mutation = transaction.mutations[0] const row = outboxMutationRow<OpsPoliticalTrendOutboxRow>(
if (!mutation) throw new NonRetriableError('Mutação de tendência vazia.') transaction,
const row = mutation.modified as OpsPoliticalTrendOutboxRow 'ops-political-trend-outbox',
)
try { try {
await setMunicipalityPoliticalTrendCas({ await setMunicipalityPoliticalTrendCas({
municipality: row.municipalityId, municipality: row.municipalityId,
@ -273,9 +284,10 @@ const createMunicipalityOfflineExecutor = (): OfflineExecutor =>
} }
}, },
[ENGAGEMENT_MUTATION_FN]: async ({ transaction }) => { [ENGAGEMENT_MUTATION_FN]: async ({ transaction }) => {
const mutation = transaction.mutations[0] const row = outboxMutationRow<OpsEngagementLevelOutboxRow>(
if (!mutation) throw new NonRetriableError('Mutação de nível vazia.') transaction,
const row = mutation.modified as OpsEngagementLevelOutboxRow 'ops-engagement-level-outbox',
)
try { try {
await setMunicipalityEngagementLevelCas({ await setMunicipalityEngagementLevelCas({
municipality: row.municipalityId, municipality: row.municipalityId,
@ -313,9 +325,7 @@ const createMunicipalityOfflineExecutor = (): OfflineExecutor =>
} }
}, },
[ADVISORS_MUTATION_FN]: async ({ transaction }) => { [ADVISORS_MUTATION_FN]: async ({ transaction }) => {
const mutation = transaction.mutations[0] const row = outboxMutationRow<OpsAdvisorsOutboxRow>(transaction, 'ops-advisors-outbox')
if (!mutation) throw new NonRetriableError('Mutação de assessores vazia.')
const row = mutation.modified as OpsAdvisorsOutboxRow
try { try {
await assignMunicipalityAdvisorsCas({ await assignMunicipalityAdvisorsCas({
municipality: row.municipalityId, municipality: row.municipalityId,