mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 07:13:12 +00:00
50 lines
1.7 KiB
C#
50 lines
1.7 KiB
C#
using Api.SeaHavenIndustries.Filters;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using SeaHaven.Services.DTOs;
|
|
using SeaHaven.Services.Interfaces;
|
|
|
|
namespace Api.SeaHavenIndustries.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("api/workorders")]
|
|
public class WorkOrderIngestController : ControllerBase
|
|
{
|
|
private const int MaxBatchSize = 100;
|
|
private readonly IWorkOrderIngestService _ingestService;
|
|
|
|
public WorkOrderIngestController(IWorkOrderIngestService ingestService)
|
|
{
|
|
_ingestService = ingestService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Idempotent upsert by ExternalWorkOrderId (Lambda cutover target).
|
|
/// Auth: X-Ingest-Key header.
|
|
/// </summary>
|
|
[HttpPost("ingest")]
|
|
[IngestApiKey]
|
|
[ProducesResponseType(typeof(WorkOrderIngestBatchResultDto), StatusCodes.Status200OK)]
|
|
public async Task<ActionResult<WorkOrderIngestBatchResultDto>> Ingest(
|
|
[FromBody] WorkOrderIngestBatchRequestDto? batch,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var items = NormalizeItems(batch);
|
|
if (items.Count == 0)
|
|
return BadRequest("At least one item with externalWorkOrderId is required.");
|
|
|
|
if (items.Count > MaxBatchSize)
|
|
return BadRequest($"Max {MaxBatchSize} items per request.");
|
|
|
|
var result = await _ingestService.UpsertBatchAsync(items, cancellationToken);
|
|
return Ok(result);
|
|
}
|
|
|
|
private static List<WorkOrderIngestPayloadDto> NormalizeItems(WorkOrderIngestBatchRequestDto? batch)
|
|
{
|
|
if (batch?.Items is { Count: > 0 })
|
|
return batch.Items;
|
|
|
|
return new List<WorkOrderIngestPayloadDto>();
|
|
}
|
|
}
|
|
}
|