112 lines
No EOL
3.4 KiB
JavaScript
112 lines
No EOL
3.4 KiB
JavaScript
import apiClient from './api';
|
|
|
|
const productReviewService = {
|
|
/**
|
|
* Get all reviews for a product
|
|
* @param {string} productId - Product ID
|
|
* @returns {Promise<Array>} - Array of reviews
|
|
*/
|
|
getProductReviews: async (productId) => {
|
|
try {
|
|
const response = await apiClient.get(`/product-reviews/${productId}`);
|
|
return response.data;
|
|
} catch (error) {
|
|
throw error.response?.data || { message: 'An unknown error occurred' };
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Add a review to a product
|
|
* @param {string} productId - Product ID
|
|
* @param {Object} reviewData - Review data
|
|
* @param {string} reviewData.title - Review title
|
|
* @param {string} reviewData.content - Review content
|
|
* @param {number} reviewData.rating - Star rating (1-5)
|
|
* @param {string} [reviewData.parentId] - Parent review ID (for replies)
|
|
* @returns {Promise<Object>} - Response with the new review
|
|
*/
|
|
addProductReview: async (productId, reviewData) => {
|
|
try {
|
|
const response = await apiClient.post(`/product-reviews/${productId}`, reviewData);
|
|
return response.data;
|
|
} catch (error) {
|
|
throw error.response?.data || { message: 'An unknown error occurred' };
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Check if user can review a product
|
|
* @param {string} productId - Product ID
|
|
* @returns {Promise<Object>} - Object with canReview, isPurchaser, and isAdmin flags
|
|
*/
|
|
canReviewProduct: async (productId) => {
|
|
try {
|
|
const response = await apiClient.get(`/product-reviews/${productId}/can-review`);
|
|
return response.data;
|
|
} catch (error) {
|
|
throw error.response?.data || { message: 'An unknown error occurred' };
|
|
}
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Admin-specific review service functions
|
|
*/
|
|
export const productReviewAdminService = {
|
|
/**
|
|
* Get all pending reviews (admin only)
|
|
* @returns {Promise<Array>} - Array of pending reviews
|
|
*/
|
|
getPendingReviews: async () => {
|
|
try {
|
|
const response = await apiClient.get('/admin/product-reviews/pending');
|
|
return response.data;
|
|
} catch (error) {
|
|
throw error.response?.data || { message: 'An unknown error occurred' };
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Get all reviews for a product (admin only)
|
|
* @param {string} productId - Product ID
|
|
* @returns {Promise<Object>} - Object with product and reviews
|
|
*/
|
|
getProductReviews: async (productId) => {
|
|
try {
|
|
const response = await apiClient.get(`/admin/product-reviews/products/${productId}`);
|
|
return response.data;
|
|
} catch (error) {
|
|
throw error.response?.data || { message: 'An unknown error occurred' };
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Approve a review (admin only)
|
|
* @param {string} reviewId - Review ID
|
|
* @returns {Promise<Object>} - Response with the approved review
|
|
*/
|
|
approveReview: async (reviewId) => {
|
|
try {
|
|
const response = await apiClient.post(`/admin/product-reviews/${reviewId}/approve`);
|
|
return response.data;
|
|
} catch (error) {
|
|
throw error.response?.data || { message: 'An unknown error occurred' };
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Delete a review (admin only)
|
|
* @param {string} reviewId - Review ID
|
|
* @returns {Promise<Object>} - Response with success message
|
|
*/
|
|
deleteReview: async (reviewId) => {
|
|
try {
|
|
const response = await apiClient.delete(`/admin/product-reviews/${reviewId}`);
|
|
return response.data;
|
|
} catch (error) {
|
|
throw error.response?.data || { message: 'An unknown error occurred' };
|
|
}
|
|
}
|
|
};
|
|
|
|
export default productReviewService; |