This commit is contained in:
NotII
2025-04-07 19:25:24 +01:00
parent 7f7dd78818
commit 2f48ee38c2
102 changed files with 1825 additions and 761 deletions

5
lib/services/index.ts Normal file
View File

@@ -0,0 +1,5 @@
// Re-export all service functionality
export * from './product-service';
export * from './shipping-service';
export * from './stats-service';
export * from '../api-client';

View File

@@ -0,0 +1,101 @@
import { clientFetch } from '../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),
});
};

View File

@@ -0,0 +1,130 @@
import { clientFetch } from '../api-client';
/**
* Shipping service - Handles shipping options
* Replaces the old shippingHelper.ts
*/
export interface ShippingOption {
_id?: string;
name: string;
price: number;
createdAt?: string;
updatedAt?: string;
}
export interface ShippingOptionsResponse {
success: boolean;
data: ShippingOption[];
}
// Get all shipping options
export const getShippingOptions = async (authToken?: string): Promise<ShippingOption[]> => {
console.log('Fetching shipping options');
const options: RequestInit = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
};
if (authToken) {
options.headers = {
...options.headers,
'Authorization': `Bearer ${authToken}`,
};
}
const response = await clientFetch<ShippingOptionsResponse>('/api/shipping', options);
return response.data || [];
};
// Get a single shipping option
export const getShippingOption = async (id: string, authToken?: string): Promise<ShippingOption> => {
const options: RequestInit = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
};
if (authToken) {
options.headers = {
...options.headers,
'Authorization': `Bearer ${authToken}`,
};
}
const response = await clientFetch<{success: boolean, data: ShippingOption}>(`/api/shipping/${id}`, options);
return response.data;
};
// Create a new shipping option
export const createShippingOption = async (data: ShippingOption, authToken?: string) => {
const options: RequestInit = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
};
if (authToken) {
options.headers = {
...options.headers,
'Authorization': `Bearer ${authToken}`,
};
}
return clientFetch<{success: boolean, data: ShippingOption}>('/api/shipping', options);
};
// Update a shipping option
export const updateShippingOption = async (id: string, data: ShippingOption, authToken?: string) => {
const options: RequestInit = {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
};
if (authToken) {
options.headers = {
...options.headers,
'Authorization': `Bearer ${authToken}`,
};
}
return clientFetch<{success: boolean, data: ShippingOption}>(`/api/shipping/${id}`, options);
};
// Delete a shipping option
export const deleteShippingOption = async (id: string, authToken?: string) => {
const options: RequestInit = {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
},
};
if (authToken) {
options.headers = {
...options.headers,
'Authorization': `Bearer ${authToken}`,
};
}
return clientFetch<{success: boolean}>(`/api/shipping/${id}`, options);
};
// Compatibility with old shippingHelper functions
export const fetchShippingMethods = getShippingOptions;
export const addShippingMethod = createShippingOption;
export const updateShippingMethod = updateShippingOption;
export const deleteShippingMethod = deleteShippingOption;
// Types for backward compatibility
export type ShippingMethod = ShippingOption;
export type ShippingData = ShippingOption;

View File

@@ -0,0 +1,47 @@
import { clientFetch } from '../api-client';
// Stats data types
export interface PlatformStats {
orders: {
completed: number;
};
vendors: {
total: number;
};
transactions: {
volume: number;
averageOrderValue: number;
};
}
/**
* Get platform statistics
*/
export const getPlatformStats = async (): Promise<PlatformStats> => {
try {
return await clientFetch('/stats/platform');
} catch (error) {
console.error('Error fetching platform stats:', error);
// Return fallback data if API fails
return {
orders: {
completed: 15800
},
vendors: {
total: 2400
},
transactions: {
volume: 3200000,
averageOrderValue: 220
}
};
}
};
/**
* Get vendor-specific statistics
*/
export const getVendorStats = async (): Promise<any> => {
return clientFetch('/stats/vendor');
};