From 8b4ad5e28d171f68195ac671389905a63c5f9fc3 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Thu, 8 May 2025 17:47:18 +0200 Subject: [PATCH] Refactor email template commands and controller logic - Updated namespace for `ResetEmailTemplateCommand` and added a constructor for flexible initialization. - Introduced `ChangedWhen` property in `UpdateEmailTemplateCommand` to track modification time. - Refactored `UpdateEmailTemplateCommandHandler` for improved email template retrieval logic. - Modified `EmailTemplateController` method signatures and logic for clarity and consistency. - Added `EmailTemplate` entry in `appsettings.json` for database trigger configuration. --- .../Reset/ResetEmailTemplateCommand.cs | 24 ++++++++++++++--- .../Update/UpdateEmailTemplateCommand.cs | 6 +++++ .../UpdateEmailTemplateCommandHandler.cs | 26 ++++++++++++++----- .../Controllers/EmailTemplateController.cs | 17 +++++++----- .../appsettings.json | 3 ++- EnvelopeGenerator.Web/appsettings.json | 3 ++- 6 files changed, 61 insertions(+), 18 deletions(-) diff --git a/EnvelopeGenerator.Application/EmailTemplates/Commands/Reset/ResetEmailTemplateCommand.cs b/EnvelopeGenerator.Application/EmailTemplates/Commands/Reset/ResetEmailTemplateCommand.cs index 6e3bce7a..1c8fb830 100644 --- a/EnvelopeGenerator.Application/EmailTemplates/Commands/Reset/ResetEmailTemplateCommand.cs +++ b/EnvelopeGenerator.Application/EmailTemplates/Commands/Reset/ResetEmailTemplateCommand.cs @@ -7,8 +7,7 @@ namespace EnvelopeGenerator.Application.EmailTemplates.Commands.Reset; /// Ein Befehl zum Zurücksetzen einer E-Mail-Vorlage auf die Standardwerte. /// Erbt von und ermöglicht die Angabe einer optionalen ID und eines Typs der E-Mail-Vorlage. /// -/// Die optionale ID der E-Mail-Vorlage, die zurückgesetzt werden soll. -/// Der Typ der E-Mail-Vorlage, z. B. (optional). Beispiele: +/// 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. @@ -20,4 +19,23 @@ namespace EnvelopeGenerator.Application.EmailTemplates.Commands.Reset; /// 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 record ResetEmailTemplateCommand(int? Id = null, Constants.EmailTemplateType? Type = null) : EmailTemplateQuery(Id, Type), IRequest; +public record ResetEmailTemplateCommand : EmailTemplateQuery, IRequest +{ + /// + /// + /// + /// + public ResetEmailTemplateCommand(EmailTemplateQuery? orginal = null) : base(orginal ?? new()) + { + + } + + /// + /// + /// + /// Die optionale ID der E-Mail-Vorlage, die zurückgesetzt werden soll. + /// Der Typ der E-Mail-Vorlage, z. B. (optional). + public ResetEmailTemplateCommand(int? Id = null, Constants.EmailTemplateType? Type = null) : base(Id, Type) + { + } +}; diff --git a/EnvelopeGenerator.Application/EmailTemplates/Commands/Update/UpdateEmailTemplateCommand.cs b/EnvelopeGenerator.Application/EmailTemplates/Commands/Update/UpdateEmailTemplateCommand.cs index b6350bd1..1f3d7525 100644 --- a/EnvelopeGenerator.Application/EmailTemplates/Commands/Update/UpdateEmailTemplateCommand.cs +++ b/EnvelopeGenerator.Application/EmailTemplates/Commands/Update/UpdateEmailTemplateCommand.cs @@ -20,4 +20,10 @@ public record UpdateEmailTemplateCommand(string? Body = null, string? Subject = /// [JsonIgnore] public EmailTemplateQuery? EmailTemplateQuery { get; set; } + + /// + /// + /// + [JsonIgnore] + public DateTime ChangedWhen { get; init; } = DateTime.Now; } diff --git a/EnvelopeGenerator.Application/EmailTemplates/Commands/Update/UpdateEmailTemplateCommandHandler.cs b/EnvelopeGenerator.Application/EmailTemplates/Commands/Update/UpdateEmailTemplateCommandHandler.cs index 09803a6c..69ef1d26 100644 --- a/EnvelopeGenerator.Application/EmailTemplates/Commands/Update/UpdateEmailTemplateCommandHandler.cs +++ b/EnvelopeGenerator.Application/EmailTemplates/Commands/Update/UpdateEmailTemplateCommandHandler.cs @@ -34,13 +34,27 @@ public class UpdateEmailTemplateCommandHandler : IRequestHandler public async Task Handle(UpdateEmailTemplateCommand request, CancellationToken cancel) { - var temp = (request.EmailTemplateQuery?.Id is int id - ? await _repository.ReadOrDefaultAsync(t => t.Id == id, single: false, cancel) - : request!.EmailTemplateQuery!.Type is Common.Constants.EmailTemplateType type - ? await _repository.ReadOrDefaultAsync(t => t.Name == type.ToString(), single: false, cancel) - : throw new InvalidOperationException("Both id and type is null. Id: " + request.EmailTemplateQuery.Id + ". Type: " + request.EmailTemplateQuery.Type.ToString())) ?? throw new NotFoundException(); + EmailTemplateDto? temp; - if(request.Body is not null) + if (request.EmailTemplateQuery?.Id is int id) + { + temp = await _repository.ReadOrDefaultAsync(t => t.Id == id, single: false, cancel); + } + else if (request!.EmailTemplateQuery!.Type is Common.Constants.EmailTemplateType type) + { + temp = await _repository.ReadOrDefaultAsync(t => t.Name == type.ToString(), single: false, cancel); + } + else + { + throw new InvalidOperationException("Both id and type is null. Id: " + request.EmailTemplateQuery.Id +". Type: " + request.EmailTemplateQuery.Type.ToString()); + } + + if (temp == null) + { + throw new NotFoundException(); + } + + if (request.Body is not null) temp.Body = request.Body; if (request.Subject is not null) diff --git a/EnvelopeGenerator.GeneratorAPI/Controllers/EmailTemplateController.cs b/EnvelopeGenerator.GeneratorAPI/Controllers/EmailTemplateController.cs index e65cfd51..7e1efe52 100644 --- a/EnvelopeGenerator.GeneratorAPI/Controllers/EmailTemplateController.cs +++ b/EnvelopeGenerator.GeneratorAPI/Controllers/EmailTemplateController.cs @@ -15,7 +15,7 @@ using EnvelopeGenerator.Application.Exceptions; namespace EnvelopeGenerator.GeneratorAPI.Controllers; /// -/// Controller for managing email templates. +/// Controller for managing temp templates. /// Steuerung zur Verwaltung von E-Mail-Vorlagen. /// [Route("api/[controller]")] @@ -84,10 +84,10 @@ public class EmailTemplateController : ControllerBase } /// - /// Updates an email template or resets it if no update command is provided. + /// Updates an temp template or resets it if no update command is provided. /// Aktualisiert eine E-Mail-Vorlage oder setzt sie zurück, wenn kein Aktualisierungsbefehl angegeben ist. /// - /// Die E-Mail-Vorlagenabfrage. + /// Die E-Mail-Vorlagenabfrage. /// Der Aktualisierungsbefehl für die E-Mail-Vorlage. /// Wird auf Standardwert aktualisiert, wenn die Anfrage ohne http-Body gesendet wird. /// @@ -105,19 +105,22 @@ public class EmailTemplateController : ControllerBase /// Wenn der Benutzer nicht authentifiziert ist. /// Wenn die gesuchte Abfrage nicht gefunden wird. [HttpPut] - public async Task Update([FromQuery] EmailTemplateQuery email, [FromBody] UpdateEmailTemplateCommand? update = null) + public async Task Update([FromQuery] EmailTemplateQuery? temp = null, [FromBody] UpdateEmailTemplateCommand? update = null) { try { if (update is null) { - var reset = _mapper.Map(email); - await _mediator.Send(new ResetEmailTemplateCommand(email?.Id, email?.Type)); + await _mediator.Send(new ResetEmailTemplateCommand(temp)); return Ok(); } + else if(temp is null) + { + return BadRequest("No both id and type"); + } else { - var reset = _mapper.Map(email); + update.EmailTemplateQuery = temp; await _mediator.Send(update); return Ok(); } diff --git a/EnvelopeGenerator.GeneratorAPI/appsettings.json b/EnvelopeGenerator.GeneratorAPI/appsettings.json index 739f29c5..a4ab1aa8 100644 --- a/EnvelopeGenerator.GeneratorAPI/appsettings.json +++ b/EnvelopeGenerator.GeneratorAPI/appsettings.json @@ -169,7 +169,8 @@ "EnvelopeHistory": [ "TBSIG_ENVELOPE_HISTORY_AFT_INS" ], "EmailOut": [ "TBEMLP_EMAIL_OUT_AFT_INS", "TBEMLP_EMAIL_OUT_AFT_UPD" ], "EnvelopeReceiverReadOnly": [ "TBSIG_ENVELOPE_RECEIVER_READ_ONLY_UPD" ], - "Receiver": [] + "Receiver": [], + "EmailTemplate": [ "TBSIG_EMAIL_TEMPLATE_AFT_UPD" ] }, "MainPageTitle": null, "AnnotationParams": { diff --git a/EnvelopeGenerator.Web/appsettings.json b/EnvelopeGenerator.Web/appsettings.json index 4a0f5c70..6c058cd7 100644 --- a/EnvelopeGenerator.Web/appsettings.json +++ b/EnvelopeGenerator.Web/appsettings.json @@ -147,7 +147,8 @@ "EnvelopeHistory": [ "TBSIG_ENVELOPE_HISTORY_AFT_INS" ], "EmailOut": [ "TBEMLP_EMAIL_OUT_AFT_INS", "TBEMLP_EMAIL_OUT_AFT_UPD" ], "EnvelopeReceiverReadOnly": [ "TBSIG_ENVELOPE_RECEIVER_READ_ONLY_UPD" ], - "Receiver": [] + "Receiver": [], + "EmailTemplate": [ "TBSIG_EMAIL_TEMPLATE_AFT_UPD" ] }, "MainPageTitle": null, "AnnotationParams": {