Ejemplo n.º 1
0
        public async Task <Either <ActionResult, ReleaseViewModel> > UpdateRelease(
            Guid releaseId, UpdateReleaseViewModel request)
        {
            return(await _persistenceHelper
                   .CheckEntityExists <Release>(releaseId, ReleaseChecklistService.HydrateReleaseForChecklist)
                   .OnSuccess(_userService.CheckCanUpdateRelease)
                   .OnSuccessDo(release => _userService.CheckCanUpdateReleaseStatus(release, request.Status))
                   .OnSuccessDo(async release => await ValidateReleaseSlugUniqueToPublication(request.Slug, release.PublicationId, releaseId))
                   .OnSuccess(async release =>
            {
                if (request.Status != ReleaseStatus.Approved && release.Published.HasValue)
                {
                    return ValidationActionResult(PublishedReleaseCannotBeUnapproved);
                }

                if (request.Status == ReleaseStatus.Approved &&
                    request.PublishMethod == PublishMethod.Scheduled &&
                    !request.PublishScheduledDate.HasValue)
                {
                    return ValidationActionResult(ApprovedReleaseMustHavePublishScheduledDate);
                }

                release.Slug = request.Slug;
                release.TypeId = request.TypeId;
                release.ReleaseName = request.ReleaseName;
                release.TimePeriodCoverage = request.TimePeriodCoverage;
                release.PreReleaseAccessList = request.PreReleaseAccessList;

                var oldStatus = release.Status;

                release.Status = request.Status;
                release.InternalReleaseNote = request.InternalReleaseNote;
                release.NextReleaseDate = request.NextReleaseDate;

                release.PublishScheduled = request.PublishMethod == PublishMethod.Immediate &&
                                           request.Status == ReleaseStatus.Approved
                        ? DateTime.UtcNow
                        : request.PublishScheduledDate;

                return await ValidateReleaseWithChecklist(release)
                .OnSuccess(async() =>
                {
                    _context.Releases.Update(release);
                    await _context.SaveChangesAsync();

                    // Only need to inform Publisher if changing release status to or from Approved
                    if (oldStatus == ReleaseStatus.Approved || request.Status == ReleaseStatus.Approved)
                    {
                        await _publishingService.ReleaseChanged(
                            releaseId,
                            request.PublishMethod == PublishMethod.Immediate
                            );
                    }

                    _context.Update(release);
                    await _context.SaveChangesAsync();

                    return await GetRelease(releaseId);
                });
            }));
        }
        public async Task <Either <ActionResult, Unit> > CreateReleaseStatus(
            Guid releaseId, ReleaseStatusCreateViewModel request)
        {
            return(await _persistenceHelper
                   .CheckEntityExists <Release>(releaseId, ReleaseChecklistService.HydrateReleaseForChecklist)
                   .OnSuccess(_userService.CheckCanUpdateRelease)
                   .OnSuccessDo(release => _userService.CheckCanUpdateReleaseStatus(release, request.ApprovalStatus))
                   .OnSuccess(async release =>
            {
                if (request.ApprovalStatus != ReleaseApprovalStatus.Approved && release.Published.HasValue)
                {
                    return ValidationActionResult(PublishedReleaseCannotBeUnapproved);
                }

                if (request.ApprovalStatus == ReleaseApprovalStatus.Approved &&
                    request.PublishMethod == PublishMethod.Scheduled &&
                    !request.PublishScheduledDate.HasValue)
                {
                    return ValidationActionResult(ApprovedReleaseMustHavePublishScheduledDate);
                }

                var oldStatus = release.ApprovalStatus;

                release.ApprovalStatus = request.ApprovalStatus;
                release.NextReleaseDate = request.NextReleaseDate;
                release.PublishScheduled = request.PublishMethod == PublishMethod.Immediate &&
                                           request.ApprovalStatus == ReleaseApprovalStatus.Approved
                        ? DateTime.UtcNow
                        : request.PublishScheduledDate;

                // NOTE: Subscribers should be notified if the release is approved and isn't amended,
                //       OR if the release is an amendment, is approved, and NotifySubscribers is true
                var notifySubscribers = request.ApprovalStatus == ReleaseApprovalStatus.Approved &&
                                        (!release.Amendment || request.NotifySubscribers.HasValue && request.NotifySubscribers.Value);

                var releaseStatus = new ReleaseStatus
                {
                    Release = release,
                    InternalReleaseNote = request.LatestInternalReleaseNote,
                    NotifySubscribers = notifySubscribers,
                    ApprovalStatus = request.ApprovalStatus,
                    Created = DateTime.UtcNow,
                    CreatedById = _userService.GetUserId()
                };

                return await ValidateReleaseWithChecklist(release)
                .OnSuccessDo(() => RemoveUnusedImages(release.Id))
                .OnSuccessVoid(async() =>
                {
                    _context.Releases.Update(release);
                    await _context.AddAsync(releaseStatus);
                    await _context.SaveChangesAsync();

                    // Only need to inform Publisher if changing release approval status to or from Approved
                    if (oldStatus == ReleaseApprovalStatus.Approved ||
                        request.ApprovalStatus == ReleaseApprovalStatus.Approved)
                    {
                        await _publishingService.ReleaseChanged(
                            releaseId,
                            releaseStatus.Id,
                            request.PublishMethod == PublishMethod.Immediate
                            );
                    }

                    _context.Update(release);
                    await _context.SaveChangesAsync();

                    if (request.ApprovalStatus == ReleaseApprovalStatus.Approved)
                    {
                        await _preReleaseUserService.SendPreReleaseUserInviteEmails(release.Id);
                    }
                });
            }));
        }