Initial role user app
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user