Skip to content

Commit

Permalink
Merge branch 'develop' into 532-support-drug-interactions-2
Browse files Browse the repository at this point in the history
  • Loading branch information
jmbrunskill committed Feb 2, 2024
2 parents 3140ef8 + 3ae26d3 commit ab277e2
Show file tree
Hide file tree
Showing 73 changed files with 2,454 additions and 37 deletions.
42 changes: 42 additions & 0 deletions backend/dgraph/src/barcode/barcode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use gql_client::GraphQLError;
use serde::Serialize;

use crate::{Barcode, BarcodeData, DgraphClient};

#[derive(Serialize, Debug, Clone)]
struct BarcodeVars {
gtin: String,
}

pub async fn barcode_by_gtin(
client: &DgraphClient,
gtin: String,
) -> Result<Option<Barcode>, GraphQLError> {
let query = r#"
query barcode($gtin: String!) {
data: queryBarcode(filter: {gtin: {eq: $gtin}}) {
gtin
manufacturer
entity {
description
name
code
}
}
}
"#;
let vars = BarcodeVars { gtin };

let result = client
.gql
.query_with_vars::<BarcodeData, BarcodeVars>(query, vars)
.await?;

match result {
Some(result) => match result.data.first() {
Some(barcode) => Ok(Some(barcode.clone())),
None => Ok(None),
},
None => Ok(None),
}
}
38 changes: 38 additions & 0 deletions backend/dgraph/src/barcode/barcodes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use gql_client::GraphQLError;
use serde::Serialize;

use crate::{BarcodeData, DgraphClient};

#[derive(Serialize, Debug)]
pub struct BarcodeQueryVars {
pub first: Option<u32>,
pub offset: Option<u32>,
}

pub async fn barcodes(
client: &DgraphClient,
vars: BarcodeQueryVars,
) -> Result<Option<BarcodeData>, GraphQLError> {
let query = r#"
query barcodes($first: Int, $offset: Int) {
data: queryBarcode(first: $first, offset: $offset) {
manufacturer
gtin
entity {
description
name
code
}
}
aggregates: aggregateBarcode {
count
}
}
"#;
let data = client
.gql
.query_with_vars::<BarcodeData, BarcodeQueryVars>(query, vars)
.await?;

Ok(data)
}
106 changes: 106 additions & 0 deletions backend/dgraph/src/barcode/delete_barcode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use gql_client::GraphQLError;
use serde::Serialize;

use crate::{DeleteResponse, DeleteResponseData, DgraphClient};

#[derive(Serialize, Debug, Clone)]
struct DeleteVars {
gtin: String,
}

pub async fn delete_barcode(
client: &DgraphClient,
gtin: String,
) -> Result<DeleteResponse, GraphQLError> {
let query = r#"
mutation DeleteBarcode($gtin: String) {
data: deleteBarcode(filter: {gtin: {eq: $gtin}}) {
numUids
}
}"#;
let variables = DeleteVars { gtin };

let result = client
.query_with_retry::<DeleteResponseData, DeleteVars>(&query, variables)
.await?;

match result {
Some(result) => {
return Ok(DeleteResponse {
numUids: result.data.numUids,
})
}
None => return Ok(DeleteResponse { numUids: 0 }),
}
}

#[cfg(test)]
#[cfg(feature = "dgraph-tests")]
mod tests {
use util::uuid::uuid;

use crate::{
barcode::barcode::barcode_by_gtin,
insert_barcode::{insert_barcode, BarcodeInput, EntityCode},
};

use super::*;

#[tokio::test]
async fn test_delete_barcode() {
let client = DgraphClient::new("http://localhost:8080/graphql");

let barcode_input = BarcodeInput {
manufacturer: "test_manufacturer".to_string(),
gtin: uuid(),
entity: EntityCode {
code: "c7750265".to_string(),
},
};

let result = insert_barcode(&client, barcode_input.clone(), true).await;
if result.is_err() {
println!(
"insert_barcode err: {:#?} {:#?}",
result,
result.clone().unwrap_err().json()
);
};

// Barcode exists
let result = barcode_by_gtin(&client, barcode_input.gtin.clone()).await;

if result.is_err() {
println!(
"barcode_by_gtin err: {:#?} {:#?}",
result,
result.clone().unwrap_err().json()
);
};

assert!(result.unwrap().is_some());

let result = delete_barcode(&client, barcode_input.gtin.clone()).await;
if result.is_err() {
println!(
"delete_barcode err: {:#?} {:#?}",
result,
result.clone().unwrap_err().json()
);
};
assert!(result.is_ok());

// Barcode no longer exists
let result = barcode_by_gtin(&client, barcode_input.gtin.clone()).await;

if result.is_err() {
println!(
"barcode_by_gtin err: {:#?} {:#?}",
result,
result.clone().unwrap_err().json()
);
};

assert!(result.unwrap().is_none());
}
}
104 changes: 104 additions & 0 deletions backend/dgraph/src/barcode/insert_barcode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use gql_client::GraphQLError;
use serde::Serialize;

use crate::{DgraphClient, UpsertResponse, UpsertResponseData};

#[derive(Serialize, Debug, Clone)]
pub struct EntityCode {
pub code: String,
}

#[derive(Serialize, Debug, Clone)]
pub struct BarcodeInput {
pub manufacturer: String,
pub gtin: String,
pub entity: EntityCode,
}

#[derive(Serialize, Debug, Clone)]
struct UpsertVars {
input: BarcodeInput,
upsert: bool,
}

pub async fn insert_barcode(
client: &DgraphClient,
barcode: BarcodeInput,
upsert: bool,
) -> Result<UpsertResponse, GraphQLError> {
let query = r#"
mutation AddBarcode($input: [AddBarcodeInput!]!, $upsert: Boolean = false) {
data: addBarcode(input: $input, upsert: $upsert) {
numUids
}
}"#;
let variables = UpsertVars {
input: barcode,
upsert,
};

let result = client
.query_with_retry::<UpsertResponseData, UpsertVars>(&query, variables)
.await?;

match result {
Some(result) => {
return Ok(UpsertResponse {
numUids: result.data.numUids,
})
}
None => return Ok(UpsertResponse { numUids: 0 }),
}
}

#[cfg(test)]
#[cfg(feature = "dgraph-tests")]
mod tests {
use crate::barcode::barcode::barcode_by_gtin;
use crate::barcode::delete_barcode::delete_barcode;
use util::uuid::uuid;

use super::*;

#[tokio::test]
async fn test_insert_barcode() {
// Create a DgraphClient instance
let client = DgraphClient::new("http://localhost:8080/graphql");

let barcode_input = BarcodeInput {
manufacturer: "test_manufacturer".to_string(),
gtin: uuid(),
entity: EntityCode {
code: "c7750265".to_string(),
},
};

let result = insert_barcode(&client, barcode_input.clone(), true).await;
if result.is_err() {
println!(
"insert_barcode err: {:#?} {:#?}",
result,
result.clone().unwrap_err().json()
);
};

// Check if the new record can be found by querying for the gtin
let result = barcode_by_gtin(&client, barcode_input.gtin.clone()).await;

if result.is_err() {
println!(
"barcode_by_gtin err: {:#?} {:#?}",
result,
result.clone().unwrap_err().json()
);
};

let data = result.unwrap().unwrap();
assert_eq!(data.manufacturer, barcode_input.manufacturer);

// Delete the record
let _result = delete_barcode(&client, barcode_input.gtin.clone())
.await
.unwrap();
}
}
22 changes: 22 additions & 0 deletions backend/dgraph/src/barcode/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use serde::Deserialize;

use crate::{AggregateResult, Entity};

pub mod barcode;
pub mod barcodes;
pub mod delete_barcode;
pub mod insert_barcode;

#[derive(Deserialize, Debug, Clone)]
pub struct Barcode {
pub gtin: String,
pub manufacturer: String,
pub entity: Entity,
}

#[derive(Deserialize, Debug, Clone)]
pub struct BarcodeData {
pub data: Vec<Barcode>,
#[serde(default)]
pub aggregates: Option<AggregateResult>,
}
8 changes: 8 additions & 0 deletions backend/dgraph/src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ pub async fn entity_by_code(
name
description
alternative_names
barcodes {
gtin
manufacturer
}
type
__typename
properties {
Expand Down Expand Up @@ -87,6 +91,10 @@ pub async fn entity_with_parents_by_code(
code
name
description
barcodes {
gtin
manufacturer
}
alternative_names
type
__typename
Expand Down
10 changes: 10 additions & 0 deletions backend/dgraph/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub mod upsert_entity;
pub use upsert_entity::*;
pub mod link_codes;
pub use link_codes::*;
pub mod barcode;
pub use barcode::*;

pub use gql_client::GraphQLError;

Expand Down Expand Up @@ -76,6 +78,8 @@ pub struct Entity {
#[serde(default)]
pub alternative_names: Option<String>,
#[serde(default)]
pub barcodes: Vec<BarcodeInfo>,
#[serde(default)]
pub properties: Vec<Property>,
#[serde(default)]
pub children: Vec<Entity>,
Expand All @@ -93,6 +97,12 @@ pub struct Property {
pub value: String,
}

#[derive(Deserialize, Debug, Clone)]
pub struct BarcodeInfo {
pub gtin: String,
pub manufacturer: String,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)]
pub enum ChangeType {
Change,
Expand Down
1 change: 1 addition & 0 deletions backend/graphql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ util = { path = "../util" }
graphql_configuration = { path = "configuration" }
graphql_drug_interactions = { path = "drug_interactions" }
graphql_core = { path = "core" }
graphql_barcode = { path = "barcode" }
graphql_types = { path = "types" }
graphql_general = { path = "general" }
graphql_user_account = { path = "user_account" }
Expand Down
16 changes: 16 additions & 0 deletions backend/graphql/barcode/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "graphql_barcode"
version = "0.1.0"
edition = "2018"

[lib]
path = "src/lib.rs"
doctest = false

[dependencies]

graphql_core = { path = "../core" }
dgraph = { path = "../../dgraph" }
service = { path = "../../service" }
async-graphql = { version = "3.0.35", features = ["dataloader", "chrono"] }
graphql_universal_codes_v1 = { path = "../../graphql_v1/universal_codes" }
Loading

0 comments on commit ab277e2

Please sign in to comment.