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:
+61
@@ -0,0 +1,61 @@
|
||||
using Unit = HrynCo.NotificationService.Services.Core.Unit;
|
||||
using HrynCo.NotificationService.Web.Infrastructure;
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using HrynCo.NotificationService.Services.TemplateDefinitions;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace HrynCo.NotificationService.Web.Controllers.Api.Services;
|
||||
|
||||
[Route("api/v1/services/{serviceId:guid}/push-template-definitions")]
|
||||
[TypeFilter(typeof(ApiRequestValidationFilter), Order = -3000)]
|
||||
public sealed class PushTemplateDefinitionsController(IMediator mediator) : ApiControllerBase(mediator)
|
||||
{
|
||||
[HttpGet]
|
||||
[ProducesResponseType<ApiResponse<PushTemplateDefinitionDto>>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType<ApiResponse<object>>(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType<ApiResponse<object>>(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> Get([FromRoute] Guid serviceId, [FromQuery] string key, CancellationToken cancellationToken) =>
|
||||
FromServiceResult(await Mediator.Send(new GetPushTemplateDefinitionQuery(serviceId, key), cancellationToken));
|
||||
|
||||
[HttpPost("upsert")]
|
||||
[ProducesResponseType<ApiResponse<PushTemplateDefinitionDto>>(StatusCodes.Status201Created)]
|
||||
[ProducesResponseType<ApiResponse<PushTemplateDefinitionDto>>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType<ApiResponse<object>>(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType<ApiResponse<object>>(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType<ApiResponse<object>>(StatusCodes.Status409Conflict)]
|
||||
public async Task<IActionResult> Upsert([FromRoute] Guid serviceId, [FromQuery] string key,
|
||||
[FromBody] UpsertPushTemplateDefinitionRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var patch = request.ToPatch(serviceId, key);
|
||||
var result = await Mediator.Send(new UpsertPushTemplateDefinitionCommand(patch), cancellationToken);
|
||||
if (!result.IsSuccess)
|
||||
return MapServiceError(result.Error!);
|
||||
if (patch.Revision is null)
|
||||
return CreatedAtAction(nameof(Get), new { serviceId, key },
|
||||
new ApiResponse<PushTemplateDefinitionDto> { Success = true, Data = result.Result });
|
||||
return FromServiceResult(result);
|
||||
}
|
||||
catch (EmailTemplateWriteException error)
|
||||
{
|
||||
return MapServiceError(new ServiceError(error.Message, ServiceErrorCode.InvalidRequest));
|
||||
}
|
||||
}
|
||||
[HttpGet("all")]
|
||||
[ProducesResponseType<ApiResponse<IReadOnlyList<PushTemplateDefinitionDto>>>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType<ApiResponse<object>>(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> GetAll([FromRoute] Guid serviceId, CancellationToken cancellationToken) =>
|
||||
FromServiceResult(await Mediator.Send(new GetServicePushTemplateDefinitionsQuery(serviceId), cancellationToken));
|
||||
|
||||
[HttpDelete]
|
||||
[ProducesResponseType<ApiResponse<Unit>>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType<ApiResponse<object>>(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType<ApiResponse<object>>(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType<ApiResponse<object>>(StatusCodes.Status409Conflict)]
|
||||
public async Task<IActionResult> Delete([FromRoute] Guid serviceId, [FromQuery] string key, [FromQuery] Guid revision, CancellationToken cancellationToken) =>
|
||||
FromServiceResult(await Mediator.Send(new DeletePushTemplateDefinitionCommand(serviceId, key, revision), cancellationToken));
|
||||
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
|
||||
namespace HrynCo.NotificationService.Web.Controllers.Api.Services;
|
||||
|
||||
public sealed class PushTemplateTranslationRequest
|
||||
{
|
||||
public required string LanguageCode { get; init; }
|
||||
public required string Title { get; init; }
|
||||
public required string Body { get; init; }
|
||||
public PushTemplateActions Actions { get; init; } = new();
|
||||
}
|
||||
+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