Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2720 cold chain missing data create temporary breaches #2913

Merged
merged 2 commits into from
Feb 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions server/server/src/cold_chain/temperature_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use log::error;
use mime_guess::mime;
use openssl::error;
use repository::RepositoryError;
use service::temperature_breach::insert::InsertTemperatureBreach;
use service::{
auth_data::AuthData,
service_provider::{ServiceContext, ServiceProvider},
Expand Down Expand Up @@ -114,6 +115,51 @@ fn upsert_temperature_log(
let datetime = NaiveDateTime::from_timestamp_opt(log.unix_timestamp, 0)
.context(format!("Unable to parse timestamp {}", log.unix_timestamp))?;

// If we have a temperature log with a breachid, we need to make sure the breach exists first, if it doesn't we create a temporary record so we don't loose data.
match &log.temperature_breach_id {
Some(breach_id) => {
let breach = service_provider
.temperature_breach_service
.get_temperature_breach(&ctx, breach_id.clone());

match breach {
Ok(_) => {}
Err(SingleRecordError::NotFound(_)) => {
// Create a temperature breach record as it doesn't exist yet
let breach = InsertTemperatureBreach {
id: breach_id.clone(),
sensor_id: log.sensor_id.clone(),
threshold_duration_milliseconds: 0,
duration_milliseconds: 0,
r#type: repository::TemperatureBreachRowType::HotConsecutive,
start_datetime: NaiveDateTime::from_timestamp_millis(log.unix_timestamp)
.unwrap_or_default(),
end_datetime: None,
unacknowledged: true,
threshold_minimum: 0.0,
threshold_maximum: 0.0,
comment: None,
location_id: None,
};
service_provider
.temperature_breach_service
.insert_temperature_breach(&ctx, breach)
.map_err(|e| {
anyhow::anyhow!("Unable to insert temperature breach {:?}", e)
})?;
}
Err(e) => {
return Err(anyhow::anyhow!(
"Unable to get temperature breach for id '{}'. {:#?}",
breach_id.clone(),
e
));
}
}
}
None => {}
};

let result = match service.get_temperature_log(&ctx, id.clone()) {
Ok(_) => {
let log = UpdateTemperatureLog {
Expand Down