forked from eatvio/eatvio_app
Compare commits
2 Commits
main
...
feat/notif
| Author | SHA1 | Date | |
|---|---|---|---|
| 280263b419 | |||
| c6915a0d94 |
@@ -1011,6 +1011,125 @@ class MealReminderNotification implements BaseNotificationModel {
|
||||
}
|
||||
}
|
||||
|
||||
/// Данные уведомления: в API-ленте лежат во вложенном `data`, а в realtime-событии могут быть в корне.
|
||||
Map<String, dynamic> _notificationData(Map<String, dynamic> json) {
|
||||
final data = json['data'];
|
||||
return data is Map<String, dynamic> ? 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<String, dynamic> 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<String, dynamic> 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;
|
||||
@@ -2526,6 +2645,8 @@ enum NotificationType {
|
||||
clientStartPlanSubscriptionToAuthor,
|
||||
clientCancelPlanSubscriptionToAuthor,
|
||||
clientCancelPlanSubscriptionToSubscriber,
|
||||
clientPlanPaymentRequired,
|
||||
consultantPlanStartDateSet,
|
||||
userDailyReportCompleted,
|
||||
chosenPlanFinishedToConsultant,
|
||||
stayOnTrackToCustomer,
|
||||
@@ -2648,6 +2769,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,
|
||||
|
||||
@@ -355,6 +355,18 @@ class NotificationsCubit extends Cubit<NotificationsState> {
|
||||
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) {
|
||||
|
||||
@@ -137,11 +137,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) {
|
||||
|
||||
@@ -495,6 +495,18 @@ class NotificationsService {
|
||||
isRead: readDate != null,
|
||||
createdAt: createdAt,
|
||||
),
|
||||
NotificationType.clientPlanPaymentRequired => ClientPlanPaymentRequiredNotification.fromJson(
|
||||
(notificationJson['data'] as Map<String, dynamic>?) ?? notificationJson,
|
||||
type,
|
||||
isRead: readDate != null,
|
||||
createdAt: createdAt,
|
||||
),
|
||||
NotificationType.consultantPlanStartDateSet => ConsultantPlanStartDateSetNotification.fromJson(
|
||||
(notificationJson['data'] as Map<String, dynamic>?) ?? notificationJson,
|
||||
type,
|
||||
isRead: readDate != null,
|
||||
createdAt: createdAt,
|
||||
),
|
||||
NotificationType.unknown => UnknownNotification(type: type, createdAt: parseDate(createdAt)),
|
||||
};
|
||||
} catch (error, stackTrace) {
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -51,6 +51,8 @@ void main() {
|
||||
'diary-reminder-for-users': NotificationType.diaryReminderForUsers,
|
||||
'personal-diet-is-defined-to-customer': NotificationType.personalDietIsDefinedToCustomer,
|
||||
'personal-diet-is-defined-to-author': NotificationType.personalDietIsDefinedToAuthor,
|
||||
'client-plan-payment-required': NotificationType.clientPlanPaymentRequired,
|
||||
'consultant-plan-start-date-set': NotificationType.consultantPlanStartDateSet,
|
||||
};
|
||||
|
||||
cases.forEach((key, expected) {
|
||||
|
||||
@@ -1654,6 +1654,100 @@ void main() {
|
||||
expect(result!['notifications'], isA<List>());
|
||||
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<BaseNotificationModel>;
|
||||
final notification = notifications.single;
|
||||
|
||||
expect(notification, isA<ClientPlanPaymentRequiredNotification>());
|
||||
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<BaseNotificationModel>;
|
||||
final notification = notifications.single;
|
||||
|
||||
expect(notification, isA<ConsultantPlanStartDateSetNotification>());
|
||||
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);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user