Files
KArtSell.Aegis/src/KArtSell.Modules.ModelOperations/TradeExecution/TradeHandlers.cs
T
kjh2064 2ccf74c410 fix: Release build breakage + Dapper mapping bugs in VS-03/VS-04/Phase3-K
- KArtSell.Host.csproj: FrontendFiles glob was evaluated at project-load
  time, before pnpm build ran, so it copied stale/missing Vite-hashed
  filenames every Release build. Move the glob inside the target, after
  the build Exec.
- ApprovalSql/AuditSql/TradeSql: fix live-DB integration failures never
  caught by unit tests: DateOnly and inet columns can't be bound/read
  directly through Dapper without conversion; kis_response (jsonb) read
  as JsonElement threw InvalidCastException; GdprRetention.RetentionEndsAt
  was typed DateTime against a DATE column.
- TradeSql: UpdateTradeStatusAsync only ever persisted status/kis_response
  /error_message, silently dropping kis_order_id, executed_quantity,
  unit_price, total_amount, commission, net_proceeds and the execution/
  settlement timestamps on every call. Changed it to take the Trade
  aggregate so the full state transition persists.
- TradeSql: add a static ctor setting Dapper.DefaultTypeMap.
  MatchNamesWithUnderscores = true. The repo's [ModuleInitializer] in
  KArtSell.BuildingBlocks only fires once that assembly is actually
  loaded; TradeSql/Trade never reference a BuildingBlocks type, so under
  test isolation (or any host that queries a trade before touching
  BuildingBlocks) every snake_case column silently mapped to null/default.
- Test fixes: seed the FK prerequisites (model_operations.models,
  sell_decisions) that ApprovalWorkflowTests/TradeExecutionTests were
  missing, correct a SellPriorityRanker test input to match the approved
  VS-10-SLICE_SPEC age-boost threshold, and fix a GDPR redaction
  assertion that called ToString() on a Dictionary instead of inspecting
  its values.

12 DbUpMigrationTests failures remain and are unrelated to this fix: the
kartsell DB user isn't the owner of kartsell_migration_test, so DbUp's
fresh-database rehearsal can't DROP/CREATE it. Needs a DBA grant.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
2026-08-07 23:21:04 +09:00

321 lines
10 KiB
C#

using System.Text.Json;
using KArtSell.BuildingBlocks.Data;
using KArtSell.BuildingBlocks.Hashing;
using KArtSell.BuildingBlocks.Reliability;
using KArtSell.BuildingBlocks.Time;
using Microsoft.Extensions.Logging;
namespace KArtSell.Modules.ModelOperations.TradeExecution;
public class SubmitTradeCommand
{
public Guid SellDecisionId { get; set; }
public int Quantity { get; set; }
public decimal LimitPrice { get; set; }
public Guid CorrelationId { get; set; }
}
public class SubmitTradeHandler
{
private readonly ITradeSql _sql;
private readonly IKisTradeExecutionService _kis;
private readonly IDbConnectionFactory _connectionFactory;
private readonly IOutboxWriter _outbox;
private readonly IClock _clock;
private readonly ILogger<SubmitTradeHandler> _logger;
public SubmitTradeHandler(
ITradeSql sql,
IKisTradeExecutionService kis,
IDbConnectionFactory connectionFactory,
IOutboxWriter outbox,
IClock clock,
ILogger<SubmitTradeHandler> logger)
{
_sql = sql;
_kis = kis;
_connectionFactory = connectionFactory;
_outbox = outbox;
_clock = clock;
_logger = logger;
}
public async Task<Guid> HandleAsync(SubmitTradeCommand command, CancellationToken ct = default)
{
var trade = Trade.Create(command.SellDecisionId, command.Quantity, command.CorrelationId, _clock.UtcNow.UtcDateTime);
await _sql.InsertTradeAsync(trade, ct);
_logger.LogInformation("Created trade: {TradeId}", trade.Id);
try
{
var (orderId, response) = await _kis.ExecuteTradeAsync(
trade.Id,
command.Quantity,
command.LimitPrice,
command.CorrelationId,
ct
);
trade.MarkSubmitted(orderId, response);
await _sql.UpdateTradeStatusAsync(trade, response, null, ct);
await PublishEventAsync(
"TradeSubmitted",
new TradeSubmittedEvent
{
TradeId = trade.Id,
SellDecisionId = command.SellDecisionId,
KisOrderId = orderId,
Quantity = command.Quantity,
CorrelationId = command.CorrelationId
},
command.CorrelationId,
ct);
return trade.Id;
}
catch (KisTradeExecutionException ex)
{
trade.MarkErrored(ex);
await _sql.UpdateTradeStatusAsync(trade, ex.KisResponse, ex.Message, ct);
_logger.LogError(
"Trade submission failed: {TradeId} {Classification}",
trade.Id, ex.Classification
);
throw;
}
}
private async Task PublishEventAsync<T>(string eventType, T @event, Guid correlationId, CancellationToken ct) where T : class
=> await TradeOutboxPublisher.PublishAsync(_connectionFactory, _outbox, _clock, eventType, @event, correlationId, ct);
}
public class PollTradeStatusCommand
{
public Guid TradeId { get; set; }
public string KisOrderId { get; set; } = string.Empty;
public Guid CorrelationId { get; set; }
}
public class PollTradeStatusHandler
{
private readonly ITradeSql _sql;
private readonly IKisTradeExecutionService _kis;
private readonly IDbConnectionFactory _connectionFactory;
private readonly IOutboxWriter _outbox;
private readonly IClock _clock;
private readonly ILogger<PollTradeStatusHandler> _logger;
public PollTradeStatusHandler(
ITradeSql sql,
IKisTradeExecutionService kis,
IDbConnectionFactory connectionFactory,
IOutboxWriter outbox,
IClock clock,
ILogger<PollTradeStatusHandler> logger)
{
_sql = sql;
_kis = kis;
_connectionFactory = connectionFactory;
_outbox = outbox;
_clock = clock;
_logger = logger;
}
public async Task HandleAsync(PollTradeStatusCommand command, CancellationToken ct = default)
{
var trade = await _sql.GetTradeByIdAsync(command.TradeId, command.CorrelationId, ct);
if (trade == null)
{
_logger.LogWarning("Trade not found: {TradeId}", command.TradeId);
return;
}
try
{
var (status, executedQty, unitPrice, response) = await _kis.GetOrderStatusAsync(
command.KisOrderId,
command.CorrelationId,
ct
);
if (status is "ACCEPTED" or "PARTIAL_FILLED" or "FULLY_FILLED")
{
trade.MarkAccepted(response);
if (status is "PARTIAL_FILLED" or "FULLY_FILLED")
{
trade.MarkFilled(executedQty, unitPrice, response, _clock.UtcNow.UtcDateTime);
}
await _sql.UpdateTradeStatusAsync(trade, response, null, ct);
if (trade.Status is TradeStatus.FullyFilled)
{
await TradeOutboxPublisher.PublishAsync(
_connectionFactory,
_outbox,
_clock,
"TradeFilled",
new TradeFilledEvent
{
TradeId = trade.Id,
ExecutedQuantity = executedQty,
UnitPrice = unitPrice,
CorrelationId = command.CorrelationId
},
command.CorrelationId,
ct);
}
_logger.LogInformation("Trade status updated: {TradeId} -> {Status}", trade.Id, status);
}
}
catch (KisTradeExecutionException ex)
{
await _sql.UpdateTradeStatusAsync(trade, ex.KisResponse, ex.Message, ct);
_logger.LogError("Failed to poll trade status: {TradeId}", trade.Id);
}
}
}
public class ConfirmSettlementCommand
{
public Guid TradeId { get; set; }
public string KisOrderId { get; set; } = string.Empty;
public decimal? Commission { get; set; }
public Guid CorrelationId { get; set; }
}
public class ConfirmSettlementHandler
{
private readonly ITradeSql _sql;
private readonly IKisTradeExecutionService _kis;
private readonly IDbConnectionFactory _connectionFactory;
private readonly IOutboxWriter _outbox;
private readonly IClock _clock;
private readonly ILogger<ConfirmSettlementHandler> _logger;
public ConfirmSettlementHandler(
ITradeSql sql,
IKisTradeExecutionService kis,
IDbConnectionFactory connectionFactory,
IOutboxWriter outbox,
IClock clock,
ILogger<ConfirmSettlementHandler> logger)
{
_sql = sql;
_kis = kis;
_connectionFactory = connectionFactory;
_outbox = outbox;
_clock = clock;
_logger = logger;
}
public async Task HandleAsync(ConfirmSettlementCommand command, CancellationToken ct = default)
{
var trade = await _sql.GetTradeByIdAsync(command.TradeId, command.CorrelationId, ct);
if (trade == null)
{
_logger.LogWarning("Trade not found for settlement: {TradeId}", command.TradeId);
return;
}
try
{
var (success, response) = await _kis.ConfirmSettlementAsync(
command.KisOrderId,
command.CorrelationId,
ct
);
if (success)
{
trade.MarkConfirmed(_clock.UtcNow.UtcDateTime, command.Commission);
await _sql.UpdateTradeStatusAsync(trade, response, null, ct);
await TradeOutboxPublisher.PublishAsync(
_connectionFactory,
_outbox,
_clock,
"TradeSettled",
new TradeSettledEvent
{
TradeId = trade.Id,
NetProceeds = trade.NetProceeds ?? 0,
CorrelationId = command.CorrelationId
},
command.CorrelationId,
ct);
_logger.LogInformation("Trade settlement confirmed: {TradeId}", trade.Id);
}
}
catch (KisTradeExecutionException ex)
{
await _sql.UpdateTradeStatusAsync(trade, ex.KisResponse, ex.Message, ct);
_logger.LogError("Failed to confirm settlement: {TradeId}", trade.Id);
}
}
}
public class TradeSubmittedEvent
{
public Guid TradeId { get; set; }
public Guid SellDecisionId { get; set; }
public string KisOrderId { get; set; } = string.Empty;
public int Quantity { get; set; }
public Guid CorrelationId { get; set; }
}
public class TradeFilledEvent
{
public Guid TradeId { get; set; }
public int ExecutedQuantity { get; set; }
public decimal UnitPrice { get; set; }
public Guid CorrelationId { get; set; }
}
public class TradeSettledEvent
{
public Guid TradeId { get; set; }
public decimal NetProceeds { get; set; }
public Guid CorrelationId { get; set; }
}
/// <summary>
/// DEBT-TRADE-001: outbox write happens in its own transaction, separate from the
/// preceding trade status update (which owns its own connection in TradeSql). Not yet
/// atomic with the state transition. See TECH_DEBT_REGISTER.md.
/// </summary>
internal static class TradeOutboxPublisher
{
public static async Task PublishAsync<T>(
IDbConnectionFactory connectionFactory,
IOutboxWriter outbox,
IClock clock,
string eventType,
T @event,
Guid correlationId,
CancellationToken ct) where T : class
{
var payload = JsonSerializer.Serialize(@event);
var message = new OutboxMessage(
Guid.NewGuid(),
eventType,
1,
payload,
correlationId.ToString(),
clock.UtcNow,
ContentHasher.Sha256(payload));
await using var connection = await connectionFactory.OpenAsync(ct);
await using var transaction = await connection.BeginTransactionAsync(ct);
await outbox.AddAsync(connection, transaction, message, ct);
await transaction.CommitAsync(ct);
}
}