using Microsoft.AspNetCore.Mvc; using eprintServer.Services; using eprintServer.Requests; using Microsoft.AspNetCore.Authorization; namespace eprintServer.Controllers { [ApiController] [Route("api/[controller]")] [Authorize(Roles = "ADMIN,SUB_ADMIN")] public class AsyadController : ControllerBase { private readonly AsyadExpressService _asyadService; private readonly OrderService _orderService; public AsyadController(AsyadExpressService asyadService, OrderService orderService) { _asyadService = asyadService; _orderService = orderService; } [HttpPost("login")] public async Task Login([FromQuery] string username, [FromQuery] string password) { try { var result = await _asyadService.LoginAsync(username, password); return Content(result, "application/json"); } catch (Exception ex) { return StatusCode(500, new { error = ex.Message }); } } [HttpPost("create-asyad-shipment")] public async Task CreateShipment([FromBody] CreateAsyadShipmentRequest request) { // Check if token is null or empty if (string.IsNullOrEmpty(request.AsyadToken)) { // Return 406 Not Acceptable with your specific message return StatusCode(406, new { message = "token must not be null" }); } try { // Pass the token from the request object to the service var response = await _asyadService.ProcessAsyadShipmentAsync(request, request.AsyadToken); return Content(response, "application/json"); } catch (Exception ex) { return StatusCode(500, new { message = ex.Message }); } } [HttpGet("asyad-me")] public async Task GetAsyadMe([FromQuery] string token) { if (string.IsNullOrEmpty(token)) { return StatusCode(406, new { message = "token must not be null" }); } try { var jsonResponse = await _asyadService.GetAsyadProfileAsync(token); // We return the raw content from Asyad return Content(jsonResponse, "application/json"); } catch (Exception ex) { return StatusCode(500, new { message = ex.Message }); } } [HttpPost("bulk-asyad-shipment")] public async Task CreateBulkShipment([FromBody] List requests) { if (requests == null || !requests.Any()) { return BadRequest("The request list cannot be empty."); } // Validate tokens for all requests if (requests.Any(r => string.IsNullOrEmpty(r.AsyadToken))) { return StatusCode(406, new { message = "token must not be null" }); } // Generate a unique Batch ID for this specific group of shipments string sharedBatchId = $"BATCH_{DateTime.UtcNow:yyyyMMdd_HHmm}"; var executionResults = new List(); foreach (var shipmentReq in requests) { try { // Pass the shared batch ID to group them in the database var result = await _asyadService.ProcessAsyadShipmentAsync(shipmentReq, shipmentReq.AsyadToken, sharedBatchId); executionResults.Add(new { OrderId = shipmentReq.OrderId, Success = true, Response = System.Text.Json.JsonSerializer.Deserialize(result) }); } catch (Exception ex) { executionResults.Add(new { OrderId = shipmentReq.OrderId, Success = false, Error = ex.Message }); } } return Ok(new { BatchId = sharedBatchId, TotalProcessed = requests.Count, Results = executionResults }); } } }