diff --git a/barcode_stock/static/src/js/screens/internal_transfer_screen.esm.js b/barcode_stock/static/src/js/screens/internal_transfer_screen.esm.js index d0f4356..f0fe46d 100644 --- a/barcode_stock/static/src/js/screens/internal_transfer_screen.esm.js +++ b/barcode_stock/static/src/js/screens/internal_transfer_screen.esm.js @@ -191,6 +191,8 @@ export class InternalTransferScreen extends Component { await this.addSerialLine(product, lotId, lotName); return; } + const lots = + product.tracking === "none" ? [] : await this.getProductLots(product.id); const existing = this.state.lines.find( (l) => l.product_id === product.id && l.lot_id === lotId ); @@ -198,14 +200,10 @@ export class InternalTransferScreen extends Component { // The input hands an edited quantity back as text. existing.qty = roundQty((parseFloat(existing.qty) || 0) + qty); if (existing.tracking !== "none" && !existing.lots?.length) { - existing.lots = [...(await this.getProductLots(product.id))]; + existing.lots = [...lots]; } return; } - const lots = - product.tracking === "none" - ? [] - : [...(await this.getProductLots(product.id))]; this.state.lines.push( ensureLineUid({ product_id: product.id, @@ -214,7 +212,7 @@ export class InternalTransferScreen extends Component { tracking: product.tracking, lot_id: lotId, lot_name: lotName, - lots: lots, + lots: [...lots], }) ); } @@ -239,6 +237,8 @@ export class InternalTransferScreen extends Component { * the operator to choose the serial on, never a second unit on another. */ async addSerialLine(product, lotId, lotName) { + const lots = await this.getProductLots(product.id); + // From here on there is no await: the check and the push run atomically if ( lotId && this.state.lines.some( @@ -262,7 +262,7 @@ export class InternalTransferScreen extends Component { tracking: product.tracking, lot_id: lotId, lot_name: lotName, - lots: [...(await this.getProductLots(product.id))], + lots: [...lots], }) ); } diff --git a/barcode_stock/static/tests/internal_transfer_double_scan.test.js b/barcode_stock/static/tests/internal_transfer_double_scan.test.js new file mode 100644 index 0000000..66e27fa --- /dev/null +++ b/barcode_stock/static/tests/internal_transfer_double_scan.test.js @@ -0,0 +1,79 @@ +import {beforeEach, describe, expect, test} from "@odoo/hoot"; +import {InternalTransferScreen} from "@barcode_stock/js/screens/internal_transfer_screen.esm"; +import {patchTranslations} from "@web/../tests/web_test_helpers"; + +const LOTS = [ + {id: 21, name: "SN-0001"}, + {id: 22, name: "SN-0002"}, +]; + +/** + * An internal transfer screen without mounting it, whose lots for a product + * arrive only when `resolveLots` is called: the scans made before that are + * all waiting on the same request, as when a PDA reads a label twice. + * + * @returns {Object} the screen, its notifications and `resolveLots` + */ +function transferScreen() { + const screen = Object.create(InternalTransferScreen.prototype); + screen.state = {lines: []}; + screen.lotsCache = new Map(); + const notifications = []; + screen.notification = {add: (message) => notifications.push(String(message))}; + let resolveLots = null; + const lots = new Promise((resolve) => { + resolveLots = resolve; + }); + screen.fetchLots = () => lots; + return {screen, notifications, resolveLots: () => resolveLots(LOTS)}; +} + +/** + * Scan `times` times without waiting for any scan to finish, then let the + * lots arrive and wait for them all. + */ +async function scanAtOnce({screen, resolveLots}, product, lot, times = 2) { + const scans = []; + for (let i = 0; i < times; i++) { + scans.push(screen.addLine(product, lot?.id || null, lot?.name || null, 1)); + } + resolveLots(); + await Promise.all(scans); +} + +describe("BarcodeStock", () => { + beforeEach(() => patchTranslations()); + + test("a serial scanned twice at once lands on a single line", async () => { + const transfer = transferScreen(); + const product = {id: 7, name: "Drill", tracking: "serial"}; + await scanAtOnce(transfer, product, LOTS[0]); + expect(transfer.screen.state.lines).toHaveLength(1); + expect(transfer.screen.state.lines[0]).toMatchObject({ + product_id: 7, + lot_id: 21, + qty: 1, + }); + expect(transfer.notifications).toEqual([ + "Serial number SN-0001 is already in this transfer.", + ]); + // Another serial of the same product still gets its own line. + await transfer.screen.addLine(product, LOTS[1].id, LOTS[1].name, 1); + const serials = transfer.screen.state.lines.map((line) => line.lot_id); + expect(serials).toEqual([21, 22]); + }); + + test("a lot scanned twice at once adds up on a single line", async () => { + const transfer = transferScreen(); + const product = {id: 7, name: "Flour", tracking: "lot"}; + await scanAtOnce(transfer, product, LOTS[0]); + expect(transfer.screen.state.lines).toHaveLength(1); + expect(transfer.screen.state.lines[0]).toMatchObject({ + product_id: 7, + lot_id: 21, + qty: 2, + }); + expect(transfer.screen.state.lines[0].lots).toEqual(LOTS); + expect(transfer.notifications).toEqual([]); + }); +}); diff --git a/barcode_stock/static/tests/quantity.test.js b/barcode_stock/static/tests/quantity.test.js index 246b779..285f393 100644 --- a/barcode_stock/static/tests/quantity.test.js +++ b/barcode_stock/static/tests/quantity.test.js @@ -141,13 +141,14 @@ describe("BarcodeStock", () => { test("an internal transfer line adds up scans and an edited quantity", async () => { const screen = Object.create(InternalTransferScreen.prototype); + const product = {id: 7, tracking: "none"}; const line = {product_id: 7, lot_id: false, qty: 0.1, tracking: "none"}; screen.state = {lines: [line]}; - await screen.addLine({id: 7}, false, false, 0.2); + await screen.addLine(product, false, false, 0.2); expect(line.qty).toBe(0.3); // The input hands an edited quantity back as text. line.qty = "2"; - await screen.addLine({id: 7}, false, false, 1); + await screen.addLine(product, false, false, 1); expect(line.qty).toBe(3); }); });