Browse Source

Improve error checking on voucher downloads.

Andrew Klopper 8 years ago
parent
commit
bf5a6f7392
1 changed files with 18 additions and 3 deletions
  1. 18 3
      BulkPrintingAPI/Controllers/Utils.cs

+ 18 - 3
BulkPrintingAPI/Controllers/Utils.cs

@@ -336,13 +336,21 @@ namespace BulkPrintingAPI.Controllers
336 336
                 voucherStream.Position = 0;
337 337
                 using (var streamReader = new StreamReader(voucherStream))
338 338
                 {
339
+                    int voucherCount = 0;
339 340
                     while (streamReader.Peek() >= 0)
340 341
                     {
341 342
                         var line = streamReader.ReadLine();
342 343
                         var parts = line.Split('|');
343 344
 
344
-                        VoucherSanityCheck(batch, decimal.Parse(parts[4]), int.Parse(parts[5]),
345
-                            parts[7]);
345
+                        VoucherSanityCheck(batch, decimal.Parse(parts[4]), int.Parse(parts[5]), parts[7]);
346
+
347
+                        // Duplicate sequence numbers are taken care of by the unique index on the vouchers table.
348
+                        int sequenceNumber = int.Parse(parts[6]);
349
+                        if ((sequenceNumber < 1) || (sequenceNumber > batch.DeliveredQuantity))
350
+                        {
351
+                            throw new Exception(String.Format("Voucher sequence number out of range in batch {0}: {1}",
352
+                                batch.Id, sequenceNumber));
353
+                        }
346 354
 
347 355
                         context.Add(new Voucher()
348 356
                         {
@@ -350,9 +358,16 @@ namespace BulkPrintingAPI.Controllers
350 358
                             ExpiryDate = DateTime.Parse(parts[1]),
351 359
                             Serial = parts[2],
352 360
                             EncryptedPIN = parts[3],
353
-                            SequenceNumber = int.Parse(parts[6]),
361
+                            SequenceNumber = sequenceNumber,
354 362
                             Batch = batch
355 363
                         });
364
+                        voucherCount++;
365
+                    }
366
+
367
+                    if (voucherCount != batch.DeliveredQuantity)
368
+                    {
369
+                        throw new Exception(String.Format("Voucher count mismatch for batch {0}: expected={1}, got={2}",
370
+                            batch.Id, batch.DeliveredQuantity, voucherCount));
356 371
                     }
357 372
                 }
358 373