-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: effect schema validator generator (#89)
- Loading branch information
Showing
4 changed files
with
260 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use super::ReadResponse; | ||
use crate::server::AppState; | ||
use axum::{ | ||
extract::{Path, State}, | ||
routing::get, | ||
Json, Router, | ||
}; | ||
use bson::{doc, Document}; | ||
use futures::StreamExt; | ||
use integrationos_domain::{ApplicationError, Id, IntegrationOSError, InternalError, Store}; | ||
use mongodb::options::FindOptions; | ||
use std::sync::Arc; | ||
|
||
pub fn get_router() -> Router<Arc<AppState>> { | ||
Router::new() | ||
.route("/projection", get(get_common_model_proj)) | ||
.route("/:id", get(generate_schema)) | ||
} | ||
|
||
pub async fn get_common_model_proj( | ||
state: State<Arc<AppState>>, | ||
) -> Result<Json<ReadResponse<Document>>, IntegrationOSError> { | ||
let collection = state | ||
.app_stores | ||
.db | ||
.collection::<Document>(&Store::CommonModels.to_string()); | ||
|
||
let filter = doc! { | ||
"deleted": false, | ||
"primary": true, | ||
"active": true, | ||
}; | ||
let options = FindOptions::builder() | ||
.projection(doc! { "_id": 1, "name": 1 }) | ||
.build(); | ||
|
||
let mut cursor = collection.find(filter, options).await?; | ||
let mut common_models: Vec<Document> = Vec::new(); | ||
|
||
while let Some(result) = cursor.next().await { | ||
match result { | ||
Ok(document) => { | ||
common_models.push(document); | ||
} | ||
_ => { | ||
return Err(InternalError::unknown( | ||
"Error while fetching common models", | ||
None, | ||
)); | ||
} | ||
} | ||
} | ||
|
||
let len = common_models.len(); | ||
|
||
Ok(Json(ReadResponse { | ||
rows: common_models, | ||
total: len as u64, | ||
skip: 0, | ||
limit: 0, | ||
})) | ||
} | ||
|
||
pub async fn generate_schema( | ||
state: State<Arc<AppState>>, | ||
Path(id): Path<Id>, | ||
) -> Result<String, IntegrationOSError> { | ||
let cm_store = state.app_stores.common_model.clone(); | ||
let ce_store = state.app_stores.common_enum.clone(); | ||
|
||
let common_model = cm_store | ||
.get_one_by_id(&id.to_string()) | ||
.await | ||
.map_err(IntegrationOSError::from)? | ||
.ok_or(ApplicationError::not_found( | ||
&format!("CommonModel with id {} not found", id), | ||
None, | ||
))?; | ||
|
||
let schema = common_model | ||
.as_typescript_schema_expanded(&cm_store, &ce_store) | ||
.await; | ||
|
||
Ok(schema) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters