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.
This commit is contained in:
2026-02-11 10:22:49 +01:00
parent ee7c92ff5b
commit d31916eab8
5 changed files with 34 additions and 4 deletions

View File

@@ -52,7 +52,7 @@ public class MappingProfile : Profile
CreateMap<EnvelopeReceiverReadOnlyCreateDto, Domain.Entities.EnvelopeReceiverReadOnly>();
CreateMap<EnvelopeReceiverReadOnlyUpdateDto, Domain.Entities.EnvelopeReceiverReadOnly>();
CreateMap<AnnotationCreateDto, ElementAnnotation>()
.ForMember(dest => dest.AddedWhen, opt => opt.MapFrom(_ => DateTime.UtcNow));
.MapAddedWhen();
// Messaging mappings
// for GTX messaging

View File

@@ -0,0 +1,25 @@
using System;
using AutoMapper;
using EnvelopeGenerator.Domain.Interfaces.Auditing;
namespace EnvelopeGenerator.Application.Common.Extensions;
/// <summary>
/// Extension methods for applying auditing timestamps during AutoMapper mappings.
/// </summary>
public static class AutoMapperAuditingExtensions
{
/// <summary>
/// Maps <see cref="IHasAddedWhen.AddedWhen"/> to the current UTC time.
/// </summary>
public static IMappingExpression<TSource, TDestination> MapAddedWhen<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression)
where TDestination : IHasAddedWhen
=> expression.ForMember(dest => dest.AddedWhen, opt => opt.MapFrom(_ => DateTime.UtcNow));
/// <summary>
/// Maps <see cref="IHasChangedWhen.ChangedWhen"/> to the current UTC time.
/// </summary>
public static IMappingExpression<TSource, TDestination> MapChangedWhen<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression)
where TDestination : IHasChangedWhen
=> expression.ForMember(dest => dest.ChangedWhen, opt => opt.MapFrom(_ => DateTime.UtcNow));
}

View File

@@ -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<CreateDocStatusCommand, DocumentStatus>()
.ForMember(dest => dest.Envelope, opt => opt.Ignore())
.ForMember(dest => dest.Receiver, opt => opt.Ignore());
.ForMember(dest => dest.Receiver, opt => opt.Ignore())
.MapAddedWhen();
CreateMap<UpdateDocStatusCommand, DocumentStatus>()
.ForMember(dest => dest.Envelope, opt => opt.Ignore())
.ForMember(dest => dest.Receiver, opt => opt.Ignore());
.ForMember(dest => dest.Receiver, opt => opt.Ignore())
.MapChangedWhen();
}
}

View File

@@ -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<EmailTemplate, EmailTemplateDto>();
CreateMap<EmailTemplateUpdateDto, EmailTemplate>()
.ForMember(dest => dest.ChangedWhen, opt => opt.MapFrom(_ => DateTime.UtcNow));
.MapChangedWhen();
}
}

View File

@@ -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
{