Skip to content

Commit b173493

Browse files
committed
OBPIH-6969 Add service method for importing products
1 parent 0362ec7 commit b173493

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

src/api/ProductService.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import BaseServiceModel from '@/api/BaseServiceModel';
22
import { ApiResponse, ProductDemandResponse, ProductResponse } from '@/types';
3-
import { parseRequestToJSON } from '@/utils/ServiceUtils';
3+
import { jsonToCsv, parseRequestToJSON } from '@/utils/ServiceUtils';
44

55
class ProductService extends BaseServiceModel {
66
async getDemand(id: string): Promise<ApiResponse<ProductDemandResponse>> {
@@ -22,6 +22,24 @@ class ProductService extends BaseServiceModel {
2222
throw new Error('Problem fetching product data');
2323
}
2424
}
25+
26+
async importProducts(data: Record<string, string>[]): Promise<ApiResponse<ProductResponse[]>> {
27+
try {
28+
const csvContent = jsonToCsv(data);
29+
30+
const apiResponse = await this.request.post(
31+
'./api/products/import',
32+
{
33+
data: csvContent,
34+
headers: { 'Content-Type': 'text/csv' }
35+
}
36+
);
37+
38+
return await parseRequestToJSON(apiResponse);
39+
} catch (error) {
40+
throw new Error('Problem importing products');
41+
}
42+
}
2543
}
2644

2745
export default ProductService;

0 commit comments

Comments
 (0)