ed34b186f9
- New /refer page with share/copy/stats - Fixed modal z-index conflicts (z-50 → z-[60]) across forum, groups, souq, halal-monitor - Seed route: forum categories, marketplace listings, private groups - Mufti/Daie personas now available in Premium tier - Fixed referral share link: falahos.my/mobile/auth?ref=CODE - Fixed client-side persona access check for premium
45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { prisma } from "@/lib/prisma";
|
|
import { requireAuth } from "@/lib/auth";
|
|
import { encodeReferralCode } from "@/lib/referral";
|
|
|
|
export async function GET(req: NextRequest) {
|
|
const jwtPayload = await requireAuth(req);
|
|
if (!jwtPayload) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
try {
|
|
const userId = jwtPayload.userId;
|
|
|
|
const [totalReferrals, upgradedCount, earnings] = await Promise.all([
|
|
prisma.referral.count({
|
|
where: { referrerId: userId },
|
|
}),
|
|
prisma.referral.count({
|
|
where: { referrerId: userId, status: "upgraded" },
|
|
}),
|
|
prisma.referral.aggregate({
|
|
where: { referrerId: userId },
|
|
_sum: { rewardFlh: true },
|
|
}),
|
|
]);
|
|
|
|
const referralCode = encodeReferralCode(userId);
|
|
|
|
return NextResponse.json({
|
|
totalReferrals,
|
|
upgradedCount,
|
|
totalEarned: earnings._sum.rewardFlh || 0,
|
|
referralCode,
|
|
shareLink: `${process.env.NEXT_PUBLIC_APP_URL || "https://falahos.my/mobile"}/auth?ref=${referralCode}`,
|
|
});
|
|
} catch (error) {
|
|
console.error("Referral stats error:", error);
|
|
return NextResponse.json(
|
|
{ error: "Failed to get referral stats" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|