Przeglądaj źródła

Handle DeliveredQuantity==0

Andrew Klopper 8 lat temu
rodzic
commit
dd31badacc

+ 25 - 14
BulkPrintingAPI/Controllers/BatchesController.cs

@@ -101,15 +101,23 @@ namespace BulkPrintingAPI.Controllers
101 101
 
102 102
             if (!batch.ReadyForDownload)
103 103
             {
104
-                try
104
+                if (batch.DeliveredQuantity == 0)
105 105
                 {
106
-                    await Utils.DownloadVouchersAsync(_sftpOptions, _context, _logger, batch);
106
+                    batch.ReadyForDownload = true;
107
+                    await _context.SaveChangesAsync();
107 108
                 }
108
-                catch (Exception e)
109
+                else
109 110
                 {
110
-                    _logger.LogError(string.Format(
111
-                        "Failed to download vouchers for {0} batchId={1}: {2}",
112
-                        credentials.ToString(), batch.Id, e.Message));
111
+                    try
112
+                    {
113
+                        await Utils.DownloadVouchersAsync(_sftpOptions, _context, _logger, batch);
114
+                    }
115
+                    catch (Exception e)
116
+                    {
117
+                        _logger.LogError(string.Format(
118
+                            "Failed to download vouchers for {0} batchId={1}: {2}",
119
+                            credentials.ToString(), batch.Id, e.Message));
120
+                    }
113 121
                 }
114 122
             }
115 123
 
@@ -154,15 +162,18 @@ namespace BulkPrintingAPI.Controllers
154 162
 
155 163
             _logger.LogDebug("Saved batchId={0} for {1}", orderResponse.Batch.Id, credentials.ToString());
156 164
 
157
-            try
165
+            if (!orderResponse.Batch.ReadyForDownload)
158 166
             {
159
-                await Utils.DownloadVouchersAsync(_sftpOptions, _context, _logger, orderResponse.Batch);
160
-            }
161
-            catch (Exception e)
162
-            {
163
-                _logger.LogError(string.Format(
164
-                    "Failed to download vouchers for {0} batchId={1}: {2}",
165
-                    credentials.ToString(), orderResponse.Batch.Id, e.Message));
167
+                try
168
+                {
169
+                    await Utils.DownloadVouchersAsync(_sftpOptions, _context, _logger, orderResponse.Batch);
170
+                }
171
+                catch (Exception e)
172
+                {
173
+                    _logger.LogError(string.Format(
174
+                        "Failed to download vouchers for {0} batchId={1}: {2}",
175
+                        credentials.ToString(), orderResponse.Batch.Id, e.Message));
176
+                }
166 177
             }
167 178
 
168 179
             return Ok(orderResponse);

+ 3 - 0
BulkPrintingAPI/Controllers/Utils.cs

@@ -313,6 +313,9 @@ namespace BulkPrintingAPI.Controllers
313 313
         public static async Task DownloadVouchersAsync(SFTPOptions sftpOptions, MAXContext context,
314 314
             ILogger logger, Batch batch)
315 315
         {
316
+            if (batch.ReadyForDownload)
317
+                return;
318
+
316 319
             var remoteFileName = string.Format("{0}_{1}.dat", batch.Account.Id, batch.Id);
317 320
             using (var voucherStream = new MemoryStream())
318 321
             {

+ 4 - 2
MAXClient/Client.cs

@@ -442,6 +442,8 @@ namespace MAX
442 442
             );
443 443
 
444 444
             var parts = response.Split('|');
445
+            var deliveredQuantity = ParseInt(parts[4], "Batch.DeliveredQuantity(4)", response);
446
+
445 447
             return new OrderResponse()
446 448
             {
447 449
                 Batch = new Batch()
@@ -449,7 +451,7 @@ namespace MAX
449 451
                     Id = ParseInt(parts[1], "Batch.Id(1)", response),
450 452
                     OrderReference = parts[2],
451 453
                     RequestedQuantity = ParseInt(parts[3], "Batch.RequestQuantity(3)", response),
452
-                    DeliveredQuantity = ParseInt(parts[4], "Batch.DeliveredQuantity(4)", response),
454
+                    DeliveredQuantity = deliveredQuantity,
453 455
                     Cost = ParseDecimal(parts[5], "Batch.Cost(5)", response),
454 456
                     InternalReference = internalReference,
455 457
                     OrderGuid = orderGuid,
@@ -464,7 +466,7 @@ namespace MAX
464 466
                     NetworkName = product.Network.Name,
465 467
                     OrderDate = DateTimeOffset.UtcNow,
466 468
                     OrderedById = _userId,
467
-                    ReadyForDownload = false
469
+                    ReadyForDownload = deliveredQuantity == 0
468 470
                 },
469 471
                 RemainingBalance = ParseDecimal(parts[6], "Batch.RemainingBalance(6)", response)
470 472
             };