feat(notification): add admin rest management and deletion
Add revision-safe service and template APIs, channel mapping, and validated admin controls. Ref: IT-1601
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
using HrynCo.NotificationService.Web.Infrastructure;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
|
||||
namespace HrynCo.NotificationService.Web.Controllers.Api.Services;
|
||||
|
||||
// Runs before ApiController's default model-state filter, only for these endpoints.
|
||||
public sealed class ApiRequestValidationFilter : IActionFilter
|
||||
{
|
||||
public void OnActionExecuting(ActionExecutingContext context)
|
||||
{
|
||||
if (context.ModelState.IsValid)
|
||||
return;
|
||||
var errors = context.ModelState.SelectMany(field => field.Value!.Errors.Select(error =>
|
||||
string.IsNullOrWhiteSpace(error.ErrorMessage) ? "Invalid JSON value at " + field.Key + "." : error.ErrorMessage));
|
||||
context.Result = new BadRequestObjectResult(new ApiResponse<object>
|
||||
{
|
||||
Success = false, Error = new ApiError { Code = "InvalidRequest", Message = string.Join(" ", errors) }
|
||||
});
|
||||
}
|
||||
|
||||
public void OnActionExecuted(ActionExecutedContext context) { }
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
namespace HrynCo.NotificationService.Web.Controllers.Api.Services;
|
||||
|
||||
public sealed class CreateNotificationServiceRequest
|
||||
{
|
||||
public required string Name { get; init; }
|
||||
public required IReadOnlyList<ServiceLanguageRequest> Languages { get; init; }
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace HrynCo.NotificationService.Web.Controllers.Api.Services;
|
||||
|
||||
public sealed class DeleteServiceLanguageVersionsRequest
|
||||
{
|
||||
[JsonRequired]
|
||||
public Guid Revision { get; init; }
|
||||
[JsonRequired]
|
||||
public int ExpectedVersionCount { get; init; }
|
||||
[JsonRequired]
|
||||
public bool ConfirmDelete { get; init; }
|
||||
}
|
||||
+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}/email-template-definitions")]
|
||||
[TypeFilter(typeof(ApiRequestValidationFilter), Order = -3000)]
|
||||
public sealed class EmailTemplateDefinitionsController(IMediator mediator) : ApiControllerBase(mediator)
|
||||
{
|
||||
[HttpGet]
|
||||
[ProducesResponseType<ApiResponse<EmailTemplateDefinitionDto>>(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 GetEmailTemplateDefinitionQuery(serviceId, key), cancellationToken));
|
||||
|
||||
[HttpPost("upsert")]
|
||||
[ProducesResponseType<ApiResponse<EmailTemplateDefinitionDto>>(StatusCodes.Status201Created)]
|
||||
[ProducesResponseType<ApiResponse<EmailTemplateDefinitionDto>>(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] UpsertTemplateDefinitionRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var patch = request.ToPatch(serviceId, key);
|
||||
var result = await Mediator.Send(new UpsertEmailTemplateDefinitionCommand(patch), cancellationToken);
|
||||
if (!result.IsSuccess)
|
||||
return MapServiceError(result.Error!);
|
||||
if (patch.Revision is null)
|
||||
return CreatedAtAction(nameof(Get), new { serviceId, key },
|
||||
new ApiResponse<EmailTemplateDefinitionDto> { Success = true, Data = result.Result });
|
||||
return FromServiceResult(result);
|
||||
}
|
||||
catch (EmailTemplateWriteException error)
|
||||
{
|
||||
return MapServiceError(new ServiceError(error.Message, ServiceErrorCode.InvalidRequest));
|
||||
}
|
||||
}
|
||||
[HttpGet("all")]
|
||||
[ProducesResponseType<ApiResponse<IReadOnlyList<EmailTemplateDefinitionDto>>>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType<ApiResponse<object>>(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> GetAll([FromRoute] Guid serviceId, CancellationToken cancellationToken) =>
|
||||
FromServiceResult(await Mediator.Send(new GetServiceTemplateDefinitionsQuery(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 DeleteEmailTemplateDefinitionCommand(serviceId, key, revision), cancellationToken));
|
||||
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
|
||||
namespace HrynCo.NotificationService.Web.Controllers.Api.Services;
|
||||
|
||||
internal static class NotificationServiceRequestMapping
|
||||
{
|
||||
public static NotificationServiceInfo Map(Guid? id, string name, IReadOnlyList<ServiceLanguageRequest>? languages, Guid revision)
|
||||
{
|
||||
if (languages is null || languages.Any(l => l is null))
|
||||
throw new EmailTemplateWriteException("languages must be a JSON array without null elements.", false);
|
||||
if (id is null && languages.Any(l => l.Id != Guid.Empty))
|
||||
throw new EmailTemplateWriteException("New services cannot reference persisted language IDs.", false);
|
||||
return new NotificationServiceInfo
|
||||
{
|
||||
Id = id ?? Guid.Empty, Name = name, Revision = revision,
|
||||
Languages = languages.Select(l => new ServiceLanguageInfo { Id = l.Id, Code = l.Code }).ToList()
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace HrynCo.NotificationService.Web.Controllers.Api.Services;
|
||||
|
||||
public sealed class ServiceLanguageRequest
|
||||
{
|
||||
public Guid Id { get; init; }
|
||||
public required string Code { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using Unit = HrynCo.NotificationService.Services.Core.Unit;
|
||||
using HrynCo.NotificationService.Web.Infrastructure;
|
||||
using HrynCo.NotificationService.Services.TemplateDefinitions;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace HrynCo.NotificationService.Web.Controllers.Api.Services;
|
||||
|
||||
[Route("api/v1/services")]
|
||||
[TypeFilter(typeof(ApiRequestValidationFilter), Order = -3000)]
|
||||
public sealed class ServicesController(IMediator mediator) : ApiControllerBase(mediator)
|
||||
{
|
||||
[HttpGet("{serviceId:guid}")]
|
||||
[ProducesResponseType<ApiResponse<NotificationServiceDto>>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType<ApiResponse<object>>(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType<ApiResponse<object>>(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> Get([FromRoute] Guid serviceId, CancellationToken cancellationToken) =>
|
||||
FromServiceResult(await Mediator.Send(new GetNotificationServiceQuery(serviceId, null), cancellationToken));
|
||||
|
||||
[HttpGet]
|
||||
[ProducesResponseType<ApiResponse<NotificationServiceDto>>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType<ApiResponse<object>>(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType<ApiResponse<object>>(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> GetByName([FromQuery] string? name, CancellationToken cancellationToken) =>
|
||||
FromServiceResult(await Mediator.Send(new GetNotificationServiceQuery(null, name), cancellationToken));
|
||||
[HttpGet("all")]
|
||||
[ProducesResponseType<ApiResponse<IReadOnlyList<NotificationServiceDto>>>(StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> GetAll(CancellationToken cancellationToken) =>
|
||||
FromServiceResult(await Mediator.Send(new GetAllNotificationServicesQuery(), cancellationToken));
|
||||
|
||||
[HttpDelete("{serviceId:guid}")]
|
||||
[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] Guid revision, CancellationToken cancellationToken) =>
|
||||
FromServiceResult(await Mediator.Send(new DeleteNotificationServiceCommand(serviceId, revision), cancellationToken));
|
||||
|
||||
[HttpPost]
|
||||
[ProducesResponseType<ApiResponse<NotificationServiceDto>>(StatusCodes.Status201Created)]
|
||||
[ProducesResponseType<ApiResponse<object>>(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType<ApiResponse<object>>(StatusCodes.Status409Conflict)]
|
||||
public Task<IActionResult> Create([FromBody] CreateNotificationServiceRequest request, CancellationToken cancellationToken) =>
|
||||
SaveServiceAsync(null, request.Name, request.Languages, Guid.Empty, cancellationToken);
|
||||
|
||||
[HttpPut("{serviceId:guid}")]
|
||||
[ProducesResponseType<ApiResponse<NotificationServiceDto>>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType<ApiResponse<object>>(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType<ApiResponse<object>>(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType<ApiResponse<object>>(StatusCodes.Status409Conflict)]
|
||||
public Task<IActionResult> Update([FromRoute] Guid serviceId, [FromBody] UpdateNotificationServiceRequest request, CancellationToken cancellationToken) =>
|
||||
SaveServiceAsync(serviceId, request.Name, request.Languages, request.Revision, cancellationToken);
|
||||
|
||||
[HttpDelete("{serviceId:guid}/languages/{languageId:guid}")]
|
||||
[ProducesResponseType<ApiResponse<Unit>>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType<ApiResponse<object>>(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType<ApiResponse<object>>(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType<ApiResponse<object>>(StatusCodes.Status409Conflict)]
|
||||
public async Task<IActionResult> DeleteUnusedLanguage([FromRoute] Guid serviceId, [FromRoute] Guid languageId,
|
||||
[FromQuery] Guid revision, CancellationToken cancellationToken) =>
|
||||
FromServiceResult(await Mediator.Send(new DeleteUnusedServiceLanguageCommand(serviceId, languageId, revision), cancellationToken));
|
||||
|
||||
[HttpPost("{serviceId:guid}/languages/{languageId:guid}/delete-with-versions")]
|
||||
[ProducesResponseType<ApiResponse<Unit>>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType<ApiResponse<object>>(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType<ApiResponse<object>>(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType<ApiResponse<object>>(StatusCodes.Status409Conflict)]
|
||||
public async Task<IActionResult> DeleteLanguageWithVersions([FromRoute] Guid serviceId, [FromRoute] Guid languageId,
|
||||
[FromBody] DeleteServiceLanguageVersionsRequest request, CancellationToken cancellationToken) =>
|
||||
FromServiceResult(await Mediator.Send(new DeleteServiceLanguageVersionsCommand(serviceId, languageId, request.Revision,
|
||||
request.ExpectedVersionCount, request.ConfirmDelete), cancellationToken));
|
||||
|
||||
private async Task<IActionResult> SaveServiceAsync(Guid? id, string name, IReadOnlyList<ServiceLanguageRequest> languages, Guid revision, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var model = NotificationServiceRequestMapping.Map(id, name, languages, revision);
|
||||
var result = await Mediator.Send(new SaveNotificationServiceCommand(model, id is null), cancellationToken);
|
||||
if (!result.IsSuccess)
|
||||
return MapServiceError(result.Error!);
|
||||
if (id is null)
|
||||
return CreatedAtAction(nameof(Get), new { serviceId = result.Result!.Id },
|
||||
new ApiResponse<NotificationServiceDto> { Success = true, Data = result.Result });
|
||||
return FromServiceResult(result);
|
||||
}
|
||||
catch (EmailTemplateWriteException error)
|
||||
{
|
||||
return MapServiceError(new ServiceError(error.Message, ServiceErrorCode.InvalidRequest));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace HrynCo.NotificationService.Web.Controllers.Api.Services;
|
||||
|
||||
public sealed class TemplateTranslationRequest
|
||||
{
|
||||
public required string LanguageCode { get; init; }
|
||||
public required string Subject { get; init; }
|
||||
public required string HtmlBody { get; init; }
|
||||
public required string TextBody { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace HrynCo.NotificationService.Web.Controllers.Api.Services;
|
||||
|
||||
public sealed class TemplateVariableRequest
|
||||
{
|
||||
public required string Name { get; init; }
|
||||
[JsonRequired]
|
||||
public bool Required { get; init; }
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace HrynCo.NotificationService.Web.Controllers.Api.Services;
|
||||
|
||||
public sealed class UpdateNotificationServiceRequest
|
||||
{
|
||||
[JsonRequired]
|
||||
public Guid Revision { get; init; }
|
||||
public required string Name { get; init; }
|
||||
public required IReadOnlyList<ServiceLanguageRequest> Languages { get; init; }
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
|
||||
namespace HrynCo.NotificationService.Web.Controllers.Api.Services;
|
||||
|
||||
public sealed class UpsertTemplateDefinitionRequest
|
||||
{
|
||||
public Guid? Revision { get; init; }
|
||||
public required IReadOnlyList<TemplateVariableRequest> Variables { get; init; }
|
||||
public required IReadOnlyList<TemplateTranslationRequest> Translations { get; init; }
|
||||
public IReadOnlyList<string>? RemoveLanguageCodes { get; init; }
|
||||
|
||||
public EmailTemplateDefinitionPatch 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 EmailTemplateDefinitionPatch
|
||||
{
|
||||
ServiceId = serviceId, Key = key, Revision = Revision,
|
||||
Variables = Variables.Select(v => new EmailTemplateVariable { Name = v.Name, Required = v.Required }).ToList(),
|
||||
Translations = Translations.Select(t => new EmailTemplate
|
||||
{
|
||||
ServiceName = "", Key = key, LanguageCode = t.LanguageCode, Created = DateTimeOffset.UtcNow,
|
||||
Subject = t.Subject, HtmlBody = t.HtmlBody, TextBody = t.TextBody
|
||||
}).ToList(),
|
||||
RemoveLanguageCodes = RemoveLanguageCodes ?? []
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user