Skip to content

Commit

Permalink
Fixing unified delete unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
sagojez committed Jan 14, 2025
1 parent 1880f1b commit 259e8f8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 46 deletions.
36 changes: 19 additions & 17 deletions integrationos-unified/src/algebra/jsruntime.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use derive_builder::Builder;
use integrationos_domain::{ApplicationError, IntegrationOSError};
use js_sandbox_ios::Script;
use serde::de::DeserializeOwned;
Expand All @@ -9,19 +8,17 @@ thread_local! {
static JS_RUNTIME: RefCell<Script> = RefCell::new(Script::new());
}

#[derive(Builder)]
#[builder(setter(into), build_fn(error = "IntegrationOSError"))]
pub struct JSRuntimeImpl {
namespace: String,
code: String,
}
#[derive(Default, Clone, Copy)]
pub struct JSRuntimeImpl;

impl JSRuntimeImpl {
/// Adds a JavaScript script to the runtime environment under a specific namespace.
///
/// # Parameters
///
/// - `fn_name`: The name of the JavaScript function to be added to the runtime.
/// - `namespace`: The namespace
/// - `code`: The JavaScript code to be added to the runtime.
///
/// # Returns
///
Expand All @@ -33,13 +30,18 @@ impl JSRuntimeImpl {
///
/// Returns an error if adding the script to the runtime fails, logging the error
/// and returning a `bad_request` application error.
pub fn create(self, fn_name: &str) -> Result<Self, IntegrationOSError> {
pub fn create(
&self,
fn_name: &str,
namespace: &str,
code: &str,
) -> Result<Self, IntegrationOSError> {
JS_RUNTIME
.with_borrow_mut(|script| script.add_script(&self.namespace, fn_name, &self.code))
.with_borrow_mut(|script| script.add_script(namespace, fn_name, code))
.map_err(|e| {
tracing::error!(
"Failed to create request schema mapping script. ID: {}, Error: {}",
self.namespace,
"Failed to create javascript function in namespace {}. Error: {}",
namespace,
e
);

Expand All @@ -49,7 +51,7 @@ impl JSRuntimeImpl {
)
})?;

Ok(self)
Ok(*self)
}

/// Executes a JavaScript function in the runtime associated with a specific namespace,
Expand All @@ -74,14 +76,14 @@ impl JSRuntimeImpl {
///
/// Returns an error if serialization of the input data fails or if the JavaScript
/// function fails to execute. Logs the error and returns a `bad_request` application error.
pub async fn run<P, R>(&self, body: &P) -> Result<R, IntegrationOSError>
pub async fn run<P, R>(&self, payload: &P, namespace: &str) -> Result<R, IntegrationOSError>
where
P: Serialize,
R: DeserializeOwned,
{
let body = serde_json::to_value(body).map_err(|e| {
let body = serde_json::to_value(payload).map_err(|e| {
tracing::error!(
"Failed to serialize request for request schema mapping script for connection model. Error: {}",
"Error serializing payload: {}",
e
);

Expand All @@ -92,10 +94,10 @@ impl JSRuntimeImpl {
})?;

let body = JS_RUNTIME
.with_borrow_mut(|script| script.call_namespace(&self.namespace, body))
.with_borrow_mut(|script| script.call_namespace(namespace, body))
.map_err(|e| {
tracing::error!(
"Failed to run request schema mapping script for connection model. Error: {}",
"Error running javascript function: {}",
e
);

Expand Down
53 changes: 24 additions & 29 deletions integrationos-unified/src/unified.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::domain::{ResponseCrudToMapBuilder, ResponseCrudToMapRequest};
use crate::{
algebra::jsruntime::{JSRuntimeImpl, JSRuntimeImplBuilder},
algebra::jsruntime::JSRuntimeImpl,
client::CallerClient,
domain::{RequestCrud, ResponseCrud, UnifiedMetadata, UnifiedMetadataBuilder},
utility::{match_route, template_route},
Expand Down Expand Up @@ -314,6 +314,7 @@ impl UnifiedDestination {
let (config, secret, cms) = self.get_dependencies(&key, &connection, &name).await.inspect_err(|e| {
error!("Failed to get dependencies for unified destination. Destination: {:?}, Error: {e}", key);
})?;
tracing::debug!("Got dependencies for unified destination. Destination: {:?}, Config: {config:?}, Secret: {secret:?}, ConnectionModelSchema: {cms:?}", key);

let metadata = metadata
.action(action.to_string())
Expand All @@ -322,46 +323,43 @@ impl UnifiedDestination {
let secret = insert_action_id(secret.as_value()?, id.as_ref());

// Namespace for js scripts
let jsruntime = JSRuntimeImpl;
let crud_namespace = generate_script_namespace(self.secrets_cache.max_capacity(), &config.id.to_string());
let schema_namespace = generate_script_namespace(self.secrets_cache.max_capacity(), &cms.id.to_string());

tracing::debug!("Got namespace for unified destination. Destination: {:?}, CrudNamespace: {crud_namespace:?}, SchemaNamespace: {schema_namespace:?}", key);

let body = params.get_body();
let body = match cms.mapping.as_ref().map(|m| m.from_common_model.as_str()) {
Some(code) => {
let namespace = schema_namespace.clone() + "_mapFromCommonModel";

let jsruntime = JSRuntimeImplBuilder::default().namespace(namespace).code(code).build()
.inspect_err(|e| {
error!("Failed to create request schema mapping script for connection model. ID: {}, Error: {}", config.id, e);
})?;

jsruntime.create("mapFromCommonModel")?.run::<Option<&Value>, Option<Value>>(&body).await?.map(|v| v.drop_nulls())
jsruntime.create("mapFromCommonModel", &namespace, &code)?.run::<Option<&Value>, Option<Value>>(&body, &namespace).await?.map(|v| v.drop_nulls())
}
None => body.cloned()
};

tracing::debug!("Got body for unified destination. Destination: {:?}, Body: {body:?}", key);

let default_params = params.clone();
let request_crud: Option<Result<RequestCrud, IntegrationOSError>> = OptionFuture::from(config.mapping.as_ref().map(|m| m.from_common_model.to_owned())
.map(|code| async {
match code {
None => Ok(params),
Some(code) => {
let namespace = crud_namespace.clone() + "_mapFromCrudRequest";
let jsruntime = JSRuntimeImplBuilder::default().namespace(namespace).code(code).build()
.inspect_err(|e| {
error!("Failed to create request schema mapping script for connection model. ID: {}, Error: {}", config.id, e);
})?;
let jsruntime = jsruntime.create("mapCrudRequest")?;
let jsruntime = jsruntime.create("mapCrudRequest", &namespace, &code)?;

let params: RequestCrud = jsruntime.run(&prepare_crud_mapping(params, &config)?).await?;
let params: RequestCrud = jsruntime.run(&prepare_crud_mapping(params, &config)?, &namespace).await?;
let params: RequestCrud = params.extend_body(body);

Ok(params)
}
}
})).await;

tracing::debug!("Got request crud for unified destination. Destination: {:?}, RequestCrud: {request_crud:?}", key);

let params: RequestCrud = request_crud.unwrap_or(Ok(default_params))?;
let secret: Value = extend_secret(secret, params.get_path_params());

Expand All @@ -371,6 +369,8 @@ impl UnifiedDestination {
let response: reqwest::Response = self.execute_model_definition_from_request(&config, &params, &secret).timed(|_, duration| {
metadata.latency(duration.as_millis() as i32);
}).await?;

tracing::debug!("Got response for unified destination. Destination: {:?}, Response: {response:?}", key);
let status: StatusCode = response.status();
let headers: HeaderMap = response.headers().clone();

Expand Down Expand Up @@ -410,11 +410,9 @@ impl UnifiedDestination {
match cms.mapping.as_ref().map(|m| m.from_common_model.as_str()) {
Some(code) => {
let namespace = crud_namespace.clone() + "_mapToCrudRequest";
let jsruntime = JSRuntimeImplBuilder::default().namespace(namespace).code(code).build()
.inspect_err(|e| {
error!("Failed to create request crud mapping script for connection model. ID: {}, Error: {}", config.id, e);
})?;
let jsruntime: JSRuntimeImpl = jsruntime.create("mapCrudRequest")?;
let jsruntime: JSRuntimeImpl = jsruntime.create("mapCrudRequest", &namespace, &code).inspect_err(|e| {
error!("Failed to create request crud mapping script for connection model. ID: {}, Error: {}", config.id, e);
})?;

let pagination = extract_pagination(&config, &body)?;
let res_to_map = ResponseCrudToMapBuilder::default()
Expand All @@ -423,7 +421,7 @@ impl UnifiedDestination {
.request(ResponseCrudToMapRequest::new(params.get_query_params()))
.build()?;

let response: ResponseCrud = jsruntime.run(&res_to_map).await?;
let response: ResponseCrud = jsruntime.run(&res_to_map, &namespace).await?;

response.get_pagination().cloned()
}
Expand All @@ -440,21 +438,18 @@ impl UnifiedDestination {
Some(code) => {
let namespace = schema_namespace.clone() + "_mapToCommonModel";

let jsruntime = JSRuntimeImplBuilder::default().namespace(namespace).code(code).build()
.inspect_err(|e| {
error!("Failed to create request schema mapping script for connection model. ID: {}, Error: {}", config.id, e);
})?;
let jsruntime = jsruntime.create("mapToCommonModel").inspect_err(|e| {
error!("Failed to create request schema mapping script for connection model. ID: {}, Error: {}", config.id, e);
let jsruntime = jsruntime.create("mapToCommonModel", &namespace, &code).inspect_err(|e| {
error!("Failed to create request schema mapping script for connection model schema. ID: {}, Error: {}", config.id, e);
})?;

let mapped_body = match body {
Ok(Some(Value::Array(arr))) => {
let futures = arr.into_iter().map(|body| {
let jsruntime = &jsruntime;
let namespace = &namespace;
async move {
let mut response = jsruntime.run::<Value, Value>(&body).await.inspect_err(|e| {
error!("Failed to run request schema mapping script for connection model. ID: {}, Error: {}", config.id, e);
let mut response = jsruntime.run::<Value, Value>(&body, namespace).await.inspect_err(|e| {
error!("Failed to run request schema mapping script for connection model schema. ID: {}, Error: {}", config.id, e);
})?.drop_nulls();

if let Value::Object(map) = &mut response{
Expand All @@ -475,8 +470,8 @@ impl UnifiedDestination {
Ok(Value::Array(values))
},
Ok(Some(body)) => {
Ok(jsruntime.run::<Value, Value>(&body).await.inspect_err(|e| {
error!("Failed to run request schema mapping script for connection model. ID: {}, Error: {}", config.id, e);
Ok(jsruntime.run::<Value, Value>(&body, &namespace).await.inspect_err(|e| {
error!("Failed to run request schema mapping script for connection model schema. ID: {}, Error: {}", config.id, e);
})?.drop_nulls())
},
Ok(_) if config.action_name == CrudAction::GetMany => Ok(Value::Array(Default::default())),
Expand Down

0 comments on commit 259e8f8

Please sign in to comment.