Files
SweetHome/app/utils/api.js
Claw AI eff0b41b78
All checks were successful
Build frontend / build (push) Successful in 57s
Add enums, AuthService, and integrate backend registration endpoints
- Add separate enum files: BuildingType, PropertyStatus, BookingStatus, CommissionType, IdentityType, UserRole, City, LoginMethod, OwnerType, CustomerType
- Add AuthService (addToken/getToken/deleteToken)
- Update api.js: use AuthService, add Owner/Add and Customer/Add endpoints
- Update login page to use AuthService for token storage
- Rewrite owner register: 3-step flow with OwnerType dropdown, backend integration, OTP verification
- Rewrite tenant register: 2-step flow with CustomerType dropdown, backend integration, OTP verification
- Update homepage and property detail to use enums instead of hardcoded maps
- Update AddPropertyForm to import from enums directly
- Add console logs and status toasts linked to API response messages
2026-03-27 18:03:12 +00:00

231 lines
6.3 KiB
JavaScript

import AuthService from '../services/AuthService';
const API_BASE = process.env.NEXT_PUBLIC_API_URL || 'http://45.93.137.91/api';
/**
* Generic API fetch — attaches auth token, unwraps { data } envelope
*/
async function apiFetch(endpoint, options = {}) {
const token = AuthService.getToken();
const headers = {
'Content-Type': 'application/json',
...(token && { Authorization: `Bearer ${token}` }),
...options.headers,
};
console.log('[API] Request:', options.method || 'GET', `${API_BASE}${endpoint}`);
const res = await fetch(`${API_BASE}${endpoint}`, {
...options,
headers,
});
console.log('[API] Response:', res.status, endpoint);
if (!res.ok && res.status !== 206) {
const text = await res.text().catch(() => '');
console.error('[API] Error:', res.status, text);
throw new Error(`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;
}
}
/**
* Auth fetch — no token attached, returns full { status, data, ok } for status-code handling
*/
async function authFetch(endpoint, body) {
console.log('[Auth] Request:', `${API_BASE}${endpoint}`);
const res = await fetch(`${API_BASE}${endpoint}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
});
console.log('[Auth] Response status:', res.status, endpoint);
const text = await res.text();
let data = null;
try {
data = text ? JSON.parse(text) : null;
if (data && typeof data === 'object' && 'data' in data) {
data = data.data;
}
} catch {
data = text;
}
// Build message from response for toast display
const message = (typeof data === 'object' && data?.message) ? data.message : null;
return { status: res.status, data, ok: res.ok || res.status === 206, message };
}
// ─── Rent Properties ───
export async function getRentProperties() {
return apiFetch('/RentProperties/GetRentProperties');
}
export async function getRentProperty(id) {
const items = await apiFetch('/RentProperties/GetRentProperties');
if (!Array.isArray(items)) return items;
return items.find(p => p.id == id) || items[0];
}
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();
return apiFetch(`/RentProperties/GetRentPropertiesLocations${query ? `?${query}` : ''}`);
}
// ─── Sale Properties ───
export async function getSaleProperties() {
return apiFetch('/SaleProperties/GetSaleProperties');
}
export async function getSaleProperty(id) {
const items = await apiFetch('/SaleProperties/GetSaleProperties');
if (!Array.isArray(items)) return items;
return items.find(p => p.id == id) || items[0];
}
// ─── Properties (generic) ───
export async function getProperty(id) {
return apiFetch(`/Properties/Get/${id}`);
}
// ─── Recommendations ───
export async function getRecommendations() {
return apiFetch('/Recommendations/GetRecommendations');
}
export async function getTopRecommendations(count = 10) {
return apiFetch(`/Recommendations/GetTopRecommendations?count=${count}`);
}
// ─── Reservations ───
export async function getReservations() {
return apiFetch('/Reservations/GetReservations');
}
export async function getReservation(id) {
return apiFetch(`/Reservations/GetReservation?id=${id}`);
}
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();
return apiFetch(`/Reservations/GetAvailable/${propertyId}${query ? `?${query}` : ''}`);
}
export async function bookReservation(data) {
return apiFetch('/Reservations/Book', {
method: 'POST',
body: JSON.stringify(data),
});
}
// ─── Terms ───
export async function getTerms() {
return apiFetch('/Terms/GetTerms');
}
// ─── Auth: Registration ───
/**
* Register a new owner
* @param {Object} data — { name, email, phoneNumber, whatsAppNumber, password, ownerType }
* @returns {Promise<{status, data, ok, message}>}
*/
export async function addOwner(data) {
console.log('[Auth] Registering owner:', data.email);
return authFetch('/Owner/Add', data);
}
/**
* Register a new customer/tenant
* @param {Object} data — { name, email, phoneNumber, password, customerType }
* @returns {Promise<{status, data, ok, message}>}
*/
export async function addCustomer(data) {
console.log('[Auth] Registering customer:', data.email);
return authFetch('/Customer/Add', data);
}
// ─── Auth: Login ───
export async function loginWithEmail(credential, password) {
console.log('[Auth] Login with email:', credential);
return authFetch('/Auth/LogInWithEmail', {
credential,
password,
device: 0,
appVersion: '',
});
}
export async function loginWithPhone(credential, password) {
console.log('[Auth] Login with phone:', credential);
return authFetch('/Auth/LogInWithPhoneNumber', {
credential,
password,
device: 0,
appVersion: '',
});
}
// ─── Auth: OTP ───
export async function sendEmailOTP() {
console.log('[Auth] Sending email OTP...');
return apiFetch('/Auth/SendEmailOTP', { method: 'POST' });
}
export async function sendPhoneOTP() {
console.log('[Auth] Sending phone OTP...');
return apiFetch('/Auth/SendPhoneNumberOTP', { method: 'POST' });
}
export async function verifyEmail(code) {
console.log('[Auth] Verifying email with code:', code);
return authFetch(`/Auth/VerifyEmail?code=${encodeURIComponent(code)}`, {});
}
export async function verifyPhone(code) {
console.log('[Auth] Verifying phone with code:', code);
return authFetch(`/Auth/VerifyPhoneNumber?code=${encodeURIComponent(code)}`, {});
}
// ─── Helpers ───
export function isEmail(value) {
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
}
export function isPhoneNumber(value) {
return /^\+?\d{7,15}$/.test(value.replace(/[\s\-()]/g, ''));
}