feat: engagement-counter denormalization, pagination fixes, dead-code cleanup
Bundles the 2026-07-12 code-audit session (Clusters A/B/C/D partial/E/F): denormalized Post/Advertisement reaction/comment/repost counters synced transactionally instead of live _count queries; real cursor-based pagination for followers/following/blocks lists; assertPetOwnership + formatRelativeTime centralized; dead r2.ts + AWS SDK deps removed; missing DB indexes added; account-deletion flow, mention notifications, pull-to-refresh feed, and mobile UI/i18n fixes from the surrounding sessions. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e91705063f
commit
f14be58c06
182 files changed
+10562
-724
No files matched your search
@@ -0,0 +1,68 @@
|
||||
import { auth, clerkClient } from "@clerk/nextjs/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { deleteFiles } from "@/lib/supabase-storage";
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const { userId } = await auth();
|
||||
if (!userId) {
|
||||
return Response.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const body = (await req.json()) as { petNameConfirmation?: string };
|
||||
const confirmation = body.petNameConfirmation?.trim();
|
||||
|
||||
if (!confirmation) {
|
||||
return Response.json({ error: "Pet name required" }, { status: 400 });
|
||||
}
|
||||
|
||||
const pets = await prisma.pet.findMany({
|
||||
where: { ownerId: userId },
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
avatarKey: true,
|
||||
posts: {
|
||||
where: { type: "PHOTO" },
|
||||
select: { images: { select: { storageKey: true } } },
|
||||
},
|
||||
stories: { select: { storageKey: true } },
|
||||
},
|
||||
});
|
||||
|
||||
const validNames = pets.map((p) => p.name.toLowerCase());
|
||||
if (!validNames.includes(confirmation.toLowerCase())) {
|
||||
return Response.json({ error: "Pet name does not match" }, { status: 400 });
|
||||
}
|
||||
|
||||
// Collect all Supabase storage keys across avatars, post images, and stories
|
||||
const storageKeys: string[] = [];
|
||||
for (const pet of pets) {
|
||||
if (pet.avatarKey) storageKeys.push(pet.avatarKey);
|
||||
for (const post of pet.posts) {
|
||||
for (const image of post.images) {
|
||||
storageKeys.push(image.storageKey);
|
||||
}
|
||||
}
|
||||
for (const story of pet.stories) {
|
||||
storageKeys.push(story.storageKey);
|
||||
}
|
||||
}
|
||||
|
||||
// Delete storage files — non-blocking on failure (logged inside deleteFiles)
|
||||
await deleteFiles(storageKeys);
|
||||
|
||||
// Delete all pets — cascades posts, comments, reactions, follows, notifications, etc.
|
||||
await prisma.pet.deleteMany({ where: { ownerId: userId } });
|
||||
|
||||
// Delete invite codes this owner issued
|
||||
await prisma.inviteCode.deleteMany({ where: { issuedById: userId } });
|
||||
|
||||
// Delete Owner record — cascades AdminRole + UserBan
|
||||
await prisma.owner.delete({ where: { id: userId } });
|
||||
|
||||
// Delete Clerk user account — this invalidates the current session
|
||||
const clerk = await clerkClient();
|
||||
await clerk.users.deleteUser(userId);
|
||||
|
||||
return Response.json({ success: true });
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { redis } from "@/lib/redis";
|
||||
import { trimFeed } from "@/lib/feed-helpers";
|
||||
|
||||
// Vercel cron sends `Authorization: Bearer <CRON_SECRET>`.
|
||||
// Set CRON_SECRET in your Vercel environment variables.
|
||||
function authorized(req: Request): boolean {
|
||||
const secret = process.env.CRON_SECRET;
|
||||
if (!secret) return false;
|
||||
return req.headers.get("authorization") === `Bearer ${secret}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trims every pet's Redis feed sorted set down to the documented 90-day
|
||||
* retention window (feed-helpers.ts FEED_RETENTION_MS). Without this cron,
|
||||
* feed:{petId} sorted sets grow unbounded forever.
|
||||
*/
|
||||
export async function GET(req: Request) {
|
||||
if (!authorized(req)) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const pets = await prisma.pet.findMany({ select: { id: true } });
|
||||
|
||||
for (const pet of pets) {
|
||||
await trimFeed(redis, pet.id);
|
||||
}
|
||||
|
||||
return NextResponse.json({ ok: true, petsTrimmed: pets.length });
|
||||
}
|
||||
Reference in new issue
Block a user