Add Optima document attributes to place_order

This commit is contained in:
2026-09-17 15:16:44 +00:00
parent cd02e6af5f
commit 31daea55cf
9 changed files with 373 additions and 10 deletions

View File

@@ -11,7 +11,8 @@ if (!options.TryGetValue("config", out var configPath) || string.IsNullOrWhiteSp
{
Console.Error.WriteLine(
"Użycie: --config <optima.json> [--negative-tests] [--write-order [--approved]] " +
"[--customer-id <ID> --product-code <KOD>]");
"[--customer-id <ID> --product-code <KOD>] " +
"[--attribute-code-1 <KOD> --attribute-value-1 <TEKST> --attribute-code-2 <KOD> --attribute-value-2 <TEKST>]");
return 2;
}
@@ -78,9 +79,28 @@ if (saveMode == OrderSaveMode.Buffer && runtime.AdapterConfiguration.SaveMode !=
}
var reference = $"SMARTB2B-IT-{DateTime.UtcNow:yyyyMMdd-HHmmss}";
var request = CreateRequest(runtime.AdapterConfiguration, customerId!, productCode!, "PLN", reference, saveMode);
var extraFields = ReadTestAttributes(options);
if (extraFields.Count > 0)
{
var missingReference = reference + "-MISSING";
var missingCode = "__SMARTB2B_MISSING_ATTR__";
ExpectError(
() => adapter.CreateOrder(CreateRequest(
runtime.AdapterConfiguration, customerId!, productCode!, "PLN", missingReference,
OrderSaveMode.Buffer, new Dictionary<string, string> { [missingCode] = "test" })),
"eu.smartb2b.erp.document_attribute_not_found");
await AssertNoOrderWithReferenceAsync(sql, missingReference);
Console.WriteLine("PASS extraFields: nieznany kod nie utworzył zamówienia.");
}
var request = CreateRequest(runtime.AdapterConfiguration, customerId!, productCode!, "PLN", reference, saveMode, extraFields);
var result = adapter.CreateOrder(request);
if (extraFields.Count > 0)
{
await AssertSavedAttributesAsync(sql, result.Id, extraFields);
Console.WriteLine("PASS extraFields: dwa atrybuty zapisano na nagłówku zamówienia.");
}
Console.WriteLine("PASS place_order:");
Console.WriteLine(JsonSerializer.Serialize(new
{
@@ -124,7 +144,8 @@ static OrderRequest CreateRequest(
string productCode,
string currency,
string reference,
OrderSaveMode saveMode = OrderSaveMode.Buffer) => new()
OrderSaveMode saveMode = OrderSaveMode.Buffer,
IReadOnlyDictionary<string, string>? extraFields = null) => new()
{
CurrencyIso = currency,
CustomerCode = customerId,
@@ -133,6 +154,7 @@ static OrderRequest CreateRequest(
DocumentDefinition = configuration.DocumentDefinition,
WarehouseCode = configuration.DefaultWarehouseCode,
SaveMode = saveMode,
ExtraFields = extraFields ?? new Dictionary<string, string>(),
Items =
[
new OrderLineRequest
@@ -146,6 +168,56 @@ static OrderRequest CreateRequest(
]
};
static IReadOnlyDictionary<string, string> ReadTestAttributes(Dictionary<string, string?> options)
{
var names = new[] { "attribute-code-1", "attribute-value-1", "attribute-code-2", "attribute-value-2" };
if (!names.Any(options.ContainsKey)) return new Dictionary<string, string>();
if (names.Any(name => !options.ContainsKey(name)))
throw new ArgumentException("Test extraFields wymaga dwóch kodów i dwóch wartości atrybutów tekstowych.");
var first = options["attribute-code-1"]?.Trim();
var second = options["attribute-code-2"]?.Trim();
if (string.IsNullOrWhiteSpace(first) || string.IsNullOrWhiteSpace(second) || first == second)
throw new ArgumentException("Kody dwóch atrybutów muszą być różne i niepuste.");
return new Dictionary<string, string>
{
[first] = options["attribute-value-1"] ?? string.Empty,
[second] = options["attribute-value-2"] ?? string.Empty
};
}
static async Task AssertNoOrderWithReferenceAsync(SqlRawHandler sql, string reference)
{
var result = (SqlRawResult?)await sql.HandleAsync(
new RpcInvocation([], new Dictionary<string, object?>
{
["query"] = "SELECT COUNT(*) AS order_count FROM CDN.TraNag WHERE TrN_NumerObcy=@1",
["params"] = new object?[] { reference }
}), CancellationToken.None);
if (Convert.ToInt32(result?.Recordsets[0][0]["order_count"]) != 0)
throw new InvalidOperationException("Nieznany atrybut pozostawił zapisane zamówienie.");
}
static async Task AssertSavedAttributesAsync(
SqlRawHandler sql, int orderId, IReadOnlyDictionary<string, string> expected)
{
var result = (SqlRawResult?)await sql.HandleAsync(
new RpcInvocation([], new Dictionary<string, object?>
{
["query"] = "SELECT DeA_Kod, DAt_WartoscTxt FROM CDN.DokAtrybuty JOIN CDN.DefAtrybuty ON DAt_DeAId=DeA_DeAId WHERE DAt_TrNId=@1",
["params"] = new object?[] { orderId }
}), CancellationToken.None);
var actual = result?.Recordsets[0].ToDictionary(
row => Convert.ToString(row["DeA_Kod"])!,
row => Convert.ToString(row["DAt_WartoscTxt"]) ?? string.Empty,
StringComparer.Ordinal);
foreach (var (code, value) in expected)
{
if (actual is null || !actual.TryGetValue(code, out var saved) || saved != value)
throw new InvalidOperationException($"Atrybut '{code}' nie ma oczekiwanej wartości na dokumencie {orderId}.");
}
}
static void ExpectError(Action action, string expectedUri)
{
try