From b227eb405109e89097420bed1a9f8c5510f55bf9 Mon Sep 17 00:00:00 2001 From: TekH Date: Mon, 9 Feb 2026 11:20:34 +0100 Subject: [PATCH] Refactor UpdateEmailTemplateCommand and handler - Introduce EmailTemplateUpdateDto for update payloads. - Expose Id and Type directly on UpdateEmailTemplateCommand; remove EmailTemplateQuery property. - Add QueryExpression for flexible template selection by Id or Type. - Remove AutoMapper and EmailTemplateDto usage from handler; update repository call to use EmailTemplateUpdateDto. - Update MappingProfile to map EmailTemplateUpdateDto to EmailTemplate and set ChangedWhen to DateTime.UtcNow. - Remove obsolete code and improve documentation. --- .../Commands/UpdateEmailTemplateCommand.cs | 94 +++++++++---------- .../EmailTemplates/MappingProfile.cs | 5 +- 2 files changed, 44 insertions(+), 55 deletions(-) diff --git a/EnvelopeGenerator.Application/EmailTemplates/Commands/UpdateEmailTemplateCommand.cs b/EnvelopeGenerator.Application/EmailTemplates/Commands/UpdateEmailTemplateCommand.cs index bddc7a52..25ed293e 100644 --- a/EnvelopeGenerator.Application/EmailTemplates/Commands/UpdateEmailTemplateCommand.cs +++ b/EnvelopeGenerator.Application/EmailTemplates/Commands/UpdateEmailTemplateCommand.cs @@ -1,37 +1,56 @@ using AutoMapper; using DigitalData.Core.Abstraction.Application.Repository; using DigitalData.Core.Exceptions; -using MediatR; -using System.Text.Json.Serialization; -using EnvelopeGenerator.Domain.Entities; -using Microsoft.EntityFrameworkCore; using EnvelopeGenerator.Domain.Constants; -using EnvelopeGenerator.Application.Common.Dto; +using EnvelopeGenerator.Domain.Entities; +using MediatR; +using System.Linq.Expressions; -namespace EnvelopeGenerator.Application.EmailTemplates.Commands.Update; +namespace EnvelopeGenerator.Application.EmailTemplates.Commands; + +/// +/// +/// +/// +/// +public record EmailTemplateUpdateDto(string Body, string Subject); /// /// Befehl zum Aktualisieren einer E-Mail-Vorlage. /// -/// -/// (Optional)Der neue Inhalt des E-Mail-Textkörpers. Wenn null, bleibt der vorhandene Inhalt unverändert. -/// -/// -/// (Optional) Der neue Betreff der E-Mail. Wenn null, bleibt der vorhandene Betreff unverändert. -/// -public record UpdateEmailTemplateCommand(string? Body = null, string? Subject = null) : IRequest +public record UpdateEmailTemplateCommand : IEmailTemplateQuery, IRequest { - /// - /// Die Abfrage, die die E-Mail-Vorlage darstellt, die aktualisiert werden soll. - /// - [JsonIgnore] - public IEmailTemplateQuery? EmailTemplateQuery { get; set; } + /// + /// Die eindeutige Kennung der E-Mail-Vorlage (optional). + /// + public int? Id { get; set; } + + /// + /// Der Typ der E-Mail-Vorlage, z. B. (optional). Beispiele:
+ /// 0 - DocumentReceived: Benachrichtigung über den Empfang eines Dokuments.
+ /// 1 - DocumentSigned: Benachrichtigung über die Unterzeichnung eines Dokuments.
+ /// 2 - DocumentDeleted: Benachrichtigung über das Löschen eines Dokuments.
+ /// 3 - DocumentCompleted: Benachrichtigung über den Abschluss eines Dokuments.
+ /// 4 - DocumentAccessCodeReceived: Benachrichtigung über den Erhalt eines Zugangscodes.
+ /// 5 - DocumentShared: Benachrichtigung über das Teilen eines Dokuments.
+ /// 6 - TotpSecret: Benachrichtigung über ein TOTP-Geheimnis.
+ /// 7 - DocumentRejected_ADM (für den Absender): Mail an den Absender, wenn das Dokument abgelehnt wird.
+ /// 8 - DocumentRejected_REC (für den ablehnenden Empfänger): Mail an den ablehnenden Empfänger, wenn das Dokument abgelehnt wird.
+ /// 9 - DocumentRejected_REC_2 (für sonstige Empfänger): Mail an andere Empfänger (Brief), wenn das Dokument abgelehnt wird. + ///
+ public EmailTemplateType? Type { get; set; } /// /// /// - [JsonIgnore] - public DateTime ChangedWhen { get; init; } = DateTime.Now; + public Expression> QueryExpression => Id is int id + ? temp => temp.Id == id + : temp => temp!.Name == Type.ToString(); + + /// + /// Die aktualisierte E-Mail-Vorlage. + /// + public EmailTemplateUpdateDto Update { get; init; } = null!; } /// @@ -41,17 +60,13 @@ public class UpdateEmailTemplateCommandHandler : IRequestHandler _repository; - private readonly IMapper _mapper; - /// /// /// /// - /// - public UpdateEmailTemplateCommandHandler(IRepository repository, IMapper mapper) + public UpdateEmailTemplateCommandHandler(IRepository repository) { _repository = repository; - _mapper = mapper; } /// @@ -62,31 +77,6 @@ public class UpdateEmailTemplateCommandHandler : IRequestHandler /// /// - [Obsolete("Use Read-method returning IReadQuery instead.")] - public async Task Handle(UpdateEmailTemplateCommand request, CancellationToken cancel) - { - EmailTemplateDto? tempDto; - - if (request.EmailTemplateQuery?.Id is int id) - { - var temp = await _repository.ReadOnly().Where(t => t.Id == id).FirstOrDefaultAsync(cancel); - tempDto = _mapper.Map(temp); - } - else if (request!.EmailTemplateQuery!.Type is EmailTemplateType type) - { - var temp = await _repository.ReadOnly().Where(t => t.Name == type.ToString()).FirstOrDefaultAsync(cancel); - tempDto = _mapper.Map(temp); - } - else - { - throw new InvalidOperationException("Both id and type is null. Id: " + request.EmailTemplateQuery.Id + ". Type: " + request.EmailTemplateQuery.Type.ToString()); - } - - if (tempDto == null) - { - throw new NotFoundException(); - } - - await _repository.UpdateAsync(tempDto, t => t.Id == tempDto.Id, cancel); - } + public Task Handle(UpdateEmailTemplateCommand request, CancellationToken cancel) + => _repository.UpdateAsync(request.Update, request.QueryExpression, cancel); } \ No newline at end of file diff --git a/EnvelopeGenerator.Application/EmailTemplates/MappingProfile.cs b/EnvelopeGenerator.Application/EmailTemplates/MappingProfile.cs index e6cba17d..bf19ddb0 100644 --- a/EnvelopeGenerator.Application/EmailTemplates/MappingProfile.cs +++ b/EnvelopeGenerator.Application/EmailTemplates/MappingProfile.cs @@ -17,8 +17,7 @@ public class MappingProfile : Profile { CreateMap(); - CreateMap() - .ForMember(dest => dest.Id, opt => opt.Ignore()) - .ForMember(dest => dest.ChangedWhen, opt => opt.MapFrom(_ => DateTime.Now)); + CreateMap() + .ForMember(dest => dest.ChangedWhen, opt => opt.MapFrom(_ => DateTime.UtcNow)); } } \ No newline at end of file