Skip to content

Open source personal finance application

refactor: extract replay helpers, remove old transfer migration, lint fix

Ilya Komichev30068de9c723
diff --git a/app/components/date/statDateParams.ts b/app/components/date/statDateParams.ts
new file mode 100644
index 00000000..40ae45f5
--- /dev/null
+++ b/app/components/date/statDateParams.ts
@@ -0,0 +1,99 @@
+import { z } from 'zod/v4'
+
+import type { Range, StatDateParams, StatDateParamsQuery } from '~/components/date/types'
+
+import { periods } from '~/components/date/types'
+import { calculateIntervalInRange, getEndOf } from '~/components/date/utils'
+
+const queryParamsSchema = z.object({
+ intervalsBy: z.enum(periods).optional(),
+ intervalsDuration: z.string().transform(val => Number(val)).pipe(z.number().int()).optional(),
+ intervalSelected: z.string().transform(val => Number(val)).pipe(z.number().int()).optional(),
+ isShowMaxRange: z.string().transform(val => val === 'true').optional(),
+ isSkipEmpty: z.string().transform(val => val === 'true').optional(),
+ rangeBy: z.enum(periods).optional(),
+ rangeDuration: z.string().transform(val => Number(val)).pipe(z.number().int()).optional(),
+ rangeOffset: z.string().transform(val => Number(val)).pipe(z.number().int()).optional(),
+})
+
+export const defaultStatDateParams: StatDateParams = {
+ customDate: false,
+ intervalsBy: 'day',
+ intervalsDuration: 1,
+ intervalSelected: -1,
+ isShowMaxRange: false,
+ isSkipEmpty: false,
+ rangeBy: 'day',
+ rangeDuration: 14,
+ rangeOffset: 0,
+}
+
+/**
+ * Parse URL query params via Zod and merge into stat date params.
+ * Returns a new params object (does not mutate input).
+ */
+export function parseStatDateQueryParams(
+ queryParams: Partial<StatDateParamsQuery>,
+ currentParams: StatDateParams,
+): StatDateParams {
+ const parsed = queryParamsSchema.safeParse(queryParams)
+ if (!parsed.success)
+ return currentParams
+
+ const data = parsed.data
+ const result = { ...currentParams }
+
+ if (data.intervalsBy)
+ result.intervalsBy = data.intervalsBy
+ if (data.intervalsDuration)
+ result.intervalsDuration = data.intervalsDuration
+ if (data.intervalSelected)
+ result.intervalSelected = data.intervalSelected
+ if (data.rangeBy)
+ result.rangeBy = data.rangeBy
+ if (data.rangeDuration)
+ result.rangeDuration = data.rangeDuration
+ if (data.rangeOffset)
+ result.rangeOffset = data.rangeOffset
+ if (data.isShowMaxRange !== undefined)
+ result.isShowMaxRange = data.isShowMaxRange
+ if (data.isSkipEmpty !== undefined)
+ result.isSkipEmpty = data.isSkipEmpty
+
+ return result
+}
+
+/**
+ * Compute the active date range from params and max range.
+ * Priority: customDate > isShowMaxRange+isSkipEmpty > isShowMaxRange > calculated.
+ */
+export function computeDateRange(
+ params: StatDateParams,
+ maxRange: Range,
+ now: number,
+): Range {
+ if (params.customDate) {
+ return params.customDate
+ }
+
+ if (params.isShowMaxRange && params.isSkipEmpty) {
+ return { ...maxRange }
+ }
+
+ if (params.isShowMaxRange && !params.isSkipEmpty) {
+ return {
+ end: getEndOf(new Date(now), params.rangeBy).getTime(),
+ start: maxRange.start,
+ }
+ }
+
+ return calculateIntervalInRange({
+ intervalsBy: params.rangeBy,
+ intervalsDuration: params.rangeDuration,
+ range: {
+ end: now,
+ start: now,
+ },
+ rangeOffset: params.rangeOffset,
+ })
+}
diff --git a/app/components/offline/replay.ts b/app/components/offline/replay.ts
index 6f2720ef..c8452803 100644
--- a/app/components/offline/replay.ts
+++ b/app/components/offline/replay.ts
@@ -1,13 +1,11 @@
import type { CategoryItem } from '~/components/categories/types'
import type { CurrencyCode } from '~/components/currencies/types'
import type { LocaleSlug } from '~/components/locale/types'
-import type { OfflineOp } from '~/components/offline/types'
-import type { TrnItem } from '~/components/trns/types'
import type { WalletItem } from '~/components/wallets/types'
import { useCategoriesStore } from '~/components/categories/useCategoriesStore'
import { clearOfflineQueue, getAllOfflineOps, getQueueUserId, removeOfflineOp, setOfflineQueueUserId, setQueueUserId } from '~/components/offline/helpers'
-import { TrnType } from '~/components/trns/types'
+import { extractRemaps, groupOpsByEntity, isTrnOrphaned, remapTrnIds, splitCategoryOps } from '~/components/offline/replayHelpers'
import { useTrnsStore } from '~/components/trns/useTrnsStore'
import { useUserStore } from '~/components/user/useUserStore'
import { useWalletsStore } from '~/components/wallets/useWalletsStore'
@@ -21,41 +19,10 @@ export function isReplaying(): boolean {
return _isReplaying
}
-/**
- * Remap local IDs in trn data using the collected remap map.
- */
-function remapTrnIds(data: TrnItem, remapIds: Map<string, string>): TrnItem {
- if (data.type === TrnType.Transfer) {
- return {
- ...data,
- expenseWalletId: remapIds.get(data.expenseWalletId) ?? data.expenseWalletId,
- incomeWalletId: remapIds.get(data.incomeWalletId) ?? data.incomeWalletId,
- }
- }
- return {
- ...data,
- categoryId: remapIds.get(data.categoryId) ?? data.categoryId,
- walletId: remapIds.get(data.walletId) ?? data.walletId,
- }
-}
-
-/**
- * Check if a trn references wallets/categories that don't exist in the store.
- */
-function isTrnOrphaned(data: TrnItem, walletsStore: ReturnType<typeof useWalletsStore>, categoriesStore: ReturnType<typeof useCategoriesStore>): boolean {
- if (data.type === TrnType.Transfer)
- return !walletsStore.items?.[data.expenseWalletId] || !walletsStore.items?.[data.incomeWalletId]
- return !walletsStore.items?.[data.walletId] || !categoriesStore.items[data.categoryId]
-}
-
-/**
- * Collect remap info from resolved promises into the remap map.
- */
async function collectRemaps(promises: (Promise<unknown> | undefined)[], remapIds: Map<string, string>) {
const results = await Promise.all(promises.map(p => p ?? Promise.resolve()))
- for (const r of results) {
- if (r && typeof r === 'object' && 'localId' in r && 'convexId' in r)
- remapIds.set(r.localId as string, r.convexId as string)
+ for (const { convexId, localId } of extractRemaps(results)) {
+ remapIds.set(localId, convexId)
}
}
@@ -100,42 +67,25 @@ export async function replayOfflineQueue(): Promise<void> {
const categoriesStore = useCategoriesStore()
const trnsStore = useTrnsStore()
- // Group operations by entity
- const walletOps: OfflineOp[] = []
- const categoryOps: OfflineOp[] = []
- const trnOps: OfflineOp[] = []
- const settingsOps: OfflineOp[] = []
-
+ // Filter out frontend-ID deletes and group by entity
+ const filteredOps = []
for (const op of queue) {
- // Frontend IDs: skip deletes (nothing to delete on server)
if (isLocalId(op.id) && op.type === 'delete') {
logger.log(`skip delete (frontend ID): ${op.entity}/${op.id}`)
await removeOfflineOp(op.entity, op.id)
continue
}
-
- switch (op.entity) {
- case 'wallets':
- walletOps.push(op)
- break
- case 'categories':
- categoryOps.push(op)
- break
- case 'trns':
- trnOps.push(op)
- break
- case 'userSettings':
- settingsOps.push(op)
- break
- }
+ filteredOps.push(op)
}
+ const { categoryOps, settingsOps, trnOps, walletOps } = groupOpsByEntity(filteredOps)
+
_isReplaying = true
const remapIds = new Map<string, string>()
let orphanCount = 0
try {
- // Phase 1: Replay wallets — creates & updates in parallel, await all, collect remaps
+ // Phase 1: Replay wallets
if (walletOps.length > 0) {
logger.log(`phase 1: ${walletOps.length} wallet ops`)
const promises = walletOps.map((op) => {
@@ -146,20 +96,9 @@ export async function replayOfflineQueue(): Promise<void> {
await collectRemaps(promises, remapIds)
}
- // Phase 2: Replay categories — parents first, then children (children may reference local parent IDs)
+ // Phase 2: Replay categories — parents first, then children
if (categoryOps.length > 0) {
- const parentOps = categoryOps.filter((op) => {
- if (op.type === 'delete')
- return true
- const data = op.data as unknown as CategoryItem
- return !data.parentId || data.parentId === 0
- })
- const childOps = categoryOps.filter((op) => {
- if (op.type === 'delete')
- return false
- const data = op.data as unknown as CategoryItem
- return data.parentId && data.parentId !== 0
- })
+ const { childOps, parentOps } = splitCategoryOps(categoryOps)
// Phase 2a: Parent categories
if (parentOps.length > 0) {
@@ -195,7 +134,7 @@ export async function replayOfflineQueue(): Promise<void> {
}
}
- // Phase 3: Replay trns — remap wallet/category IDs, fire-and-forget
+ // Phase 3: Replay trns — remap wallet/category IDs
if (trnOps.length > 0) {
logger.log(`phase 3: ${trnOps.length} trn ops`)
for (const op of trnOps) {
@@ -207,8 +146,7 @@ export async function replayOfflineQueue(): Promise<void> {
const data = op.data as unknown as TrnItem
const remapped = remapTrnIds(data, remapIds)
- // Orphan detection with remapped IDs
- if (isTrnOrphaned(remapped, walletsStore, categoriesStore)) {
+ if (isTrnOrphaned(remapped, walletsStore.items ?? {}, categoriesStore.items)) {
orphanCount++
logger.log(`skip (orphaned): ${op.entity}/${op.id}`)
await removeOfflineOp(op.entity, op.id)
@@ -219,7 +157,7 @@ export async function replayOfflineQueue(): Promise<void> {
}
}
- // Phase 4: Settings — fire-and-forget
+ // Phase 4: Settings
for (const op of settingsOps) {
if (op.data?.baseCurrency)
userStore.saveUserBaseCurrency(op.data.baseCurrency as CurrencyCode)
diff --git a/app/components/offline/replayHelpers.ts b/app/components/offline/replayHelpers.ts
new file mode 100644
index 00000000..b89cdbbb
--- /dev/null
+++ b/app/components/offline/replayHelpers.ts
@@ -0,0 +1,112 @@
+import type { CategoryItem } from '~/components/categories/types'
+import type { OfflineOp } from '~/components/offline/types'
+import type { TrnItem } from '~/components/trns/types'
+
+import { TrnType } from '~/components/trns/types'
+
+/**
+ * Remap local IDs in trn data using the collected remap map.
+ */
+export function remapTrnIds(data: TrnItem, remapIds: Map<string, string>): TrnItem {
+ if (data.type === TrnType.Transfer) {
+ return {
+ ...data,
+ expenseWalletId: remapIds.get(data.expenseWalletId) ?? data.expenseWalletId,
+ incomeWalletId: remapIds.get(data.incomeWalletId) ?? data.incomeWalletId,
+ }
+ }
+ return {
+ ...data,
+ categoryId: remapIds.get(data.categoryId) ?? data.categoryId,
+ walletId: remapIds.get(data.walletId) ?? data.walletId,
+ }
+}
+
+/**
+ * Check if a trn references wallets/categories that don't exist.
+ */
+export function isTrnOrphaned(
+ data: TrnItem,
+ walletsItems: Record<string, unknown>,
+ categoriesItems: Record<string, unknown>,
+): boolean {
+ if (data.type === TrnType.Transfer)
+ return !walletsItems[data.expenseWalletId] || !walletsItems[data.incomeWalletId]
+ return !walletsItems[data.walletId] || !categoriesItems[data.categoryId]
+}
+
+type GroupedOps = {
+ categoryOps: OfflineOp[]
+ settingsOps: OfflineOp[]
+ trnOps: OfflineOp[]
+ walletOps: OfflineOp[]
+}
+
+/**
+ * Group offline operations by entity type.
+ */
+export function groupOpsByEntity(ops: OfflineOp[]): GroupedOps {
+ const walletOps: OfflineOp[] = []
+ const categoryOps: OfflineOp[] = []
+ const trnOps: OfflineOp[] = []
+ const settingsOps: OfflineOp[] = []
+
+ for (const op of ops) {
+ switch (op.entity) {
+ case 'wallets':
+ walletOps.push(op)
+ break
+ case 'categories':
+ categoryOps.push(op)
+ break
+ case 'trns':
+ trnOps.push(op)
+ break
+ case 'userSettings':
+ settingsOps.push(op)
+ break
+ }
+ }
+
+ return { categoryOps, settingsOps, trnOps, walletOps }
+}
+
+/**
+ * Split category ops into parent-first and child-second groups.
+ * Delete ops go into parents (processed first).
+ */
+export function splitCategoryOps(ops: OfflineOp[]): {
+ childOps: OfflineOp[]
+ parentOps: OfflineOp[]
+} {
+ const parentOps: OfflineOp[] = []
+ const childOps: OfflineOp[] = []
+
+ for (const op of ops) {
+ if (op.type === 'delete') {
+ parentOps.push(op)
+ continue
+ }
+ const data = op.data as unknown as CategoryItem
+ if (!data.parentId || data.parentId === 0) {
+ parentOps.push(op)
+ }
+ else {
+ childOps.push(op)
+ }
+ }
+
+ return { childOps, parentOps }
+}
+
+/**
+ * Extract remap entries (localId → convexId) from resolved promise results.
+ */
+export function extractRemaps(results: unknown[]): Array<{ convexId: string, localId: string }> {
+ const remaps: Array<{ convexId: string, localId: string }> = []
+ for (const r of results) {
+ if (r && typeof r === 'object' && 'localId' in r && 'convexId' in r)
+ remaps.push({ convexId: r.convexId as string, localId: r.localId as string })
+ }
+ return remaps
+}
diff --git a/convex/migrate.ts b/convex/migrate.ts
index 924cd2b6..6eef26d6 100644
--- a/convex/migrate.ts
+++ b/convex/migrate.ts
@@ -2,7 +2,6 @@ import { v } from 'convex/values'
import { components, internal } from './_generated/api'
import { internalAction, internalMutation } from './_generated/server'
-import { TrnType } from './shared'
import { addTrnsToHash, fnv1aNum, getOrCreateSyncMeta } from './trnsHash'
export const deleteTablePage = internalMutation({
@@ -205,54 +204,3 @@ export const recalcHashForUser = internalAction({
}
},
})
-
-// --- Migrate old transfers to adjustment ---
-// Old transfers have categoryId: 'transfer' with type: Expense(0) or Income(1).
-// Modern transfers use type: Transfer(2). Old transfers are semantically adjustments.
-
-export const migrateOldTransfersPage = internalMutation({
- args: {
- cursor: v.union(v.string(), v.null()),
- },
- handler: async (ctx, { cursor }) => {
- const page = await ctx.db
- .query('trns')
- .paginate({ cursor, numItems: 500 })
-
- let patched = 0
- for (const trn of page.page) {
- if (trn.categoryId === 'transfer' && (trn.type === TrnType.Expense || trn.type === TrnType.Income)) {
- await ctx.db.patch(trn._id, { categoryId: 'adjustment' })
- patched++
- }
- }
-
- return {
- continueCursor: page.continueCursor,
- hasMore: !page.isDone,
- patched,
- }
- },
-})
-
-export const migrateOldTransfers = internalAction({
- args: {},
- handler: async (ctx) => {
- let cursor: string | null = null
- let totalPatched = 0
-
- while (true) {
- const result: { continueCursor: string, hasMore: boolean, patched: number }
- = await ctx.runMutation(internal.migrate.migrateOldTransfersPage, { cursor })
- totalPatched += result.patched
-
- if (!result.hasMore)
- break
-
- cursor = result.continueCursor
- }
-
- console.log(`migrateOldTransfers: patched ${totalPatched} transactions`)
- return totalPatched
- },
-})
diff --git a/convex/test-utils/setup.ts b/convex/test-utils/setup.ts
index f361b10c..d0f646b9 100644
--- a/convex/test-utils/setup.ts
+++ b/convex/test-utils/setup.ts
@@ -14,7 +14,8 @@ beforeEach(() => {
vi.mock('../auth', () => ({
authComponent: {
safeGetAuthUser: vi.fn(async () => {
- if (!mockUserId) return null
+ if (!mockUserId)
+ return null
return { _id: mockUserId }
}),
},