import { prisma } from "./src/constants";

const roles = await prisma.role.findMany({ select: { title: true, moniker: true, permissions: true } });

for (const role of roles) {
  const token = role.permissions;
  if (!token) { console.log(role.title, "- NO PERMISSIONS"); continue; }
  try {
    const payload = JSON.parse(Buffer.from(token.split(".")[1], "base64").toString());
    const perms: string[] = payload.permissions || [];
    const relevant = perms.filter((p: string) =>
      p.includes("sales.opportunity.product") ||
      p.includes("sales.opportunity.*") ||
      p.includes("sales.*") ||
      p === "*"
    );
    if (relevant.length > 0) {
      console.log("=== " + role.title + " (" + role.moniker + ") ===");
      relevant.forEach((p: string) => console.log("  " + p));
    }
  } catch(e: any) { console.log(role.title, "- parse error:", e.message); }
}

await prisma.$disconnect();
