Andrew Klopper лет назад: 8
Родитель
Сommit
dd31badacc
3 измененных файлов с 32 добавлено и 16 удалено
  1. 25 14
      BulkPrintingAPI/Controllers/BatchesController.cs
  2. 3 0
      BulkPrintingAPI/Controllers/Utils.cs
  3. 4 2
      MAXClient/Client.cs

+ 25 - 14
BulkPrintingAPI/Controllers/BatchesController.cs

101
 
101
 
102
             if (!batch.ReadyForDownload)
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
 
162
 
155
             _logger.LogDebug("Saved batchId={0} for {1}", orderResponse.Batch.Id, credentials.ToString());
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
             return Ok(orderResponse);
179
             return Ok(orderResponse);

+ 3 - 0
BulkPrintingAPI/Controllers/Utils.cs

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

+ 4 - 2
MAXClient/Client.cs

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