2026-03-26 22:20:33 +00:00
|
|
|
const API_BASE = process.env.NEXT_PUBLIC_API_URL || 'http://45.93.137.91/api';
|
|
|
|
|
|
|
|
|
|
async function apiFetch(endpoint, options = {}) {
|
|
|
|
|
const token = typeof window !== 'undefined' ? localStorage.getItem('token') : null;
|
|
|
|
|
|
|
|
|
|
const headers = {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
...(token && { Authorization: `Bearer ${token}` }),
|
|
|
|
|
...options.headers,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const res = await fetch(`${API_BASE}${endpoint}`, {
|
|
|
|
|
...options,
|
|
|
|
|
headers,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
|
const text = await res.text().catch(() => '');
|
|
|
|
|
throw new Error(`API ${res.status}: ${text || res.statusText}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const text = await res.text();
|
2026-03-26 22:46:57 +00:00
|
|
|
if (!text) return null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const json = JSON.parse(text);
|
|
|
|
|
// API wraps responses in { data, errors, isSuccess, isFailure, statusCode }
|
|
|
|
|
if (json && typeof json === 'object' && 'data' in json) {
|
|
|
|
|
return json.data;
|
|
|
|
|
}
|
|
|
|
|
return json;
|
|
|
|
|
} catch {
|
|
|
|
|
return text;
|
|
|
|
|
}
|
2026-03-26 22:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── Rent Properties ───
|
|
|
|
|
|
|
|
|
|
export async function getRentProperties() {
|
2026-03-26 22:59:08 +00:00
|
|
|
const rentList = await apiFetch('/RentProperties/GetRentProperties');
|
|
|
|
|
if (!Array.isArray(rentList) || rentList.length === 0) return rentList;
|
|
|
|
|
|
|
|
|
|
// Fetch property info for each rent property's propInfoId
|
|
|
|
|
const enriched = await Promise.all(
|
|
|
|
|
rentList.map(async (item) => {
|
|
|
|
|
try {
|
|
|
|
|
const propInfo = await apiFetch(`/Properties/Get/${item.propInfoId}`);
|
|
|
|
|
return { ...item, propertyInformation: propInfo };
|
|
|
|
|
} catch {
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
return enriched;
|
2026-03-26 22:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getRentProperty(id) {
|
2026-03-26 22:59:08 +00:00
|
|
|
const item = await apiFetch(`/RentProperties/GetRentProperties?id=${id}`);
|
|
|
|
|
if (!item) return item;
|
|
|
|
|
|
|
|
|
|
// If it's an array (all results), pick the matching one
|
|
|
|
|
const property = Array.isArray(item) ? item.find(p => p.id == id) || item[0] : item;
|
|
|
|
|
|
|
|
|
|
if (property?.propInfoId) {
|
|
|
|
|
try {
|
|
|
|
|
const propInfo = await apiFetch(`/Properties/Get/${property.propInfoId}`);
|
|
|
|
|
return { ...property, propertyInformation: propInfo };
|
|
|
|
|
} catch {
|
|
|
|
|
// ignore
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return property;
|
2026-03-26 22:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getRentPropertyLocations(params = {}) {
|
|
|
|
|
const qs = new URLSearchParams();
|
|
|
|
|
if (params.maxOffset != null) qs.set('maxOffset', params.maxOffset);
|
|
|
|
|
if (params.minOffset != null) qs.set('minOffset', params.minOffset);
|
|
|
|
|
const query = qs.toString();
|
2026-03-26 22:46:57 +00:00
|
|
|
return apiFetch(`/RentProperties/GetRentPropertiesLocations${query ? `?${query}` : ''}`);
|
2026-03-26 22:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── Sale Properties ───
|
|
|
|
|
|
|
|
|
|
export async function getSaleProperties() {
|
2026-03-26 22:59:08 +00:00
|
|
|
const saleList = await apiFetch('/SaleProperties/GetSaleProperties');
|
|
|
|
|
if (!Array.isArray(saleList) || saleList.length === 0) return saleList;
|
|
|
|
|
|
|
|
|
|
const enriched = await Promise.all(
|
|
|
|
|
saleList.map(async (item) => {
|
|
|
|
|
try {
|
|
|
|
|
const propInfo = await apiFetch(`/Properties/Get/${item.propInfoId}`);
|
|
|
|
|
return { ...item, propertyInformation: propInfo };
|
|
|
|
|
} catch {
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
return enriched;
|
2026-03-26 22:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getSaleProperty(id) {
|
2026-03-26 22:59:08 +00:00
|
|
|
const item = await apiFetch(`/SaleProperties/GetSaleProperties?id=${id}`);
|
|
|
|
|
const property = Array.isArray(item) ? item.find(p => p.id == id) || item[0] : item;
|
|
|
|
|
|
|
|
|
|
if (property?.propInfoId) {
|
|
|
|
|
try {
|
|
|
|
|
const propInfo = await apiFetch(`/Properties/Get/${property.propInfoId}`);
|
|
|
|
|
return { ...property, propertyInformation: propInfo };
|
|
|
|
|
} catch {
|
|
|
|
|
// ignore
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return property;
|
2026-03-26 22:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── Properties (generic) ───
|
|
|
|
|
|
|
|
|
|
export async function getProperty(id) {
|
|
|
|
|
return apiFetch(`/Properties/Get/${id}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── Recommendations ───
|
|
|
|
|
|
|
|
|
|
export async function getRecommendations() {
|
2026-03-26 22:46:57 +00:00
|
|
|
return apiFetch('/Recommendations/GetRecommendations');
|
2026-03-26 22:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getTopRecommendations(count = 10) {
|
2026-03-26 22:46:57 +00:00
|
|
|
return apiFetch(`/Recommendations/GetTopRecommendations?count=${count}`);
|
2026-03-26 22:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── Reservations ───
|
|
|
|
|
|
|
|
|
|
export async function getReservations() {
|
2026-03-26 22:46:57 +00:00
|
|
|
return apiFetch('/Reservations/GetReservations');
|
2026-03-26 22:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getReservation(id) {
|
2026-03-26 22:46:57 +00:00
|
|
|
return apiFetch(`/Reservations/GetReservation?id=${id}`);
|
2026-03-26 22:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function checkAvailability(propertyId, fromDate = null, toDate = null) {
|
|
|
|
|
const qs = new URLSearchParams();
|
|
|
|
|
if (fromDate) qs.set('fromDate', fromDate);
|
|
|
|
|
if (toDate) qs.set('toDate', toDate);
|
|
|
|
|
const query = qs.toString();
|
2026-03-26 22:46:57 +00:00
|
|
|
return apiFetch(`/Reservations/GetAvailable/${propertyId}${query ? `?${query}` : ''}`);
|
2026-03-26 22:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function bookReservation(data) {
|
2026-03-26 22:46:57 +00:00
|
|
|
return apiFetch('/Reservations/Book', {
|
2026-03-26 22:20:33 +00:00
|
|
|
method: 'POST',
|
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── Terms ───
|
|
|
|
|
|
|
|
|
|
export async function getTerms() {
|
2026-03-26 22:46:57 +00:00
|
|
|
return apiFetch('/Terms/GetTerms');
|
2026-03-26 22:20:33 +00:00
|
|
|
}
|