import { jsonSuccess } from "@/modules/core/server/api";
import { requirePermission } from "@/modules/core/server/auth";
import { readData } from "@/modules/core/server/store";

export async function GET(): Promise<Response> {
  const auth = await requirePermission("account:read", {
    action: "joinRequest.list",
    targetType: "joinRequest",
  });

  if (!auth.success) {
    return auth.response;
  }

  const data = await readData();
  const organizationId = auth.context.organization?.id;

  return jsonSuccess("加入申请已加载", {
    joinRequests: organizationId
      ? data.joinRequests.filter((request) => request.organizationId === organizationId)
      : data.joinRequests,
  });
}
