From d31916eab85ef5420492d06459a0dbcacf0d7f00 Mon Sep 17 00:00:00 2001 From: TekH Date: Wed, 11 Feb 2026 10:22:49 +0100 Subject: [PATCH] Add AutoMapper auditing extensions for timestamp fields Introduced AutoMapperAuditingExtensions with MapAddedWhen and MapChangedWhen methods to standardize mapping of auditing timestamps. Refactored MappingProfile to use these extensions for AddedWhen and ChangedWhen fields, improving code clarity. Updated DocumentStatus to implement auditing interfaces and added necessary imports. --- .../Common/Dto/MappingProfile.cs | 2 +- .../AutoMapperAuditingExtensions.cs | 25 +++++++++++++++++++ .../DocStatus/MappingProfile.cs | 7 ++++-- .../EmailTemplates/MappingProfile.cs | 3 ++- .../Entities/DocumentStatus.cs | 1 + 5 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 EnvelopeGenerator.Application/Common/Extensions/AutoMapperAuditingExtensions.cs diff --git a/EnvelopeGenerator.Application/Common/Dto/MappingProfile.cs b/EnvelopeGenerator.Application/Common/Dto/MappingProfile.cs index 40ad5f7e..5f5f8a03 100644 --- a/EnvelopeGenerator.Application/Common/Dto/MappingProfile.cs +++ b/EnvelopeGenerator.Application/Common/Dto/MappingProfile.cs @@ -52,7 +52,7 @@ public class MappingProfile : Profile CreateMap(); CreateMap(); CreateMap() - .ForMember(dest => dest.AddedWhen, opt => opt.MapFrom(_ => DateTime.UtcNow)); + .MapAddedWhen(); // Messaging mappings // for GTX messaging diff --git a/EnvelopeGenerator.Application/Common/Extensions/AutoMapperAuditingExtensions.cs b/EnvelopeGenerator.Application/Common/Extensions/AutoMapperAuditingExtensions.cs new file mode 100644 index 00000000..f485302e --- /dev/null +++ b/EnvelopeGenerator.Application/Common/Extensions/AutoMapperAuditingExtensions.cs @@ -0,0 +1,25 @@ +using System; +using AutoMapper; +using EnvelopeGenerator.Domain.Interfaces.Auditing; + +namespace EnvelopeGenerator.Application.Common.Extensions; + +/// +/// Extension methods for applying auditing timestamps during AutoMapper mappings. +/// +public static class AutoMapperAuditingExtensions +{ + /// + /// Maps to the current UTC time. + /// + public static IMappingExpression MapAddedWhen(this IMappingExpression expression) + where TDestination : IHasAddedWhen + => expression.ForMember(dest => dest.AddedWhen, opt => opt.MapFrom(_ => DateTime.UtcNow)); + + /// + /// Maps to the current UTC time. + /// + public static IMappingExpression MapChangedWhen(this IMappingExpression expression) + where TDestination : IHasChangedWhen + => expression.ForMember(dest => dest.ChangedWhen, opt => opt.MapFrom(_ => DateTime.UtcNow)); +} diff --git a/EnvelopeGenerator.Application/DocStatus/MappingProfile.cs b/EnvelopeGenerator.Application/DocStatus/MappingProfile.cs index eb420037..e1474d7e 100644 --- a/EnvelopeGenerator.Application/DocStatus/MappingProfile.cs +++ b/EnvelopeGenerator.Application/DocStatus/MappingProfile.cs @@ -1,4 +1,5 @@ using AutoMapper; +using EnvelopeGenerator.Application.Common.Extensions; using EnvelopeGenerator.Application.DocStatus.Commands; using EnvelopeGenerator.Domain.Entities; @@ -16,10 +17,12 @@ public class MappingProfile : Profile { CreateMap() .ForMember(dest => dest.Envelope, opt => opt.Ignore()) - .ForMember(dest => dest.Receiver, opt => opt.Ignore()); + .ForMember(dest => dest.Receiver, opt => opt.Ignore()) + .MapAddedWhen(); CreateMap() .ForMember(dest => dest.Envelope, opt => opt.Ignore()) - .ForMember(dest => dest.Receiver, opt => opt.Ignore()); + .ForMember(dest => dest.Receiver, opt => opt.Ignore()) + .MapChangedWhen(); } } \ No newline at end of file diff --git a/EnvelopeGenerator.Application/EmailTemplates/MappingProfile.cs b/EnvelopeGenerator.Application/EmailTemplates/MappingProfile.cs index bf19ddb0..699cd17d 100644 --- a/EnvelopeGenerator.Application/EmailTemplates/MappingProfile.cs +++ b/EnvelopeGenerator.Application/EmailTemplates/MappingProfile.cs @@ -1,5 +1,6 @@ using AutoMapper; using EnvelopeGenerator.Application.Common.Dto; +using EnvelopeGenerator.Application.Common.Extensions; using EnvelopeGenerator.Application.EmailTemplates.Commands; using EnvelopeGenerator.Domain.Entities; @@ -18,6 +19,6 @@ public class MappingProfile : Profile CreateMap(); CreateMap() - .ForMember(dest => dest.ChangedWhen, opt => opt.MapFrom(_ => DateTime.UtcNow)); + .MapChangedWhen(); } } \ No newline at end of file diff --git a/EnvelopeGenerator.Domain/Entities/DocumentStatus.cs b/EnvelopeGenerator.Domain/Entities/DocumentStatus.cs index 2a5fe111..c61bb4cd 100644 --- a/EnvelopeGenerator.Domain/Entities/DocumentStatus.cs +++ b/EnvelopeGenerator.Domain/Entities/DocumentStatus.cs @@ -3,6 +3,7 @@ using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using DigitalData.Core.Abstractions.Interfaces; using EnvelopeGenerator.Domain.Interfaces; +using EnvelopeGenerator.Domain.Interfaces.Auditing; namespace EnvelopeGenerator.Domain.Entities {