Files
hrynco-notification-service/HrynCo.NotificationService.Services/TemplateDefinitions/DeleteUnusedServiceLanguageHandler.cs
agrynco 8e81572386 feat(notification): add admin rest management and deletion
Add revision-safe service and template APIs, channel mapping, and validated admin controls.

Ref: IT-1601
2026-10-17 22:51:01 +03:00

35 lines
1.9 KiB
C#

using HrynCo.NotificationService.DAL.Abstract.Repositories;
using HrynCo.NotificationService.DAL.Abstract.Templates;
using HrynCo.NotificationService.Services.Core;
using HrynCo.NotificationService.Services.Logging;
using static HrynCo.NotificationService.Services.Core.ServiceResultHelper;
namespace HrynCo.NotificationService.Services.TemplateDefinitions;
internal sealed class DeleteUnusedServiceLanguageHandler(
IContextualSerilogLogger<DeleteUnusedServiceLanguageCommand> logger, INotificationServiceRepository services)
: RequestHandler<DeleteUnusedServiceLanguageCommand, ServiceResult<Unit>>(logger)
{
protected override async Task<ServiceResult<Unit>> DoOnHandle(DeleteUnusedServiceLanguageCommand request, CancellationToken cancellationToken)
{
if (request.Revision == Guid.Empty)
return Failure<Unit>("A nonempty service revision is required.", ServiceErrorCode.InvalidRequest);
var service = await services.GetAsync(request.ServiceId, cancellationToken);
var language = service?.Languages.SingleOrDefault(l => l.Id == request.LanguageId);
if (language is null)
return Failure<Unit>("Service language not found.", ServiceErrorCode.NotFound);
if (language.VersionCount > 0)
return Failure<Unit>("This language has template versions. Use delete-with-versions with explicit confirmation and the current count.", ServiceErrorCode.Conflict);
try
{
// The repository repeats the zero-count and revision checks under its mutation lock.
await services.RemoveLanguageAsync(request.ServiceId, request.LanguageId, 0, request.Revision, cancellationToken);
return Success(Unit.Value);
}
catch (EmailTemplateWriteException error)
{
return Failure<Unit>(error.Message, error.IsConflict ? ServiceErrorCode.Conflict : ServiceErrorCode.InvalidRequest);
}
}
}