feat(logging): add logging to calendar repository
This commit is contained in:
@@ -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 response = await _apiClient.get<Map<String, dynamic>>(
|
||||||
|
'$_prefix/$id',
|
||||||
|
);
|
||||||
final data = response.data;
|
final data = response.data;
|
||||||
if (data == null) {
|
if (data == null) {
|
||||||
throw StateError('Invalid getEventById response: empty payload');
|
throw StateError('Invalid getEventById response: empty payload');
|
||||||
}
|
}
|
||||||
final event = ScheduleItemModel.fromJson(data);
|
final event = ScheduleItemModel.fromJson(data);
|
||||||
await _reminderReconcileService?.reconcileEvent(_toReminderSnapshot(event));
|
await _reminderReconcileService?.reconcileEvent(
|
||||||
|
_toReminderSnapshot(event),
|
||||||
|
);
|
||||||
return 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) {
|
Future<ScheduleItemModel> getById(String id) {
|
||||||
@@ -111,14 +127,25 @@ class CalendarRepository extends CachedRepository<List<ScheduleItemModel>> {
|
|||||||
required bool accept,
|
required bool accept,
|
||||||
}) async {
|
}) async {
|
||||||
final action = accept ? 'accept' : 'reject';
|
final action = accept ? 'accept' : 'reject';
|
||||||
|
try {
|
||||||
await _apiClient.post<void>('$_prefix/$itemId/$action');
|
await _apiClient.post<void>('$_prefix/$itemId/$action');
|
||||||
await store.clearByPrefix('cache:${CacheScope.token()}:calendar:');
|
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 {
|
||||||
|
try {
|
||||||
final start = Uri.encodeQueryComponent(startAt.toUtc().toIso8601String());
|
final start = Uri.encodeQueryComponent(startAt.toUtc().toIso8601String());
|
||||||
final end = Uri.encodeQueryComponent(endAt.toUtc().toIso8601String());
|
final end = Uri.encodeQueryComponent(endAt.toUtc().toIso8601String());
|
||||||
final response = await _apiClient.get<List<dynamic>>(
|
final response = await _apiClient.get<List<dynamic>>(
|
||||||
@@ -136,6 +163,18 @@ class CalendarRepository extends CachedRepository<List<ScheduleItemModel>> {
|
|||||||
events.map(_toReminderSnapshot).toList(growable: false),
|
events.map(_toReminderSnapshot).toList(growable: false),
|
||||||
);
|
);
|
||||||
return events;
|
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) {
|
ReminderEventSnapshot _toReminderSnapshot(ScheduleItemModel event) {
|
||||||
|
|||||||
Reference in New Issue
Block a user