nomy_data_models/models/
service_state.rs1#![allow(clippy::too_many_arguments, unused_imports, non_camel_case_types)]
2use serde::{Deserialize, Serialize};
8
9use chrono::{DateTime, Utc};
11use rust_decimal::Decimal;
12use serde_json::Value as JsonValue;
13use uuid::Uuid;
14
15use crate::enums::ProcessingState;
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
20pub struct ServiceState {
21 pub service_name: String,
22 pub instance_id: String,
23 pub service_state: String,
24 pub error_message: Option<String>,
25 pub service_result: Option<JsonValue>,
26 pub id: Uuid,
27 pub created_at: DateTime<Utc>,
28 pub updated_at: DateTime<Utc>,
29 pub created_by: Option<String>,
30 pub updated_by: Option<String>,
31}
32
33impl ServiceState {
34 pub fn new(
36 service_name: String,
37 instance_id: String,
38 service_state: String,
39 error_message: String,
40 service_result: JsonValue,
41 id: Uuid,
42 created_at: DateTime<Utc>,
43 updated_at: DateTime<Utc>,
44 created_by: String,
45 updated_by: String,
46 ) -> Self {
47 Self {
48 service_name,
49 instance_id,
50 service_state,
51 error_message: Some(error_message),
52 service_result: Some(service_result),
53 id,
54 created_at,
55 updated_at,
56 created_by: Some(created_by),
57 updated_by: Some(updated_by),
58 }
59 }
60
61 pub fn to_json(&self) -> Result<String, serde_json::Error> {
63 serde_json::to_string(self)
64 }
65
66 pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
68 serde_json::from_str(json)
69 }
70
71 pub fn to_dict(&self) -> serde_json::Map<String, serde_json::Value> {
73 let json = serde_json::to_value(self).unwrap_or(serde_json::Value::Null);
74 if let serde_json::Value::Object(map) = json {
75 map
76 } else {
77 serde_json::Map::new()
78 }
79 }
80}