h-1.flutter.0/lib/services/calendar_sync_diagnostics.dart
2026-03-04 14:55:40 +09:00

65 lines
2.3 KiB
Dart

import 'package:flutter/foundation.dart';
import '../models/receivable_models.dart';
import '../models/shipment_models.dart';
import 'business_calendar_mapper.dart';
import 'receivable_repository.dart';
import 'shipment_repository.dart';
/// Result object describing a manual Google Calendar sync run.
class CalendarSyncDiagnosticsResult {
const CalendarSyncDiagnosticsResult({
required this.shipmentsSynced,
required this.receivablesSynced,
required this.errors,
});
final int shipmentsSynced;
final int receivablesSynced;
final List<String> errors;
bool get hasErrors => errors.isNotEmpty;
}
/// Utility helper to trigger a full calendar sync for diagnostics or manual refresh.
class CalendarSyncDiagnostics {
CalendarSyncDiagnostics({
ShipmentRepository? shipmentRepository,
ReceivableRepository? receivableRepository,
BusinessCalendarMapper? calendarMapper,
}) : _shipmentRepository = shipmentRepository ?? ShipmentRepository(),
_receivableRepository = receivableRepository ?? ReceivableRepository(),
_calendarMapper = calendarMapper ?? BusinessCalendarMapper();
final ShipmentRepository _shipmentRepository;
final ReceivableRepository _receivableRepository;
final BusinessCalendarMapper _calendarMapper;
Future<CalendarSyncDiagnosticsResult> runFullSync({bool includeSettledReceivables = true}) async {
final errors = <String>[];
List<Shipment> shipments = const [];
List<ReceivableInvoiceSummary> receivables = const [];
try {
shipments = await _shipmentRepository.fetchShipments();
await _calendarMapper.syncShipments(shipments);
} catch (e, stack) {
debugPrint('Calendar diagnostics: shipment sync failed: $e\n$stack');
errors.add('出荷イベント同期に失敗: $e');
}
try {
receivables = await _receivableRepository.fetchSummaries(includeSettled: includeSettledReceivables);
await _calendarMapper.syncReceivables(receivables);
} catch (e, stack) {
debugPrint('Calendar diagnostics: receivable sync failed: $e\n$stack');
errors.add('債権イベント同期に失敗: $e');
}
return CalendarSyncDiagnosticsResult(
shipmentsSynced: shipments.length,
receivablesSynced: receivables.length,
errors: errors,
);
}
}