Skip to content

Commit

Permalink
Code Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
jmbrunskill committed Nov 14, 2024
1 parent 45a0db7 commit 1d76ccd
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const AddFromScannerButtonComponent = () => {
AppRoute.Equipment
);
const { mutateAsync: fetchAsset } = useAssets.document.fetch();
const { mutateAsync: fetchGS1 } = useAssets.document.gs1();
const { mutateAsync: fetchOrCreateFromGS1 } = useAssets.document.gs1();
const { mutateAsync: saveNewAsset } = useAssets.document.insert();
const { insertLog, invalidateQueries } = useAssets.log.insert();
const newAssetData = useRef<DraftAsset>();
Expand Down Expand Up @@ -69,8 +69,7 @@ export const AddFromScannerButtonComponent = () => {

if (!gs1) {
// try to fetch the asset by id, as it could be an id from our own barcode
const { content } = result;
const id = content;
const { content: id } = result;
const asset = await fetchAsset(id).catch(() => {});
if (asset) {
navigate(equipmentRoute.addPart(id).build());
Expand All @@ -82,7 +81,7 @@ export const AddFromScannerButtonComponent = () => {
}

// send the GS1 data to backend to handle
const asset = await fetchGS1(gs1).catch(() => {});
const asset = await fetchOrCreateFromGS1(gs1).catch(() => {});

if (asset?.__typename !== 'AssetNode') {
error(t('error.no-matching-asset', { id: result.content }))();
Expand Down
2 changes: 1 addition & 1 deletion server/service/src/asset/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub trait AssetServiceTrait: Sync + Send {
ctx: &ServiceContext,
gs1_data: Vec<GS1DataElement>,
) -> Result<Asset, AssetFromGs1Error> {
parse::create_from_gs1_data(ctx, gs1_data)
parse::get_or_create_from_gs1_data(ctx, gs1_data)
}
}

Expand Down
8 changes: 4 additions & 4 deletions server/service/src/asset/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn create_draft_asset_from_gs1(ctx: &ServiceContext, gs1: GS1) -> Result<Asset,
Ok(asset)
}

pub fn create_from_gs1_data(
pub fn get_or_create_from_gs1_data(
ctx: &ServiceContext,
gs1_data: Vec<GS1DataElement>,
) -> Result<Asset, AssetFromGs1Error> {
Expand All @@ -113,7 +113,7 @@ pub fn create_from_gs1_data(

#[cfg(test)]
mod test {
use crate::{asset::parse::create_from_gs1_data, service_provider::ServiceProvider};
use crate::{asset::parse::get_or_create_from_gs1_data, service_provider::ServiceProvider};
use repository::{
mock::{mock_asset_a, mock_store_a, MockDataInserts},
test_db::setup_all,
Expand All @@ -139,7 +139,7 @@ mod test {

let gs1 = GS1::from_human_readable_string(example_gs1.to_string()).unwrap();

let draft_asset = create_from_gs1_data(&ctx, gs1.to_data_elements()).unwrap();
let draft_asset = get_or_create_from_gs1_data(&ctx, gs1.to_data_elements()).unwrap();

assert_eq!(draft_asset.id, ""); // Draft asset has an empty ID
assert_eq!(draft_asset.serial_number, Some("S12345678".to_string()));
Expand Down Expand Up @@ -180,7 +180,7 @@ mod test {

let gs1 = GS1::from_human_readable_string(gs1_data).unwrap();

let existing_asset = create_from_gs1_data(&ctx, gs1.to_data_elements()).unwrap();
let existing_asset = get_or_create_from_gs1_data(&ctx, gs1.to_data_elements()).unwrap();

assert_eq!(existing_asset.id, mock_asset_a().id);
}
Expand Down

0 comments on commit 1d76ccd

Please sign in to comment.