39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import { backendErrorResponse, BackendError, backendRequest } from "@/lib/backend";
|
|
import type { ApiEnvelope, AuthUser, StoreDetail } from "@/lib/types";
|
|
|
|
function fallbackStore(user: AuthUser): StoreDetail | null {
|
|
if (!user.storeId) return null;
|
|
|
|
return {
|
|
id: user.storeId,
|
|
name: user.storeName ?? "当前门店",
|
|
address: null,
|
|
phone: null,
|
|
status: undefined,
|
|
employees: []
|
|
};
|
|
}
|
|
|
|
export async function GET() {
|
|
try {
|
|
const user = await backendRequest<AuthUser>("/auth/me");
|
|
|
|
if (!user.storeId) {
|
|
return Response.json({ success: true, data: null } satisfies ApiEnvelope<StoreDetail | null>);
|
|
}
|
|
|
|
try {
|
|
const store = await backendRequest<StoreDetail>(`/stores/${user.storeId}`);
|
|
return Response.json({ success: true, data: store } satisfies ApiEnvelope<StoreDetail>);
|
|
} catch (error) {
|
|
if (error instanceof BackendError && [403, 404].includes(error.status)) {
|
|
return Response.json({ success: true, data: fallbackStore(user) } satisfies ApiEnvelope<StoreDetail | null>);
|
|
}
|
|
|
|
throw error;
|
|
}
|
|
} catch (error) {
|
|
return backendErrorResponse(error);
|
|
}
|
|
}
|