feat(verification): self-service request flow with mandatory review checklist

Gitea #10 Option B:
- new VerificationRequest model — owner submits a message via the pet
  edit page, moderator reviews against a fixed 4-item checklist
  (authenticity/impersonation risk, complete profile, good standing,
  established presence). All four met -> approved, Pet.isVerified flips
  on; any unmet -> auto-rejected with an itemized note of what's missing
- new admin queue /p/[secret]/verification (Moderator+ only, same gate
  as the audit log), status filter, review dialog enforces the checklist
- owner is notified automatically either way via a "PawFeed Team" DM —
  extracted the team-pet lookup/create logic (previously only inline in
  startTeamConversation) into src/lib/team-pet.ts, adding sendTeamMessage
  which reuses the normal Conversation/Message + MESSAGE notification
- review decisions log to ModerationLog with role+IP, same as every
  other Gitea #10 action this session
This commit is contained in:
admin committed 2026-08-12 10:03:59 +02:00
1 parent bc2dfa9a2d
commit a4c4568065
7 files changed
+606 -41

No files matched your search

+87
View File
@@ -0,0 +1,87 @@
import "server-only";
import type { PrismaClient } from "@prisma/client";
import { TRPCError } from "@trpc/server";
import { notify } from "@/lib/notify";
/**
* Ensures the "PawFeed Team" system pet (owned by the env-bootstrapped
* ADMIN_OWNER_ID) exists, creating it on first use. Shared by
* admin.startTeamConversation and sendTeamMessage below — previously this
* lookup/create logic lived only inline in startTeamConversation.
*/
export async function ensureTeamPet(prisma: PrismaClient): Promise<{ id: string }> {
const adminOwnerId = process.env.ADMIN_OWNER_ID;
if (!adminOwnerId) {
throw new TRPCError({
code: "PRECONDITION_FAILED",
message: "ADMIN_OWNER_ID is not configured.",
});
}
let teamPet = await prisma.pet.findFirst({
where: { ownerId: adminOwnerId, name: "PawFeed Team" },
select: { id: true },
});
if (!teamPet) {
// Ensure the Owner row + at least one Species exist before creating
// the pet — same FK requirements as any normal pet.create.
await prisma.owner.upsert({
where: { id: adminOwnerId },
update: {},
create: { id: adminOwnerId },
});
const species = await prisma.species.findFirst({ select: { id: true } });
if (!species) {
throw new TRPCError({
code: "PRECONDITION_FAILED",
message: "No Species rows exist — run the seed script first.",
});
}
teamPet = await prisma.pet.create({
data: {
ownerId: adminOwnerId,
speciesId: species.id,
name: "PawFeed Team",
bio: "Offizieller Support-Account von PawFeed.",
},
select: { id: true },
});
}
return teamPet;
}
/**
* Sends an automated message from the "PawFeed Team" system pet to a target
* pet — reuses the normal Conversation/Message model (same as any pet-to-pet
* DM) and fires the standard MESSAGE notification, so it shows up in the
* recipient's inbox/notifications exactly like a real message would.
*/
export async function sendTeamMessage(
prisma: PrismaClient,
targetPetId: string,
body: string
): Promise<void> {
const teamPet = await ensureTeamPet(prisma);
if (teamPet.id === targetPetId) return;
const [petAId, petBId] = [teamPet.id, targetPetId].sort();
const conversation = await prisma.conversation.upsert({
where: { petAId_petBId: { petAId, petBId } },
update: {},
create: { petAId, petBId },
});
await prisma.$transaction([
prisma.message.create({
data: { conversationId: conversation.id, senderPetId: teamPet.id, body },
}),
prisma.conversation.update({
where: { id: conversation.id },
data: { updatedAt: new Date() },
}),
]);
notify(prisma, { recipientPetId: targetPetId, type: "MESSAGE", actorPetId: teamPet.id });
}