Refactor API layer into structured domain-specific modules
- Created new api/ directory with domain-specific API modules: - api/client.ts: Base API client with error handling - api/auth.ts: Authentication endpoints - api/exchange.ts: Exchange/price endpoints - api/trades.ts: User trade endpoints - api/profile.ts: Profile management endpoints - api/invites.ts: Invite endpoints - api/admin.ts: Admin endpoints - api/index.ts: Centralized exports - Migrated all API calls from ad-hoc api.get/post/put to typed domain APIs - Updated all imports across codebase - Fixed test mocks to use new API structure - Fixed type issues in validation utilities - Removed old api.ts file Benefits: - Type-safe endpoints (no more string typos) - Centralized API surface (easy to discover endpoints) - Better organization (domain-specific modules) - Uses generated OpenAPI types automatically
This commit is contained in:
parent
6d0f125536
commit
a6fa6a8012
24 changed files with 529 additions and 255 deletions
156
frontend/app/api/admin.ts
Normal file
156
frontend/app/api/admin.ts
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
import { client } from "./client";
|
||||
import { components } from "../generated/api";
|
||||
|
||||
type AdminUserResponse = components["schemas"]["AdminUserResponse"];
|
||||
type PaginatedInvites = components["schemas"]["PaginatedResponse_InviteResponse_"];
|
||||
type AdminExchangeResponse = components["schemas"]["AdminExchangeResponse"];
|
||||
type AvailabilityResponse = components["schemas"]["AvailabilityResponse"];
|
||||
type PriceHistoryRecord = components["schemas"]["PriceHistoryResponse"];
|
||||
|
||||
interface CreateInviteRequest {
|
||||
godfather_id: number;
|
||||
}
|
||||
|
||||
interface UpdateAvailabilityRequest {
|
||||
date: string;
|
||||
slots: Array<{ start_time: string; end_time: string }>;
|
||||
}
|
||||
|
||||
interface CopyAvailabilityRequest {
|
||||
source_date: string;
|
||||
target_dates: string[];
|
||||
}
|
||||
|
||||
interface GetPastTradesParams {
|
||||
status?: string;
|
||||
user_search?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Admin API endpoints
|
||||
*/
|
||||
export const adminApi = {
|
||||
/**
|
||||
* Get all users (for dropdowns, etc.)
|
||||
*/
|
||||
getUsers(): Promise<AdminUserResponse[]> {
|
||||
return client.get<AdminUserResponse[]>("/api/admin/users");
|
||||
},
|
||||
|
||||
/**
|
||||
* Get paginated invites
|
||||
*/
|
||||
getInvites(page: number, perPage: number = 10, status?: string): Promise<PaginatedInvites> {
|
||||
const params = new URLSearchParams();
|
||||
params.append("page", page.toString());
|
||||
params.append("per_page", perPage.toString());
|
||||
if (status) {
|
||||
params.append("status", status);
|
||||
}
|
||||
return client.get<PaginatedInvites>(`/api/admin/invites?${params.toString()}`);
|
||||
},
|
||||
|
||||
/**
|
||||
* Create a new invite
|
||||
*/
|
||||
createInvite(request: CreateInviteRequest): Promise<void> {
|
||||
return client.post<void>("/api/admin/invites", request);
|
||||
},
|
||||
|
||||
/**
|
||||
* Revoke an invite
|
||||
*/
|
||||
revokeInvite(inviteId: number): Promise<void> {
|
||||
return client.post<void>(`/api/admin/invites/${inviteId}/revoke`);
|
||||
},
|
||||
|
||||
/**
|
||||
* Get upcoming trades
|
||||
*/
|
||||
getUpcomingTrades(): Promise<AdminExchangeResponse[]> {
|
||||
return client.get<AdminExchangeResponse[]>("/api/admin/trades/upcoming");
|
||||
},
|
||||
|
||||
/**
|
||||
* Get past trades with optional filters
|
||||
*/
|
||||
getPastTrades(params?: GetPastTradesParams): Promise<AdminExchangeResponse[]> {
|
||||
const searchParams = new URLSearchParams();
|
||||
if (params?.status) {
|
||||
searchParams.append("status", params.status);
|
||||
}
|
||||
if (params?.user_search) {
|
||||
searchParams.append("user_search", params.user_search);
|
||||
}
|
||||
const queryString = searchParams.toString();
|
||||
const url = queryString ? `/api/admin/trades/past?${queryString}` : "/api/admin/trades/past";
|
||||
return client.get<AdminExchangeResponse[]>(url);
|
||||
},
|
||||
|
||||
/**
|
||||
* Complete a trade
|
||||
*/
|
||||
completeTrade(publicId: string): Promise<AdminExchangeResponse> {
|
||||
return client.post<AdminExchangeResponse>(
|
||||
`/api/admin/trades/${encodeURIComponent(publicId)}/complete`,
|
||||
{}
|
||||
);
|
||||
},
|
||||
|
||||
/**
|
||||
* Mark a trade as no-show
|
||||
*/
|
||||
noShowTrade(publicId: string): Promise<AdminExchangeResponse> {
|
||||
return client.post<AdminExchangeResponse>(
|
||||
`/api/admin/trades/${encodeURIComponent(publicId)}/no-show`,
|
||||
{}
|
||||
);
|
||||
},
|
||||
|
||||
/**
|
||||
* Cancel a trade (admin)
|
||||
*/
|
||||
cancelTrade(publicId: string): Promise<AdminExchangeResponse> {
|
||||
return client.post<AdminExchangeResponse>(
|
||||
`/api/admin/trades/${encodeURIComponent(publicId)}/cancel`,
|
||||
{}
|
||||
);
|
||||
},
|
||||
|
||||
/**
|
||||
* Get availability for a date range
|
||||
*/
|
||||
getAvailability(fromDate: string, toDate: string): Promise<AvailabilityResponse> {
|
||||
return client.get<AvailabilityResponse>(
|
||||
`/api/admin/availability?from=${encodeURIComponent(fromDate)}&to=${encodeURIComponent(toDate)}`
|
||||
);
|
||||
},
|
||||
|
||||
/**
|
||||
* Update availability for a specific date
|
||||
*/
|
||||
updateAvailability(request: UpdateAvailabilityRequest): Promise<void> {
|
||||
return client.put<void>("/api/admin/availability", request);
|
||||
},
|
||||
|
||||
/**
|
||||
* Copy availability from one date to multiple dates
|
||||
*/
|
||||
copyAvailability(request: CopyAvailabilityRequest): Promise<void> {
|
||||
return client.post<void>("/api/admin/availability/copy", request);
|
||||
},
|
||||
|
||||
/**
|
||||
* Get price history records
|
||||
*/
|
||||
getPriceHistory(): Promise<PriceHistoryRecord[]> {
|
||||
return client.get<PriceHistoryRecord[]>("/api/audit/price-history");
|
||||
},
|
||||
|
||||
/**
|
||||
* Trigger a price fetch
|
||||
*/
|
||||
fetchPrice(): Promise<PriceHistoryRecord> {
|
||||
return client.post<PriceHistoryRecord>("/api/audit/price-history/fetch", {});
|
||||
},
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue