diff --git a/lib/features/notifications/models/notification_model.dart b/lib/features/notifications/models/notification_model.dart index b178eb91..036206a3 100644 --- a/lib/features/notifications/models/notification_model.dart +++ b/lib/features/notifications/models/notification_model.dart @@ -1011,6 +1011,125 @@ class MealReminderNotification implements BaseNotificationModel { } } +/// Данные уведомления: в API-ленте лежат во вложенном `data`, а в realtime-событии могут быть в корне. +Map _notificationData(Map json) { + final data = json['data']; + return data is Map ? data : json; +} + +DateTime? parseOptionalDate(dynamic value) { + if (value == null) { + return null; + } + final raw = value.toString(); + if (raw.isEmpty) { + return null; + } + return DateTime.tryParse(raw)?.toLocal(); +} + +class ClientPlanPaymentRequiredNotification implements BaseNotificationModel { + @override + final NotificationType type; + @override + final bool isRead; + final String planName; + final int planId; + final String authorName; + final int authorId; + final String authorPhotoPath; + final String chatId; + final String chatUrl; + final DateTime? startDate; + final DateTime? paymentDeadlineAt; + @override + final DateTime createdAt; + + ClientPlanPaymentRequiredNotification({ + required this.type, + required this.isRead, + required this.planName, + required this.planId, + required this.authorName, + required this.authorId, + required this.authorPhotoPath, + required this.chatId, + required this.chatUrl, + required this.startDate, + required this.paymentDeadlineAt, + required this.createdAt, + }); + + factory ClientPlanPaymentRequiredNotification.fromJson(Map json, NotificationType type, {bool? isRead, String? createdAt}) { + final data = _notificationData(json); + return ClientPlanPaymentRequiredNotification( + type: type, + isRead: isRead ?? false, + planName: data['plan_name'] ?? '', + planId: data['plan_id'] ?? 0, + authorName: data['author_name'] ?? '', + authorId: data['author_id'] ?? 0, + authorPhotoPath: data['author_photo_path'] ?? '', + chatId: data['chat_id'] ?? '', + chatUrl: data['chat_url'] ?? '', + startDate: parseOptionalDate(data['start_date']), + paymentDeadlineAt: parseOptionalDate(data['payment_deadline_at']), + createdAt: parseDate(createdAt ?? json['created_at'] ?? data['created_at'] ?? DateTime.now().toIso8601String()), + ); + } +} + +class ConsultantPlanStartDateSetNotification implements BaseNotificationModel { + @override + final NotificationType type; + @override + final bool isRead; + final String planName; + final int planId; + final String clientName; + final int userId; + final String userPhotoPath; + final String chatId; + final String chatUrl; + final DateTime? startDate; + final DateTime? paymentDeadlineAt; + @override + final DateTime createdAt; + + ConsultantPlanStartDateSetNotification({ + required this.type, + required this.isRead, + required this.planName, + required this.planId, + required this.clientName, + required this.userId, + required this.userPhotoPath, + required this.chatId, + required this.chatUrl, + required this.startDate, + required this.paymentDeadlineAt, + required this.createdAt, + }); + + factory ConsultantPlanStartDateSetNotification.fromJson(Map json, NotificationType type, {bool? isRead, String? createdAt}) { + final data = _notificationData(json); + return ConsultantPlanStartDateSetNotification( + type: type, + isRead: isRead ?? false, + planName: data['plan_name'] ?? '', + planId: data['plan_id'] ?? 0, + clientName: data['user_name'] ?? '', + userId: data['user_id'] ?? 0, + userPhotoPath: data['user_photo_path'] ?? '', + chatId: data['chat_id'] ?? '', + chatUrl: data['chat_url'] ?? '', + startDate: parseOptionalDate(data['start_date']), + paymentDeadlineAt: parseOptionalDate(data['payment_deadline_at']), + createdAt: parseDate(createdAt ?? json['created_at'] ?? data['created_at'] ?? DateTime.now().toIso8601String()), + ); + } +} + class UnknownNotification implements BaseNotificationModel { @override final NotificationType type; @@ -2476,6 +2595,8 @@ enum NotificationType { clientStartPlanSubscriptionToAuthor, clientCancelPlanSubscriptionToAuthor, clientCancelPlanSubscriptionToSubscriber, + clientPlanPaymentRequired, + consultantPlanStartDateSet, userDailyReportCompleted, chosenPlanFinishedToConsultant, stayOnTrackToCustomer, @@ -2597,6 +2718,8 @@ enum NotificationType { 'client-start-plan-subscription-to-author' => NotificationType.clientStartPlanSubscriptionToAuthor, 'client-cancel-plan-subscription-to-author' => NotificationType.clientCancelPlanSubscriptionToAuthor, 'client-cancel-plan-subscription-to-subscriber' => NotificationType.clientCancelPlanSubscriptionToSubscriber, + 'client-plan-payment-required' => NotificationType.clientPlanPaymentRequired, + 'consultant-plan-start-date-set' => NotificationType.consultantPlanStartDateSet, 'user-daily-report-completed' => NotificationType.userDailyReportCompleted, 'chosen-plan-finished-to-consultant' => NotificationType.chosenPlanFinishedToConsultant, 'stay-on-track-to-customer' => NotificationType.stayOnTrackToCustomer, diff --git a/lib/features/notifications/presentation/notifications_cubit/notifications_cubit.dart b/lib/features/notifications/presentation/notifications_cubit/notifications_cubit.dart index a3c5827d..4061779c 100644 --- a/lib/features/notifications/presentation/notifications_cubit/notifications_cubit.dart +++ b/lib/features/notifications/presentation/notifications_cubit/notifications_cubit.dart @@ -344,6 +344,18 @@ class NotificationsCubit extends Cubit { isRead: isRead, createdAt: createdAt, ), + NotificationType.clientPlanPaymentRequired => ClientPlanPaymentRequiredNotification.fromJson( + notificationJson, + type, + isRead: isRead, + createdAt: createdAt, + ), + NotificationType.consultantPlanStartDateSet => ConsultantPlanStartDateSetNotification.fromJson( + notificationJson, + type, + isRead: isRead, + createdAt: createdAt, + ), NotificationType.unknown => UnknownNotification(type: type, createdAt: parseDate(createdAt ?? jsonEncode(notificationJson))), }; } catch (e, st) { diff --git a/lib/features/notifications/presentation/widgets/notification_widget.dart b/lib/features/notifications/presentation/widgets/notification_widget.dart index 26714425..e798c3f3 100644 --- a/lib/features/notifications/presentation/widgets/notification_widget.dart +++ b/lib/features/notifications/presentation/widgets/notification_widget.dart @@ -134,11 +134,54 @@ class NotificationWidget extends StatelessWidget { CommentComplaintNotification() => _CommentComplaintWidget(notification: notification as CommentComplaintNotification), CommentDeletedNotification() => _CommentDeletedWidget(notification: notification as CommentDeletedNotification), UserSubscribedNotification() => _UserSubscribedWidget(notification: notification as UserSubscribedNotification), + ClientPlanPaymentRequiredNotification() => _ClientPlanPaymentRequiredWidget( + notification: notification as ClientPlanPaymentRequiredNotification, + ), + ConsultantPlanStartDateSetNotification() => _ConsultantPlanStartDateSetWidget( + notification: notification as ConsultantPlanStartDateSetNotification, + ), UnknownNotification() => const SizedBox.shrink(), }; } } +VoidCallback? _chatTap(BuildContext context, String chatUrl) { + final uri = Uri.tryParse(chatUrl); + final uuid = (uri != null && uri.pathSegments.isNotEmpty) ? uri.pathSegments.last : ''; + if (uuid.isEmpty) { + return null; + } + return () => context.push('${RouteConstants.chatScreen}?uuid=$uuid'); +} + +class _ClientPlanPaymentRequiredWidget extends StatelessWidget { + const _ClientPlanPaymentRequiredWidget({required this.notification}); + final ClientPlanPaymentRequiredNotification notification; + @override + Widget build(BuildContext context) { + return _BaseNotificationWidget( + isRead: notification.isRead, + notificationsText: 'Необходимо оплатить консультацию «${notification.planName}» у консультанта ${notification.authorName}', + createdAt: notification.createdAt, + onTap: _chatTap(context, notification.chatUrl), + ); + } +} + +class _ConsultantPlanStartDateSetWidget extends StatelessWidget { + const _ConsultantPlanStartDateSetWidget({required this.notification}); + final ConsultantPlanStartDateSetNotification notification; + @override + Widget build(BuildContext context) { + return _BaseNotificationWidget( + isRead: notification.isRead, + notificationsText: 'Дата старта установлена: «${notification.planName}». Клиент ${notification.clientName} ещё не оплатил', + createdAt: notification.createdAt, + onTap: _chatTap(context, notification.chatUrl), + ); + } +} + void _navigateToBottomNavRoute(BuildContext context, String path, [Object? extra]) { final controller = BottomNavScope.maybeOf(context); if (controller != null) { diff --git a/lib/features/notifications/service/notifications_service.dart b/lib/features/notifications/service/notifications_service.dart index c934fb53..4615ce15 100644 --- a/lib/features/notifications/service/notifications_service.dart +++ b/lib/features/notifications/service/notifications_service.dart @@ -485,6 +485,18 @@ class NotificationsService { isRead: readDate != null, createdAt: createdAt, ), + NotificationType.clientPlanPaymentRequired => ClientPlanPaymentRequiredNotification.fromJson( + (notificationJson['data'] as Map?) ?? notificationJson, + type, + isRead: readDate != null, + createdAt: createdAt, + ), + NotificationType.consultantPlanStartDateSet => ConsultantPlanStartDateSetNotification.fromJson( + (notificationJson['data'] as Map?) ?? notificationJson, + type, + isRead: readDate != null, + createdAt: createdAt, + ), NotificationType.unknown => UnknownNotification(type: type, createdAt: parseDate(createdAt)), }; } catch (e) { diff --git a/test/features/notifications/models/notification_model_fromjson_bulk_test.dart b/test/features/notifications/models/notification_model_fromjson_bulk_test.dart index 8ee7fe97..71d8fb5a 100644 --- a/test/features/notifications/models/notification_model_fromjson_bulk_test.dart +++ b/test/features/notifications/models/notification_model_fromjson_bulk_test.dart @@ -63,6 +63,8 @@ void main() { ScoreAdded.fromJson(scoreLike, NotificationType.scoreAdded); ScoreSubstractedNotification.fromJson(scoreLike, NotificationType.scoreSubstracted); EntityCommented.fromJson(entityCommented, NotificationType.entityCommented); + ClientPlanPaymentRequiredNotification.fromJson(base, NotificationType.clientPlanPaymentRequired); + ConsultantPlanStartDateSetNotification.fromJson(base, NotificationType.consultantPlanStartDateSet); UnknownNotification.fromJson(base, NotificationType.unknown); ConsultantConfirmPlanSubscriptionToSubscriberNotification.fromJson( diff --git a/test/features/notifications/models/notification_model_test.dart b/test/features/notifications/models/notification_model_test.dart index d7f809e0..8727dce0 100644 --- a/test/features/notifications/models/notification_model_test.dart +++ b/test/features/notifications/models/notification_model_test.dart @@ -50,6 +50,8 @@ void main() { 'stay-on-track-to-customer': NotificationType.stayOnTrackToCustomer, 'diary-reminder-for-users': NotificationType.diaryReminderForUsers, 'personal-diet-is-defined-to-customer': NotificationType.personalDietIsDefinedToCustomer, + 'client-plan-payment-required': NotificationType.clientPlanPaymentRequired, + 'consultant-plan-start-date-set': NotificationType.consultantPlanStartDateSet, }; cases.forEach((key, expected) { diff --git a/test/features/notifications/service/notifications_service_test.dart b/test/features/notifications/service/notifications_service_test.dart index 5c2b62ab..0a191319 100644 --- a/test/features/notifications/service/notifications_service_test.dart +++ b/test/features/notifications/service/notifications_service_test.dart @@ -1,4 +1,5 @@ import 'package:dio/dio.dart'; +import 'package:eatvio_app/features/notifications/models/notification_model.dart'; import 'package:eatvio_app/features/notifications/service/notifications_service.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mockito/annotations.dart'; @@ -1617,6 +1618,100 @@ void main() { expect(result!['notifications'], isA()); expect(result['notifications'].length, 1); }); + + test('getNotifications - распознаёт client-plan-payment-required', () async { + const page = 1; + final response = Response( + data: { + 'data': [ + { + 'type': 'client-plan-payment-required', + 'data': { + 'plan_name': 'План 3054', + 'plan_id': 101, + 'author_name': 'Консультант', + 'author_id': 7, + 'author_photo_path': '/storage/a.jpg', + 'chat_id': 'uuid-101', + 'chat_url': 'https://eatvio.ru/ru/chat/uuid-101', + 'start_date': '2026-11-27T00:00:00+03:00', + 'payment_deadline_at': '2026-11-26T12:00:00+03:00', + }, + 'read_at': null, + 'created_at': '2026-11-24T10:00:00Z', + }, + ], + 'current_page': 1, + 'last_page': 1, + 'per_page': 10, + 'total': 1, + }, + statusCode: 200, + requestOptions: RequestOptions(path: '/api/profile/notifications'), + ); + + when(mockDio.get(any)).thenAnswer((_) async => response); + + final result = await notificationsService.getNotifications(page: page); + final notifications = result!['notifications'] as List; + final notification = notifications.single; + + expect(notification, isA()); + expect(notification.type, NotificationType.clientPlanPaymentRequired); + expect(notification.isRead, isFalse); + final clientNotification = notification as ClientPlanPaymentRequiredNotification; + expect(clientNotification.planName, 'План 3054'); + expect(clientNotification.authorName, 'Консультант'); + expect(clientNotification.chatUrl, 'https://eatvio.ru/ru/chat/uuid-101'); + expect(clientNotification.paymentDeadlineAt, isNotNull); + }); + + test('getNotifications - распознаёт consultant-plan-start-date-set', () async { + const page = 1; + final response = Response( + data: { + 'data': [ + { + 'type': 'consultant-plan-start-date-set', + 'data': { + 'plan_name': 'План 3054', + 'plan_id': 102, + 'user_name': 'Клиент', + 'user_id': 8, + 'user_photo_path': '/storage/c.jpg', + 'chat_id': 'uuid-102', + 'chat_url': 'https://eatvio.ru/ru/chat/uuid-102', + 'start_date': '2026-11-27T00:00:00+03:00', + 'payment_deadline_at': null, + }, + 'read_at': '2026-11-24T11:00:00Z', + 'created_at': '2026-11-24T10:00:00Z', + }, + ], + 'current_page': 1, + 'last_page': 1, + 'per_page': 10, + 'total': 1, + }, + statusCode: 200, + requestOptions: RequestOptions(path: '/api/profile/notifications'), + ); + + when(mockDio.get(any)).thenAnswer((_) async => response); + + final result = await notificationsService.getNotifications(page: page); + final notifications = result!['notifications'] as List; + final notification = notifications.single; + + expect(notification, isA()); + expect(notification.type, NotificationType.consultantPlanStartDateSet); + expect(notification.isRead, isTrue); + final consultantNotification = notification as ConsultantPlanStartDateSetNotification; + expect(consultantNotification.planName, 'План 3054'); + expect(consultantNotification.clientName, 'Клиент'); + expect(consultantNotification.chatUrl, 'https://eatvio.ru/ru/chat/uuid-102'); + expect(consultantNotification.paymentDeadlineAt, isNull); + }); }); }