Skip to content

Commit

Permalink
chore: update workflow managewment
Browse files Browse the repository at this point in the history
  • Loading branch information
shanithkk committed Aug 1, 2023
1 parent 37c3ef7 commit 908e0cc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 48 deletions.
70 changes: 23 additions & 47 deletions actions/workflow_management/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ struct Input {
#[serde(default = "empty_string")]
topic: String,
#[serde(default = "empty_string")]
endpoint: String,
#[serde(default = "empty_string")]
validator: String,
#[serde(default = "empty_string")]
key: String,
token: String,
#[serde(default)]
input: Value,
}

fn empty_string() -> String {
Expand Down Expand Up @@ -107,38 +105,28 @@ impl Action {
}
}

pub fn add_data_to_db(
&self,
endpoint: &str,
validator: &str,
key: &str,
topic: &str,
) -> Result<String, Error> {
pub fn add_data_to_db(&mut self) -> Result<String, Error> {
#[cfg(not(test))]
self.user_validate()?;

let mut db_input = self.params.input.clone();
db_input["token"] = serde_json::json!(self.params.token.clone());
let topic = self.params.topic.clone();

let db = self.connect_db(&self.params.db_url, &self.params.workflow_management_db);
let context = Context::new(db, None);
if context.get_document(topic).is_err() {
if context.get_document(&topic).is_err() {
context.insert_document(
&serde_json::json!({
"data": [DbDatas{
endpoint: endpoint.to_string(),
validator: validator.to_string(),
key: key.to_string(),
}]
"data": [db_input]
}),
Some(topic.to_string()),
)
} else {
let mut doc: Topic = serde_json::from_value(context.get_document(topic)?)?;

doc.data.push(DbDatas {
endpoint: endpoint.to_string(),
validator: validator.to_string(),
key: key.to_string(),
});
context.update_document(topic, &doc.rev, &serde_json::to_value(doc.clone())?)
let mut doc: Topic = serde_json::from_value(context.get_document(&topic)?)?;

doc.data.push(db_input);
context.update_document(&topic, &doc.rev, &serde_json::to_value(doc.clone())?)
}
}
}
Expand All @@ -154,12 +142,7 @@ pub fn main(args: Value) -> Result<Value, Error> {

match action.method().as_ref() {
"post" => {
let _id = action.add_data_to_db(
&action.params.endpoint,
&action.params.validator,
&action.params.key,
&action.params.topic,
)?;
let _id = action.add_data_to_db()?;
Ok(serde_json::json!({
"statusCode": 200,
"headers": { "Content-Type": "application/json" },
Expand Down Expand Up @@ -202,9 +185,12 @@ mod tests {
event_registration_db: "event_registration_db".to_string(),
auth_token: "1".to_string(),
topic: "418a8b8c-02b8-11ec-9a03-0242ac130003".to_string(),
endpoint: "".to_string(),
key: "".to_string(),
validator: "".to_string(),
token: "akjDSIJGFIJHNSdmngknomlmxcgknhNDlnglnlkoNSDG".to_string(),
input: serde_json::json!({
"endpoint": "".to_string(),
"key": "".to_string(),
"validator": "".to_string(),
}),
});
action.init(&config);

Expand All @@ -224,18 +210,8 @@ mod tests {
let workflow_db =
action.connect_db(&action.params.db_url, &action.params.workflow_management_db);
let workflow_management_db_context = Context::new(workflow_db, None);
let _res = action.add_data_to_db(
&action.params.endpoint,
&action.params.validator,
&action.params.key,
&action.params.topic,
);
let _res = action.add_data_to_db(
&action.params.endpoint,
&action.params.validator,
&action.params.key,
&action.params.topic,
);
let _res = action.add_data_to_db();
let _res = action.add_data_to_db();
let res_data =
workflow_management_db_context.get_document("418a8b8c-02b8-11ec-9a03-0242ac130003");
let res = serde_json::from_value::<Topic>(res_data.unwrap());
Expand Down
2 changes: 1 addition & 1 deletion actions/workflow_management/src/types/topic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub struct Topic {
pub id: String,
#[serde(rename = "_rev")]
pub rev: String,
pub data: Vec<DbDatas>,
pub data: Vec<serde_json::Value>,
}

#[allow(clippy::derive_partial_eq_without_eq)]
Expand Down

0 comments on commit 908e0cc

Please sign in to comment.