Skip to content

Commit

Permalink
fix bug in counting people
Browse files Browse the repository at this point in the history
  • Loading branch information
blackmann committed Feb 6, 2024
1 parent 9e17fca commit 12847e8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
11 changes: 9 additions & 2 deletions client/app/lib/prisma.server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Prisma, PrismaClient } from "@prisma/client";

let prisma: PrismaClient;
let prisma: PrismaClient | ReturnType<typeof installMiddleware>;
declare global {
var __db: ReturnType<typeof installMiddleware> | undefined;
// biome-ignore lint/style/noVar: <explanation>
var __db: typeof prisma | undefined;
}

if (process.env.NODE_ENV === "production") {
Expand Down Expand Up @@ -33,6 +34,12 @@ function installMiddleware(prisma: PrismaClient) {
where: { ...args.where, deleted: false },
});
},
async count({ args }) {
return prisma.post.count({
...args,
where: { ...args.where, deleted: false },
});
},
async findFirst({ args }) {
return prisma.post.findFirst({
...args,
Expand Down
36 changes: 23 additions & 13 deletions client/app/routes/discussions_.$id.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ export const action = async ({ request, params }: ActionFunctionArgs) => {
const post = await prisma.post.findFirst({ where: { id: postId } });

await prisma.post.delete({ where: { id: postId, userId: userId } });
if (post?.parentId) {
await updatePostProps(post.parentId);
}

if (!post?.parentId) {
return redirect("/discussions");
Expand All @@ -63,19 +66,7 @@ export const action = async ({ request, params }: ActionFunctionArgs) => {
const data = await request.json();

await createPost(data, userId);

const comments = await prisma.post.count({
where: { parentId: data.parentId },
});

const people = await prisma.user.count({
where: { Post: { every: { id: data.parentId } } },
});

await prisma.post.update({
where: { id: data.parentId },
data: { commentsCount: comments, people },
});
await updatePostProps(data.parentId);

return json({}, { status: 201 });
}
Expand All @@ -84,6 +75,25 @@ export const action = async ({ request, params }: ActionFunctionArgs) => {
return json({}, { status: 405 });
};

async function updatePostProps(postId: number) {
const comments = await prisma.post.count({
where: { parentId: postId },
});

const people = await prisma.user.count({
where: {
Post: {
some: { OR: [{ id: postId }, { parentId: postId }], deleted: false },
},
},
});

await prisma.post.update({
where: { id: postId },
data: { commentsCount: comments, people },
});
}

export const meta: MetaFunction<typeof loader> = ({ data }) => {
return [{ title: `Discussions | ${data?.schoolName} | compa` }];
};
Expand Down
6 changes: 5 additions & 1 deletion client/app/routes/people.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ export const loader = async ({ request }: LoaderFunctionArgs) => {
const postId = Number(post);

const people = await prisma.user.findMany({
where: { Post: { some: { OR: [{ id: postId }, { parentId: postId }] } } },
where: {
Post: {
some: { OR: [{ id: postId }, { parentId: postId }], deleted: false },
},
},
});

return json({ people });
Expand Down

0 comments on commit 12847e8

Please sign in to comment.