diff --git a/backend/services/productionOrderService.js b/backend/services/productionOrderService.js index 832c624..7f658e0 100644 --- a/backend/services/productionOrderService.js +++ b/backend/services/productionOrderService.js @@ -1195,7 +1195,9 @@ const isSellableComposition = (order) => { }; const getTinyProductIdFromCompositionReference = (tinyId) => ( - tinyId.slice(TINY_OLIST_V3_COMPOSITION_PREFIX.length) + tinyId.startsWith(TINY_OLIST_V3_COMPOSITION_PREFIX) + ? tinyId.slice(TINY_OLIST_V3_COMPOSITION_PREFIX.length) + : tinyId ); const normalizeCompositionComponents = (components) => { @@ -1291,6 +1293,7 @@ const findProductCompositionsBySource = async (source, productId, client = pool) AND ( $2 = '' OR composition.finished_tiny_product_id = $2 + OR composition.external_source_id = $2 OR composition.finished_product_sku = UPPER($2) ) ORDER BY composition.finished_product_sku, composition.finished_product_description; diff --git a/backend/test/productCompositionService.test.js b/backend/test/productCompositionService.test.js index 4b03fe6..9117a60 100644 --- a/backend/test/productCompositionService.test.js +++ b/backend/test/productCompositionService.test.js @@ -208,7 +208,7 @@ const withFakeDatabase = async (run) => { if (normalizedSql.includes('FROM product_compositions composition')) { const compositions = state.compositions.filter(item => ( item.source === params[0] - && (params[1] === '' || item.finished_tiny_product_id === params[1] || item.finished_product_sku === String(params[1]).toUpperCase()) + && (params[1] === '' || item.finished_tiny_product_id === params[1] || item.external_source_id === params[1] || item.finished_product_sku === String(params[1]).toUpperCase()) )); return { rows: compositions.map(composition => ({ @@ -342,6 +342,36 @@ test('product composition detail lookup returns stored components', async () => }); }); +test('product composition detail lookup supports an exported composition linked by external product ID', async () => { + await withFakeDatabase(async () => { + const response = await invokeHandler(tinyCompositionImportHandler, { + body: { + compositions: [{ + externalSourceId: '919483307', + finishedProductSku: 'BLCS.PTO.G', + finishedProductDescription: 'BASE LISA CAMISETA COR PRETO TAMANHO - G', + finishedProductUnit: 'UN', + finishedTinyProductId: '', + components: [{ + componentSku: 'ETIQ.T.G', + componentName: 'ETIQUETA DE TAMANHO G', + quantityPerUnit: 1, + unit: 'UN' + }] + }] + } + }); + + assert.equal(response.statusCode, 201); + + const detail = await invokeHandler(productCompositionHandler, { params: { productId: '919483307' } }); + assert.equal(detail.statusCode, 200); + assert.equal(detail.body.composition.finishedProductSku, 'BLCS.PTO.G'); + assert.equal(detail.body.composition.finishedTinyProductId, '919483307'); + assert.equal(detail.body.composition.components.length, 1); + }); +}); + test('product composition export endpoint returns every stored composition', async () => { await withFakeDatabase(async () => { await invokeHandler(tinySyncHandler, { body: compositionPayload() });