|
|
|
@ -4,16 +4,43 @@ import { Vote } from '@prisma/client';
|
|
|
|
|
import { createRouter } from '../context';
|
|
|
|
|
|
|
|
|
|
import type { AnswerComment } from '~/types/questions';
|
|
|
|
|
import { SortOrder, SortType } from '~/types/questions.d';
|
|
|
|
|
|
|
|
|
|
export const questionsAnswerCommentRouter = createRouter().query(
|
|
|
|
|
'getAnswerComments',
|
|
|
|
|
{
|
|
|
|
|
input: z.object({
|
|
|
|
|
answerId: z.string(),
|
|
|
|
|
cursor: z.string().nullish(),
|
|
|
|
|
limit: z.number().min(1).default(50),
|
|
|
|
|
sortOrder: z.nativeEnum(SortOrder),
|
|
|
|
|
sortType: z.nativeEnum(SortType),
|
|
|
|
|
}),
|
|
|
|
|
async resolve({ ctx, input }) {
|
|
|
|
|
const { answerId, cursor } = input;
|
|
|
|
|
|
|
|
|
|
const sortCondition =
|
|
|
|
|
input.sortType === SortType.TOP
|
|
|
|
|
? [
|
|
|
|
|
{
|
|
|
|
|
upvotes: input.sortOrder,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: input.sortOrder,
|
|
|
|
|
},
|
|
|
|
|
]
|
|
|
|
|
: [
|
|
|
|
|
{
|
|
|
|
|
updatedAt: input.sortOrder,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: input.sortOrder,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const questionAnswerCommentsData =
|
|
|
|
|
await ctx.prisma.questionsAnswerComment.findMany({
|
|
|
|
|
cursor: cursor ? { id: cursor } : undefined,
|
|
|
|
|
include: {
|
|
|
|
|
user: {
|
|
|
|
|
select: {
|
|
|
|
@ -23,14 +50,13 @@ export const questionsAnswerCommentRouter = createRouter().query(
|
|
|
|
|
},
|
|
|
|
|
votes: true,
|
|
|
|
|
},
|
|
|
|
|
orderBy: {
|
|
|
|
|
createdAt: 'desc',
|
|
|
|
|
},
|
|
|
|
|
orderBy: sortCondition,
|
|
|
|
|
take: input.limit + 1,
|
|
|
|
|
where: {
|
|
|
|
|
answerId: input.answerId,
|
|
|
|
|
answerId,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
return questionAnswerCommentsData.map((data) => {
|
|
|
|
|
const processedQuestionAnswerCommentsData = questionAnswerCommentsData.map((data) => {
|
|
|
|
|
const votes: number = data.votes.reduce(
|
|
|
|
|
(previousValue: number, currentValue) => {
|
|
|
|
|
let result: number = previousValue;
|
|
|
|
@ -59,6 +85,22 @@ export const questionsAnswerCommentRouter = createRouter().query(
|
|
|
|
|
};
|
|
|
|
|
return answerComment;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let nextCursor: typeof cursor | undefined = undefined;
|
|
|
|
|
|
|
|
|
|
if (questionAnswerCommentsData.length > input.limit) {
|
|
|
|
|
const nextItem = questionAnswerCommentsData.pop()!;
|
|
|
|
|
processedQuestionAnswerCommentsData.pop();
|
|
|
|
|
|
|
|
|
|
const nextIdCursor: string | undefined = nextItem.id;
|
|
|
|
|
|
|
|
|
|
nextCursor = nextIdCursor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
nextCursor,
|
|
|
|
|
processedQuestionAnswerCommentsData,
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|