public async Task <Either <ActionResult, MethodologyVersion> > UpdateApprovalStatus( Guid methodologyVersionId, MethodologyApprovalUpdateRequest request) { return(await _persistenceHelper .CheckEntityExists <MethodologyVersion>(methodologyVersionId, q => q.Include(m => m.Methodology)) .OnSuccess(methodology => UpdateStatus(methodology, request))); }
private async Task <Either <ActionResult, MethodologyVersion> > UpdateStatus( MethodologyVersion methodologyVersionToUpdate, MethodologyApprovalUpdateRequest request) { if (!request.IsStatusUpdateForMethodology(methodologyVersionToUpdate)) { return(methodologyVersionToUpdate); } return(await _methodologyApprovalService .UpdateApprovalStatus(methodologyVersionToUpdate.Id, request) .OnSuccess(_ => _context .MethodologyVersions .Include(mv => mv.Methodology) .SingleAsync(mv => mv.Id == methodologyVersionToUpdate.Id))); }
private async Task <Either <ActionResult, MethodologyVersion> > UpdateStatus( MethodologyVersion methodologyVersionToUpdate, MethodologyApprovalUpdateRequest request) { if (!request.IsStatusUpdateForMethodology(methodologyVersionToUpdate)) { // Status unchanged return(methodologyVersionToUpdate); } return(await CheckCanUpdateStatus(methodologyVersionToUpdate, request.Status) .OnSuccessDo(methodology => CheckMethodologyCanDependOnRelease(methodology, request)) .OnSuccessDo(RemoveUnusedImages) .OnSuccess(async methodology => { methodology.Status = request.Status; methodology.PublishingStrategy = request.PublishingStrategy; methodology.ScheduledWithReleaseId = WithRelease == request.PublishingStrategy ? request.WithReleaseId : null; methodology.InternalReleaseNote = Approved == request.Status ? request.LatestInternalReleaseNote : null; methodology.Updated = DateTime.UtcNow; _context.MethodologyVersions.Update(methodology); if (await _methodologyVersionRepository.IsPubliclyAccessible(methodology.Id)) { methodology.Published = DateTime.UtcNow; await _publishingService.PublishMethodologyFiles(methodology.Id); // Invalidate the 'All Methodologies' cache item await _publicBlobCacheService.DeleteItem(new AllMethodologiesCacheKey()); } _context.MethodologyVersions.Update(methodology); await _context.SaveChangesAsync(); return methodology; })); }
private async Task <Either <ActionResult, Unit> > CheckMethodologyCanDependOnRelease( MethodologyVersion methodologyVersion, MethodologyApprovalUpdateRequest request) { if (request.PublishingStrategy != WithRelease) { return(Unit.Instance); } if (!request.WithReleaseId.HasValue) { return(new NotFoundResult()); } // Check that this release exists, that it's not already published, and that it's using the methodology return(await _persistenceHelper .CheckEntityExists <Release>(request.WithReleaseId.Value) .OnSuccess <ActionResult, Release, Unit>(async release => { if (release.Live) { return ValidationActionResult(MethodologyCannotDependOnPublishedRelease); } await _context.Entry(methodologyVersion) .Reference(m => m.Methodology) .LoadAsync(); await _context.Entry(methodologyVersion.Methodology) .Collection(mp => mp.Publications) .LoadAsync(); var publicationIds = methodologyVersion.Methodology.Publications .Select(pm => pm.PublicationId) .ToList(); if (!publicationIds.Contains(release.PublicationId)) { return ValidationActionResult(MethodologyCannotDependOnRelease); } return Unit.Instance; })); }
public async Task UpdateApprovalStatus_MethodologyHasImages() { var methodologyVersion = new MethodologyVersion { Methodology = new Methodology { OwningPublicationTitle = "Publication title", Publications = ListOf(new PublicationMethodology { Owner = true, Publication = new Publication() }) } }; var imageFile1 = new MethodologyFile { MethodologyVersion = methodologyVersion, File = new File { RootPath = Guid.NewGuid(), Filename = "image1.png", Type = FileType.Image } }; var imageFile2 = new MethodologyFile { MethodologyVersion = methodologyVersion, File = new File { RootPath = Guid.NewGuid(), Filename = "image2.png", Type = FileType.Image } }; var request = new MethodologyApprovalUpdateRequest { LatestInternalReleaseNote = "Test approval", PublishingStrategy = Immediately, Status = Approved, }; var contentDbContextId = Guid.NewGuid().ToString(); await using (var context = InMemoryApplicationDbContext(contentDbContextId)) { await context.MethodologyVersions.AddAsync(methodologyVersion); await context.MethodologyFiles.AddRangeAsync(imageFile1, imageFile2); await context.SaveChangesAsync(); } var contentService = new Mock <IMethodologyContentService>(Strict); var methodologyVersionRepository = new Mock <IMethodologyVersionRepository>(Strict); contentService.Setup(mock => mock.GetContentBlocks <HtmlBlock>(methodologyVersion.Id)) .ReturnsAsync(new List <HtmlBlock> { new() { Body = $@" <img src=""/api/methodologies/{{methodologyId}}/images/{imageFile1.File.Id}""/> <img src=""/api/methodologies/{{methodologyId}}/images/{imageFile2.File.Id}""/>" } });