Skip to content

Commit

Permalink
🐛 fix: create documents with categories and description (#766)
Browse files Browse the repository at this point in the history
* chore: add command to run all tests

* chore: prisma logs to pino

* 🐛 fix: don't discard description and categories when creating documents
  • Loading branch information
larwaa authored Jun 27, 2024
1 parent 9b79c32 commit c14b746
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 4 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
"test:integration:ci": "run-s setup 'integration-test {@}' -- --reporters='jest-junit' --reporters='default' --collect-coverage",
"test:integration": "run-s docker:up 'integration-test {@}' --",
"test": "dotenv -e .env.test -- pnpm jest",
"test:all": "run-s docker:up 'test-all {@}' --",
"test-all": "dotenv -e .env.test -- pnpm jest --test-match \"**/!(dev).test.ts\"",
"test:ci": "pnpm run setup && dotenv -e .env.test -- pnpm jest --reporters='jest-junit' --reporters='default' --collect-coverage",
"test:dev": "NODE_ENV=production pnpm jest --reporters='jest-junit' --reporters='default' --config jest.dev.config.cjs",
"jest": "node --experimental-vm-modules --no-warnings=ExperimentalWarning ./node_modules/jest/bin/jest.js",
Expand Down
21 changes: 20 additions & 1 deletion src/lib/fastify/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,26 @@ type FastifyPrismaPluginOptions = {

declare module "fastify" {
interface FastifyInstance {
database: PrismaClient;
database: PrismaClient<{
log: (
| {
emit: "event";
level: "error";
}
| {
emit: "event";
level: "warn";
}
| {
emit: "event";
level: "info";
}
| {
emit: "event";
level: "query";
}
)[];
}>;
}
}

Expand Down
23 changes: 22 additions & 1 deletion src/lib/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,28 @@ import { env } from "~/config.js";
const globalForPrisma = globalThis as unknown as {
prisma: PrismaClient;
};
export const prisma = globalForPrisma.prisma || new PrismaClient();
export const prisma =
globalForPrisma.prisma ||
new PrismaClient({
log: [
{
emit: "event",
level: "error",
},
{
emit: "event",
level: "warn",
},
{
emit: "event",
level: "info",
},
{
emit: "event",
level: "query",
},
],
});
if (env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;

export default prisma;
Expand Down
12 changes: 12 additions & 0 deletions src/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,18 @@ async function registerServices(
Sentry.setupFastifyErrorHandler(serverInstance);
await serverInstance.register(fastifyPrisma, { client: prisma });
const database = serverInstance.database;
database.$on("error", (e) => {
serverInstance.log.child({ service: "prisma" }).error(e);
});
database.$on("warn", (e) => {
serverInstance.log.child({ service: "prisma" }).warn(e);
});
database.$on("info", (e) => {
serverInstance.log.child({ service: "prisma" }).info(e);
});
database.$on("query", (e) => {
serverInstance.log.child({ service: "prisma" }).debug(e);
});

const cabinRepository = new CabinRepository(database);
const userRepository = new UserRepository(database);
Expand Down
10 changes: 9 additions & 1 deletion src/services/documents/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ function buildDocuments({

const schema = z.object({
name: z.string().min(1),
description: z
.string()
.nullish()
.transform((val) => val ?? undefined),
categories: z
.array(z.object({ name: z.string().min(1) }))
.nullish()
.transform((val) => val ?? undefined),
});
const validationResult = schema.safeParse(data);
if (!validationResult.success)
Expand Down Expand Up @@ -154,7 +162,7 @@ function buildDocuments({

const createDocumentResult = await repository.documents.create(ctx, {
fileId: file.id,
name: data.name,
...validationResult.data,
});

if (!createDocumentResult.ok)
Expand Down
15 changes: 14 additions & 1 deletion src/services/documents/documents.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,27 @@ describe("Documents service", () => {
document: makeDocument(),
},
});
const input = {
name: faker.word.adjective(),
description: faker.lorem.sentence(),
categories: [
{
name: faker.lorem.word(),
},
],
};

const result = await service.create(
makeMockContext({ id: faker.string.uuid() }),
{
name: faker.word.adjective(),
fileExtension: faker.string.uuid(),
...input,
},
);
expect(repository.documents.create).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining(input),
);
expect(result).toEqual(
Result.success({
document: expect.objectContaining({
Expand Down

0 comments on commit c14b746

Please sign in to comment.