aboutsummaryrefslogtreecommitdiff
path: root/lib/Emails.Transactional.Plugin/src/EmailTransaction.cs
blob: bb7b4a878e5110d70acc801521d96f28186b0595 (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* Copyright (c) 2022 Vaughn Nugent
* 
* Library: VNLib
* Package: Transactional Emails
* File: EmailTransaction.cs 
*
* EmailTransaction.cs is part of Transactional Emails which is part of the larger 
* VNLib collection of libraries and utilities.
*
* Transactional Emails is free software: you can redistribute it and/or modify 
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 2 of the License,
* or (at your option) any later version.
*
* Transactional Emails is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License 
* along with Transactional Emails. If not, see http://www.gnu.org/licenses/.
*/

using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

using VNLib.Plugins.Essentials;
using VNLib.Plugins.Extensions.Data;
using VNLib.Plugins.Extensions.Data.Abstractions;

#nullable enable

namespace Emails.Transactional
{   
    /// <summary>
    /// Represents an email transaction request and its status reflected 
    /// in the database
    /// </summary>
    internal class EmailTransaction : DbModelBase, IUserEntity
    {
        ///<inheritdoc/>
        [Key]
        [JsonPropertyName("id")]
        public override string Id { get; set; }
        ///<inheritdoc/>
        [JsonIgnore]
        public override DateTime Created { get; set; }
        ///<inheritdoc/>
        [JsonIgnore]
        public override DateTime LastModified { get; set; }
        
        /// <summary>
        /// The sever side user-id that send the email
        /// </summary>
        [JsonIgnore]
        public string? UserId { get; set; }

    //To address
        [JsonIgnore]
        public string? To
        {
            //Use json to serialize/deserialze the to addresses
            get => JsonSerializer.Serialize(ToAddresses, Statics.SR_OPTIONS);
            set => ToAddresses = JsonSerializer.Deserialize<Dictionary<string, string>>(value!, Statics.SR_OPTIONS);
        }

        /// <summary>
        /// A dictionary of to email address and name pairs
        /// </summary>
        [NotMapped]
        [JsonPropertyName("to")]
        public Dictionary<string, string>? ToAddresses { get; set; }
    
    //From
        /// <summary>
        /// The from email address
        /// </summary>
        [JsonPropertyName("from")]
        public string? From { get; set; }

        /// <summary>
        /// The optional from name (not mapped in db)
        /// </summary>
        [NotMapped]
        [JsonPropertyName("from_name")]
        public string? FromName { get; set; }
        /// <summary>
        /// The email subject
        /// </summary>
        [JsonPropertyName("subject")]
        public string? Subject { get; set; }

    //CC names
        //ccs are stored in the db as a json serialized string
        [JsonIgnore]
        public string Ccs
        {
            //Use json to serialize/deserialze the to addresses
            get => JsonSerializer.Serialize(CcAddresses, Statics.SR_OPTIONS);
            set => CcAddresses = JsonSerializer.Deserialize<Dictionary<string, string>>(value, Statics.SR_OPTIONS);
        }

        [JsonPropertyName("cc")]
        [NotMapped]
        public Dictionary<string, string>? CcAddresses { get; set; }

    //BCC Names
        
        //bccs are stored in the db as a json serialized string
        [JsonIgnore]
        public string Bccs
        {
            //Store bccs as a comma separated list of addresses
            get => JsonSerializer.Serialize(BccAddresses, Statics.SR_OPTIONS);
            set => BccAddresses = JsonSerializer.Deserialize<Dictionary<string, string>>(value, Statics.SR_OPTIONS);
        }

        /// <summary>
        /// A dictionary of bcc addresses and names
        /// </summary>
        [JsonPropertyName("bcc")]
        [NotMapped]
        public Dictionary<string, string>? BccAddresses { get; set; }    

        /// <summary>
        /// The replyto email address
        /// </summary>
        [JsonPropertyName("reply_to")]
        public string? ReplyTo { get; set; }
        /// <summary>
        /// Optional reply-to name for the address (not stored in db)
        /// </summary>
        [NotMapped]
        [JsonPropertyName("reply_to_name")]
        public string? ReplyToName { get; set; }

        /// <summary>
        /// The object id of the template to send
        /// </summary>
        [JsonPropertyName("template_id")]
        public string? TemplateId { get; set; }

        /// <summary>
        /// The result of the STMP transaction
        /// </summary>
        [JsonIgnore]
        public string? Result { get; set; }

        /// <summary>
        /// Variables requested from the client to embed in the template
        /// during processing. These are not stored in the database
        /// </summary>
        [NotMapped]
        [JsonPropertyName("variables")]
        public Dictionary<string, string>? Variables { get; set; }
    }
}