private async Task SaveDeviceProperties(string tenantId, string deploymentId, List <TwinServiceModel> deviceTwins)
        {
            if (deviceTwins != null)
            {
                TwinServiceListModel existingDeviceTwins = await this.GetTwins(tenantId, string.Format(DeploymentDevicePropertiesCollection, deploymentId));

                if (existingDeviceTwins == null || (existingDeviceTwins != null && existingDeviceTwins.Items.Count == 0))
                {
                    await this.SaveDeviceTwins(tenantId, deploymentId, deviceTwins);
                }
                else
                {
                    foreach (var deviceTwin in deviceTwins)
                    {
                        CosmosOperations storageClient = await CosmosOperations.GetClientAsync();

                        var existingDeviceTwin = existingDeviceTwins.Items.FirstOrDefault(x => x.DeviceId == deviceTwin.DeviceId);
                        var value = JsonConvert.SerializeObject(
                            deviceTwin,
                            Formatting.Indented,
                            new JsonSerializerSettings
                        {
                            NullValueHandling = NullValueHandling.Ignore,
                        });

                        try
                        {
                            await storageClient.SaveDocumentAsync(string.Format(DeploymentDevicePropertiesCollection, deploymentId), deviceTwin.DeviceId, new ValueServiceModel()
                            {
                                Data = value, ETag = existingDeviceTwin.ETag
                            }, this.GenerateCollectionLink(tenantId));
                        }
                        catch (Exception)
                        {
                        }

                        // archive exisiting Device Twin
                        var archiveDeviceTwinValue = JsonConvert.SerializeObject(
                            existingDeviceTwin,
                            Formatting.Indented,
                            new JsonSerializerSettings
                        {
                            NullValueHandling = NullValueHandling.Ignore,
                        });
                        await storageClient.SaveDocumentAsync(string.Format(DeploymentHistoryPropertiesCollection, deploymentId, Guid.NewGuid().ToString()), deviceTwin.DeviceId, new ValueServiceModel()
                        {
                            Data = archiveDeviceTwinValue
                        }, this.GenerateCollectionLink(tenantId));
                    }
                }
            }
        }
        private async Task GetModuleTwins(string tenantId, string query, string continuationToken, List <TwinServiceModel> twins)
        {
            TwinServiceListModel moduleTwins = null;

            moduleTwins = await this.GetModuleTwinsByQueryAsync(tenantId, query, null);

            if (moduleTwins != null && moduleTwins.Items.Count() > 0)
            {
                twins.AddRange(moduleTwins.Items);
                if (!string.IsNullOrWhiteSpace(moduleTwins.ContinuationToken))
                {
                    await this.GetModuleTwins(tenantId, query, continuationToken, twins);
                }
            }
        }
        private async Task StoreModuleTwinsInStorage(string tenantId, List <TwinServiceModel> moduleTwins, string deploymentId)
        {
            if (moduleTwins != null && moduleTwins.Count > 0)
            {
                TwinServiceListModel existingModuleTwins = await this.GetTwins(tenantId, string.Format(DeploymentEdgeModulePropertiesCollection, deploymentId));

                if (existingModuleTwins == null || (existingModuleTwins != null && existingModuleTwins.Items.Count == 0))
                {
                    foreach (var moduleTwin in moduleTwins)
                    {
                        await this.SaveModuleTwin(tenantId, deploymentId, moduleTwin);
                    }
                }
                else
                {
                    foreach (var moduleTwin in moduleTwins)
                    {
                        var existingModuleTwin         = existingModuleTwins.Items.FirstOrDefault(x => x.ModuleId == moduleTwin.ModuleId && x.DeviceId == moduleTwin.DeviceId);
                        CosmosOperations storageClient = await CosmosOperations.GetClientAsync();

                        var value = JsonConvert.SerializeObject(
                            moduleTwin,
                            Formatting.Indented,
                            new JsonSerializerSettings
                        {
                            NullValueHandling = NullValueHandling.Ignore,
                        });

                        try
                        {
                            await storageClient.SaveDocumentAsync(string.Format(DeploymentEdgeModulePropertiesCollection, deploymentId), $"{moduleTwin.DeviceId}-{this.RemoveSpecialCharacters(moduleTwin.ModuleId)}", new ValueServiceModel()
                            {
                                Data = value, ETag = existingModuleTwin.ETag
                            }, this.GenerateCollectionLink(tenantId));
                        }
                        catch (Exception)
                        {
                        }

                        // archive exisiting Device Twin
                        var archiveModuleTwinValue = JsonConvert.SerializeObject(
                            existingModuleTwin,
                            Formatting.Indented,
                            new JsonSerializerSettings
                        {
                            NullValueHandling = NullValueHandling.Ignore,
                        });
                        try
                        {
                            await storageClient.SaveDocumentAsync(string.Format(DeploymentModuleHistoryPropertiesCollection, deploymentId, Guid.NewGuid().ToString()), $"{moduleTwin.DeviceId}-{this.RemoveSpecialCharacters(moduleTwin.ModuleId)}", new ValueServiceModel()
                            {
                                Data = archiveModuleTwinValue
                            }, this.GenerateCollectionLink(tenantId));
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
            }
        }