@@ -21,7 +21,6 @@ public final class PointOfSaleLocalBarcodeScanService: PointOfSaleBarcodeScanSer
2121 /// - Parameter barcode: The barcode string from a scan (global unique identifier)
2222 /// - Returns: A POSItem if found, or throws an error
2323 public func getItem( barcode: String ) async throws ( PointOfSaleBarcodeScanError) -> POSItem {
24- // Search for product or variation by global unique ID
2524 do {
2625 if let product = try searchProductByGlobalUniqueID ( barcode) {
2726 return try convertProductToItem ( product, scannedCode: barcode)
@@ -31,7 +30,6 @@ public final class PointOfSaleLocalBarcodeScanService: PointOfSaleBarcodeScanSer
3130 return try await convertVariationToItem ( variationAndParent. variation, parentProduct: variationAndParent. parentProduct, scannedCode: barcode)
3231 }
3332
34- // No match found
3533 throw PointOfSaleBarcodeScanError . notFound ( scannedCode: barcode)
3634 } catch let error as PointOfSaleBarcodeScanError {
3735 throw error
@@ -69,7 +67,6 @@ public final class PointOfSaleLocalBarcodeScanService: PointOfSaleBarcodeScanSer
6967 do {
7068 let posProduct = try persistedProduct. toPOSProduct ( db: grdbManager. databaseConnection)
7169
72- // Validate product is not downloadable (should already be filtered by query, but double-check)
7370 guard !posProduct. downloadable else {
7471 throw PointOfSaleBarcodeScanError . downloadableProduct ( scannedCode: scannedCode, productName: posProduct. name)
7572 }
@@ -102,27 +99,20 @@ public final class PointOfSaleLocalBarcodeScanService: PointOfSaleBarcodeScanSer
10299 parentProduct: PersistedProduct ,
103100 scannedCode: String ) async throws ( PointOfSaleBarcodeScanError) -> POSItem {
104101 do {
105- // Validate variation is not downloadable (should already be filtered by query, but double-check)
106- guard !persistedVariation. downloadable else {
107- // We don't have the product name for variations, so use a generic message
108- throw PointOfSaleBarcodeScanError . downloadableProduct ( scannedCode: scannedCode, productName: " Product variation " )
109- }
110-
111102 // Convert both variation and parent to POS models
112103 let posVariation = try persistedVariation. toPOSProductVariation ( db: grdbManager. databaseConnection)
113104 let parentPOSProduct = try parentProduct. toPOSProduct ( db: grdbManager. databaseConnection)
114105
115- // Map parent to POSVariableParentProduct
116- let mappedParents = itemMapper. mapProductsToPOSItems ( products: [ parentPOSProduct] )
117- guard let mappedParent = mappedParents . first ,
118- case . variableParentProduct ( let variableParentProduct ) = mappedParent else {
106+ // Map to POSItem
107+ guard let mappedParent = itemMapper. mapProductsToPOSItems ( products: [ parentPOSProduct] ) . first ,
108+ case . variableParentProduct ( let variableParentProduct ) = mappedParent ,
109+ let item = itemMapper . mapVariationsToPOSItems ( variations : [ posVariation ] , parentProduct : variableParentProduct ) . first else {
119110 throw PointOfSaleBarcodeScanError . variationCouldNotBeConverted ( scannedCode: scannedCode)
120111 }
121112
122- // Convert to POSItem
123- let items = itemMapper. mapVariationsToPOSItems ( variations: [ posVariation] , parentProduct: variableParentProduct)
124- guard let item = items. first else {
125- throw PointOfSaleBarcodeScanError . variationCouldNotBeConverted ( scannedCode: scannedCode)
113+ guard !persistedVariation. downloadable else {
114+ throw PointOfSaleBarcodeScanError . downloadableProduct ( scannedCode: scannedCode,
115+ productName: variationName ( for: item) )
126116 }
127117
128118 return item
@@ -132,4 +122,20 @@ public final class PointOfSaleLocalBarcodeScanService: PointOfSaleBarcodeScanSer
132122 throw PointOfSaleBarcodeScanError . mappingError ( scannedCode: scannedCode, underlyingError: error)
133123 }
134124 }
125+
126+ private func variationName( for item: POSItem ) -> String {
127+ guard case . variation( let posVariation) = item else {
128+ return Localization . unknownVariationName
129+ }
130+ return posVariation. name
131+ }
132+ }
133+
134+ private extension PointOfSaleLocalBarcodeScanService {
135+ enum Localization {
136+ static let unknownVariationName = NSLocalizedString (
137+ " pointOfSale.barcodeScanning.unresolved.variation.name " ,
138+ value: " Unknown " ,
139+ comment: " A placeholder name when we can't determine the name of a variation for an error message " )
140+ }
135141}
0 commit comments