chitfund/luckychit/lib/core/models/notification.dart

221 lines
6.4 KiB
Dart

import 'package:flutter/material.dart';
class NotificationModel {
final String id;
final String type;
final String userId;
final String? groupId;
final String? groupName;
final String? paymentId;
final String? drawId;
final String channel;
final String title;
final String message;
final String status;
final int? daysBeforeDue;
final String priority;
final DateTime? sentAt;
final DateTime? readAt;
final String? failureReason;
final Map<String, dynamic>? metadata;
final DateTime createdAt;
final DateTime updatedAt;
NotificationModel({
required this.id,
required this.type,
required this.userId,
this.groupId,
this.groupName,
this.paymentId,
this.drawId,
required this.channel,
required this.title,
required this.message,
required this.status,
this.daysBeforeDue,
required this.priority,
this.sentAt,
this.readAt,
this.failureReason,
this.metadata,
required this.createdAt,
required this.updatedAt,
});
bool get isUnread => readAt == null && status == 'sent';
bool get isRead => readAt != null;
bool get isPending => status == 'pending';
bool get isFailed => status == 'failed';
factory NotificationModel.fromJson(Map<String, dynamic> json) {
return NotificationModel(
id: json['id'] ?? '',
type: json['type'] ?? '',
userId: json['user_id'] ?? '',
groupId: json['group_id'],
groupName: json['group']?['name'],
paymentId: json['payment_id'],
drawId: json['draw_id'],
channel: json['channel'] ?? '',
title: json['title'] ?? '',
message: json['message'] ?? '',
status: json['status'] ?? 'pending',
daysBeforeDue: json['days_before_due'],
priority: json['priority'] ?? 'medium',
sentAt: json['sent_at'] != null ? DateTime.parse(json['sent_at']) : null,
readAt: json['read_at'] != null ? DateTime.parse(json['read_at']) : null,
failureReason: json['failure_reason'],
metadata: json['metadata'],
createdAt: DateTime.parse(json['createdAt'] ?? json['created_at']),
updatedAt: DateTime.parse(json['updatedAt'] ?? json['updated_at']),
);
}
Map<String, dynamic> toJson() {
return {
'id': id,
'type': type,
'user_id': userId,
'group_id': groupId,
'payment_id': paymentId,
'draw_id': drawId,
'channel': channel,
'title': title,
'message': message,
'status': status,
'days_before_due': daysBeforeDue,
'priority': priority,
'sent_at': sentAt?.toIso8601String(),
'read_at': readAt?.toIso8601String(),
'failure_reason': failureReason,
'metadata': metadata,
'createdAt': createdAt.toIso8601String(),
'updatedAt': updatedAt.toIso8601String(),
};
}
// Get icon based on notification type
IconData getIcon() {
switch (type) {
case 'payment_reminder':
case 'payment_overdue':
return Icons.payment_rounded;
case 'payment_confirmation':
return Icons.check_circle_rounded;
case 'draw_result':
return Icons.casino_rounded;
case 'group_invite':
return Icons.group_add_rounded;
case 'member_joined':
return Icons.person_add_rounded;
case 'member_removed':
return Icons.person_remove_rounded;
case 'group_started':
case 'group_completed':
return Icons.flag_rounded;
case 'welcome_message':
return Icons.waving_hand_rounded;
case 'manager_notification':
return Icons.admin_panel_settings_rounded;
case 'system_alert':
return Icons.warning_rounded;
default:
return Icons.notifications_rounded;
}
}
// Get color based on notification type
Color getColor() {
switch (type) {
case 'payment_reminder':
return const Color(0xFFF57C00); // Orange
case 'payment_overdue':
return const Color(0xFFD32F2F); // Red
case 'payment_confirmation':
return const Color(0xFF2E7D32); // Green
case 'draw_result':
return const Color(0xFF1976D2); // Blue
case 'group_invite':
case 'member_joined':
return const Color(0xFF4CAF50); // Light green
case 'member_removed':
return const Color(0xFF757575); // Gray
case 'group_started':
case 'group_completed':
return const Color(0xFF7B1FA2); // Purple
case 'welcome_message':
return const Color(0xFF00BCD4); // Cyan
case 'system_alert':
return const Color(0xFFFF5722); // Deep orange
default:
return const Color(0xFF757575); // Gray
}
}
// Get formatted time ago
String getTimeAgo() {
final now = DateTime.now();
final difference = now.difference(createdAt);
if (difference.inSeconds < 60) {
return 'Just now';
} else if (difference.inMinutes < 60) {
return '${difference.inMinutes}m ago';
} else if (difference.inHours < 24) {
return '${difference.inHours}h ago';
} else if (difference.inDays < 7) {
return '${difference.inDays}d ago';
} else if (difference.inDays < 30) {
return '${(difference.inDays / 7).floor()}w ago';
} else {
return '${(difference.inDays / 30).floor()}mo ago';
}
}
NotificationModel copyWith({
String? id,
String? type,
String? userId,
String? groupId,
String? groupName,
String? paymentId,
String? drawId,
String? channel,
String? title,
String? message,
String? status,
int? daysBeforeDue,
String? priority,
DateTime? sentAt,
DateTime? readAt,
String? failureReason,
Map<String, dynamic>? metadata,
DateTime? createdAt,
DateTime? updatedAt,
}) {
return NotificationModel(
id: id ?? this.id,
type: type ?? this.type,
userId: userId ?? this.userId,
groupId: groupId ?? this.groupId,
groupName: groupName ?? this.groupName,
paymentId: paymentId ?? this.paymentId,
drawId: drawId ?? this.drawId,
channel: channel ?? this.channel,
title: title ?? this.title,
message: message ?? this.message,
status: status ?? this.status,
daysBeforeDue: daysBeforeDue ?? this.daysBeforeDue,
priority: priority ?? this.priority,
sentAt: sentAt ?? this.sentAt,
readAt: readAt ?? this.readAt,
failureReason: failureReason ?? this.failureReason,
metadata: metadata ?? this.metadata,
createdAt: createdAt ?? this.createdAt,
updatedAt: updatedAt ?? this.updatedAt,
);
}
}