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

View file

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