Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

559 GS1 codes #564

Merged
merged 35 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
e5208fe
add GS1 to dgraph schema
lache-melvin Jan 29, 2024
1fec9b8
fix the detail redirect
lache-melvin Jan 30, 2024
0afd341
gs1 dgraph layer
lache-melvin Jan 30, 2024
4468b61
gs1 service layer
lache-melvin Jan 30, 2024
1de52fd
add graphQL gs1 layer
lache-melvin Jan 30, 2024
8c97d36
add GS1 table view
lache-melvin Jan 30, 2024
e82e5c8
add basic addGS1 modal
lache-melvin Jan 30, 2024
3fba4d9
add barcodes link to app drawer
lache-melvin Jan 30, 2024
df31738
truncate description if too large
lache-melvin Jan 30, 2024
84567e9
add lookup button
lache-melvin Jan 30, 2024
dfafd91
Merge branch 'develop' into 559-gs1-codes
lache-melvin Jan 30, 2024
5a4a99f
autocomplete pack size code selection
lache-melvin Jan 30, 2024
a955a09
create generic delete lines dropdown item
lache-melvin Jan 31, 2024
796efb3
delete gs1 ui
lache-melvin Jan 31, 2024
201e5b8
refactor getParentDescription to helpers
lache-melvin Jan 31, 2024
15fcb06
add pagination
lache-melvin Jan 31, 2024
ce09724
extract gs1 list from list view
lache-melvin Jan 31, 2024
49a12e9
show pack size code in table
lache-melvin Jan 31, 2024
1bc8510
view GS1s for a pack size
lache-melvin Jan 31, 2024
579b0ed
display GS1 codes for entity from any level
lache-melvin Jan 31, 2024
4499ff6
collapse nodes below unit
lache-melvin Jan 31, 2024
7c0a4a0
add body text to nothing here component
lache-melvin Jan 31, 2024
51ff442
show existing pack sizes in edit form
lache-melvin Jan 31, 2024
fec01bd
add link to entity GS1 page
lache-melvin Jan 31, 2024
ade8744
gs1s -> barcodes
lache-melvin Jan 31, 2024
3d50235
invalidate entity barcodes on delete
lache-melvin Jan 31, 2024
b3abf0e
remove test gs1 reference
lache-melvin Jan 31, 2024
4bf82d9
fix comments
lache-melvin Jan 31, 2024
2774915
move barcodes to within browse section
lache-melvin Feb 1, 2024
861b2ea
add barcodes to app bar links
lache-melvin Feb 1, 2024
b8332ce
hide admin components when unauthenticated
lache-melvin Feb 1, 2024
d0da174
Merge branch 'develop' into 559-gs1-codes
lache-melvin Feb 1, 2024
df8a34d
use dgraph aggregate to determine total barcode count
lache-melvin Feb 1, 2024
428c3ca
internal error -> bad user input
lache-melvin Feb 1, 2024
58f5ad1
revert "fix the detail redirect"
lache-melvin Feb 1, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
}
}
35 changes: 35 additions & 0 deletions backend/dgraph/src/barcode/barcodes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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
}
}
}
"#;
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();
}
}
20 changes: 20 additions & 0 deletions backend/dgraph/src/barcode/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use serde::Deserialize;

use crate::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>,
}
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 @@ -28,6 +28,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 @@ -74,6 +76,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 @@ -91,6 +95,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
Loading