Creado apartir del commit b888de6c92056de294456f581a56e25e734d4ab7 de develop

This commit is contained in:
jrojas
2026-06-26 09:52:35 +02:00
commit 319fd3dfb0
746 changed files with 106610 additions and 0 deletions
@@ -0,0 +1,496 @@
using MongoDB.Bson;
using MongoDB.Driver;
using MongoMigrations.Core;
namespace adas_core.Infrastructure.Migrations.MongoMigrations;
// ReSharper disable once InconsistentNaming
public class U_0_1_0_UpdateDataPatien : Migration
{
public U_0_1_0_UpdateDataPatien() : base(10)
{
Description =
"Transforma person.historicalIds y historicalLocations en arrays con fechas y realiza merge sobre patients.";
}
public override void Update()
{
var collectionPatient = Database.GetCollection<BsonDocument>("patients");
var pipelinePatient = new[]
{
new BsonDocument("$addFields", new BsonDocument
{
{
"person.historicalIds", new BsonDocument("$cond", new BsonDocument
{
// si ya es array → no tocar
{ "if", new BsonDocument("$isArray", "$person.historicalIds") },
{ "then", "$person.historicalIds" },
// si es document → migrar
{
"else", new BsonDocument("$map", new BsonDocument
{
{ "input", new BsonDocument("$objectToArray", "$person.historicalIds") },
{ "as", "id" },
{
"in", new BsonDocument
{
{
"time",
new BsonDocument("$dateFromString",
new BsonDocument("dateString", "$$id.k"))
},
{ "patientIds", "$$id.v" }
}
}
})
}
})
},
{
"historicalLocations", new BsonDocument("$cond", new BsonDocument
{
{ "if", new BsonDocument("$isArray", "$historicalLocations") },
{ "then", "$historicalLocations" },
{
"else", new BsonDocument("$map", new BsonDocument
{
{ "input", new BsonDocument("$objectToArray", "$historicalLocations") },
{ "as", "location" },
{
"in", new BsonDocument
{
{
"admTime",
new BsonDocument("$dateFromString",
new BsonDocument("dateString", "$$location.k"))
},
{ "patientLocation", "$$location.v" }
}
}
})
}
})
}
}),
new BsonDocument("$merge", new BsonDocument
{
{ "into", "patients" },
{ "whenMatched", "replace" }, // aquí SÍ es seguro (no hay _t)
{ "whenNotMatched", "discard" }
})
};
collectionPatient.Aggregate<BsonDocument>(pipelinePatient).ToList();
var collectionAdmission = Database.GetCollection<BsonDocument>("admissions");
var pipelineAdmission = new[]
{
new BsonDocument("$addFields", new BsonDocument
{
{
"person.historicalIds", new BsonDocument("$cond", new BsonDocument
{
// si ya es array → no tocar
{ "if", new BsonDocument("$isArray", "$person.historicalIds") },
{ "then", "$person.historicalIds" },
// si es document → migrar
{
"else", new BsonDocument("$map", new BsonDocument
{
{ "input", new BsonDocument("$objectToArray", "$person.historicalIds") },
{ "as", "id" },
{
"in", new BsonDocument
{
{
"time",
new BsonDocument("$dateFromString",
new BsonDocument("dateString", "$$id.k"))
},
{ "patientIds", "$$id.v" }
}
}
})
}
})
},
{
"historicalLocations", new BsonDocument("$cond", new BsonDocument
{
{ "if", new BsonDocument("$isArray", "$historicalLocations") },
{ "then", "$historicalLocations" },
{
"else", new BsonDocument("$map", new BsonDocument
{
{ "input", new BsonDocument("$objectToArray", "$historicalLocations") },
{ "as", "location" },
{
"in", new BsonDocument
{
{
"admTime",
new BsonDocument("$dateFromString",
new BsonDocument("dateString", "$$location.k"))
},
{ "patientLocation", "$$location.v" }
}
}
})
}
})
}
}),
new BsonDocument("$merge", new BsonDocument
{
{ "into", "admissions" },
{ "whenMatched", "replace" }, // aquí SÍ es seguro (no hay _t)
{ "whenNotMatched", "discard" }
})
};
collectionAdmission.Aggregate<BsonDocument>(pipelineAdmission).ToList();
var collectionArchivePatient = Database.GetCollection<BsonDocument>("archive_patient");
var pipelineArchivePatient = new[]
{
new BsonDocument("$addFields", new BsonDocument
{
{
"person.historicalIds", new BsonDocument("$cond", new BsonDocument
{
// si ya es array → no tocar
{ "if", new BsonDocument("$isArray", "$person.historicalIds") },
{ "then", "$person.historicalIds" },
// si es document → migrar
{
"else", new BsonDocument("$map", new BsonDocument
{
{ "input", new BsonDocument("$objectToArray", "$person.historicalIds") },
{ "as", "id" },
{
"in", new BsonDocument
{
{
"time",
new BsonDocument("$dateFromString",
new BsonDocument("dateString", "$$id.k"))
},
{ "patientIds", "$$id.v" }
}
}
})
}
})
},
{
"historicalLocations", new BsonDocument("$cond", new BsonDocument
{
{ "if", new BsonDocument("$isArray", "$historicalLocations") },
{ "then", "$historicalLocations" },
{
"else", new BsonDocument("$map", new BsonDocument
{
{ "input", new BsonDocument("$objectToArray", "$historicalLocations") },
{ "as", "location" },
{
"in", new BsonDocument
{
{
"admTime",
new BsonDocument("$dateFromString",
new BsonDocument("dateString", "$$location.k"))
},
{ "patientLocation", "$$location.v" }
}
}
})
}
})
}
}),
new BsonDocument("$merge", new BsonDocument
{
{ "into", "archive_patient" },
{ "whenMatched", "replace" }, // aquí SÍ es seguro (no hay _t)
{ "whenNotMatched", "discard" }
})
};
collectionArchivePatient.Aggregate<BsonDocument>(pipelineArchivePatient).ToList();
}
// NO usado por MongoMigrations.Core
// Solo para ejecución manual si hiciera falta
public void Down()
{
var collectionPatient = Database.GetCollection<BsonDocument>("patients");
var pipelinePatient = new[]
{
new BsonDocument("$addFields", new BsonDocument
{
{
"person.historicalIds", new BsonDocument("$cond", new BsonDocument
{
// si ya es document → no tocar
{ "if", new BsonDocument("$not", new BsonArray { new BsonDocument("$isArray", "$person.historicalIds") }) },
{ "then", "$person.historicalIds" },
// si es array → revertir
{
"else", new BsonDocument("$arrayToObject", new BsonArray
{
new BsonDocument
{
{
"$map", new BsonDocument
{
{ "input", "$person.historicalIds" },
{ "as", "id" },
{
"in", new BsonDocument
{
{
"k", new BsonDocument("$dateToString", new BsonDocument
{
{ "format", "%Y-%m-%dT%H:%M:%S.%LZ" },
{ "date", "$$id.time" }
})
},
{ "v", "$$id.patientIds" }
}
}
}
}
}
})
}
})
},
{
"historicalLocations", new BsonDocument("$cond", new BsonDocument
{
{ "if", new BsonDocument("$not", new BsonArray { new BsonDocument("$isArray", "$historicalLocations") }) },
{ "then", "$historicalLocations" },
{
"else", new BsonDocument("$arrayToObject", new BsonArray
{
new BsonDocument
{
{
"$map", new BsonDocument
{
{ "input", "$historicalLocations" },
{ "as", "location" },
{
"in", new BsonDocument
{
{
"k", new BsonDocument("$dateToString", new BsonDocument
{
{ "format", "%Y-%m-%dT%H:%M:%S.%LZ" },
{ "date", "$$location.admTime" }
})
},
{ "v", "$$location.patientLocation" }
}
}
}
}
}
})
}
})
}
}),
new BsonDocument("$merge", new BsonDocument
{
{ "into", "patients" },
{ "whenMatched", "replace" },
{ "whenNotMatched", "discard" }
})
};
collectionPatient.Aggregate<BsonDocument>(pipelinePatient).ToList();
var collectionAdm = Database.GetCollection<BsonDocument>("admissions");
var pipelineAdm = new[]
{
new BsonDocument("$addFields", new BsonDocument
{
{
"person.historicalIds", new BsonDocument("$cond", new BsonDocument
{
// si ya es document → no tocar
{ "if", new BsonDocument("$not", new BsonArray { new BsonDocument("$isArray", "$person.historicalIds") }) },
{ "then", "$person.historicalIds" },
// si es array → revertir
{
"else", new BsonDocument("$arrayToObject", new BsonArray
{
new BsonDocument
{
{
"$map", new BsonDocument
{
{ "input", "$person.historicalIds" },
{ "as", "id" },
{
"in", new BsonDocument
{
{
"k", new BsonDocument("$dateToString", new BsonDocument
{
{ "format", "%Y-%m-%dT%H:%M:%S.%LZ" },
{ "date", "$$id.time" }
})
},
{ "v", "$$id.patientIds" }
}
}
}
}
}
})
}
})
},
{
"historicalLocations", new BsonDocument("$cond", new BsonDocument
{
{ "if", new BsonDocument("$not", new BsonArray { new BsonDocument("$isArray", "$historicalLocations") }) },
{ "then", "$historicalLocations" },
{
"else", new BsonDocument("$arrayToObject", new BsonArray
{
new BsonDocument
{
{
"$map", new BsonDocument
{
{ "input", "$historicalLocations" },
{ "as", "location" },
{
"in", new BsonDocument
{
{
"k", new BsonDocument("$dateToString", new BsonDocument
{
{ "format", "%Y-%m-%dT%H:%M:%S.%LZ" },
{ "date", "$$location.admTime" }
})
},
{ "v", "$$location.patientLocation" }
}
}
}
}
}
})
}
})
}
}),
new BsonDocument("$merge", new BsonDocument
{
{ "into", "admissions" },
{ "whenMatched", "replace" },
{ "whenNotMatched", "discard" }
})
};
collectionAdm.Aggregate<BsonDocument>(pipelineAdm).ToList();
var collectionArcP = Database.GetCollection<BsonDocument>("archive_patient");
var pipelineArcP = new[]
{
new BsonDocument("$addFields", new BsonDocument
{
{
"person.historicalIds", new BsonDocument("$cond", new BsonDocument
{
// si ya es document → no tocar
{ "if", new BsonDocument("$not", new BsonArray { new BsonDocument("$isArray", "$person.historicalIds") }) },
{ "then", "$person.historicalIds" },
// si es array → revertir
{
"else", new BsonDocument("$arrayToObject", new BsonArray
{
new BsonDocument
{
{
"$map", new BsonDocument
{
{ "input", "$person.historicalIds" },
{ "as", "id" },
{
"in", new BsonDocument
{
{
"k", new BsonDocument("$dateToString", new BsonDocument
{
{ "format", "%Y-%m-%dT%H:%M:%S.%LZ" },
{ "date", "$$id.time" }
})
},
{ "v", "$$id.patientIds" }
}
}
}
}
}
})
}
})
},
{
"historicalLocations", new BsonDocument("$cond", new BsonDocument
{
{ "if", new BsonDocument("$not", new BsonArray { new BsonDocument("$isArray", "$historicalLocations") }) },
{ "then", "$historicalLocations" },
{
"else", new BsonDocument("$arrayToObject", new BsonArray
{
new BsonDocument
{
{
"$map", new BsonDocument
{
{ "input", "$historicalLocations" },
{ "as", "location" },
{
"in", new BsonDocument
{
{
"k", new BsonDocument("$dateToString", new BsonDocument
{
{ "format", "%Y-%m-%dT%H:%M:%S.%LZ" },
{ "date", "$$location.admTime" }
})
},
{ "v", "$$location.patientLocation" }
}
}
}
}
}
})
}
})
}
}),
new BsonDocument("$merge", new BsonDocument
{
{ "into", "archive_patient" },
{ "whenMatched", "replace" },
{ "whenNotMatched", "discard" }
})
};
collectionArcP.Aggregate<BsonDocument>(pipelineArcP).ToList();
}
}
@@ -0,0 +1,66 @@
using MongoDB.Bson;
using MongoDB.Driver;
using MongoMigrations.Core;
namespace adas_core.Infrastructure.Migrations.MongoMigrations;
public class U_0_1_1_UpdateDisplayConfigDriver : Migration
{
public U_0_1_1_UpdateDisplayConfigDriver() : base(11)
{
Description =
"Genera el campo Type en todos los documentos de la coleccion DisplayConfig por que el nuevo driver no expone _t para polimorfia";
}
public override void Update()
{
var collection = Database.GetCollection<BsonDocument>("config_displays");
var pipeline = new[]
{
new BsonDocument("$set", new BsonDocument
{
{
"type", new BsonDocument("$switch", new BsonDocument
{
{
"branches", new BsonArray
{
new BsonDocument
{
{ "case", new BsonDocument("$eq", new BsonArray { "$_t", "DisplayNurse" }) },
{ "then", "DisplayNurse" }
},
new BsonDocument
{
{ "case", new BsonDocument("$eq", new BsonArray { "$_t", "StandarDisplay" }) },
{ "then", "StandarDisplay" }
},
new BsonDocument
{
{ "case", new BsonDocument("$eq", new BsonArray { "$_t", "SmartDisplay" }) },
{ "then", "SmartDisplay" }
}
}
},
{ "default", "Unknown" }
})
}
}),
new BsonDocument("$unset", "_t"),
new BsonDocument("$merge", new BsonDocument
{
{ "into", "config_displays" },
{ "whenMatched", "merge" },
{ "whenNotMatched", "discard" }
})
};
collection.AggregateToCollection<BsonDocument>(pipeline);
// collection.AggregateToCollection<BsonDocument>(pipeline, "config_displays");
}
}
@@ -0,0 +1,59 @@
using MongoDB.Bson;
using MongoDB.Driver;
using MongoMigrations.Core;
namespace adas_core.Infrastructure.Migrations.MongoMigrations;
public class U_0_1_2_UpdatePointOfCareConfig : Migration
{
public U_0_1_2_UpdatePointOfCareConfig() : base(12)
{
Description =
"Borra los elementos de configuracion de poc, relay, camera y beacon y deja el place holder del nuevo modelo";
}
public override void Update()
{
var collection = Database.GetCollection<BsonDocument>("pointOfCares");
var filter = Builders<BsonDocument>.Filter.Exists("configuration");
var documents = collection.Find(filter).ToList();
foreach (var doc in documents)
{
var configuration = doc["configuration"].AsBsonDocument;
// Listas nuevas de IDs
var beaconIdList = new BsonArray();
var cameraIdList = new BsonArray();
var relayIdList = new BsonArray();
// 1. Procesar Beacons (si existen)
if (configuration.Contains("beacons") && configuration["beacons"].IsBsonArray)
{
configuration.Remove("beacons");
}
configuration["beaconIdList"] = beaconIdList;
// 2. Procesar Cameras (si existen)
if (configuration.Contains("cameras") && configuration["cameras"].IsBsonArray)
{
configuration.Remove("cameras");
}
configuration["cameraIdList"] = cameraIdList;
// 3. Procesar RelayList (si existen)
if (configuration.Contains("relayList") && configuration["relayList"].IsBsonArray)
{
configuration.Remove("relayList");
}
configuration["relayIdList"] = relayIdList;
// Actualizar el documento en la base de datos
collection.ReplaceOne(Builders<BsonDocument>.Filter.Eq("_id", doc["_id"]), doc);
}
}
}
@@ -0,0 +1,50 @@
using MongoDB.Bson;
using MongoDB.Driver;
using MongoMigrations.Core;
namespace adas_core.Infrastructure.Migrations.MongoMigrations;
public class U_0_1_3_UpdateLanguageBarrier : Migration
{
public U_0_1_3_UpdateLanguageBarrier() : base(13)
{
Description =
"Borra los elementos de configuracion de poc, relay, camera y beacon y deja el place holder del nuevo modelo";
}
public override void Update()
{
string[] collectionsToUpdate = { "patients", "admissions" };
foreach (var collectionName in collectionsToUpdate)
{
var collection = Database.GetCollection<BsonDocument>(collectionName);
// Filtramos documentos donde languageBarrier existe y NO es ya un array
var filter = Builders<BsonDocument>.Filter.And(
Builders<BsonDocument>.Filter.Exists("languageBarrier"),
Builders<BsonDocument>.Filter.Not(Builders<BsonDocument>.Filter.Type("languageBarrier", BsonType.Array))
);
var documents = collection.Find(filter).ToList();
foreach (var doc in documents)
{
var currentValue = doc["languageBarrier"];
BsonArray newArray = new BsonArray();
// Si el valor actual no es nulo, lo añadimos como primer elemento del array
if (!currentValue.IsBsonNull)
{
newArray.Add(currentValue);
}
// Actualizamos el campo en el documento Bson
doc["languageBarrier"] = newArray;
// Guardamos los cambios en la base de datos
collection.ReplaceOne(Builders<BsonDocument>.Filter.Eq("_id", doc["_id"]), doc);
}
}
}
}