internal static Task <TResult> CreateAsync <TResult>(Guid processStageId, Guid actorId,
                                                             Guid processStageTypeId, string title,
                                                             KeyValuePair <Guid[], Guid>[] confirmableActorIds, Guid[] editableActorIds, Guid[] viewableActorIds,
                                                             Func <TResult> onSuccess,
                                                             Func <TResult> onAlreadyExists)
        {
            return(AzureStorageRepository.Connection(
                       azureStorageRepository =>
            {
                var rollback = new RollbackAsync <TResult>();

                var procStageDoc = new ProcessStageDocument()
                {
                    Owner = actorId,
                    ProcessStageType = processStageTypeId,
                    Title = title,
                };
                procStageDoc.SetConfirmables(confirmableActorIds);
                procStageDoc.SetEditables(editableActorIds);
                procStageDoc.SetViewables(viewableActorIds);
                rollback.AddTaskCreate(processStageId, procStageDoc,
                                       onAlreadyExists, azureStorageRepository);

                rollback.AddTaskCreateOrUpdate <TResult, Documents.ProcessStageActorLookupDocument>(actorId,
                                                                                                    (created, lookupDoc) => lookupDoc.AddLookupDocumentId(processStageId),
                                                                                                    lookupDoc => lookupDoc.RemoveLookupDocumentId(processStageId),
                                                                                                    azureStorageRepository);


                return rollback.ExecuteAsync(onSuccess);
            }));
        }
 internal static ProcessStage Convert(ProcessStageDocument processStageDocument)
 {
     return(new ProcessStage
     {
         processStageId = processStageDocument.Id,
         ownerId = processStageDocument.Owner,
         processStageTypeId = processStageDocument.ProcessStageType,
         title = processStageDocument.Title,
         confirmableIds = processStageDocument.GetConfirmables(),
         editableIds = processStageDocument.GetEditables(),
         completableIds = processStageDocument.GetCompletables(),
         viewableIds = processStageDocument.GetViewables(),
     });
 }