use std::sync::Arc;
use aion::Engine;
use aion_store::PackageRecord;
use super::document::read_document;
use super::list::project_versions;
use super::types::{DeployedDocument, DeployedError, DeployedVersion};
pub async fn versions(engine: &Engine) -> Result<Vec<DeployedVersion>, DeployedError> {
let catalog = engine
.list_workflow_versions()
.map_err(|error| DeployedError::Catalog(error.to_string()))?;
let archives = persisted_archives(engine).await?;
tokio::task::spawn_blocking(move || project_versions(catalog, &archives))
.await
.map_err(|error| DeployedError::Catalog(format!("deployed listing task failed: {error}")))
}
pub async fn document(
engine: &Engine,
workflow_type: &str,
content_hash: &str,
) -> Result<DeployedDocument, DeployedError> {
let archives = persisted_archives(engine).await?;
let workflow_type = workflow_type.to_owned();
let content_hash = content_hash.to_owned();
tokio::task::spawn_blocking(move || read_document(&archives, &workflow_type, &content_hash))
.await
.map_err(|error| {
DeployedError::Catalog(format!("deployed document task failed: {error}"))
})?
}
async fn persisted_archives(engine: &Engine) -> Result<Vec<PackageRecord>, DeployedError> {
let store: Arc<dyn aion_store::EventStore> = engine.store();
store
.list_packages()
.await
.map_err(|error| DeployedError::Catalog(error.to_string()))
}