2026-04-26 13:46:30 +03:00
|
|
|
// // Rating API endpoints for SweetHome
|
|
|
|
|
// // Handles both customer ratings and property ratings
|
2026-04-14 14:22:39 +00:00
|
|
|
|
2026-04-26 13:46:30 +03:00
|
|
|
// import AuthService from '../services/AuthService';
|
2026-04-14 14:22:39 +00:00
|
|
|
|
2026-04-26 13:46:30 +03:00
|
|
|
// const API_BASE = process.env.NEXT_PUBLIC_API_URL || 'https://45.93.137.91.nip.io/api';
|
2026-04-14 14:22:39 +00:00
|
|
|
|
2026-04-26 13:46:30 +03:00
|
|
|
// /**
|
|
|
|
|
// * Rate a property as a customer
|
|
|
|
|
// * @param {Object} data - Rating data
|
|
|
|
|
// * @param {number} data.propertyId - ID of the property being rated
|
|
|
|
|
// * @param {number} data.customerId - ID of the customer doing the rating
|
|
|
|
|
// * @param {number} data.rating - Rating value (1-5)
|
|
|
|
|
// * @param {string} data.comment - Optional comment
|
|
|
|
|
// * @returns {Promise} - API response
|
|
|
|
|
// */
|
|
|
|
|
// export async function rateProperty(data) {
|
|
|
|
|
// console.log('[Rating] Customer rating property:', data);
|
|
|
|
|
// return apiFetch('/Ratings/CustomerRateProperty', {
|
|
|
|
|
// method: 'POST',
|
|
|
|
|
// body: JSON.stringify(data),
|
|
|
|
|
// });
|
|
|
|
|
// }
|
2026-04-14 14:22:39 +00:00
|
|
|
|
2026-04-26 13:46:30 +03:00
|
|
|
// /**
|
|
|
|
|
// * Rate a customer as a property owner
|
|
|
|
|
// * @param {Object} data - Rating data
|
|
|
|
|
// * @param {number} data.propertyId - ID of the property
|
|
|
|
|
// * @param {number} data.customerId - ID of the customer being rated
|
|
|
|
|
// * @param {number} data.rating - Rating value (1-5)
|
|
|
|
|
// * @param {string} data.comment - Optional comment
|
|
|
|
|
// * @returns {Promise} - API response
|
|
|
|
|
// */
|
|
|
|
|
// export async function rateCustomer(data) {
|
|
|
|
|
// console.log('[Rating] Property owner rating customer:', data);
|
|
|
|
|
// return apiFetch('/Ratings/PropertyRateCustomer', {
|
|
|
|
|
// method: 'POST',
|
|
|
|
|
// body: JSON.stringify(data),
|
|
|
|
|
// });
|
|
|
|
|
// }
|
2026-04-14 14:22:39 +00:00
|
|
|
|
2026-04-26 13:46:30 +03:00
|
|
|
// /**
|
|
|
|
|
// * Get all ratings for a property
|
|
|
|
|
// * @param {number} propertyId - ID of the property
|
|
|
|
|
// * @returns {Promise} - Array of ratings
|
|
|
|
|
// */
|
|
|
|
|
// export async function getPropertyRatings(propertyId) {
|
|
|
|
|
// console.log('[Rating] Fetching property ratings for:', propertyId);
|
|
|
|
|
// return apiFetch(`/Ratings/GetPropertyRatings?propertyId=${propertyId}`);
|
|
|
|
|
// }
|
2026-04-14 14:22:39 +00:00
|
|
|
|
2026-04-26 13:46:30 +03:00
|
|
|
// /**
|
|
|
|
|
// * Get all ratings for a customer
|
|
|
|
|
// * @param {number} customerId - ID of the customer
|
|
|
|
|
// * @returns {Promise} - Array of ratings
|
|
|
|
|
// */
|
|
|
|
|
// export async function getCustomerRatings(customerId) {
|
|
|
|
|
// console.log('[Rating] Fetching customer ratings for:', customerId);
|
|
|
|
|
// return apiFetch(`/Ratings/GetCustomerRatings?customerId=${customerId}`);
|
|
|
|
|
// }
|
2026-04-14 14:22:39 +00:00
|
|
|
|
2026-04-26 13:46:30 +03:00
|
|
|
// /**
|
|
|
|
|
// * Get average rating for a property
|
|
|
|
|
// * @param {number} propertyId - ID of the property
|
|
|
|
|
// * @returns {Promise} - Average rating
|
|
|
|
|
// */
|
|
|
|
|
// export async function getPropertyAverageRating(propertyId) {
|
|
|
|
|
// console.log('[Rating] Fetching average rating for property:', propertyId);
|
|
|
|
|
// const ratings = await getPropertyRatings(propertyId);
|
|
|
|
|
// if (!Array.isArray(ratings) || ratings.length === 0) return 0;
|
2026-04-14 14:22:39 +00:00
|
|
|
|
2026-04-26 13:46:30 +03:00
|
|
|
// const total = ratings.reduce((sum, rating) => sum + rating.rating, 0);
|
|
|
|
|
// return Math.round((total / ratings.length) * 10) / 10; // Round to 1 decimal
|
|
|
|
|
// }
|
2026-04-14 14:22:39 +00:00
|
|
|
|
2026-04-26 13:46:30 +03:00
|
|
|
// /**
|
|
|
|
|
// * Get average rating for a customer
|
|
|
|
|
// * @param {number} customerId - ID of the customer
|
|
|
|
|
// * @returns {Promise} - Average rating
|
|
|
|
|
// */
|
|
|
|
|
// export async function getCustomerAverageRating(customerId) {
|
|
|
|
|
// console.log('[Rating] Fetching average rating for customer:', customerId);
|
|
|
|
|
// const ratings = await getCustomerRatings(customerId);
|
|
|
|
|
// if (!Array.isArray(ratings) || ratings.length === 0) return 0;
|
2026-04-14 14:22:39 +00:00
|
|
|
|
2026-04-26 13:46:30 +03:00
|
|
|
// const total = ratings.reduce((sum, rating) => sum + rating.rating, 0);
|
|
|
|
|
// return Math.round((total / ratings.length) * 10) / 10; // Round to 1 decimal
|
|
|
|
|
// }
|
2026-04-14 14:22:39 +00:00
|
|
|
|
2026-04-26 13:46:30 +03:00
|
|
|
// /**
|
|
|
|
|
// * Get user's rating for a specific property (if any)
|
|
|
|
|
// * @param {number} propertyId - ID of the property
|
|
|
|
|
// * @param {number} userId - ID of the user
|
|
|
|
|
// * @returns {Promise} - User's rating or null
|
|
|
|
|
// */
|
|
|
|
|
// export async function getUserPropertyRating(propertyId, userId) {
|
|
|
|
|
// console.log('[Rating] Fetching user rating for property:', propertyId, 'user:', userId);
|
|
|
|
|
// const allRatings = await getPropertyRatings(propertyId);
|
|
|
|
|
// if (!Array.isArray(allRatings)) return null;
|
2026-04-14 14:22:39 +00:00
|
|
|
|
2026-04-26 13:46:30 +03:00
|
|
|
// return allRatings.find(r => r.userId === userId) || null;
|
|
|
|
|
// }
|
2026-04-14 14:22:39 +00:00
|
|
|
|
2026-04-26 13:46:30 +03:00
|
|
|
// /**
|
|
|
|
|
// * Get user's rating for a specific customer (if any)
|
|
|
|
|
// * @param {number} customerId - ID of the customer
|
|
|
|
|
// * @param {number} userId - ID of the user
|
|
|
|
|
// * @returns {Promise} - User's rating or null
|
|
|
|
|
// */
|
|
|
|
|
// export async function getUserCustomerRating(customerId, userId) {
|
|
|
|
|
// console.log('[Rating] Fetching user rating for customer:', customerId, 'user:', userId);
|
|
|
|
|
// const allRatings = await getCustomerRatings(customerId);
|
|
|
|
|
// if (!Array.isArray(allRatings)) return null;
|
2026-04-14 14:22:39 +00:00
|
|
|
|
2026-04-26 13:46:30 +03:00
|
|
|
// return allRatings.find(r => r.userId === userId) || null;
|
|
|
|
|
// }
|
2026-04-14 14:22:39 +00:00
|
|
|
|
2026-04-26 13:46:30 +03:00
|
|
|
// /**
|
|
|
|
|
// * Check if user can rate a property (after renting)
|
|
|
|
|
// * @param {number} propertyId - ID of the property
|
|
|
|
|
// * @param {number} userId - ID of the user
|
|
|
|
|
// * @returns {Promise} - Boolean indicating if rating is allowed
|
|
|
|
|
// */
|
|
|
|
|
// export async function canRateProperty(propertyId, userId) {
|
|
|
|
|
// console.log('[Rating] Checking if user can rate property:', propertyId, 'user:', userId);
|
2026-04-14 14:22:39 +00:00
|
|
|
|
2026-04-26 13:46:30 +03:00
|
|
|
// // Logic: User can rate if they have completed a rental in the past
|
|
|
|
|
// // This would typically check reservation history
|
|
|
|
|
// // For now, we'll simulate this with a simple check
|
2026-04-14 14:22:39 +00:00
|
|
|
|
2026-04-26 13:46:30 +03:00
|
|
|
// // In a real implementation, this would check:
|
|
|
|
|
// // 1. User's reservation history for this property
|
|
|
|
|
// // 2. Whether the rental period has ended
|
|
|
|
|
// // 3. Whether they've already rated
|
2026-04-14 14:22:39 +00:00
|
|
|
|
2026-04-26 13:46:30 +03:00
|
|
|
// const userRating = await getUserPropertyRating(propertyId, userId);
|
|
|
|
|
// return !userRating; // Can rate if no existing rating
|
|
|
|
|
// }
|
2026-04-14 14:22:39 +00:00
|
|
|
|
2026-04-26 13:46:30 +03:00
|
|
|
// /**
|
|
|
|
|
// * Check if user can rate a customer (after renting to them)
|
|
|
|
|
// * @param {number} customerId - ID of the customer
|
|
|
|
|
// * @param {number} userId - ID of the user (owner)
|
|
|
|
|
// * @returns {Promise} - Boolean indicating if rating is allowed
|
|
|
|
|
// */
|
|
|
|
|
// export async function canRateCustomer(customerId, userId) {
|
|
|
|
|
// console.log('[Rating] Checking if user can rate customer:', customerId, 'user:', userId);
|
2026-04-14 14:22:39 +00:00
|
|
|
|
2026-04-26 13:46:30 +03:00
|
|
|
// // Logic: Owner can rate if they have rented to this customer
|
|
|
|
|
// // This would typically check reservation history
|
2026-04-14 14:22:39 +00:00
|
|
|
|
2026-04-26 13:46:30 +03:00
|
|
|
// const userRating = await getUserCustomerRating(customerId, userId);
|
|
|
|
|
// return !userRating; // Can rate if no existing rating
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// // Helper function for API calls
|
|
|
|
|
// async function apiFetch(endpoint, options = {}) {
|
|
|
|
|
// const token = AuthService.getToken();
|
|
|
|
|
|
|
|
|
|
// const headers = {
|
|
|
|
|
// 'Content-Type': 'application/json',
|
|
|
|
|
// ...(token && { Authorization: `Bearer ${token}` }),
|
|
|
|
|
// ...options.headers,
|
|
|
|
|
// };
|
2026-04-14 14:22:39 +00:00
|
|
|
|
2026-04-26 13:46:30 +03:00
|
|
|
// console.log('[Rating API] Request:', options.method || 'GET', `${API_BASE}${endpoint}`);
|
|
|
|
|
|
|
|
|
|
// const res = await fetch(`${API_BASE}${endpoint}`, {
|
|
|
|
|
// ...options,
|
|
|
|
|
// headers,
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// console.log('[Rating API] Response:', res.status, endpoint);
|
|
|
|
|
|
|
|
|
|
// if (!res.ok && res.status !== 206) {
|
|
|
|
|
// const text = await res.text().catch(() => '');
|
|
|
|
|
// console.error('[Rating API] Error:', res.status, text);
|
|
|
|
|
// throw new Error(`Rating API ${res.status}: ${text || res.statusText}`);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// const text = await res.text();
|
|
|
|
|
// if (!text) return null;
|
|
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
// const json = JSON.parse(text);
|
|
|
|
|
// if (json && typeof json === 'object' && 'data' in json) {
|
|
|
|
|
// return json.data;
|
|
|
|
|
// }
|
|
|
|
|
// return json;
|
|
|
|
|
// } catch {
|
|
|
|
|
// return text;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// utils/ratings.js
|
|
|
|
|
import AuthService from '../services/AuthService';
|
|
|
|
|
|
|
|
|
|
const API_BASE = process.env.NEXT_PUBLIC_API_URL || 'https://45.93.137.91.nip.io/api';
|
2026-04-14 14:22:39 +00:00
|
|
|
async function apiFetch(endpoint, options = {}) {
|
|
|
|
|
const token = AuthService.getToken();
|
|
|
|
|
const headers = {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
...(token && { Authorization: `Bearer ${token}` }),
|
|
|
|
|
...options.headers,
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-26 13:46:30 +03:00
|
|
|
const response = await fetch(`${API_BASE}${endpoint}`, {
|
2026-04-14 14:22:39 +00:00
|
|
|
...options,
|
|
|
|
|
headers,
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-26 13:46:30 +03:00
|
|
|
if (!response.ok && response.status !== 206) {
|
|
|
|
|
const errorText = await response.text().catch(() => '');
|
|
|
|
|
throw new Error(`API Error ${response.status}: ${errorText}`);
|
2026-04-14 14:22:39 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-26 13:46:30 +03:00
|
|
|
const text = await response.text();
|
2026-04-14 14:22:39 +00:00
|
|
|
if (!text) return null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const json = JSON.parse(text);
|
2026-04-26 13:46:30 +03:00
|
|
|
return json && typeof json === 'object' && 'data' in json ? json.data : json;
|
2026-04-14 14:22:39 +00:00
|
|
|
} catch {
|
|
|
|
|
return text;
|
|
|
|
|
}
|
2026-04-26 13:46:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* POST /Rating/AddPropertyRating
|
|
|
|
|
* @param {Object} data - { reservationId, cleanRating, servicesRating, ownerBehaviorRating, experienceRating, comment? }
|
|
|
|
|
*/
|
|
|
|
|
export async function addPropertyRating(data) {
|
|
|
|
|
return apiFetch('/Rating/AddPropertyRating', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* POST /Rating/AddCustomerRating
|
|
|
|
|
* @param {Object} data - { reservationId, furnitureIntegrityRating, termsComplianceRating, renterBehaviorRating, comment? }
|
|
|
|
|
*/
|
|
|
|
|
export async function addCustomerRating(data) {
|
|
|
|
|
return apiFetch('/Rating/AddCustomerRating', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* GET /Rating/GetPropertyRatings
|
|
|
|
|
* @param {number} propertyId
|
|
|
|
|
* @param {number} page - default 1
|
|
|
|
|
* @param {number} pageSize - default 10
|
|
|
|
|
* @returns {Promise<{ items: Array, totalPages: number, currentPage: number }>}
|
|
|
|
|
*/
|
|
|
|
|
export async function getPropertyRatings(propertyId, page = 1, pageSize = 10) {
|
|
|
|
|
const query = new URLSearchParams({
|
|
|
|
|
propertyId: String(propertyId),
|
|
|
|
|
page: String(page),
|
|
|
|
|
pageSize: String(pageSize),
|
|
|
|
|
}).toString();
|
|
|
|
|
return apiFetch(`/Rating/GetPropertyRatings?${query}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* GET /Rating/GetCustomerRatings
|
|
|
|
|
* @param {number} renterId
|
|
|
|
|
* @param {number} page
|
|
|
|
|
* @param {number} pageSize
|
|
|
|
|
*/
|
|
|
|
|
export async function getCustomerRatings(renterId, page = 1, pageSize = 10) {
|
|
|
|
|
const query = new URLSearchParams({
|
|
|
|
|
renterId: String(renterId),
|
|
|
|
|
page: String(page),
|
|
|
|
|
pageSize: String(pageSize),
|
|
|
|
|
}).toString();
|
|
|
|
|
return apiFetch(`/Rating/GetCustomerRatings?${query}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* GET /Rating/GetPropertyAverage
|
|
|
|
|
* @param {number} propertyId
|
|
|
|
|
* @returns {Promise<number>} average rating (0 if none)
|
|
|
|
|
*/
|
|
|
|
|
export async function getPropertyAverageRating(propertyId) {
|
|
|
|
|
const result = await apiFetch(`/Rating/GetPropertyAverage?propertyId=${propertyId}`);
|
|
|
|
|
if (typeof result === 'number') return result;
|
|
|
|
|
if (result && typeof result.average === 'number') return result.average;
|
|
|
|
|
return 0;
|
2026-04-14 14:22:39 +00:00
|
|
|
}
|