using adas_core.Domain.Models.MongoModels;
using adas_core.Infrastructure.Utils.MongoMaps.Interfaz;
using MongoDB.Bson.Serialization;
namespace adas_core.Infrastructure.Utils.MongoMaps;
///
/// Represents an entity map contributor that provides authentication-related mapping logic.
///
public class AuthMapContributor : IEntityMapContributor
{
///
/// Registers BSON class maps for the and types when no class map has been registered yet, configuring field serialization rules such as ignoring null or empty values, mapping the identifier to the "_id" element name, and unmapping navigation properties not intended for persistence.
///
public void RegisterMaps()
{
if (!BsonClassMap.IsClassMapRegistered(typeof(User)))
BsonClassMap.RegisterClassMap(cm =>
{
cm.AutoMap();
cm.MapMember(c => c.Password)
.SetIgnoreIfNull(true)
.SetShouldSerializeMethod(obj => !string.IsNullOrEmpty(((User)obj).Password));
cm.MapIdMember(c => c.Id).SetElementName("_id");
cm.MapMember(c => c.LockExpirationDate)
.SetIgnoreIfNull(true);
cm.MapMember(c => c.Authorization).SetElementName("Authorization")
.SetShouldSerializeMethod(_ => false);
});
if (!BsonClassMap.IsClassMapRegistered(typeof(Authorization)))
BsonClassMap.RegisterClassMap(cm =>
{
cm.AutoMap();
cm.MapIdMember(c => c.Id).SetElementName("_id");
cm.MapMember(c => c.UnitId).SetIgnoreIfNull(true);
cm.UnmapProperty(c => c.User);
cm.UnmapProperty(c => c.Display);
cm.UnmapProperty(c => c.Unit);
});
}
}