Commit 858d1270 authored by Szymon's avatar Szymon
Browse files

use find or create

parent 218ecaad
Showing with 16 additions and 53 deletions
+16 -53
......@@ -93,84 +93,47 @@ pub async fn init_service(init_db: &InitDb) -> Result<()> {
/// Sets the connection Alice - GrantAccess -> Bob
pub async fn grant_access(tx: &AsyncTx, owner_key: &str, req: &GrantAccessReq) -> Result<()> {
// TODO: add scope to Alice database, using her key?
// TODO: check if target account exists?
let mut nodes = vec![];
// Get or create source
let mut res = graphql::execute_query_with_args(
let source_id = get_item_or_create(
tx,
"query {
PodOwnerNode(filter: {ownerKey: {eq: ARG1 }}) {
id,
}
}",
PodOwnerNode(filter: {ownerKey: {eq: ARG1 }}) {
id,
}
}",
&[json!(owner_key)],
)
.await?;
debug!("source result: {res:#?}");
let source_id = if let Some(source) = res.pop_front() {
source.id
} else {
let source_id = ItemId::new();
nodes.push(json!(
json!(
{
"name": "PodOwnerNode",
"id": source_id,
"properties": {
"ownerKey": owner_key
}
}));
source_id
};
}),
)
.await?;
// Get or create target
let mut res = graphql::execute_query_with_args(
let target_id = get_item_or_create(
tx,
"query {
PodOwnerNode(filter: {ownerKey: {eq: ARG1 }}) {
id,
}
}",
PodOwnerNode(filter: {ownerKey: {eq: ARG1 }}) {
id,
}
}",
&[json!(req.to)],
)
.await?;
debug!("target result: {res:#?}");
let target_id = if let Some(target) = res.pop_front() {
target.id
} else {
let target_id = ItemId::new();
nodes.push(json!(
json!(
{
"name": "PodOwnerNode",
"id": target_id,
"properties": {
"ownerKey": req.to.as_str()
}
}));
target_id
};
v5::internal_api::create_item_tx(
tx,
&serde_json::from_value(json!({
"nodes": nodes
}))
.unwrap(),
}),
)
.await?;
// Get or create edge
let edge = v5::internal_api::get_edge_between_source_and_target(
tx,
&source_id,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment