-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #564 from msupply-foundation/559-gs1-codes
559 GS1 codes
- Loading branch information
Showing
67 changed files
with
2,407 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } |
Oops, something went wrong.