using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
namespace Emails.Transactional.Client
{
///
/// A transactional email request to send an email
/// template.
///
public class EmailTransactionRequest
{
///
/// The transactional send endpoint address
///
[JsonIgnore]
public Uri Endpoint { get; init; }
///
/// A dictionary of email addresses/names of
/// users to send this email to
///
[JsonPropertyName("to")]
public Dictionary ToAddresses { get; set; }
///
/// A dictionary of email addresses/names of
/// users to carbon copy this email to
///
[JsonPropertyName("cc")]
public Dictionary CcAddresses { get; set; }
///
/// A dictionary of email addresses/names of
/// users to blind carbon copy this email to
///
[JsonPropertyName("bcc")]
public Dictionary BccAddresses { get; set; }
///
/// A dictionary of variables to substitute into the liquid
/// email template
///
[JsonPropertyName("variables")]
public Dictionary Variables { get; set; }
///
/// The subject of the email to send
///
[JsonPropertyName("subject")]
public string Subject { get; set; }
///
/// The unique id of the email template to send
///
[JsonPropertyName("template_id")]
public string TemplateId { get; set; }
///
/// The system from email name. NOTE: This is a protected value
///
[JsonPropertyName("from_name")]
public string FromName { get; set; }
///
/// The system from email address. NOTE: This is a protected value
///
[JsonPropertyName("from_address")]
public string FromAddress { get; set; }
///
/// Creates a new email transaction with the specified email template to send
///
/// The id of the template to send
///
public EmailTransactionRequest(string templateId)
{
this.TemplateId = templateId ?? throw new ArgumentNullException(nameof(templateId));
}
///
/// Creates a new email transaction with the specified email template
/// and a single recipient
///
/// The id of the template to send
/// A singular recipient name
/// A singlular recipient email address
///
public EmailTransactionRequest(string templateId, string toName, string toAddress)
{
this.TemplateId = templateId ?? throw new ArgumentNullException(nameof(templateId));
AddToAddress(toName, toAddress);
}
///
/// Adds a recipient to the To email address dictionary
///
/// The name of the user to send the email to
/// The unique email address of the user to add to the recipient collection
public void AddToAddress(string toName, string toAddress)
{
ToAddresses ??= new(1);
ToAddresses.Add(toAddress, toName);
}
///
/// Adds a recipient to the To email address dictionary
///
/// The unique email address of the user to add to the recipient collection
public void AddToAddress(string toAddress)
{
string name = toAddress.Split('@')[0];
AddToAddress(name, toAddress);
}
///
/// Adds a carbon copy recipient to the current cc dictionary
///
/// The name of the recipient
/// The unique email address of the bcc recipient
public void AddCcAddress(string ccName, string ccAddress)
{
CcAddresses ??= new(1);
CcAddresses.Add(ccAddress, ccName);
}
///
/// Adds a blind carbon copy recipient to the current bcc dictionary
///
/// The name of the recipient
/// The unique email address of the bcc recipient
public void AddBccAddress(string bccName, string bccAddress)
{
BccAddresses ??= new(1);
BccAddresses.Add(bccAddress, bccName);
}
///
/// Adds a liquid template variable to be subsituted by the template
/// renderer.
///
/// The unique name of the variable to add to the collection
/// The value if the variable that will be substituted into the template
public void AddVariable(string varName, string varValue)
{
Variables ??= new(1);
Variables.Add(varName, varValue);
}
}
}