forked from eatvio/eatvio_app
211 lines
9.1 KiB
Dart
211 lines
9.1 KiB
Dart
import 'package:dio/dio.dart';
|
||
import 'package:eatvio_app/features/notifications/models/notification_model.dart';
|
||
|
||
class NotificationsService {
|
||
final Dio dio;
|
||
NotificationsService(this.dio);
|
||
|
||
Future<Map<String, dynamic>?> getNotifications({required int page}) async {
|
||
try {
|
||
final response = await dio.get('/api/profile/notifications?page=$page');
|
||
if (response.statusCode == 200) {
|
||
final data = response.data['data'] as List<dynamic>;
|
||
|
||
final notifications = data.map((notificationJson) {
|
||
try {
|
||
final stringifiedType = notificationJson['data']['notification_class_name'];
|
||
final readDate = notificationJson['read_at'];
|
||
final createdAt = notificationJson['created_at'];
|
||
final type = NotificationType.getTypeFromString(stringifiedType);
|
||
|
||
return switch (type) {
|
||
NotificationType.planSubscriptionStartedToAuthor => PlanSubscriptionStartedToAuthorNotification.fromJson(
|
||
notificationJson['data'],
|
||
type,
|
||
isRead: readDate != null,
|
||
createdAt: createdAt,
|
||
),
|
||
NotificationType.planSubscriptionCanceledToAuthor => PlanSubscriptionCanceledToAuthorNotification.fromJson(
|
||
notificationJson['data'],
|
||
type,
|
||
isRead: readDate != null,
|
||
createdAt: createdAt,
|
||
),
|
||
NotificationType.chosenPlanStartedToClientNotification => ChosenPlanStartedToClientNotification.fromJson(
|
||
notificationJson['data'],
|
||
type,
|
||
isRead: readDate != null,
|
||
createdAt: createdAt,
|
||
),
|
||
NotificationType.chosenPlanTomorrowReminderToClientNotification => ChosenPlanTomorrowReminderToClientNotification.fromJson(
|
||
notificationJson['data'],
|
||
type,
|
||
isRead: readDate != null,
|
||
createdAt: createdAt,
|
||
),
|
||
NotificationType.diaryReminderNotification => DiaryReminderNotification.fromJson(
|
||
notificationJson['data'],
|
||
type,
|
||
isRead: readDate != null,
|
||
createdAt: createdAt,
|
||
),
|
||
NotificationType.consultantLeftComment => ConsultantLeftCommentNotification.fromJson(
|
||
notificationJson['data'],
|
||
type,
|
||
isRead: readDate != null,
|
||
createdAt: createdAt,
|
||
),
|
||
NotificationType.personalizedDietIsDefined => PersonalizedDietIsDefinedNotification.fromJson(
|
||
notificationJson['data'],
|
||
type,
|
||
isRead: readDate != null,
|
||
createdAt: createdAt,
|
||
),
|
||
NotificationType.chosenPlanFinishedToClient => ChosenPlanFinishedToClientNotification.fromJson(
|
||
notificationJson['data'],
|
||
type,
|
||
isRead: readDate != null,
|
||
createdAt: createdAt,
|
||
),
|
||
NotificationType.planSubscriptionCanceledToCustomer => PlanSubscriptionCanceledToAuthorNotification.fromJson(
|
||
notificationJson['data'],
|
||
type,
|
||
isRead: readDate != null,
|
||
createdAt: createdAt,
|
||
),
|
||
NotificationType.chosenPlanStartedToConsultantNotification => ChosenPlanStartedToConsultantNotification.fromJson(
|
||
notificationJson['data'],
|
||
type,
|
||
isRead: readDate != null,
|
||
createdAt: createdAt,
|
||
),
|
||
NotificationType.chosenPlanTomorrowReminderToConsultantNotification => ChosenPlanTomorrowReminderToConsultantNotification.fromJson(
|
||
notificationJson['data'],
|
||
type,
|
||
isRead: readDate != null,
|
||
createdAt: createdAt,
|
||
),
|
||
NotificationType.planSubscriptionPaidToAuthor => PlanSubscriptionPaidToAuthorNotification.fromJson(
|
||
notificationJson['data'],
|
||
type,
|
||
isRead: readDate != null,
|
||
createdAt: createdAt,
|
||
),
|
||
NotificationType.chosenPlanFinishedToConsultant => ChosenPlanFinishedToConsultantNotification.fromJson(
|
||
notificationJson['data'],
|
||
type,
|
||
isRead: readDate != null,
|
||
createdAt: createdAt,
|
||
),
|
||
NotificationType.chosenPlanLastDayFinishedForUser => ChosenPlanLastDayFinishedForUser.fromJson(
|
||
notificationJson['data'],
|
||
type,
|
||
isRead: readDate != null,
|
||
createdAt: createdAt,
|
||
),
|
||
// Новые типы уведомлений
|
||
NotificationType.chosenPlanConsultantLastDayStarted => ChosenPlanConsultantLastDayStarted.fromJson(
|
||
notificationJson['data'],
|
||
type,
|
||
isRead: readDate != null,
|
||
createdAt: createdAt,
|
||
),
|
||
NotificationType.complaintToModerationForUser => ComplaintToModerationForUser.fromJson(
|
||
notificationJson['data'],
|
||
type,
|
||
isRead: readDate != null,
|
||
createdAt: createdAt,
|
||
),
|
||
NotificationType.commentReplied => CommentReplied.fromJson(notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt),
|
||
NotificationType.scoreAdded => ScoreAdded.fromJson(notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt),
|
||
NotificationType.entityCommented => EntityCommented.fromJson(notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt),
|
||
// Новые типы уведомлений
|
||
NotificationType.planSubscriptionExpired => PlanSubscriptionExpiredNotification.fromJson(
|
||
notificationJson['data'],
|
||
type,
|
||
isRead: readDate != null,
|
||
createdAt: createdAt,
|
||
),
|
||
NotificationType.planSubscriptionRenewed => PlanSubscriptionRenewedNotification.fromJson(
|
||
notificationJson['data'],
|
||
type,
|
||
isRead: readDate != null,
|
||
createdAt: createdAt,
|
||
),
|
||
NotificationType.consultantAssigned => ConsultantAssignedNotification.fromJson(
|
||
notificationJson['data'],
|
||
type,
|
||
isRead: readDate != null,
|
||
createdAt: createdAt,
|
||
),
|
||
NotificationType.consultantUnassigned => ConsultantUnassignedNotification.fromJson(
|
||
notificationJson['data'],
|
||
type,
|
||
isRead: readDate != null,
|
||
createdAt: createdAt,
|
||
),
|
||
NotificationType.paymentFailed => PaymentFailedNotification.fromJson(
|
||
notificationJson['data'],
|
||
type,
|
||
isRead: readDate != null,
|
||
createdAt: createdAt,
|
||
),
|
||
NotificationType.paymentSuccessful => PaymentSuccessfulNotification.fromJson(
|
||
notificationJson['data'],
|
||
type,
|
||
isRead: readDate != null,
|
||
createdAt: createdAt,
|
||
),
|
||
NotificationType.goalAchieved => GoalAchievedNotification.fromJson(
|
||
notificationJson['data'],
|
||
type,
|
||
isRead: readDate != null,
|
||
createdAt: createdAt,
|
||
),
|
||
NotificationType.workoutReminder => WorkoutReminderNotification.fromJson(
|
||
notificationJson['data'],
|
||
type,
|
||
isRead: readDate != null,
|
||
createdAt: createdAt,
|
||
),
|
||
NotificationType.mealReminder => MealReminderNotification.fromJson(
|
||
notificationJson['data'],
|
||
type,
|
||
isRead: readDate != null,
|
||
createdAt: createdAt,
|
||
),
|
||
NotificationType.unknown => UnknownNotification(type: type, createdAt: parseDate(createdAt)),
|
||
};
|
||
} catch (e) {
|
||
return UnknownNotification(type: NotificationType.unknown, createdAt: DateTime.now());
|
||
}
|
||
}).toList();
|
||
|
||
// Извлекаем информацию о пагинации из корня ответа
|
||
final currentPage = response.data['current_page'] as int;
|
||
final lastPage = response.data['last_page'] as int;
|
||
final perPage = response.data['per_page'] as int;
|
||
final total = response.data['total'] as int;
|
||
|
||
return {
|
||
'notifications': notifications,
|
||
'pagination': {'current_page': currentPage, 'last_page': lastPage, 'per_page': perPage, 'total': total},
|
||
};
|
||
}
|
||
return null;
|
||
} catch (e) {
|
||
rethrow;
|
||
}
|
||
}
|
||
|
||
Future<void> markNotificationsAsRead() async {
|
||
try {
|
||
await dio.post('/api/profile/mark_notifications_as_readed');
|
||
} catch (e) {
|
||
rethrow;
|
||
}
|
||
}
|
||
}
|
||
|
||
DateTime parseDate(String date) => DateTime.parse(date).toLocal();
|