feat(sms): Transport recipient PhoneNumber + SmsConfig round-trip (S10)
This commit is contained in:
@@ -210,7 +210,9 @@ public sealed class ArtifactDiff
|
||||
incoming.Recipients,
|
||||
e => e.Name,
|
||||
i => i.Name,
|
||||
(e, i) => e.EmailAddress == i.EmailAddress,
|
||||
// S10: a recipient is unchanged only when BOTH contacts match — a phone
|
||||
// number added/changed on an existing recipient is a real diff.
|
||||
(e, i) => e.EmailAddress == i.EmailAddress && e.PhoneNumber == i.PhoneNumber,
|
||||
"Recipients",
|
||||
changes);
|
||||
|
||||
|
||||
@@ -2411,7 +2411,7 @@ public sealed class BundleImporter : IBundleImporter
|
||||
existing.Recipients.Clear();
|
||||
foreach (var r in dto.Recipients)
|
||||
{
|
||||
existing.Recipients.Add(new NotificationRecipient(r.Name, r.EmailAddress));
|
||||
existing.Recipients.Add(BuildRecipient(r));
|
||||
}
|
||||
await _notificationRepo.UpdateNotificationListAsync(existing, ct).ConfigureAwait(false);
|
||||
await _auditService.LogAsync(user, "Update", "NotificationList", existing.Id.ToString(), existing.Name,
|
||||
@@ -2438,11 +2438,29 @@ public sealed class BundleImporter : IBundleImporter
|
||||
var list = new NotificationList(overrideName ?? dto.Name) { Type = dto.Type };
|
||||
foreach (var r in dto.Recipients)
|
||||
{
|
||||
list.Recipients.Add(new NotificationRecipient(r.Name, r.EmailAddress));
|
||||
list.Recipients.Add(BuildRecipient(r));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
// S10: reconstruct a recipient carrying whichever contact(s) the bundle holds.
|
||||
// Email-only and SMS-only round-trip exactly; a recipient with both keeps both.
|
||||
// Build via the email ctor when an address is present (so the historical
|
||||
// non-null-email path is unchanged) and additively restore the phone number;
|
||||
// otherwise fall back to the SMS factory for phone-only recipients.
|
||||
private static NotificationRecipient BuildRecipient(NotificationRecipientDto r)
|
||||
{
|
||||
if (r.EmailAddress is not null)
|
||||
{
|
||||
return new NotificationRecipient(r.Name, r.EmailAddress)
|
||||
{
|
||||
PhoneNumber = r.PhoneNumber,
|
||||
};
|
||||
}
|
||||
|
||||
return NotificationRecipient.ForSms(r.Name, r.PhoneNumber ?? string.Empty);
|
||||
}
|
||||
|
||||
private async Task ApplySmtpConfigsAsync(
|
||||
IReadOnlyList<SmtpConfigDto> dtos,
|
||||
Dictionary<(string, string), ImportResolution> map,
|
||||
|
||||
@@ -34,6 +34,13 @@ public sealed record EntityAggregate(
|
||||
public IReadOnlyList<Site> Sites { get; init; } = Array.Empty<Site>();
|
||||
public IReadOnlyList<DataConnection> DataConnections { get; init; } = Array.Empty<DataConnection>();
|
||||
public IReadOnlyList<Instance> Instances { get; init; } = Array.Empty<Instance>();
|
||||
|
||||
// SMS (S10): carried alongside SmtpConfigurations. Init-only with an empty
|
||||
// default for the same source-compat reason as the M8 collections above —
|
||||
// every existing positional `new EntityAggregate(...)` caller keeps compiling
|
||||
// and never sees null; producers that resolve SMS config opt in via
|
||||
// object-initializer.
|
||||
public IReadOnlyList<SmsConfiguration> SmsConfigurations { get; init; } = Array.Empty<SmsConfiguration>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -77,6 +84,17 @@ public sealed record BundleContentDto(
|
||||
public IReadOnlyList<SiteDto> Sites { get; init; } = Array.Empty<SiteDto>();
|
||||
public IReadOnlyList<DataConnectionDto> DataConnections { get; init; } = Array.Empty<DataConnectionDto>();
|
||||
public IReadOnlyList<InstanceDto> Instances { get; init; } = Array.Empty<InstanceDto>();
|
||||
|
||||
// SMS (S10): central-only SMS provider configs, carried alongside SmtpConfigs.
|
||||
// Modeled as an init-only property with an empty default (NOT a positional ctor
|
||||
// param) for the same two reasons as the M8 site/instance arrays above:
|
||||
// 1. Forward-compat: deserializing a bundle written before SMS support leaves
|
||||
// this at Array.Empty<SmsConfigDto>() — consumers always see an empty list,
|
||||
// never null.
|
||||
// 2. Source-compat: every existing positional `new BundleContentDto(...)` caller
|
||||
// keeps compiling; producers that pack SMS configs opt in via the
|
||||
// object-initializer.
|
||||
public IReadOnlyList<SmsConfigDto> SmsConfigs { get; init; } = Array.Empty<SmsConfigDto>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -171,7 +189,16 @@ public sealed record NotificationListDto(
|
||||
|
||||
public sealed record NotificationRecipientDto(
|
||||
string Name,
|
||||
string EmailAddress);
|
||||
// S10: nullable so an SMS-only recipient (no email) round-trips faithfully
|
||||
// instead of being coerced to an empty string. Email-only recipients (the
|
||||
// historical case) still carry a non-null address; the entity enforces that at
|
||||
// least one contact is present.
|
||||
string? EmailAddress,
|
||||
// S10: SMS recipients carry an E.164 phone number instead of (or in addition
|
||||
// to) an email. Trailing + nullable so a bundle written before SMS support
|
||||
// deserializes this as null (backward-compatible); the serializer's
|
||||
// WhenWritingNull policy keeps it out of email-only bundles' JSON.
|
||||
string? PhoneNumber = null);
|
||||
|
||||
public sealed record SmtpConfigDto(
|
||||
string Host,
|
||||
@@ -185,6 +212,22 @@ public sealed record SmtpConfigDto(
|
||||
TimeSpan RetryDelay,
|
||||
SecretsBlock? Secrets);
|
||||
|
||||
/// <summary>
|
||||
/// An SMS provider configuration (the <c>SmsConfiguration</c> entity). Mirrors
|
||||
/// <see cref="SmtpConfigDto"/>: all non-sensitive fields are carried directly and
|
||||
/// the provider auth token rides inside <see cref="Secrets"/> so a future
|
||||
/// "share without secrets" export can drop it as a unit.
|
||||
/// </summary>
|
||||
public sealed record SmsConfigDto(
|
||||
string AccountSid,
|
||||
string FromNumber,
|
||||
string? MessagingServiceSid,
|
||||
string? ApiBaseUrl,
|
||||
int ConnectionTimeoutSeconds,
|
||||
int MaxRetries,
|
||||
TimeSpan RetryDelay,
|
||||
SecretsBlock? Secrets);
|
||||
|
||||
// Legacy DTO: only deserialized from pre-C4 bundles so the importer can count and
|
||||
// ignore the keys they contain. New exports never emit an ApiKeys array.
|
||||
public sealed record ApiKeyDto(
|
||||
|
||||
@@ -138,11 +138,17 @@ public sealed class EntitySerializer
|
||||
NotificationLists: aggregate.NotificationLists.Select(nl => new NotificationListDto(
|
||||
Name: nl.Name,
|
||||
Type: nl.Type,
|
||||
// S10: carry BOTH contacts. Keep any recipient that has at least
|
||||
// one contact (email OR phone) — SMS-only recipients have a null
|
||||
// EmailAddress and would have been dropped by the old email-only
|
||||
// filter. EmailAddress/PhoneNumber are nullable on the DTO so a
|
||||
// recipient round-trips exactly the contact(s) it holds.
|
||||
Recipients: nl.Recipients
|
||||
.Where(r => r.EmailAddress is not null)
|
||||
.Where(r => r.EmailAddress is not null || r.PhoneNumber is not null)
|
||||
.Select(r => new NotificationRecipientDto(
|
||||
Name: r.Name,
|
||||
EmailAddress: r.EmailAddress!)).ToList())).ToList(),
|
||||
EmailAddress: r.EmailAddress,
|
||||
PhoneNumber: r.PhoneNumber)).ToList())).ToList(),
|
||||
SmtpConfigs: aggregate.SmtpConfigurations.Select(smtp =>
|
||||
{
|
||||
SecretsBlock? secrets = null;
|
||||
@@ -212,6 +218,32 @@ public sealed class EntitySerializer
|
||||
FailoverRetryCount: c.FailoverRetryCount,
|
||||
Secrets: secretValues.Count > 0 ? new SecretsBlock(secretValues) : null);
|
||||
}).ToList(),
|
||||
// SMS (S10): mirror the SMTP secret handling exactly — the provider
|
||||
// auth token (never a public field) rides inside the SecretsBlock, so a
|
||||
// "share without secrets" export can drop it as a unit. Omit the secret
|
||||
// entirely when the token is null/empty so the importer's TryGetValue
|
||||
// cleanly yields "no value".
|
||||
SmsConfigs = aggregate.SmsConfigurations.Select(sms =>
|
||||
{
|
||||
SecretsBlock? secrets = null;
|
||||
if (!string.IsNullOrEmpty(sms.AuthToken))
|
||||
{
|
||||
secrets = new SecretsBlock(new Dictionary<string, string>(StringComparer.Ordinal)
|
||||
{
|
||||
["AuthToken"] = sms.AuthToken,
|
||||
});
|
||||
}
|
||||
|
||||
return new SmsConfigDto(
|
||||
AccountSid: sms.AccountSid,
|
||||
FromNumber: sms.FromNumber,
|
||||
MessagingServiceSid: sms.MessagingServiceSid,
|
||||
ApiBaseUrl: sms.ApiBaseUrl,
|
||||
ConnectionTimeoutSeconds: sms.ConnectionTimeoutSeconds,
|
||||
MaxRetries: sms.MaxRetries,
|
||||
RetryDelay: sms.RetryDelay,
|
||||
Secrets: secrets);
|
||||
}).ToList(),
|
||||
Instances = aggregate.Instances.Select(inst => new InstanceDto(
|
||||
UniqueName: inst.UniqueName,
|
||||
TemplateName: templateNameById.TryGetValue(inst.TemplateId, out var tn) ? tn : string.Empty,
|
||||
@@ -392,10 +424,16 @@ public sealed class EntitySerializer
|
||||
var list = new NotificationList(dto.Name) { Id = ix + 1, Type = dto.Type };
|
||||
foreach (var r in dto.Recipients)
|
||||
{
|
||||
list.Recipients.Add(new NotificationRecipient(r.Name, r.EmailAddress)
|
||||
{
|
||||
NotificationListId = list.Id,
|
||||
});
|
||||
// S10: reconstruct whichever contact(s) the bundle carries.
|
||||
// Email-present recipients use the email ctor (unchanged
|
||||
// historical path) and additively restore the phone number;
|
||||
// phone-only recipients (null email) take the SMS factory so we
|
||||
// never pass null to the non-null email ctor.
|
||||
var recipient = r.EmailAddress is not null
|
||||
? new NotificationRecipient(r.Name, r.EmailAddress) { PhoneNumber = r.PhoneNumber }
|
||||
: NotificationRecipient.ForSms(r.Name, r.PhoneNumber ?? string.Empty);
|
||||
recipient.NotificationListId = list.Id;
|
||||
list.Recipients.Add(recipient);
|
||||
}
|
||||
return list;
|
||||
})
|
||||
@@ -415,6 +453,22 @@ public sealed class EntitySerializer
|
||||
})
|
||||
.ToList();
|
||||
|
||||
// SMS (S10): mirror the SMTP path — restore AccountSid/FromNumber and decrypt
|
||||
// the auth token back out of the SecretsBlock (null when the key was omitted
|
||||
// because the source token was null/empty).
|
||||
var smsConfigurations = content.SmsConfigs
|
||||
.Select((dto, ix) => new SmsConfiguration(dto.AccountSid, dto.FromNumber)
|
||||
{
|
||||
Id = ix + 1,
|
||||
AuthToken = dto.Secrets?.Values.TryGetValue("AuthToken", out var token) == true ? token : null,
|
||||
MessagingServiceSid = dto.MessagingServiceSid,
|
||||
ApiBaseUrl = dto.ApiBaseUrl,
|
||||
ConnectionTimeoutSeconds = dto.ConnectionTimeoutSeconds,
|
||||
MaxRetries = dto.MaxRetries,
|
||||
RetryDelay = dto.RetryDelay,
|
||||
})
|
||||
.ToList();
|
||||
|
||||
// Inbound API keys are not transported (re-arch C4) — content.ApiKeys is a
|
||||
// legacy field only present on pre-C4 bundles; it is ignored here. The
|
||||
// BundleImporter is responsible for counting and reporting any such keys.
|
||||
@@ -538,6 +592,7 @@ public sealed class EntitySerializer
|
||||
Sites = sites,
|
||||
DataConnections = dataConnections,
|
||||
Instances = instances,
|
||||
SmsConfigurations = smsConfigurations,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user