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 '../../../../data/network/i_api_client.dart';
import '../../../../core/notification/models/reminder_alarm.dart'; import '../../../../core/notification/models/reminder_alarm.dart';
import '../../../../core/notification/services/reminder_reconcile_service.dart'; import '../../../../core/notification/services/reminder_reconcile_service.dart';
import '../../../../core/logging/logger.dart';
import '../models/schedule_item_model.dart'; import '../models/schedule_item_model.dart';
class CalendarRepository extends CachedRepository<List<ScheduleItemModel>> { class CalendarRepository extends CachedRepository<List<ScheduleItemModel>> {
final IApiClient _apiClient; final IApiClient _apiClient;
final ReminderReconcileService? _reminderReconcileService; final ReminderReconcileService? _reminderReconcileService;
final Logger _logger = getLogger('features.calendar.repository');
static const _prefix = '/api/v1/schedule-items'; static const _prefix = '/api/v1/schedule-items';
CalendarRepository({ CalendarRepository({
@@ -70,14 +72,28 @@ class CalendarRepository extends CachedRepository<List<ScheduleItemModel>> {
} }
Future<ScheduleItemModel> getEventById(String id) async { Future<ScheduleItemModel> getEventById(String id) async {
final response = await _apiClient.get<Map<String, dynamic>>('$_prefix/$id'); try {
final data = response.data; final response = await _apiClient.get<Map<String, dynamic>>(
if (data == null) { '$_prefix/$id',
throw StateError('Invalid getEventById response: empty payload'); );
final data = response.data;
if (data == null) {
throw StateError('Invalid getEventById response: empty payload');
}
final event = ScheduleItemModel.fromJson(data);
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;
} }
final event = ScheduleItemModel.fromJson(data);
await _reminderReconcileService?.reconcileEvent(_toReminderSnapshot(event));
return event;
} }
Future<ScheduleItemModel> getById(String id) { Future<ScheduleItemModel> getById(String id) {
@@ -111,31 +127,54 @@ class CalendarRepository extends CachedRepository<List<ScheduleItemModel>> {
required bool accept, required bool accept,
}) async { }) async {
final action = accept ? 'accept' : 'reject'; final action = accept ? 'accept' : 'reject';
await _apiClient.post<void>('$_prefix/$itemId/$action'); try {
await store.clearByPrefix('cache:${CacheScope.token()}:calendar:'); 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({ Future<List<ScheduleItemModel>> _listByRange({
required DateTime startAt, required DateTime startAt,
required DateTime endAt, required DateTime endAt,
}) async { }) async {
final start = Uri.encodeQueryComponent(startAt.toUtc().toIso8601String()); try {
final end = Uri.encodeQueryComponent(endAt.toUtc().toIso8601String()); final start = Uri.encodeQueryComponent(startAt.toUtc().toIso8601String());
final response = await _apiClient.get<List<dynamic>>( final end = Uri.encodeQueryComponent(endAt.toUtc().toIso8601String());
'$_prefix?start_at=$start&end_at=$end', final response = await _apiClient.get<List<dynamic>>(
); '$_prefix?start_at=$start&end_at=$end',
final data = response.data; );
if (data == null) { final data = response.data;
throw StateError('Invalid listByRange response: empty payload'); if (data == null) {
throw StateError('Invalid listByRange response: empty payload');
}
final events = data
.whereType<Map<String, dynamic>>()
.map(ScheduleItemModel.fromJson)
.toList(growable: false);
await _reminderReconcileService?.reconcileEvents(
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;
} }
final events = data
.whereType<Map<String, dynamic>>()
.map(ScheduleItemModel.fromJson)
.toList(growable: false);
await _reminderReconcileService?.reconcileEvents(
events.map(_toReminderSnapshot).toList(growable: false),
);
return events;
} }
ReminderEventSnapshot _toReminderSnapshot(ScheduleItemModel event) { ReminderEventSnapshot _toReminderSnapshot(ScheduleItemModel event) {