-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #288 from msupply-foundation/285-generate-paramete…
…rs-from-sql 285 generate parameters from sql
- Loading branch information
Showing
20 changed files
with
205 additions
and
51 deletions.
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
1 change: 1 addition & 0 deletions
1
backend/repository/migrations/2024-02-07-154900_add_parameter_query_id/down.sql
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 @@ | ||
-- This file should undo anything in `up.sql` |
1 change: 1 addition & 0 deletions
1
backend/repository/migrations/2024-02-07-154900_add_parameter_query_id/up.sql
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 @@ | ||
ALTER TABLE notification_config ADD COLUMN parameter_query_id TEXT; |
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
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
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,68 @@ | ||
use std::collections::HashMap; | ||
|
||
use crate::service_provider::ServiceContext; | ||
use crate::notification::NotificationServiceError; | ||
use super::query::NotificationConfig; | ||
use repository::NotificationQueryRowRepository; | ||
|
||
pub fn get_notification_parameters( | ||
ctx: &ServiceContext, | ||
notification_config: &NotificationConfig, | ||
) -> Result<Vec<HashMap<String, serde_json::Value>>, NotificationServiceError> { | ||
// Fetch default parameters from config | ||
let params_string = match notification_config.parameters.len() { | ||
0 => "[]".to_string(), | ||
_ => notification_config.parameters.clone(), | ||
}; | ||
let sql_params_string = match ¬ification_config.parameter_query_id { | ||
None => "[]".to_string(), | ||
Some(query_id) => get_sql_parameters(ctx, query_id)?, | ||
}; | ||
|
||
// Parse both sets of parameter strings, then merge the resulting vectors | ||
let mut all_params: Vec<HashMap<String, serde_json::Value>> = serde_json::from_str(¶ms_string) | ||
.map_err(|e| { | ||
NotificationServiceError::InternalError(format!( | ||
"Failed to parse notification config parameters (expecting an array of params_string): {:?} - {}", | ||
e, params_string | ||
)) | ||
})?; | ||
let sql_params: Vec<HashMap<String, serde_json::Value>> = serde_json::from_str(&sql_params_string) | ||
.map_err(|e| { | ||
NotificationServiceError::InternalError(format!( | ||
"Failed to parse notification sql parameters (expecting an array of params_string): {:?} - {}", | ||
e, params_string | ||
)) | ||
})?; | ||
all_params.extend(sql_params); | ||
|
||
return Ok(all_params); | ||
} | ||
|
||
fn get_sql_parameters( | ||
ctx: &ServiceContext, | ||
parameter_query_id: &String, | ||
) -> Result<String, NotificationServiceError> { | ||
// TODO: Maybe split these to a new database table | ||
let repository = NotificationQueryRowRepository::new(&ctx.connection); | ||
let query_record = repository.find_one_by_id(¶meter_query_id)?; | ||
|
||
let sql_query = match query_record { | ||
None => return Err(NotificationServiceError::InternalError(format!( | ||
"No query found for parameter_query_id: {}", parameter_query_id) | ||
)), | ||
Some(record) => record.query, | ||
}; | ||
|
||
let query_result = ctx | ||
.service_provider | ||
.datasource_service | ||
.run_sql_query(sql_query) | ||
.map_err(|e| { | ||
NotificationServiceError::InternalError(format!("Error when fetching parameter_query_id: {} - {:?}", | ||
parameter_query_id, e | ||
)) | ||
})?; | ||
|
||
return Ok(query_result.results); | ||
} |
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
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
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
Oops, something went wrong.