App contract list

Implement retrieve app contracts by UUID, admin, and operator
This commit is contained in:
Noor 2025-05-14 00:09:36 +05:30
parent 1e8014a5d6
commit 38a73fd003
Signed by: noormohammedb
GPG Key ID: D83EFB8B3B967146
2 changed files with 68 additions and 5 deletions

@ -233,6 +233,12 @@ pub struct ActiveAppWithNode {
} }
impl ActiveAppWithNode { impl ActiveAppWithNode {
pub async fn get_by_uuid(db: &Surreal<Client>, uuid: &str) -> Result<Option<Self>, Error> {
let contract: Option<Self> =
db.query(format!("select * from {ACTIVE_APP}:{uuid} fetch out;")).await?.take(0)?;
Ok(contract)
}
pub async fn list_by_node(db: &Surreal<Client>, node_pubkey: &str) -> Result<Vec<Self>, Error> { pub async fn list_by_node(db: &Surreal<Client>, node_pubkey: &str) -> Result<Vec<Self>, Error> {
let mut query_result = db let mut query_result = db
.query(format!( .query(format!(
@ -244,10 +250,38 @@ impl ActiveAppWithNode {
Ok(contract) Ok(contract)
} }
pub async fn get_by_uuid(db: &Surreal<Client>, uuid: &str) -> Result<Option<Self>, Error> { pub async fn list_by_admin(db: &Surreal<Client>, admin: &str) -> Result<Vec<Self>, Error> {
let contract: Option<Self> = let mut query_result = db
db.query(format!("select * from {ACTIVE_APP}:{uuid} fetch out;")).await?.take(0)?; .query(format!("select * from {ACTIVE_APP} where in = {ACCOUNT}:{admin} fetch out;"))
Ok(contract) .await?;
let app_contracts: Vec<Self> = query_result.take(0)?;
Ok(app_contracts)
}
pub async fn list_by_operator(
db: &Surreal<Client>,
operator: &str,
) -> Result<Vec<Self>, Error> {
let mut query_result = db
.query(format!(
"select
(select * from ->operator->app_node<-{ACTIVE_APP} fetch out)
as app_contracts
from {ACCOUNT}:{operator}"
))
.await?;
#[derive(Deserialize)]
struct Wrapper {
app_contracts: Vec<ActiveAppWithNode>,
}
let c: Option<Wrapper> = query_result.take(0)?;
match c {
Some(contracts_wrapper) => Ok(contracts_wrapper.app_contracts),
None => Ok(vec![]),
}
} }
} }

@ -210,7 +210,36 @@ impl BrainAppCli for AppCliServer {
let req = check_sig_from_req(req)?; let req = check_sig_from_req(req)?;
info!("list_app_contracts process starting for {:?}", req); info!("list_app_contracts process starting for {:?}", req);
todo!() let mut app_contracts = Vec::new();
if !req.uuid.is_empty() {
if let Some(app_contract) =
db::ActiveAppWithNode::get_by_uuid(&self.db, &req.uuid).await?
{
if app_contract.admin.key().to_string() == req.admin_pubkey {
app_contracts.push(app_contract);
}
// TODO: allow operator to inspect contracts
}
} else if req.as_operator {
app_contracts.append(
&mut db::ActiveAppWithNode::list_by_operator(&self.db, &req.admin_pubkey).await?,
);
} else {
app_contracts.append(
&mut db::ActiveAppWithNode::list_by_admin(&self.db, &req.admin_pubkey).await?,
);
}
let (tx, rx) = mpsc::channel(6);
tokio::spawn(async move {
for app_contract in app_contracts {
let _ = tx.send(Ok(app_contract.into())).await;
}
});
let resp_stream = ReceiverStream::new(rx);
Ok(Response::new(Box::pin(resp_stream)))
} }
async fn list_app_nodes( async fn list_app_nodes(