profit-planet-frontend/src/app/admin/pool-management/hooks/addPool.ts

54 lines
1.2 KiB
TypeScript

import { authFetch } from '../../../utils/authFetch';
export type AddPoolPayload = {
pool_name: string;
description?: string;
price: number;
pool_type: 'coffee' | 'other';
is_active?: boolean;
};
export async function addPool(payload: AddPoolPayload) {
const BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:3001';
const url = `${BASE_URL}/api/admin/pools`;
const res = await authFetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
body: JSON.stringify(payload),
});
let body: any = null;
try {
body = await res.json();
} catch {
body = null;
}
const ok = res.status === 201 || res.ok;
const message =
body?.message ||
(res.status === 409
? 'Pool name already exists.'
: res.status === 400
? 'Invalid request. Check pool data.'
: res.status === 401
? 'Unauthorized.'
: res.status === 403
? 'Forbidden.'
: res.status === 500
? 'Internal server error.'
: !ok
? `Request failed (${res.status}).`
: '');
return {
ok,
status: res.status,
body,
message,
};
}