|
|
|
@ -12,57 +12,52 @@ export const questionsAnswerCommentRouter = createProtectedRouter()
|
|
|
|
|
answerId: z.string(),
|
|
|
|
|
}),
|
|
|
|
|
async resolve({ ctx, input }) {
|
|
|
|
|
const questionAnswerCommentsData = await ctx.prisma.questionsAnswerComment.findMany({
|
|
|
|
|
include: {
|
|
|
|
|
user: {
|
|
|
|
|
select: {
|
|
|
|
|
name: true,
|
|
|
|
|
const questionAnswerCommentsData =
|
|
|
|
|
await ctx.prisma.questionsAnswerComment.findMany({
|
|
|
|
|
include: {
|
|
|
|
|
user: {
|
|
|
|
|
select: {
|
|
|
|
|
name: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
votes: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
votes: true,
|
|
|
|
|
},
|
|
|
|
|
orderBy: {
|
|
|
|
|
createdAt: 'desc',
|
|
|
|
|
},
|
|
|
|
|
where: {
|
|
|
|
|
...input,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
return questionAnswerCommentsData.map((data) => {
|
|
|
|
|
const votes:number = data.votes.reduce(
|
|
|
|
|
(previousValue:number, currentValue) => {
|
|
|
|
|
let result:number = previousValue;
|
|
|
|
|
|
|
|
|
|
switch(currentValue.vote) {
|
|
|
|
|
case Vote.UPVOTE:
|
|
|
|
|
result += 1
|
|
|
|
|
break;
|
|
|
|
|
case Vote.DOWNVOTE:
|
|
|
|
|
result -= 1
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
},
|
|
|
|
|
0
|
|
|
|
|
orderBy: {
|
|
|
|
|
createdAt: 'desc',
|
|
|
|
|
},
|
|
|
|
|
where: {
|
|
|
|
|
...input,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
return questionAnswerCommentsData.map((data) => {
|
|
|
|
|
const votes: number = data.votes.reduce(
|
|
|
|
|
(previousValue: number, currentValue) => {
|
|
|
|
|
let result: number = previousValue;
|
|
|
|
|
|
|
|
|
|
switch (currentValue.vote) {
|
|
|
|
|
case Vote.UPVOTE:
|
|
|
|
|
result += 1;
|
|
|
|
|
break;
|
|
|
|
|
case Vote.DOWNVOTE:
|
|
|
|
|
result -= 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
},
|
|
|
|
|
0,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let userName = "";
|
|
|
|
|
|
|
|
|
|
if (data.user) {
|
|
|
|
|
userName = data.user.name!;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const answerComment: AnswerComment = {
|
|
|
|
|
content: data.content,
|
|
|
|
|
id: data.id,
|
|
|
|
|
numVotes: votes,
|
|
|
|
|
updatedAt: data.updatedAt,
|
|
|
|
|
user: userName,
|
|
|
|
|
};
|
|
|
|
|
return answerComment;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
const answerComment: AnswerComment = {
|
|
|
|
|
content: data.content,
|
|
|
|
|
createdAt: data.createdAt,
|
|
|
|
|
id: data.id,
|
|
|
|
|
numVotes: votes,
|
|
|
|
|
updatedAt: data.updatedAt,
|
|
|
|
|
user: data.user?.name ?? '',
|
|
|
|
|
};
|
|
|
|
|
return answerComment;
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
.mutation('create', {
|
|
|
|
|
input: z.object({
|
|
|
|
@ -88,11 +83,12 @@ export const questionsAnswerCommentRouter = createProtectedRouter()
|
|
|
|
|
async resolve({ ctx, input }) {
|
|
|
|
|
const userId = ctx.session?.user?.id;
|
|
|
|
|
|
|
|
|
|
const answerCommentToUpdate = await ctx.prisma.questionsAnswerComment.findUnique({
|
|
|
|
|
where: {
|
|
|
|
|
id: input.id,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const answerCommentToUpdate =
|
|
|
|
|
await ctx.prisma.questionsAnswerComment.findUnique({
|
|
|
|
|
where: {
|
|
|
|
|
id: input.id,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (answerCommentToUpdate?.id !== userId) {
|
|
|
|
|
throw new TRPCError({
|
|
|
|
@ -118,11 +114,12 @@ export const questionsAnswerCommentRouter = createProtectedRouter()
|
|
|
|
|
async resolve({ ctx, input }) {
|
|
|
|
|
const userId = ctx.session?.user?.id;
|
|
|
|
|
|
|
|
|
|
const answerCommentToDelete = await ctx.prisma.questionsAnswerComment.findUnique({
|
|
|
|
|
where: {
|
|
|
|
|
id: input.id,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const answerCommentToDelete =
|
|
|
|
|
await ctx.prisma.questionsAnswerComment.findUnique({
|
|
|
|
|
where: {
|
|
|
|
|
id: input.id,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (answerCommentToDelete?.id !== userId) {
|
|
|
|
|
throw new TRPCError({
|
|
|
|
@ -144,11 +141,11 @@ export const questionsAnswerCommentRouter = createProtectedRouter()
|
|
|
|
|
}),
|
|
|
|
|
async resolve({ ctx, input }) {
|
|
|
|
|
const userId = ctx.session?.user?.id;
|
|
|
|
|
const {answerCommentId} = input
|
|
|
|
|
const { answerCommentId } = input;
|
|
|
|
|
|
|
|
|
|
return await ctx.prisma.questionsAnswerCommentVote.findUnique({
|
|
|
|
|
where: {
|
|
|
|
|
answerCommentId_userId : {answerCommentId,userId },
|
|
|
|
|
answerCommentId_userId: { answerCommentId, userId },
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
@ -176,13 +173,14 @@ export const questionsAnswerCommentRouter = createProtectedRouter()
|
|
|
|
|
}),
|
|
|
|
|
async resolve({ ctx, input }) {
|
|
|
|
|
const userId = ctx.session?.user?.id;
|
|
|
|
|
const {id, vote} = input
|
|
|
|
|
const { id, vote } = input;
|
|
|
|
|
|
|
|
|
|
const voteToUpdate = await ctx.prisma.questionsAnswerCommentVote.findUnique({
|
|
|
|
|
where: {
|
|
|
|
|
id: input.id,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const voteToUpdate =
|
|
|
|
|
await ctx.prisma.questionsAnswerCommentVote.findUnique({
|
|
|
|
|
where: {
|
|
|
|
|
id: input.id,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (voteToUpdate?.id !== userId) {
|
|
|
|
|
throw new TRPCError({
|
|
|
|
@ -208,10 +206,12 @@ export const questionsAnswerCommentRouter = createProtectedRouter()
|
|
|
|
|
async resolve({ ctx, input }) {
|
|
|
|
|
const userId = ctx.session?.user?.id;
|
|
|
|
|
|
|
|
|
|
const voteToDelete = await ctx.prisma.questionsAnswerCommentVote.findUnique({
|
|
|
|
|
where: {
|
|
|
|
|
id: input.id,
|
|
|
|
|
},});
|
|
|
|
|
const voteToDelete =
|
|
|
|
|
await ctx.prisma.questionsAnswerCommentVote.findUnique({
|
|
|
|
|
where: {
|
|
|
|
|
id: input.id,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (voteToDelete?.id !== userId) {
|
|
|
|
|
throw new TRPCError({
|
|
|
|
@ -226,4 +226,4 @@ export const questionsAnswerCommentRouter = createProtectedRouter()
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|