#![allow(clippy::too_many_arguments, unused_imports, non_camel_case_types)]
use serde::{Deserialize, Serialize};
use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use serde_json::Value as JsonValue;
use uuid::Uuid;
use crate::enums::ProcessingState;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceState {
pub service_name: String,
pub instance_id: String,
pub service_state: String,
pub error_message: Option<String>,
pub service_result: Option<JsonValue>,
pub id: Uuid,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub created_by: Option<String>,
pub updated_by: Option<String>,
}
impl ServiceState {
pub fn new(
service_name: String,
instance_id: String,
service_state: String,
error_message: String,
service_result: JsonValue,
id: Uuid,
created_at: DateTime<Utc>,
updated_at: DateTime<Utc>,
created_by: String,
updated_by: String,
) -> Self {
Self {
service_name,
instance_id,
service_state,
error_message: Some(error_message),
service_result: Some(service_result),
id,
created_at,
updated_at,
created_by: Some(created_by),
updated_by: Some(updated_by),
}
}
pub fn to_json(&self) -> Result<String, serde_json::Error> {
serde_json::to_string(self)
}
pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
serde_json::from_str(json)
}
pub fn to_dict(&self) -> serde_json::Map<String, serde_json::Value> {
let json = serde_json::to_value(self).unwrap_or(serde_json::Value::Null);
if let serde_json::Value::Object(map) = json {
map
} else {
serde_json::Map::new()
}
}
}