private async Task <TwinServiceModel> GetPreviousFirmwareReportedProperties(string tenantId, string deviceId)
        {
            var sql = QueryBuilder.GetDeploymentDeviceDocumentsSqlByKey("CollectionId", $"deviceDeploymentHistory-{deviceId}");

            List <Document> docs = new List <Document>();

            try
            {
                CosmosOperations storageClient = await CosmosOperations.GetClientAsync();

                docs = await storageClient.QueryDocumentsAsync(
                    "pcs-storage",
                    $"pcs-{tenantId}",
                    this.DefaultQueryOptions,
                    sql,
                    0,
                    100);

                var result = docs.Select(doc => new ValueServiceModel(doc));
                if (result != null && result.Count() > 0)
                {
                    var previousDeployment = JsonConvert.DeserializeObject <DeploymentHistoryModel>(result.FirstOrDefault()?.Data);
                    var previousTwin       = previousDeployment.Twin;
                    return(previousTwin != null ? previousTwin : null);
                }

                return(null);
            }
            catch (ResourceNotFoundException e)
            {
                throw new ResourceNotFoundException($"No deployments exist in CosmosDb. The telemetry collection {$"pcs-{tenantId}"} does not exist.", e);
            }
        }
        private async Task <List <DeploymentServiceModel> > GetDeploymentsFromStorage(string tenantId, IEnumerable <string> deploymentIds)
        {
            var sql = QueryBuilder.GetDocumentsSql(
                "deployments",
                null,
                null,
                null,
                "_ts",
                null,
                "_ts",
                "asc",
                "_ts",
                0,
                100,
                deploymentIds.ToArray(),
                "Key");

            FeedOptions queryOptions = new FeedOptions
            {
                EnableCrossPartitionQuery = true,
                EnableScanInQuery         = true,
            };

            List <Document> docs = new List <Document>();

            try
            {
                CosmosOperations storageClient = await CosmosOperations.GetClientAsync();

                docs = await storageClient.QueryDocumentsAsync(
                    "pcs-storage",
                    $"pcs-{tenantId}",
                    queryOptions,
                    sql,
                    0,
                    100);

                var result = docs.Select(doc => new ValueServiceModel(doc));
                var deploymentFromStorage = result.Select(res => this.CreateDeploymentServiceModel(res)).ToList();

                return(deploymentFromStorage);
            }
            catch (ResourceNotFoundException e)
            {
                throw new ResourceNotFoundException($"No deployments exist in CosmosDb. The telemetry collection {$"pcs-{tenantId}"} does not exist.", e);
            }
        }
        public async Task <List <DeploymentServiceModel> > GetDeploymentsByIdFromStorage(string tenantId, string[] deploymentIds)
        {
            var sql = QueryBuilder.GetDocumentsSql(
                "deployments",
                null,
                null,
                null,
                "_ts",
                null,
                "_ts",
                "desc",
                "_ts",
                0,
                100,
                deploymentIds,
                "Key");

            List <Document> docs = new List <Document>();

            try
            {
                CosmosOperations storageClient = await CosmosOperations.GetClientAsync();

                docs = await storageClient.QueryDocumentsAsync(
                    "pcs-storage",
                    $"pcs-{tenantId}",
                    this.DefaultQueryOptions,
                    sql,
                    0,
                    100);

                var result = docs.Select(doc => new ValueServiceModel(doc));
                var deploymentFromStorage = result.Select(res => this.CreateDeploymentServiceModel(res)).ToList();

                return(deploymentFromStorage);
            }
            catch (ResourceNotFoundException e)
            {
                throw new ResourceNotFoundException($"No deployments exist in CosmosDb. The telemetry collection {$"pcs-{tenantId}"} does not exist.", e);
            }
        }
        private async Task <bool> DoesDeploymentTwinsExist(string tenantId, string deploymentId)
        {
            var         sql          = QueryBuilder.GetDeploymentDeviceDocumentsSqlByKey("Key", deploymentId);
            FeedOptions queryOptions = new FeedOptions
            {
                EnableCrossPartitionQuery = true,
                EnableScanInQuery         = true,
            };

            CosmosOperations storageClient = await CosmosOperations.GetClientAsync();

            List <Document> docs = new List <Document>();

            docs = await storageClient.QueryDocumentsAsync(
                "pcs-storage",
                $"pcs-{tenantId}",
                this.DefaultQueryOptions,
                sql,
                0,
                1);

            return(docs != null && docs.Count > 0);
        }