Files
ember-market-frontend/lib/services/product-service.ts
g fe01f31538
Some checks failed
Build Frontend / build (push) Failing after 7s
Refactor UI imports and update component paths
Replaces imports from 'components/ui' with 'components/common' across the app and dashboard pages, and updates model and API imports to use new paths under 'lib'. Removes redundant authentication checks from several dashboard pages. Adds new dashboard components and utility files, and reorganizes hooks and services into the 'lib' directory for improved structure.
2026-01-13 05:02:13 +00:00

101 lines
2.4 KiB
TypeScript

import { clientFetch } from '@/lib/api/api-client';
// Product data types
export interface Product {
_id: string;
name: string;
description: string;
price: number;
imageUrl?: string;
category?: string;
stock?: number;
status: 'active' | 'inactive';
}
export interface ProductsResponse {
products: Product[];
total: number;
success?: boolean;
}
export interface StockData {
currentStock: number;
stockTracking?: boolean;
lowStockThreshold?: number;
}
/**
* Get all products with pagination
*/
export const getProducts = async (page: number = 1, limit: number = 25): Promise<ProductsResponse> => {
return clientFetch(`/products?page=${page}&limit=${limit}`);
};
/**
* Get a specific product by ID
*/
export const getProductDetails = async (productId: string): Promise<Product> => {
return clientFetch(`/products/${productId}`);
};
/**
* Create a new product
*/
export const createProduct = async (productData: Omit<Product, '_id'>): Promise<Product> => {
return clientFetch('/products', {
method: 'POST',
body: JSON.stringify(productData),
});
};
/**
* Update a product
*/
export const updateProduct = async (productId: string, productData: Partial<Product>): Promise<Product> => {
return clientFetch(`/products/${productId}`, {
method: 'PUT',
body: JSON.stringify(productData),
});
};
/**
* Delete a product
*/
export const deleteProduct = async (productId: string): Promise<{ success: boolean }> => {
return clientFetch(`/products/${productId}`, {
method: 'DELETE',
});
};
/**
* Upload a product image
*/
export const uploadProductImage = async (productId: string, file: File): Promise<{ imageUrl: string }> => {
const formData = new FormData();
formData.append('file', file);
return clientFetch(`/products/${productId}/image`, {
method: 'PUT',
body: formData,
headers: {
// Don't set Content-Type when sending FormData, the browser will set it with the boundary
}
});
};
/**
* Get product stock information
*/
export const getProductStock = async (productId: string): Promise<StockData> => {
return clientFetch(`/stock/${productId}`);
};
/**
* Update product stock
*/
export const updateProductStock = async (productId: string, stockData: StockData): Promise<StockData> => {
return clientFetch(`/stock/${productId}`, {
method: 'PUT',
body: JSON.stringify(stockData),
});
};