public async Task <ActionResult <CollectionViewModel <PhysicalDocumentTypeDto> > > GetDocumentTypes()
        {
            var documentTypes = (await _documentTypeRepository.GetGeneratedDocumentTypesAsync()).ToList();

            var contractAdviceType  = documentTypes.FirstOrDefault(t => t.PhysicalDocumentTypeId == (int)PhysicalDocumentType.ContractAdvice);
            var authorizationResult = await _authorizationService.AuthorizeAsync(User, Policies.GenerateContractAdvicePolicy);

            if (!authorizationResult.Succeeded)
            {
                documentTypes.Remove(contractAdviceType);
            }

            var response = new CollectionViewModel <PhysicalDocumentTypeDto>(documentTypes);

            return(Ok(response));
        }
        public async Task <IEnumerable <DocumentTemplate> > GetTemplates(PhysicalDocumentType physicalDocumentType, string company, bool recursive, string module = "")
        {
            var documentTypes = await _documentTypeRepository.GetGeneratedDocumentTypesAsync();

            var documentType = documentTypes.First(t => t.PhysicalDocumentTypeId == (int)physicalDocumentType);

            // Retrieve data from cache
            string cacheKey   = $"SSRS_Templates_{company}_{documentType.PhysicalDocumentTypeId}_{recursive}_{module}";
            var    cachedJson = await _distributedCache.GetStringAsync(cacheKey);

            if (cachedJson != null)
            {
                var cachedDocumentTemplates = JsonConvert.DeserializeObject <List <DocumentTemplate> >(cachedJson);

                return(cachedDocumentTemplates);
            }

            try
            {
                var paths = documentType.TemplatesPaths.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

                List <ReportService2010.CatalogItem> catalogItems = new List <ReportService2010.CatalogItem>();

                foreach (var path in paths)
                {
                    var templatesPath = path.Replace("{company}", company);

                    if (physicalDocumentType == PhysicalDocumentType.CustomReport && module != "module")
                    {
                        templatesPath = path + @"/" + company + @"/" + module;
                    }

                    var companyCatalogItems = await ListChildren(templatesPath, true);

                    catalogItems.AddRange(companyCatalogItems);

                    if (physicalDocumentType == PhysicalDocumentType.CustomReport)
                    {
                        catalogItems.RemoveAll(catalog => catalog.Path.ToLower().Contains(@"/archive/"));
                    }
                }

                var documentTemplates = catalogItems
                                        .Where(i => i.TypeName == "Report" && !i.Hidden)
                                        //&& (i.Name.StartsWith(company, StringComparison.InvariantCultureIgnoreCase) || i.Name.StartsWith("cm", StringComparison.InvariantCultureIgnoreCase))
                                        //&& i.Name.IndexOf(keyword, StringComparison.InvariantCultureIgnoreCase) >= 0)
                                        .Select(i => new DocumentTemplate
                {
                    Id               = i.ID,
                    Name             = i.Name,
                    Path             = i.Path,
                    Description      = i.Description,
                    CreatedDateTime  = i.CreationDate,
                    CreatedBy        = i.CreatedBy,
                    ModifiedDateTime = i.ModifiedDate,
                    ModifiedBy       = i.ModifiedBy
                }).ToList();

                // Save the data in cache
                var jsonToCache = JsonConvert.SerializeObject(documentTemplates);
                await _distributedCache.SetStringAsync(cacheKey, jsonToCache, new DistributedCacheEntryOptions
                {
                    AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10) // TODO: use settings ?
                });

                return(documentTemplates.OrderBy(doc => doc.Name));
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Error retrieving templates from SSRS.");

                return(Enumerable.Empty <DocumentTemplate>());
            }
        }