92 lines
2.4 KiB
TypeScript
92 lines
2.4 KiB
TypeScript
import { RowComment } from "@toolctrl/db/prisma-browser";
|
|
import { db } from "~/utils/db.server";
|
|
import { UserSimple } from "../users.db.server";
|
|
import { RowCommentReactionWithDetails } from "./rowCommentReaction.db.server";
|
|
import UserModelHelper from "~/utils/helpers/models/UserModelHelper";
|
|
|
|
export type RowCommentWithDetails = RowComment & {
|
|
createdByUser: UserSimple;
|
|
reactions: RowCommentReactionWithDetails[];
|
|
// replies: (RowComment & { createdByUser: User })[];
|
|
};
|
|
|
|
export async function getRowComments(rowId: string): Promise<RowCommentWithDetails[]> {
|
|
return await db.rowComment.findMany({
|
|
where: {
|
|
rowId,
|
|
// parentCommentId: null,
|
|
},
|
|
include: {
|
|
...UserModelHelper.includeSimpleCreatedByUser,
|
|
reactions: {
|
|
include: {
|
|
...UserModelHelper.includeSimpleCreatedByUser,
|
|
},
|
|
},
|
|
// replies: {
|
|
// include: {
|
|
// ...UserModelHelper.includeSimpleCreatedByUser,
|
|
// },
|
|
// },
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function getRowComment(id: string): Promise<RowCommentWithDetails | null> {
|
|
return await db.rowComment.findUnique({
|
|
where: {
|
|
id,
|
|
},
|
|
include: {
|
|
...UserModelHelper.includeSimpleCreatedByUser,
|
|
reactions: {
|
|
include: {
|
|
...UserModelHelper.includeSimpleCreatedByUser,
|
|
},
|
|
},
|
|
// replies: {
|
|
// include: {
|
|
// ...UserModelHelper.includeSimpleCreatedByUser,
|
|
// },
|
|
// },
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function getRowCommentInRow({ id, rowId }: { id: string; rowId: string }): Promise<RowCommentWithDetails | null> {
|
|
return await db.rowComment.findFirst({
|
|
where: {
|
|
id,
|
|
rowId,
|
|
},
|
|
include: {
|
|
...UserModelHelper.includeSimpleCreatedByUser,
|
|
reactions: {
|
|
include: {
|
|
...UserModelHelper.includeSimpleCreatedByUser,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function createRowComment(data: { createdByUserId: string; rowId: string; value: string }) {
|
|
return await db.rowComment.create({
|
|
data,
|
|
});
|
|
}
|
|
|
|
export async function updateRowCommentInRow({ id, rowId }: { id: string; rowId: string }, data: { value?: string; isDeleted?: boolean }) {
|
|
const updated = await db.rowComment.updateMany({
|
|
where: {
|
|
id,
|
|
rowId,
|
|
},
|
|
data,
|
|
});
|
|
if (updated.count !== 1) {
|
|
throw new Error("Row comment not found");
|
|
}
|
|
return updated;
|
|
}
|