private List<CommandEnvelope> GetUnDeliveredEnvelopesFromThePastByDestinationCostCentreApplicationId(Guid costCentreApplicationId,
            Guid costCentreId, int batchSize, bool includeArchived)
        {
            var tracker = _CommandEnvelopeRoutingTrackerCollection.AsQueryable().FirstOrDefault(s => s.Id == costCentreApplicationId);
            if (tracker == null)
            {
                tracker = new CommandEnvelopeRoutingTracker
               {
                   Id = costCentreApplicationId,
                   EnvelopeArrivalAtServerTick = 0,
                   EnvelopeId = Guid.NewGuid()
               };
                _CommandEnvelopeRoutingTrackerCollection.Save(tracker);
            }
            List<Guid> undeliveredIds;

            var allDeliveredIds = _commandEnvelopeRoutingStatusCollection
                .AsQueryable()
                .Where(n => n.DestinationCostCentreApplicationId == costCentreApplicationId)
                .Where(n => n.DocumentType != DocumentType.InventoryTransferNote.ToString())
                .Where(n => n.EnvelopeArrivalAtServerTick > tracker.EnvelopeArrivalAtServerTick)
                .Select(s => s.EnvelopeId).Distinct().ToList();
            var allRoutedIds = _commandEnvelopeRouteOnRequestCostcentreCollection.AsQueryable()
                  .Where(s => s.CostCentreId == costCentreId && s.IsValid && s.DocumentType != DocumentType.InventoryTransferNote.ToString())
                  .Where(s => s.GeneratedByCostCentreApplicationId != costCentreApplicationId)
                  .Where(n => n.EnvelopeArrivalAtServerTick > tracker.EnvelopeArrivalAtServerTick)
                  .Select(s => s.EnvelopeId).Distinct().ToList();
            undeliveredIds = allRoutedIds.Except(allDeliveredIds).ToList();


            var undeliveredEnvelopeIds =
             _commandEnvelopeRouteOnRequestCostcentreCollection.AsQueryable()
                 .Where(s => s.GeneratedByCostCentreApplicationId != costCentreApplicationId)
                 .Where(s => undeliveredIds.Contains(s.EnvelopeId))
                 .OrderBy(n => n.EnvelopeArrivalAtServerTick)
                 .Select(s => s.EnvelopeId).ToList().Distinct().ToList()
                 .Take(batchSize)
                 .ToList();



            var envelopes =
                _commandEnvelopeProcessingAuditCollection.AsQueryable()
                    .Where(s => s.GeneratedByCostCentreApplicationId != costCentreApplicationId)
                    .Where(s => undeliveredEnvelopeIds.Contains(s.Id)).OrderBy(s => s.EnvelopeArrivalAtServerTick);
            var envelopeToSend = new List<CommandEnvelope>();
            foreach (var commandEnvelopeProcessingAudit in envelopes)
            {
                var envelope =
                    JsonConvert.DeserializeObject<CommandEnvelope>(commandEnvelopeProcessingAudit.JsonEnvelope);
                envelopeToSend.Add(envelope);
            }

            return envelopeToSend;
        }
        public void MarkEnvelopesAsDelivered(List<Guid> envelopesIdList, Guid costCentreApplicationId, Guid costCentreId)
        {
            var envelopes =
                _commandEnvelopeProcessingAuditCollection.AsQueryable()
                    .Where(s => envelopesIdList.Contains(s.Id))
                    .ToList()
                    .Select(n => new { Id = n.Id, ArrivalTick = n.EnvelopeArrivalAtServerTick, n.DocumentType })
                    .OrderBy(n => n.ArrivalTick);
            foreach (var envelope in envelopes)
            {
                var alreadyMarked = _commandEnvelopeRoutingStatusCollection.AsQueryable()
                    .Any(
                        s =>
                            s.EnvelopeId == envelope.Id &&
                            s.DestinationCostCentreApplicationId == costCentreApplicationId);
                if (!alreadyMarked)
                {
                    bool isReconcile =
                        _commandEnvelopeRoutingStatusCollection.AsQueryable().
                        Where(s => s.DestinationCostCentreApplicationId == costCentreApplicationId)
                            .Any(s => s.EnvelopeArrivalAtServerTick > envelope.ArrivalTick);
                    if (isReconcile)
                    {
                        var tracker = _CommandEnvelopeRoutingTrackerCollection.AsQueryable().FirstOrDefault(s => s.Id == costCentreApplicationId);
                        if (tracker == null)
                        {
                            tracker = new CommandEnvelopeRoutingTracker
                            {
                                Id = costCentreApplicationId,
                                EnvelopeArrivalAtServerTick = envelope.ArrivalTick,
                                EnvelopeId = envelope.Id
                            };
                            _CommandEnvelopeRoutingTrackerCollection.Save(tracker);
                        }
                        else
                        {
                            tracker = new CommandEnvelopeRoutingTracker
                            {
                                Id = costCentreApplicationId,
                                EnvelopeArrivalAtServerTick = envelope.ArrivalTick,
                                EnvelopeId = envelope.Id
                            };
                            _CommandEnvelopeRoutingTrackerCollection.Save(tracker);
                        }

                    }
                    _commandEnvelopeRoutingStatusCollection.Save(new CommandEnvelopeRoutingStatus
                    {
                        DateAdded = DateTime.Now,
                        DateDelivered = DateTime.Now,
                        DateExecuted = DateTime.Now,
                        Delivered = true,
                        DestinationCostCentreApplicationId =
                            costCentreApplicationId,
                        EnvelopeDeliveredAtServerTick = isReconcile ? envelope.ArrivalTick : DateTime.Now.Ticks,
                        EnvelopeArrivalAtServerTick = envelope.ArrivalTick,
                        EnvelopeId = envelope.Id,
                        Id = Guid.NewGuid(),
                        CostCentreId = costCentreId,
                        DocumentType = envelope.DocumentType.ToString(),
                        IsReconcile = isReconcile

                    });
                }
            }
        }