feat(api): manage localized push template definitions
Add revision-protected reads, atomic upsert and deletion with structured actions. Preserve omitted translations and fix insertion of new push languages. Ref: IT-1627
This commit is contained in:
+32
@@ -0,0 +1,32 @@
|
||||
using System.Text.Json;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
|
||||
namespace HrynCo.NotificationService.Web.Controllers.Api.Services;
|
||||
|
||||
public sealed class UpsertPushTemplateDefinitionRequest
|
||||
{
|
||||
private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web);
|
||||
public Guid? Revision { get; init; }
|
||||
public required IReadOnlyList<TemplateVariableRequest> Variables { get; init; }
|
||||
public required IReadOnlyList<PushTemplateTranslationRequest> Translations { get; init; }
|
||||
public IReadOnlyList<string>? RemoveLanguageCodes { get; init; }
|
||||
|
||||
public PushTemplateDefinitionPatch ToPatch(Guid serviceId, string key)
|
||||
{
|
||||
if (Variables is null || Translations is null)
|
||||
throw new EmailTemplateWriteException("variables and translations must be JSON arrays, including when empty.", false);
|
||||
if (Variables.Any(v => v is null) || Translations.Any(t => t is null))
|
||||
throw new EmailTemplateWriteException("variables and translations cannot contain null elements.", false);
|
||||
return new PushTemplateDefinitionPatch
|
||||
{
|
||||
ServiceId = serviceId, Key = key, Revision = Revision,
|
||||
Variables = Variables.Select(v => new EmailTemplateVariable { Name = v.Name, Required = v.Required }).ToList(),
|
||||
Translations = Translations.Select(t => new PushTemplateTranslation
|
||||
{
|
||||
LanguageCode = t.LanguageCode, Title = t.Title, Body = t.Body,
|
||||
ActionsJson = JsonSerializer.Serialize(t.Actions, JsonOptions)
|
||||
}).ToList(),
|
||||
RemoveLanguageCodes = RemoveLanguageCodes ?? []
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user