Skip to content

application.commands.handlers.sync_accounts_handler

src.application.commands.handlers.sync_accounts_handler

SyncAccounts command handler.

Handles blocking account synchronization from provider connections. Fetches account data from provider API and upserts to repository.

Architecture
  • Application layer handler (orchestrates sync)
  • Blocking operation (not background job)
  • Uses provider adapter for external API calls
  • Publishes domain events for audit/observability
Reference
  • docs/architecture/cqrs-pattern.md
  • docs/architecture/api-design-patterns.md

Classes

SyncAccountsError

SyncAccounts-specific errors.

Source code in src/application/commands/handlers/sync_accounts_handler.py
class SyncAccountsError:
    """SyncAccounts-specific errors."""

    CONNECTION_NOT_FOUND = "Provider connection not found"
    NOT_OWNED_BY_USER = "Provider connection not owned by user"
    CONNECTION_NOT_ACTIVE = "Provider connection is not active"
    CREDENTIALS_INVALID = "Provider credentials are invalid"
    CREDENTIALS_DECRYPTION_FAILED = "Failed to decrypt provider credentials"
    PROVIDER_ERROR = "Provider API error"
    RECENTLY_SYNCED = "Accounts were recently synced"

SyncAccountsHandler

Handler for SyncAccounts command.

Synchronizes account data from provider to local repository. Blocking operation - waits for provider API response.

Flow
  1. Verify connection exists and is owned by user
  2. Check if sync is needed (unless force=True)
  3. Decrypt provider credentials
  4. Call provider.fetch_accounts()
  5. Upsert accounts to repository
  6. Update connection last_sync_at

Dependencies (injected via constructor): - ProviderConnectionRepository: For connection lookup - AccountRepository: For account persistence - EncryptionService: For credential decryption - ProviderFactoryProtocol: Factory for runtime provider resolution - EventBus: For domain events

Source code in src/application/commands/handlers/sync_accounts_handler.py
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
class SyncAccountsHandler:
    """Handler for SyncAccounts command.

    Synchronizes account data from provider to local repository.
    Blocking operation - waits for provider API response.

    Flow:
        1. Verify connection exists and is owned by user
        2. Check if sync is needed (unless force=True)
        3. Decrypt provider credentials
        4. Call provider.fetch_accounts()
        5. Upsert accounts to repository
        6. Update connection last_sync_at

    Dependencies (injected via constructor):
        - ProviderConnectionRepository: For connection lookup
        - AccountRepository: For account persistence
        - EncryptionService: For credential decryption
        - ProviderFactoryProtocol: Factory for runtime provider resolution
        - EventBus: For domain events
    """

    def __init__(
        self,
        connection_repo: ProviderConnectionRepository,
        account_repo: AccountRepository,
        encryption_service: EncryptionProtocol,
        provider_factory: ProviderFactoryProtocol,
        event_bus: EventBusProtocol,
    ) -> None:
        """Initialize handler with dependencies.

        Args:
            connection_repo: Provider connection repository.
            account_repo: Account repository.
            encryption_service: For decrypting credentials.
            provider_factory: Factory for runtime provider resolution.
            event_bus: For publishing domain events.
        """
        self._connection_repo = connection_repo
        self._account_repo = account_repo
        self._encryption_service = encryption_service
        self._provider_factory = provider_factory
        self._event_bus = event_bus

    async def handle(self, command: SyncAccounts) -> Result[SyncAccountsResult, str]:
        """Handle SyncAccounts command.

        Args:
            command: SyncAccounts command with connection_id and user_id.

        Returns:
            Success(SyncAccountsResult): Sync completed with counts.
            Failure(error): Connection not found, not owned, or provider error.
        """
        # 1. Emit ATTEMPTED event
        await self._event_bus.publish(
            AccountSyncAttempted(
                event_id=uuid7(),
                occurred_at=datetime.now(UTC),
                connection_id=command.connection_id,
                user_id=command.user_id,
            )
        )

        # 2. Fetch connection
        connection = await self._connection_repo.find_by_id(command.connection_id)

        if connection is None:
            await self._event_bus.publish(
                AccountSyncFailed(
                    event_id=uuid7(),
                    occurred_at=datetime.now(UTC),
                    connection_id=command.connection_id,
                    user_id=command.user_id,
                    reason="connection_not_found",
                )
            )
            return cast(
                Result[SyncAccountsResult, str],
                Failure(error=SyncAccountsError.CONNECTION_NOT_FOUND),
            )

        # 3. Verify ownership
        if connection.user_id != command.user_id:
            await self._event_bus.publish(
                AccountSyncFailed(
                    event_id=uuid7(),
                    occurred_at=datetime.now(UTC),
                    connection_id=command.connection_id,
                    user_id=command.user_id,
                    reason="not_owned_by_user",
                )
            )
            return cast(
                Result[SyncAccountsResult, str],
                Failure(error=SyncAccountsError.NOT_OWNED_BY_USER),
            )

        # 4. Verify connection is active
        if not connection.is_connected():
            await self._event_bus.publish(
                AccountSyncFailed(
                    event_id=uuid7(),
                    occurred_at=datetime.now(UTC),
                    connection_id=command.connection_id,
                    user_id=command.user_id,
                    reason="connection_not_active",
                )
            )
            return cast(
                Result[SyncAccountsResult, str],
                Failure(error=SyncAccountsError.CONNECTION_NOT_ACTIVE),
            )

        # 5. Check if recently synced (unless force=True)
        if not command.force and connection.last_sync_at:
            time_since_sync = datetime.now(UTC) - connection.last_sync_at
            if time_since_sync < MIN_SYNC_INTERVAL:
                await self._event_bus.publish(
                    AccountSyncFailed(
                        event_id=uuid7(),
                        occurred_at=datetime.now(UTC),
                        connection_id=command.connection_id,
                        user_id=command.user_id,
                        reason="recently_synced",
                    )
                )
                return cast(
                    Result[SyncAccountsResult, str],
                    Failure(error=SyncAccountsError.RECENTLY_SYNCED),
                )

        # 6. Get and decrypt credentials
        if connection.credentials is None:
            await self._event_bus.publish(
                AccountSyncFailed(
                    event_id=uuid7(),
                    occurred_at=datetime.now(UTC),
                    connection_id=command.connection_id,
                    user_id=command.user_id,
                    reason="credentials_invalid",
                )
            )
            return cast(
                Result[SyncAccountsResult, str],
                Failure(error=SyncAccountsError.CREDENTIALS_INVALID),
            )

        decrypt_result = self._encryption_service.decrypt(
            connection.credentials.encrypted_data
        )

        if isinstance(decrypt_result, Failure):
            await self._event_bus.publish(
                AccountSyncFailed(
                    event_id=uuid7(),
                    occurred_at=datetime.now(UTC),
                    connection_id=command.connection_id,
                    user_id=command.user_id,
                    reason="credentials_decryption_failed",
                )
            )
            return cast(
                Result[SyncAccountsResult, str],
                Failure(error=SyncAccountsError.CREDENTIALS_DECRYPTION_FAILED),
            )

        credentials_data = decrypt_result.value

        # 7. Resolve provider from connection slug
        provider = self._provider_factory.get_provider(connection.provider_slug)

        # 8. Fetch accounts from provider (pass full credentials dict)
        # Provider extracts what it needs (access_token for OAuth, api_key for API Key, etc.)
        fetch_result = await provider.fetch_accounts(credentials_data)

        if isinstance(fetch_result, Failure):
            await self._event_bus.publish(
                AccountSyncFailed(
                    event_id=uuid7(),
                    occurred_at=datetime.now(UTC),
                    connection_id=command.connection_id,
                    user_id=command.user_id,
                    reason=f"provider_error:{fetch_result.error.message}",
                )
            )
            return Failure(
                error=f"{SyncAccountsError.PROVIDER_ERROR}: {fetch_result.error.message}"
            )

        provider_accounts = fetch_result.value

        # 9. Sync accounts to repository
        sync_result = await self._sync_accounts_to_repository(
            connection_id=connection.id,
            provider_accounts=provider_accounts,
        )

        # 10. Update connection last_sync_at
        connection.record_sync()
        await self._connection_repo.save(connection)

        # 11. Emit SUCCEEDED event
        total_accounts = (
            sync_result.created + sync_result.updated + sync_result.unchanged
        )
        await self._event_bus.publish(
            AccountSyncSucceeded(
                event_id=uuid7(),
                occurred_at=datetime.now(UTC),
                connection_id=command.connection_id,
                user_id=command.user_id,
                account_count=total_accounts,
            )
        )

        # 12. Emit balance change events for portfolio notifications
        for balance_change in sync_result.balance_changes:
            await self._event_bus.publish(
                AccountBalanceUpdated(
                    event_id=uuid7(),
                    occurred_at=datetime.now(UTC),
                    user_id=command.user_id,
                    account_id=balance_change.account_id,
                    previous_balance=balance_change.previous,
                    new_balance=balance_change.current,
                    delta=balance_change.current - balance_change.previous,
                    currency=balance_change.currency,
                )
            )

        return Success(value=sync_result)

    async def _sync_accounts_to_repository(
        self,
        connection_id: UUID,
        provider_accounts: list[ProviderAccountData],
    ) -> SyncAccountsResult:
        """Sync provider accounts to repository using upsert logic.

        Tracks balance changes for portfolio notifications.

        Args:
            connection_id: Provider connection ID.
            provider_accounts: Accounts fetched from provider.

        Returns:
            SyncAccountsResult with counts and balance changes.
        """
        created = 0
        updated = 0
        unchanged = 0
        errors = 0
        balance_changes: list[BalanceChange] = []

        for provider_account in provider_accounts:
            try:
                # Check if account exists
                existing = await self._account_repo.find_by_provider_account_id(
                    connection_id=connection_id,
                    provider_account_id=provider_account.provider_account_id,
                )

                if existing is None:
                    # Create new account
                    account = self._create_account_from_provider_data(
                        connection_id=connection_id,
                        data=provider_account,
                    )
                    await self._account_repo.save(account)
                    created += 1
                    # New account - balance went from 0 to new_balance
                    if provider_account.balance != 0:
                        balance_changes.append(
                            BalanceChange(
                                account_id=account.id,
                                previous=provider_account.balance.__class__(0),
                                current=provider_account.balance,
                                currency=provider_account.currency,
                            )
                        )
                else:
                    # Track previous balance before update
                    previous_balance = existing.balance.amount

                    # Update existing account
                    was_updated = self._update_account_from_provider_data(
                        account=existing,
                        data=provider_account,
                    )
                    if was_updated:
                        await self._account_repo.save(existing)
                        updated += 1
                        # Check if balance changed
                        if previous_balance != provider_account.balance:
                            balance_changes.append(
                                BalanceChange(
                                    account_id=existing.id,
                                    previous=previous_balance,
                                    current=provider_account.balance,
                                    currency=provider_account.currency,
                                )
                            )
                    else:
                        unchanged += 1

            except Exception:
                # Log error but continue with other accounts
                errors += 1

        total = created + updated + unchanged
        message = (
            f"Synced {total} accounts: "
            f"{created} created, {updated} updated, {unchanged} unchanged"
        )
        if errors > 0:
            message += f", {errors} errors"

        return SyncAccountsResult(
            created=created,
            updated=updated,
            unchanged=unchanged,
            errors=errors,
            message=message,
            balance_changes=balance_changes,
        )

    def _create_account_from_provider_data(
        self,
        connection_id: UUID,
        data: ProviderAccountData,
    ) -> Account:
        """Create Account entity from provider data.

        Args:
            connection_id: Provider connection ID.
            data: Account data from provider.

        Returns:
            New Account entity.
        """
        # Map account type string to enum
        try:
            account_type = AccountType(data.account_type)
        except ValueError:
            account_type = AccountType.OTHER

        # Create balance Money object
        balance = Money(amount=data.balance, currency=data.currency)

        # Create available balance if present
        available_balance = None
        if data.available_balance is not None:
            available_balance = Money(
                amount=data.available_balance,
                currency=data.currency,
            )

        return Account(
            id=uuid7(),
            connection_id=connection_id,
            provider_account_id=data.provider_account_id,
            account_number_masked=data.account_number_masked,
            name=data.name,
            account_type=account_type,
            balance=balance,
            currency=data.currency,
            available_balance=available_balance,
            is_active=data.is_active,
            last_synced_at=datetime.now(UTC),
            provider_metadata=data.raw_data,
        )

    def _update_account_from_provider_data(
        self,
        account: Account,
        data: ProviderAccountData,
    ) -> bool:
        """Update existing Account from provider data.

        Args:
            account: Existing account entity.
            data: Fresh data from provider.

        Returns:
            True if account was modified, False if unchanged.
        """
        changed = False

        # Check balance change
        new_balance = Money(amount=data.balance, currency=data.currency)
        if account.balance != new_balance:
            account.update_balance(
                balance=new_balance,
                available_balance=(
                    Money(amount=data.available_balance, currency=data.currency)
                    if data.available_balance is not None
                    else None
                ),
            )
            changed = True

        # Check name change
        if account.name != data.name:
            account.update_from_provider(name=data.name)
            changed = True

        # Check active status change
        if account.is_active != data.is_active:
            if data.is_active:
                account.activate()
            else:
                account.deactivate()
            changed = True

        # Update metadata
        if data.raw_data and account.provider_metadata != data.raw_data:
            account.update_from_provider(provider_metadata=data.raw_data)
            changed = True

        # Always mark as synced
        account.mark_synced()

        return changed
Functions
__init__
__init__(
    connection_repo: ProviderConnectionRepository,
    account_repo: AccountRepository,
    encryption_service: EncryptionProtocol,
    provider_factory: ProviderFactoryProtocol,
    event_bus: EventBusProtocol,
) -> None

Parameters:

Name Type Description Default
connection_repo ProviderConnectionRepository

Provider connection repository.

required
account_repo AccountRepository

Account repository.

required
encryption_service EncryptionProtocol

For decrypting credentials.

required
provider_factory ProviderFactoryProtocol

Factory for runtime provider resolution.

required
event_bus EventBusProtocol

For publishing domain events.

required
Source code in src/application/commands/handlers/sync_accounts_handler.py
def __init__(
    self,
    connection_repo: ProviderConnectionRepository,
    account_repo: AccountRepository,
    encryption_service: EncryptionProtocol,
    provider_factory: ProviderFactoryProtocol,
    event_bus: EventBusProtocol,
) -> None:
    """Initialize handler with dependencies.

    Args:
        connection_repo: Provider connection repository.
        account_repo: Account repository.
        encryption_service: For decrypting credentials.
        provider_factory: Factory for runtime provider resolution.
        event_bus: For publishing domain events.
    """
    self._connection_repo = connection_repo
    self._account_repo = account_repo
    self._encryption_service = encryption_service
    self._provider_factory = provider_factory
    self._event_bus = event_bus
handle async
handle(
    command: SyncAccounts,
) -> Result[SyncAccountsResult, str]

Handle SyncAccounts command.

Parameters:

Name Type Description Default
command SyncAccounts

SyncAccounts command with connection_id and user_id.

required

Returns:

Name Type Description
Success SyncAccountsResult

Sync completed with counts.

Failure error

Connection not found, not owned, or provider error.

Source code in src/application/commands/handlers/sync_accounts_handler.py
async def handle(self, command: SyncAccounts) -> Result[SyncAccountsResult, str]:
    """Handle SyncAccounts command.

    Args:
        command: SyncAccounts command with connection_id and user_id.

    Returns:
        Success(SyncAccountsResult): Sync completed with counts.
        Failure(error): Connection not found, not owned, or provider error.
    """
    # 1. Emit ATTEMPTED event
    await self._event_bus.publish(
        AccountSyncAttempted(
            event_id=uuid7(),
            occurred_at=datetime.now(UTC),
            connection_id=command.connection_id,
            user_id=command.user_id,
        )
    )

    # 2. Fetch connection
    connection = await self._connection_repo.find_by_id(command.connection_id)

    if connection is None:
        await self._event_bus.publish(
            AccountSyncFailed(
                event_id=uuid7(),
                occurred_at=datetime.now(UTC),
                connection_id=command.connection_id,
                user_id=command.user_id,
                reason="connection_not_found",
            )
        )
        return cast(
            Result[SyncAccountsResult, str],
            Failure(error=SyncAccountsError.CONNECTION_NOT_FOUND),
        )

    # 3. Verify ownership
    if connection.user_id != command.user_id:
        await self._event_bus.publish(
            AccountSyncFailed(
                event_id=uuid7(),
                occurred_at=datetime.now(UTC),
                connection_id=command.connection_id,
                user_id=command.user_id,
                reason="not_owned_by_user",
            )
        )
        return cast(
            Result[SyncAccountsResult, str],
            Failure(error=SyncAccountsError.NOT_OWNED_BY_USER),
        )

    # 4. Verify connection is active
    if not connection.is_connected():
        await self._event_bus.publish(
            AccountSyncFailed(
                event_id=uuid7(),
                occurred_at=datetime.now(UTC),
                connection_id=command.connection_id,
                user_id=command.user_id,
                reason="connection_not_active",
            )
        )
        return cast(
            Result[SyncAccountsResult, str],
            Failure(error=SyncAccountsError.CONNECTION_NOT_ACTIVE),
        )

    # 5. Check if recently synced (unless force=True)
    if not command.force and connection.last_sync_at:
        time_since_sync = datetime.now(UTC) - connection.last_sync_at
        if time_since_sync < MIN_SYNC_INTERVAL:
            await self._event_bus.publish(
                AccountSyncFailed(
                    event_id=uuid7(),
                    occurred_at=datetime.now(UTC),
                    connection_id=command.connection_id,
                    user_id=command.user_id,
                    reason="recently_synced",
                )
            )
            return cast(
                Result[SyncAccountsResult, str],
                Failure(error=SyncAccountsError.RECENTLY_SYNCED),
            )

    # 6. Get and decrypt credentials
    if connection.credentials is None:
        await self._event_bus.publish(
            AccountSyncFailed(
                event_id=uuid7(),
                occurred_at=datetime.now(UTC),
                connection_id=command.connection_id,
                user_id=command.user_id,
                reason="credentials_invalid",
            )
        )
        return cast(
            Result[SyncAccountsResult, str],
            Failure(error=SyncAccountsError.CREDENTIALS_INVALID),
        )

    decrypt_result = self._encryption_service.decrypt(
        connection.credentials.encrypted_data
    )

    if isinstance(decrypt_result, Failure):
        await self._event_bus.publish(
            AccountSyncFailed(
                event_id=uuid7(),
                occurred_at=datetime.now(UTC),
                connection_id=command.connection_id,
                user_id=command.user_id,
                reason="credentials_decryption_failed",
            )
        )
        return cast(
            Result[SyncAccountsResult, str],
            Failure(error=SyncAccountsError.CREDENTIALS_DECRYPTION_FAILED),
        )

    credentials_data = decrypt_result.value

    # 7. Resolve provider from connection slug
    provider = self._provider_factory.get_provider(connection.provider_slug)

    # 8. Fetch accounts from provider (pass full credentials dict)
    # Provider extracts what it needs (access_token for OAuth, api_key for API Key, etc.)
    fetch_result = await provider.fetch_accounts(credentials_data)

    if isinstance(fetch_result, Failure):
        await self._event_bus.publish(
            AccountSyncFailed(
                event_id=uuid7(),
                occurred_at=datetime.now(UTC),
                connection_id=command.connection_id,
                user_id=command.user_id,
                reason=f"provider_error:{fetch_result.error.message}",
            )
        )
        return Failure(
            error=f"{SyncAccountsError.PROVIDER_ERROR}: {fetch_result.error.message}"
        )

    provider_accounts = fetch_result.value

    # 9. Sync accounts to repository
    sync_result = await self._sync_accounts_to_repository(
        connection_id=connection.id,
        provider_accounts=provider_accounts,
    )

    # 10. Update connection last_sync_at
    connection.record_sync()
    await self._connection_repo.save(connection)

    # 11. Emit SUCCEEDED event
    total_accounts = (
        sync_result.created + sync_result.updated + sync_result.unchanged
    )
    await self._event_bus.publish(
        AccountSyncSucceeded(
            event_id=uuid7(),
            occurred_at=datetime.now(UTC),
            connection_id=command.connection_id,
            user_id=command.user_id,
            account_count=total_accounts,
        )
    )

    # 12. Emit balance change events for portfolio notifications
    for balance_change in sync_result.balance_changes:
        await self._event_bus.publish(
            AccountBalanceUpdated(
                event_id=uuid7(),
                occurred_at=datetime.now(UTC),
                user_id=command.user_id,
                account_id=balance_change.account_id,
                previous_balance=balance_change.previous,
                new_balance=balance_change.current,
                delta=balance_change.current - balance_change.previous,
                currency=balance_change.currency,
            )
        )

    return Success(value=sync_result)