Example #1
0
        public async Task <bool> HandleAsync(CreateBulkPrenotification message)
        {
            using (var transaction = context.Database.BeginTransaction())
            {
                try
                {
                    var draftMovements = await draftMovementRepository.GetDraftMovementById(message.DraftBulkUploadId);

                    var notification = await notificationRepository.GetById(message.NotificationId);

                    foreach (var draftMovement in draftMovements)
                    {
                        var movement = movementFactory.Create(message.NotificationId, draftMovement.ShipmentNumber,
                                                              draftMovement.Date.Value);

                        context.Movements.Add(movement);

                        await context.SaveChangesAsync();

                        await SaveMovementDetails(movement, draftMovement, notification);

                        await
                        SaveSupportingDocument(movement, draftMovement, message.FileExtension,
                                               message.SupportingDocument);

                        await SavePrenotifiedAudit(movement);
                    }

                    await draftMovementRepository.DeleteDraftMovementByNotificationId(message.NotificationId);
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
                transaction.Commit();
            }

            return(true);
        }
        public async Task <bool> HandleAsync(CreateReceiptRecovery message)
        {
            using (var transaction = context.Database.BeginTransaction())
            {
                try
                {
                    var draftMovements =
                        (await draftMovementRepository.GetDraftMovementById(message.DraftBulkUploadId)).ToList();

                    if (!draftMovements.Any())
                    {
                        return(false);
                    }

                    var notificationType = (await notificationRepository.GetById(message.NotificationId)).NotificationType;

                    foreach (var draftMovement in draftMovements)
                    {
                        var movement = await movementRepository.GetByNumberOrDefault(draftMovement.ShipmentNumber,
                                                                                     message.NotificationId);

                        var auditDate = SystemTime.Now;

                        if (draftMovement.ReceivedDate.HasValue &&
                            draftMovement.ReceivedDate.Value != DateTime.MinValue)
                        {
                            var fileId =
                                await
                                SaveSupportingDocument(movement, receiptNameGenerator, message.SupportingDocument, message.FileExtension);

                            movement.Receive(fileId, draftMovement.ReceivedDate.GetValueOrDefault(),
                                             new Domain.ShipmentQuantity(draftMovement.Quantity, draftMovement.Units),
                                             userContext.UserId);

                            await context.SaveChangesAsync();

                            await SaveReceivedAudit(movement, auditDate);
                        }

                        if (draftMovement.RecoveredDisposedDate.HasValue &&
                            draftMovement.RecoveredDisposedDate.Value != DateTime.MinValue)
                        {
                            var fileId =
                                await
                                SaveSupportingDocument(movement, recoveryNameGenerator, message.SupportingDocument,
                                                       message.FileExtension);

                            movement.Complete(draftMovement.RecoveredDisposedDate.Value, fileId, userContext.UserId);

                            await context.SaveChangesAsync();

                            await
                            SaveRecoveredDisposedAudit(movement, notificationType,
                                                       auditDate.AddSeconds(AuditTimeOffSet));
                        }
                    }

                    await draftMovementRepository.DeleteDraftMovementByNotificationId(message.NotificationId);
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
                transaction.Commit();
            }

            return(true);
        }