-
Notifications
You must be signed in to change notification settings - Fork 15
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 #5347 from msupply-foundation/5305-add-mutate-prog…
…ram-indicator-values 5305 add mutate program indicator values
- Loading branch information
Showing
18 changed files
with
575 additions
and
68 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
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod errors; | ||
pub mod request_requisition; | ||
pub mod response_requisition; | ||
pub mod update_indicator_value; |
90 changes: 90 additions & 0 deletions
90
server/graphql/requisition/src/mutations/update_indicator_value.rs
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,90 @@ | ||
use async_graphql::*; | ||
use graphql_core::standard_graphql_error::StandardGraphqlError; | ||
use graphql_core::{ | ||
simple_generic_errors::RecordNotFound, standard_graphql_error::validate_auth, ContextExt, | ||
}; | ||
use graphql_types::types::program_indicator::IndicatorValueNode; | ||
use service::auth::{Resource, ResourceAccessRequest}; | ||
use service::requisition::indicator_value::{UpdateIndicatorValue, UpdateIndicatorValueError}; | ||
|
||
#[derive(InputObject)] | ||
pub struct UpdateIndicatorValueInput { | ||
pub id: String, | ||
pub value: String, | ||
} | ||
|
||
#[derive(Interface)] | ||
#[graphql(name = "UpdateIndicatorValueErrorInterface")] | ||
#[graphql(field(name = "description", ty = "String"))] | ||
pub enum UpdateErrorInterface { | ||
RecordNotFound(RecordNotFound), | ||
} | ||
|
||
#[derive(SimpleObject)] | ||
#[graphql(name = "UpdateIndicatorValueError")] | ||
pub struct UpdateError { | ||
pub error: UpdateErrorInterface, | ||
} | ||
|
||
#[derive(Union)] | ||
pub enum UpdateIndicatorValueResponse { | ||
Response(IndicatorValueNode), | ||
Error(UpdateError), | ||
} | ||
|
||
pub fn update( | ||
ctx: &Context<'_>, | ||
store_id: String, | ||
input: UpdateIndicatorValueInput, | ||
) -> Result<UpdateIndicatorValueResponse> { | ||
let user = validate_auth( | ||
ctx, | ||
&ResourceAccessRequest { | ||
resource: Resource::MutateRequisition, | ||
store_id: Some(store_id.clone()), | ||
}, | ||
)?; | ||
|
||
let service_provider = ctx.service_provider(); | ||
let service_context = service_provider.context(store_id.to_string(), user.user_id)?; | ||
|
||
let response = match service_provider | ||
.indicator_value_service | ||
.update_indicator_value(&service_context, input.to_domain()) | ||
{ | ||
Ok(indicator_value) => { | ||
UpdateIndicatorValueResponse::Response(IndicatorValueNode::from_domain(indicator_value)) | ||
} | ||
Err(error) => UpdateIndicatorValueResponse::Error(UpdateError { | ||
error: map_error(error)?, | ||
}), | ||
}; | ||
|
||
Ok(response) | ||
} | ||
|
||
impl UpdateIndicatorValueInput { | ||
pub fn to_domain(self) -> UpdateIndicatorValue { | ||
let UpdateIndicatorValueInput { id, value } = self; | ||
UpdateIndicatorValue { id, value } | ||
} | ||
} | ||
|
||
fn map_error(error: UpdateIndicatorValueError) -> Result<UpdateErrorInterface> { | ||
use StandardGraphqlError::*; | ||
let formatted_error = format!("{:?}", error); | ||
let graphql_error = match error { | ||
// Structured Errors | ||
UpdateIndicatorValueError::IndicatorValueDoesNotExist => { | ||
return Ok(UpdateErrorInterface::RecordNotFound(RecordNotFound {})) | ||
} | ||
// Standard graphql errors | ||
UpdateIndicatorValueError::NotThisStoreValue | ||
| UpdateIndicatorValueError::IndicatorColumnDoesNotExist | ||
| UpdateIndicatorValueError::ValueNotCorrectType | ||
| UpdateIndicatorValueError::IndicatorLineDoesNotExist => BadUserInput(formatted_error), | ||
UpdateIndicatorValueError::DatabaseError(_) => InternalError(formatted_error), | ||
}; | ||
|
||
Err(graphql_error.extend()) | ||
} |
File renamed without changes.
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,21 @@ | ||
use crate::IndicatorValueRow; | ||
|
||
use super::{ | ||
mock_indicator_column_a, mock_indicator_line_a, mock_period, mock_store_a, mock_store_b, | ||
}; | ||
|
||
pub fn mock_indicator_value_a() -> IndicatorValueRow { | ||
IndicatorValueRow { | ||
id: String::from("id_a"), | ||
customer_name_link_id: mock_store_b().name_link_id, | ||
store_id: mock_store_a().id, | ||
period_id: mock_period().id, | ||
indicator_line_id: mock_indicator_line_a().id, | ||
indicator_column_id: mock_indicator_column_a().id, | ||
value: String::from("test_value"), | ||
} | ||
} | ||
|
||
pub fn mock_indicator_values() -> Vec<IndicatorValueRow> { | ||
vec![mock_indicator_value_a()] | ||
} |
Oops, something went wrong.