Skip to content

Commit d990fbf

Browse files
committed
OBPIH-6969 Add inventories import
1 parent 8e98f8d commit d990fbf

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

src/api/InventoryService.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import BaseServiceModel from '@/api/BaseServiceModel';
2+
import { jsonToCsv } from '@/utils/ServiceUtils';
3+
4+
class InventoryService extends BaseServiceModel {
5+
async importInventories(data: Record<string, string>[], locationId: string): Promise<void> {
6+
try {
7+
const csvContent = jsonToCsv(data);
8+
9+
const response = await this.request.post(`./api/locations/${locationId}/inventories/import`, {
10+
data: csvContent,
11+
headers: { 'Content-Type': 'text/csv' },
12+
});
13+
14+
if (!response.ok()) {
15+
throw new Error(`Import failed with status ${response.status()}: ${await response.text()}`);
16+
}
17+
} catch (error) {
18+
throw new Error(`Problem importing inventories: ${error instanceof Error ? error.message : String(error)}`);
19+
}
20+
}
21+
}
22+
23+
export default InventoryService;

src/config/AppConfig.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class AppConfig {
5757

5858
public static PRODUCTS_IMPORT_FILE_PATH = path.join(AppConfig.DATA_IMPORT_DIRECTORY_PATH, '/products.csv');
5959

60+
public static INVENTORY_IMPORT_FILE_PATH = path.join(AppConfig.DATA_IMPORT_DIRECTORY_PATH, '/inventory.csv');
61+
6062
// Base URL to use in actions like `await page.goto('./dashboard')`.
6163
public appURL!: string;
6264

src/setup/dataImport.setup.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import InventoryService from '@/api/InventoryService';
12
import ProductService from '@/api/ProductService';
23
import AppConfig from '@/config/AppConfig';
34
import { test } from '@/fixtures/fixtures';
@@ -24,5 +25,17 @@ test('import data', async ({ request }) => {
2425
})
2526
})
2627

28+
// INVENTORIES
29+
const inventoryService = new InventoryService(request);
30+
31+
const inventoriesData = readCsvFile(AppConfig.INVENTORY_IMPORT_FILE_PATH);
32+
33+
await test.step(`importing ${inventoriesData.length} inventories`, async () => {
34+
await inventoryService.importInventories(
35+
inventoriesData,
36+
AppConfig.instance.locations.main.id,
37+
);
38+
});
39+
2740
writeToFile(AppConfig.TEST_DATA_FILE_PATH, seedData);
28-
});
41+
})

src/setup/dataImport/inventory.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Product code,Product,Lot number,Expiration date,Bin location,Quantity,Comment
2+
1,E2E-product-one,,,,122,
3+
2,E2E-product-two,,,,123,

0 commit comments

Comments
 (0)