110 lines
3.9 KiB
C#
110 lines
3.9 KiB
C#
using System.Diagnostics.CodeAnalysis;
|
|
using SlpModularCms.Modules.Contact.Data.Entities;
|
|
|
|
namespace SlpModularCms.Modules.Contact.Models;
|
|
|
|
// JSON shapes of the management routes. Statuses are lower-camel strings:
|
|
// status: new | read | archived | spam | converted
|
|
// mailStatus: pending | sent | failed | notApplicable
|
|
|
|
[ExcludeFromCodeCoverage]
|
|
public record ContactMessageListItem(
|
|
string Id,
|
|
DateTimeOffset ReceivedAt,
|
|
string Name,
|
|
string Email,
|
|
string Subject,
|
|
string Status,
|
|
string MailStatus);
|
|
|
|
[ExcludeFromCodeCoverage]
|
|
public record ContactMessageList(
|
|
IReadOnlyList<ContactMessageListItem> Items,
|
|
int Page,
|
|
int PageSize,
|
|
int TotalCount,
|
|
int UnreadCount);
|
|
|
|
[ExcludeFromCodeCoverage]
|
|
public record ContactMessageDetail(
|
|
string Id,
|
|
DateTimeOffset ReceivedAt,
|
|
string Name,
|
|
string Email,
|
|
string Subject,
|
|
string Message,
|
|
string Status,
|
|
string MailStatus,
|
|
int MailAttempts,
|
|
string? MailError,
|
|
DateTimeOffset RetentionUntil,
|
|
DateTimeOffset? NextMailAttemptAt = null);
|
|
|
|
/// <summary>Body of PATCH /contact-messages/{id}. Nullable so a missing field is reported in our own shape.</summary>
|
|
public record UpdateContactMessageStatusRequest(string? Status);
|
|
|
|
[ExcludeFromCodeCoverage]
|
|
public record ContactResendResult(string MailStatus, int MailAttempts, bool Mailed, string? MailError = null);
|
|
|
|
[ExcludeFromCodeCoverage]
|
|
public record ContactSettingsDto(
|
|
bool Enabled,
|
|
bool HostEnabled,
|
|
string? RecipientEmail = null,
|
|
string RecipientSource = ContactRecipientSource.None);
|
|
|
|
/// <summary>Where the address contact mails go to comes from. The address from the environment is never returned.</summary>
|
|
public static class ContactRecipientSource
|
|
{
|
|
public const string Setting = "setting";
|
|
public const string Config = "config";
|
|
public const string None = "none";
|
|
}
|
|
|
|
/// <summary>Body of PUT /contact-settings/recipient. null or empty clears the setting (back to the server configuration).</summary>
|
|
public record UpdateContactRecipientRequest(string? RecipientEmail);
|
|
|
|
/// <summary>Body of PUT /contact-settings. Nullable so a missing or non-boolean value is reported as errors.enabled.</summary>
|
|
public record UpdateContactSettingsRequest(bool? Enabled);
|
|
|
|
/// <summary>The wire names of the statuses.</summary>
|
|
public static class ContactStatusNames
|
|
{
|
|
public static string Of(ContactMessageStatus status) => status switch
|
|
{
|
|
ContactMessageStatus.New => "new",
|
|
ContactMessageStatus.Read => "read",
|
|
ContactMessageStatus.Archived => "archived",
|
|
ContactMessageStatus.Spam => "spam",
|
|
ContactMessageStatus.Converted => "converted",
|
|
_ => throw new ArgumentOutOfRangeException(nameof(status)),
|
|
};
|
|
|
|
public static string Of(ContactMailStatus status) => status switch
|
|
{
|
|
ContactMailStatus.Pending => "pending",
|
|
ContactMailStatus.Sent => "sent",
|
|
ContactMailStatus.Failed => "failed",
|
|
ContactMailStatus.NotApplicable => "notApplicable",
|
|
_ => throw new ArgumentOutOfRangeException(nameof(status)),
|
|
};
|
|
|
|
/// <summary>Parses a filter value (case-insensitive); only the statuses that can be listed.</summary>
|
|
public static bool TryParseListable(string? value, out ContactMessageStatus status)
|
|
{
|
|
status = default;
|
|
switch (value?.Trim().ToLowerInvariant())
|
|
{
|
|
case "new": status = ContactMessageStatus.New; return true;
|
|
case "read": status = ContactMessageStatus.Read; return true;
|
|
case "archived": status = ContactMessageStatus.Archived; return true;
|
|
case "spam": status = ContactMessageStatus.Spam; return true;
|
|
default: return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>Parses a value an administrator may set: new, read or archived.</summary>
|
|
public static bool TryParseSettable(string? value, out ContactMessageStatus status)
|
|
=> TryParseListable(value, out status) && status != ContactMessageStatus.Spam;
|
|
}
|