From 2bdc7cd39a66d9973c749c467d04ae2fbf5ae4d0 Mon Sep 17 00:00:00 2001 From: mbecker20 Date: Tue, 25 Jul 2023 02:33:48 -0400 Subject: [PATCH] list updates for admin --- bin/core/src/helpers/mod.rs | 32 ++++++++++++++++++++++++++-- bin/core/src/requests/read/build.rs | 16 +++++++++++++- bin/core/src/requests/read/update.rs | 17 +++++++++++++-- lib/macros/src/lib.rs | 1 + lib/types/src/requests/read/build.rs | 2 +- 5 files changed, 62 insertions(+), 6 deletions(-) diff --git a/bin/core/src/helpers/mod.rs b/bin/core/src/helpers/mod.rs index ac92d5647..84f0c0168 100644 --- a/bin/core/src/helpers/mod.rs +++ b/bin/core/src/helpers/mod.rs @@ -17,14 +17,15 @@ use monitor_types::{ monitor_timestamp, permissioned::Permissioned, }; +use mungos::{mongodb::bson::doc, AggStage::*}; use periphery_client::{requests, PeripheryClient}; use rand::{thread_rng, Rng}; use crate::{auth::RequestUser, state::State}; +pub mod alert; pub mod cache; pub mod channel; -pub mod alert; pub mod db; pub fn empty_or_only_spaces(word: &str) -> bool { @@ -59,7 +60,6 @@ pub fn make_update( } impl State { - // USER pub async fn get_user(&self, user_id: &str) -> anyhow::Result { @@ -230,6 +230,34 @@ impl State { Ok(build.get_user_permissions(user_id)) } + pub async fn get_build_ids_for_non_admin(&self, user_id: &str) -> anyhow::Result> { + self.db + .builds + .aggregate_collect( + [ + Match(doc! { + format!("permissions.{}", user_id): { "$in": ["update", "execute", "read"] } + }), + Project(doc! { "_id": 1 }), + ], + None, + ) + .await + .context("failed to get build ids for non admin | aggregation")? + .into_iter() + .map(|d| { + let id = d + .get("_id") + .context("no _id field")? + .as_object_id() + .context("_id not ObjectId")? + .to_string(); + anyhow::Ok(id) + }) + .collect::>>() + .context("failed to get build ids for non admin | extract id from document") + } + // BUILDER pub async fn get_builder(&self, builder_id: &str) -> anyhow::Result { diff --git a/bin/core/src/requests/read/build.rs b/bin/core/src/requests/read/build.rs index ca56b40ea..d1adfb123 100644 --- a/bin/core/src/requests/read/build.rs +++ b/bin/core/src/requests/read/build.rs @@ -8,6 +8,7 @@ use monitor_types::{ permissioned::Permissioned, requests::read::*, }; +use mungos::mongodb::bson::doc; use resolver_api::Resolve; use crate::{auth::RequestUser, state::State}; @@ -79,6 +80,19 @@ impl Resolve for State { GetBuildsSummary {}: GetBuildsSummary, user: RequestUser, ) -> anyhow::Result { - todo!() + let query = if user.is_admin { + None + } else { + let doc = doc! { + + }; + Some(doc) + }; + let total = self.db.builds.collection.count_documents(query, None).await.context("failed to count all build documents")?; + + let res = GetBuildsSummaryResponse { + total: total as u32 + }; + Ok(res) } } diff --git a/bin/core/src/requests/read/update.rs b/bin/core/src/requests/read/update.rs index 39f99a406..abd1d2530 100644 --- a/bin/core/src/requests/read/update.rs +++ b/bin/core/src/requests/read/update.rs @@ -1,5 +1,6 @@ use async_trait::async_trait; use monitor_types::{entities::update::Update, requests::read::ListUpdates}; +use mungos::mongodb::{bson::doc, options::FindOptions}; use resolver_api::Resolve; use crate::{auth::RequestUser, state::State}; @@ -11,7 +12,19 @@ impl Resolve for State { ListUpdates { query }: ListUpdates, user: RequestUser, ) -> anyhow::Result> { - - todo!() + if user.is_admin { + let updates = self + .db + .updates + .get_some( + query, + FindOptions::builder().sort(doc! { "ts": -1 }).build(), + ) + .await?; + Ok(updates) + } else { + let build_ids = self.get_build_ids_for_non_admin(&user.id).await?; + todo!() + } } } diff --git a/lib/macros/src/lib.rs b/lib/macros/src/lib.rs index 737359aff..cfb7302d7 100644 --- a/lib/macros/src/lib.rs +++ b/lib/macros/src/lib.rs @@ -58,3 +58,4 @@ pub fn derive_crud_requests(input: proc_macro::TokenStream) -> proc_macro::Token } .into() } + diff --git a/lib/types/src/requests/read/build.rs b/lib/types/src/requests/read/build.rs index 83de192a1..55cee7700 100644 --- a/lib/types/src/requests/read/build.rs +++ b/lib/types/src/requests/read/build.rs @@ -57,5 +57,5 @@ pub struct GetBuildsSummary {} #[typeshare] #[derive(Serialize, Deserialize, Debug, Clone)] pub struct GetBuildsSummaryResponse { - pub total: I64, + pub total: u32, }