feat(logging): add logging to calendar repository

This commit is contained in:
qzl
2026-04-01 14:29:38 +08:00
parent 63d225c567
commit 11ab3d0e22
@@ -4,11 +4,13 @@ import '../../../../data/cache/cache_scope.dart';
import '../../../../data/network/i_api_client.dart';
import '../../../../core/notification/models/reminder_alarm.dart';
import '../../../../core/notification/services/reminder_reconcile_service.dart';
import '../../../../core/logging/logger.dart';
import '../models/schedule_item_model.dart';
class CalendarRepository extends CachedRepository<List<ScheduleItemModel>> {
final IApiClient _apiClient;
final ReminderReconcileService? _reminderReconcileService;
final Logger _logger = getLogger('features.calendar.repository');
static const _prefix = '/api/v1/schedule-items';
CalendarRepository({
@@ -70,14 +72,28 @@ class CalendarRepository extends CachedRepository<List<ScheduleItemModel>> {
}
Future<ScheduleItemModel> getEventById(String id) async {
final response = await _apiClient.get<Map<String, dynamic>>('$_prefix/$id');
try {
final response = await _apiClient.get<Map<String, dynamic>>(
'$_prefix/$id',
);
final data = response.data;
if (data == null) {
throw StateError('Invalid getEventById response: empty payload');
}
final event = ScheduleItemModel.fromJson(data);
await _reminderReconcileService?.reconcileEvent(_toReminderSnapshot(event));
await _reminderReconcileService?.reconcileEvent(
_toReminderSnapshot(event),
);
return event;
} catch (e, stackTrace) {
_logger.error(
message: 'Failed to get event by id',
error: e,
stackTrace: stackTrace,
extra: {'event_id': id},
);
rethrow;
}
}
Future<ScheduleItemModel> getById(String id) {
@@ -111,14 +127,25 @@ class CalendarRepository extends CachedRepository<List<ScheduleItemModel>> {
required bool accept,
}) async {
final action = accept ? 'accept' : 'reject';
try {
await _apiClient.post<void>('$_prefix/$itemId/$action');
await store.clearByPrefix('cache:${CacheScope.token()}:calendar:');
} catch (e, stackTrace) {
_logger.error(
message: 'Failed to $action subscription',
error: e,
stackTrace: stackTrace,
extra: {'item_id': itemId, 'action': action},
);
rethrow;
}
}
Future<List<ScheduleItemModel>> _listByRange({
required DateTime startAt,
required DateTime endAt,
}) async {
try {
final start = Uri.encodeQueryComponent(startAt.toUtc().toIso8601String());
final end = Uri.encodeQueryComponent(endAt.toUtc().toIso8601String());
final response = await _apiClient.get<List<dynamic>>(
@@ -136,6 +163,18 @@ class CalendarRepository extends CachedRepository<List<ScheduleItemModel>> {
events.map(_toReminderSnapshot).toList(growable: false),
);
return events;
} catch (e, stackTrace) {
_logger.error(
message: 'Failed to list events by range',
error: e,
stackTrace: stackTrace,
extra: {
'start_at': startAt.toIso8601String(),
'end_at': endAt.toIso8601String(),
},
);
rethrow;
}
}
ReminderEventSnapshot _toReminderSnapshot(ScheduleItemModel event) {