Closes the server-side/data-layer piece of Phase 6.4 Stream B.2-B.4. The CSV-import preview + modal UI (Stream B.3/B.5) still belongs to the Admin UI follow-up — this PR owns the staging tables + atomic finalise alone. Configuration: - New EquipmentImportBatch entity (Id, ClusterId, CreatedBy, CreatedAtUtc, RowsStaged/Accepted/Rejected, FinalisedAtUtc?). Composite index on (CreatedBy, FinalisedAtUtc) powers the Admin preview modal's "my open batches" query. - New EquipmentImportRow entity — one row per CSV row, 8 required columns from decision #117 + 9 optional from decision #139 + IsAccepted flag + RejectReason. FK to EquipmentImportBatch with cascade delete so DropBatch collapses the whole tree. - EF migration 20260419_..._AddEquipmentImportBatch. - SchemaComplianceTests expected tables list gains the two new tables. Admin.Services.EquipmentImportBatchService: - CreateBatchAsync — new header row, caller-supplied ClusterId + CreatedBy. - StageRowsAsync(batchId, acceptedRows, rejectedRows) — bulk-inserts the parsed CSV rows into staging. Rejected rows carry LineNumberInFile + RejectReason for the preview modal. Throws when the batch is finalised. - DropBatchAsync — removes batch + cascaded rows. Throws when the batch was already finalised (rollback via staging is not a time machine). - FinaliseBatchAsync(batchId, generationId, driverInstanceId, unsLineId) — atomic apply. Opens an EF transaction when the provider supports it (SQL Server in prod; InMemory in tests skips the tx), bulk-inserts every accepted staging row into Equipment, stamps EquipmentImportBatch.FinalisedAtUtc, commits. Failure rolls back so Equipment never partially mutates. Idempotent-under-double-call: second finalise throws ImportBatchAlreadyFinalisedException. - ListByUserAsync(createdBy, includeFinalised) — the Admin preview modal's backing query. OrderByDescending on CreatedAtUtc so the most-recent batch shows first. - Two exception types: ImportBatchNotFoundException + ImportBatchAlreadyFinalisedException. ExternalIdReservation merging (ZTag + SAPID fleet-wide uniqueness) is NOT done here — a narrower follow-up wires it once the concurrent-insert test matrix is green. Tests (10 new EquipmentImportBatchServiceTests, all pass): - CreateBatch populates Id + CreatedAtUtc + zero-ed counters. - StageRows accepted + rejected both persist; counters advance. - DropBatch cascades row delete. - DropBatch after finalise throws. - Finalise translates accepted staging rows → Equipment under the target GenerationId + DriverInstanceId + UnsLineId. - Finalise twice throws. - Finalise of unknown batch throws. - Stage after finalise throws. - ListByUserAsync filters by creator + finalised flag. - Drop of unknown batch is a no-op (idempotent rollback). Full solution dotnet test: 1235 passing (was 1225, +10). Pre-existing Client.CLI Subscribe flake unchanged. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
69 lines
2.9 KiB
C#
69 lines
2.9 KiB
C#
namespace ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
|
|
|
/// <summary>
|
|
/// Staged equipment-import batch per Phase 6.4 Stream B.2. Rows land in the child
|
|
/// <see cref="EquipmentImportRow"/> table under a batch header; operator reviews + either
|
|
/// drops (via <c>DropImportBatch</c>) or finalises (via <c>FinaliseImportBatch</c>) in one
|
|
/// bounded transaction. The live <c>Equipment</c> table never sees partial state.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// <para>User-scoped visibility: the preview modal only shows batches where
|
|
/// <see cref="CreatedBy"/> equals the current operator. Prevents accidental
|
|
/// cross-operator finalise during concurrent imports. An admin finalise / drop surface
|
|
/// can override this — tracked alongside the UI follow-up.</para>
|
|
///
|
|
/// <para><see cref="FinalisedAtUtc"/> stamps the moment the batch promoted from staging
|
|
/// into <c>Equipment</c>. Null = still in staging; non-null = archived / finalised.</para>
|
|
/// </remarks>
|
|
public sealed class EquipmentImportBatch
|
|
{
|
|
public Guid Id { get; set; }
|
|
public required string ClusterId { get; set; }
|
|
public required string CreatedBy { get; set; }
|
|
public DateTime CreatedAtUtc { get; set; }
|
|
public int RowsStaged { get; set; }
|
|
public int RowsAccepted { get; set; }
|
|
public int RowsRejected { get; set; }
|
|
public DateTime? FinalisedAtUtc { get; set; }
|
|
|
|
public ICollection<EquipmentImportRow> Rows { get; set; } = [];
|
|
}
|
|
|
|
/// <summary>
|
|
/// One staged row under an <see cref="EquipmentImportBatch"/>. Mirrors the decision #117
|
|
/// + decision #139 columns from the CSV importer's output + an
|
|
/// <see cref="IsAccepted"/> flag + a <see cref="RejectReason"/> string the preview modal
|
|
/// renders.
|
|
/// </summary>
|
|
public sealed class EquipmentImportRow
|
|
{
|
|
public Guid Id { get; set; }
|
|
public Guid BatchId { get; set; }
|
|
public int LineNumberInFile { get; set; }
|
|
public bool IsAccepted { get; set; }
|
|
public string? RejectReason { get; set; }
|
|
|
|
// Required (decision #117)
|
|
public required string ZTag { get; set; }
|
|
public required string MachineCode { get; set; }
|
|
public required string SAPID { get; set; }
|
|
public required string EquipmentId { get; set; }
|
|
public required string EquipmentUuid { get; set; }
|
|
public required string Name { get; set; }
|
|
public required string UnsAreaName { get; set; }
|
|
public required string UnsLineName { get; set; }
|
|
|
|
// Optional (decision #139 — OPC 40010 Identification)
|
|
public string? Manufacturer { get; set; }
|
|
public string? Model { get; set; }
|
|
public string? SerialNumber { get; set; }
|
|
public string? HardwareRevision { get; set; }
|
|
public string? SoftwareRevision { get; set; }
|
|
public string? YearOfConstruction { get; set; }
|
|
public string? AssetLocation { get; set; }
|
|
public string? ManufacturerUri { get; set; }
|
|
public string? DeviceManualUri { get; set; }
|
|
|
|
public EquipmentImportBatch? Batch { get; set; }
|
|
}
|