feat(01-03): extend pets router (update/delete/listSpecies/listBreeds) + breed seed

- Add assertPetOwnership helper (T-3-01: ownership check before every mutation)
- Add pets.update with Zod limits (name<=30, bio<=150, adoptionStory<=500)
- Add pets.delete with ownership assertion
- Add pets.listSpecies (protectedProcedure — T-3-04 scraping prevention)
- Add pets.listBreeds filtered by speciesId
- Extend seed with 45 breeds: 20 dog, 15 cat, 10 bird (idempotent upserts)
- Install react-hook-form + @hookform/resolvers for PetForm (Task 2)
This commit is contained in:
admin committed 2026-06-06 10:07:04 +02:00
1 parent 5f781fc20e
commit 69553b103b
4 files changed
+189 -11

No files matched your search

+36
View File
@@ -12,6 +12,7 @@
"@aws-sdk/s3-request-presigner": "^3.1062.0",
"@base-ui/react": "^1.5.0",
"@clerk/nextjs": "^7.4.3",
"@hookform/resolvers": "^5.4.0",
"@prisma/adapter-pg": "^7.8.0",
"@prisma/client": "^7.8.0",
"@prisma/extension-accelerate": "^3.0.1",
@@ -31,6 +32,7 @@
"prisma": "^7.8.0",
"react": "19.2.4",
"react-dom": "19.2.4",
"react-hook-form": "^7.77.0",
"server-only": "^0.0.1",
"shadcn": "^4.10.0",
"sonner": "^2.0.7",
@@ -2209,6 +2211,18 @@
"hono": "^4"
}
},
"node_modules/@hookform/resolvers": {
"version": "5.4.0",
"resolved": "https://registry.npmjs.org/@hookform/resolvers/-/resolvers-5.4.0.tgz",
"integrity": "sha512-EIsqr/t/qbinPIhGjMdtvutIN1Kk4uwbROE9/UQ93CAVGR7GkA7Y92+fX80OzXi/OB67jVFYwKGO1WzkxmkFZw==",
"license": "MIT",
"dependencies": {
"@standard-schema/utils": "^0.3.0"
},
"peerDependencies": {
"react-hook-form": "^7.55.0"
}
},
"node_modules/@humanfs/core": {
"version": "0.19.2",
"resolved": "https://registry.npmjs.org/@humanfs/core/-/core-0.19.2.tgz",
@@ -4041,6 +4055,12 @@
"integrity": "sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w==",
"license": "MIT"
},
"node_modules/@standard-schema/utils": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/@standard-schema/utils/-/utils-0.3.0.tgz",
"integrity": "sha512-e7Mew686owMaPJVNNLs55PUvgz371nKgwsc4vxE49zsODpJEnxgxRo2y/OKrqueavXgZNMDVj3DdHFlaSAeU8g==",
"license": "MIT"
},
"node_modules/@supabase/auth-js": {
"version": "2.107.0",
"resolved": "https://registry.npmjs.org/@supabase/auth-js/-/auth-js-2.107.0.tgz",
@@ -11481,6 +11501,22 @@
"react": "^19.2.4"
}
},
"node_modules/react-hook-form": {
"version": "7.77.0",
"resolved": "https://registry.npmjs.org/react-hook-form/-/react-hook-form-7.77.0.tgz",
"integrity": "sha512-Sslh9YDYc0GDlWT/lxasnIduNo4v3yyvqRGvmGKUre5AFjDs/HV9/OafHGD8d+sB2yoL4UIL9L8X9i0WlZZebg==",
"license": "MIT",
"engines": {
"node": ">=18.0.0"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/react-hook-form"
},
"peerDependencies": {
"react": "^16.8.0 || ^17 || ^18 || ^19"
}
},
"node_modules/react-is": {
"version": "16.13.1",
"resolved": "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz",
+2
View File
@@ -16,6 +16,7 @@
"@aws-sdk/s3-request-presigner": "^3.1062.0",
"@base-ui/react": "^1.5.0",
"@clerk/nextjs": "^7.4.3",
"@hookform/resolvers": "^5.4.0",
"@prisma/adapter-pg": "^7.8.0",
"@prisma/client": "^7.8.0",
"@prisma/extension-accelerate": "^3.0.1",
@@ -35,6 +36,7 @@
"prisma": "^7.8.0",
"react": "19.2.4",
"react-dom": "19.2.4",
"react-hook-form": "^7.77.0",
"server-only": "^0.0.1",
"shadcn": "^4.10.0",
"sonner": "^2.0.7",
+73 -9
View File
@@ -4,8 +4,8 @@ import { Pool } from "pg";
/**
* Seed script for the OnlyPets database.
* Seeds Species (Dog, Cat, Bird) idempotently.
* Plan 03 will extend this with breed taxonomy data.
* Seeds Species (Dog, Cat, Bird) and Breed taxonomy idempotently.
* Uses upsert so it is safe to run multiple times.
*
* Run: npx tsx prisma/seed.ts
* Or: npx prisma db seed
@@ -21,22 +21,86 @@ if (!databaseUrl) {
const adapter = new PrismaPg(new Pool({ connectionString: databaseUrl }));
const prisma = new PrismaClient({ adapter });
const DOG_BREEDS = [
"Labrador Retriever",
"Golden Retriever",
"German Shepherd",
"French Bulldog",
"Bulldog",
"Poodle",
"Beagle",
"Rottweiler",
"German Shorthaired Pointer",
"Dachshund",
"Pembroke Welsh Corgi",
"Australian Shepherd",
"Yorkshire Terrier",
"Boxer",
"Cavalier King Charles Spaniel",
"Doberman Pinscher",
"Great Dane",
"Miniature Schnauzer",
"Siberian Husky",
"Border Collie",
];
const CAT_BREEDS = [
"Domestic Shorthair",
"Domestic Longhair",
"Maine Coon",
"Ragdoll",
"Siamese",
"Persian",
"Bengal",
"British Shorthair",
"Sphynx",
"Scottish Fold",
"American Shorthair",
"Abyssinian",
"Norwegian Forest Cat",
"Birman",
"Russian Blue",
];
const BIRD_BREEDS = [
"Budgerigar (Budgie)",
"Cockatiel",
"Lovebird",
"African Grey Parrot",
"Cockatoo",
"Conure",
"Macaw",
"Canary",
"Finch",
"Amazon Parrot",
];
async function main() {
console.log("Seeding species...");
const species = [
{ name: "Dog" },
{ name: "Cat" },
{ name: "Bird" },
const speciesData = [
{ name: "Dog", breeds: DOG_BREEDS },
{ name: "Cat", breeds: CAT_BREEDS },
{ name: "Bird", breeds: BIRD_BREEDS },
];
for (const s of species) {
const result = await prisma.species.upsert({
for (const s of speciesData) {
const species = await prisma.species.upsert({
where: { name: s.name },
update: {},
create: { name: s.name },
});
console.log(` Upserted species: ${result.name} (${result.id})`);
console.log(` Upserted species: ${species.name} (${species.id})`);
console.log(` Seeding ${s.breeds.length} breeds for ${species.name}...`);
for (const breedName of s.breeds) {
const breed = await prisma.breed.upsert({
where: { speciesId_name: { speciesId: species.id, name: breedName } },
update: {},
create: { speciesId: species.id, name: breedName },
});
console.log(` Upserted breed: ${breed.name}`);
}
}
console.log("Seeding complete.");
+78 -2
View File
@@ -1,16 +1,17 @@
import { z } from "zod";
import { TRPCError } from "@trpc/server";
import { router, protectedProcedure } from "@/trpc/init";
import type { PrismaClient } from "@prisma/client";
/**
* Ownership assertion helper — verifies that a pet belongs to the requesting
* owner before any mutation or scoped query proceeds.
*
* Security (T-1-02): never trust client-supplied pet ids without DB verification.
* Security (T-3-01, T-1-02): never trust client-supplied pet ids without DB verification.
* Every pet mutation asserts pet.ownerId === ctx.userId (ASVS V4).
*/
async function assertPetOwnership(
prisma: Parameters<Parameters<typeof protectedProcedure.mutation>[0]>[0]["ctx"]["prisma"],
prisma: PrismaClient,
petId: string,
userId: string
) {
@@ -30,6 +31,7 @@ export const petsRouter = router({
* Upserts the Owner row first (ensures FK exists for new Clerk users).
*
* Security: ownerId is always set from ctx.userId (server-side) — never from client input.
* T-3-02: Zod enforces name<=30, bio<=150, adoptionStory<=500 at the tRPC layer.
*/
create: protectedProcedure
.input(
@@ -79,6 +81,7 @@ export const petsRouter = router({
/**
* Get a single pet by id.
* Asserts ownership: returns FORBIDDEN if the pet doesn't belong to the requesting owner.
* T-3-03: ownership assertion prevents information disclosure.
*/
byId: protectedProcedure
.input(z.object({ petId: z.string().min(1) }))
@@ -100,4 +103,77 @@ export const petsRouter = router({
return pet;
}),
/**
* Update a pet's details.
* Asserts ownership before any DB write (T-3-01).
* T-3-02: Zod enforces character limits server-side.
*/
update: protectedProcedure
.input(
z.object({
petId: z.string().min(1),
name: z.string().max(30).optional(),
bio: z.string().max(150).optional().nullable(),
adoptionStory: z.string().max(500).optional().nullable(),
breedId: z.string().optional().nullable(),
})
)
.mutation(async ({ ctx, input }) => {
await assertPetOwnership(ctx.prisma, input.petId, ctx.userId);
const { petId, ...data } = input;
// Only update fields that were explicitly provided
const updateData: Record<string, unknown> = {};
if (data.name !== undefined) updateData.name = data.name;
if (data.bio !== undefined) updateData.bio = data.bio;
if (data.adoptionStory !== undefined)
updateData.adoptionStory = data.adoptionStory;
if (data.breedId !== undefined) updateData.breedId = data.breedId;
return ctx.prisma.pet.update({
where: { id: petId },
data: updateData,
include: {
species: true,
breed: true,
},
});
}),
/**
* Delete a pet by id.
* Asserts ownership before deletion (T-3-01).
*/
delete: protectedProcedure
.input(z.object({ petId: z.string().min(1) }))
.mutation(async ({ ctx, input }) => {
await assertPetOwnership(ctx.prisma, input.petId, ctx.userId);
await ctx.prisma.pet.delete({ where: { id: input.petId } });
return { success: true };
}),
/**
* List all species.
* Protected procedure — auth required to prevent unauthenticated taxonomy scraping (T-3-04).
*/
listSpecies: protectedProcedure.query(async ({ ctx }) => {
return ctx.prisma.species.findMany({ orderBy: { name: "asc" } });
}),
/**
* List breeds for a given species.
* Protected procedure (T-3-04).
*/
listBreeds: protectedProcedure
.input(z.object({ speciesId: z.string().min(1) }))
.query(async ({ ctx, input }) => {
return ctx.prisma.breed.findMany({
where: { speciesId: input.speciesId },
orderBy: { name: "asc" },
});
}),
});