-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotification.cs
More file actions
112 lines (87 loc) · 3.36 KB
/
Copy pathNotification.cs
File metadata and controls
112 lines (87 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace SplitzBackend.Models;
// --- Notification Data Types ---
[JsonDerivedType(typeof(InvoiceCreatedNotification), "InvoiceCreated")]
[JsonDerivedType(typeof(SettlementRecordedNotification), "SettlementRecorded")]
[JsonDerivedType(typeof(InvoiceSettledNotification), "InvoiceSettled")]
public abstract class NotificationData;
public class InvoiceCreatedNotification : NotificationData
{
public required string CreatorName { get; set; }
public string? InvoiceName { get; set; }
public required Guid InvoiceId { get; set; }
public required Guid GroupId { get; set; }
}
public class SettlementRecordedNotification : NotificationData
{
public required string RecorderName { get; set; }
public required decimal Amount { get; set; }
public required string Currency { get; set; }
public required string FromUserId { get; set; }
public required string ToUserId { get; set; }
public required Guid InvoiceId { get; set; }
public string? InvoiceName { get; set; }
}
public class InvoiceSettledNotification : NotificationData
{
public required Guid InvoiceId { get; set; }
public string? InvoiceName { get; set; }
public required Guid GroupId { get; set; }
}
// --- Notification Entity ---
public class Notification
{
public Guid NotificationId { get; set; }
public required string UserId { get; set; }
[ForeignKey(nameof(UserId))]
public SplitzUser User { get; set; } = null!;
[Required]
[MaxLength(50)]
public required string Type { get; set; }
[MaxLength(256)] public string? ReferenceId { get; set; }
/// <summary>
/// JSON-serialized data for the notification. Frontend uses Type + Data to build localized messages.
/// </summary>
[Required]
[MaxLength(1024)]
public required string Data { get; set; }
/// <summary>
/// Deserialize Data into a typed NotificationData object.
/// </summary>
public NotificationData? GetTypedData()
{
return Type switch
{
"InvoiceCreated" => JsonSerializer.Deserialize<InvoiceCreatedNotification>(Data, JsonOptions),
"SettlementRecorded" => JsonSerializer.Deserialize<SettlementRecordedNotification>(Data, JsonOptions),
"InvoiceSettled" => JsonSerializer.Deserialize<InvoiceSettledNotification>(Data, JsonOptions),
_ => null
};
}
internal static readonly JsonSerializerOptions JsonOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
public required bool IsRead { get; set; } = false;
public required bool IsDismissed { get; set; } = false;
public required DateTime CreateTime { get; set; }
}
public class NotificationDto
{
public required Guid NotificationId { get; set; }
[Required]
[MaxLength(50)]
public required string Type { get; set; }
[MaxLength(256)] public string? ReferenceId { get; set; }
/// <summary>
/// Deserialized notification data object. The concrete type depends on Type.
/// </summary>
[Required]
public required object Data { get; set; }
public required bool IsRead { get; set; }
public required bool IsDismissed { get; set; }
public required DateTime CreateTime { get; set; }
}