|
|
|
@ -101,128 +101,212 @@ export const questionsQuestionCommentUserRouter = createProtectedRouter()
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
.mutation('createVote', {
|
|
|
|
|
.mutation('setUpVote', {
|
|
|
|
|
input: z.object({
|
|
|
|
|
questionCommentId: z.string(),
|
|
|
|
|
vote: z.nativeEnum(Vote),
|
|
|
|
|
}),
|
|
|
|
|
async resolve({ ctx, input }) {
|
|
|
|
|
const userId = ctx.session?.user?.id;
|
|
|
|
|
const { questionCommentId, vote } = input;
|
|
|
|
|
|
|
|
|
|
const incrementValue: number = vote === Vote.UPVOTE ? 1 : -1;
|
|
|
|
|
const { questionCommentId } = input;
|
|
|
|
|
|
|
|
|
|
const [questionCommentVote] = await ctx.prisma.$transaction([
|
|
|
|
|
ctx.prisma.questionsQuestionCommentVote.create({
|
|
|
|
|
data: {
|
|
|
|
|
questionCommentId,
|
|
|
|
|
userId,
|
|
|
|
|
vote,
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
ctx.prisma.questionsQuestionComment.update({
|
|
|
|
|
data: {
|
|
|
|
|
upvotes: {
|
|
|
|
|
increment: incrementValue,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
return await ctx.prisma.$transaction(async (tx) => {
|
|
|
|
|
const vote = await tx.questionsQuestionCommentVote.findUnique({
|
|
|
|
|
where: {
|
|
|
|
|
id: questionCommentId,
|
|
|
|
|
questionCommentId_userId: { questionCommentId, userId },
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
]);
|
|
|
|
|
return questionCommentVote;
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (vote === null) {
|
|
|
|
|
const createdVote = await tx.questionsQuestionCommentVote.create({
|
|
|
|
|
data: {
|
|
|
|
|
questionCommentId,
|
|
|
|
|
userId,
|
|
|
|
|
vote: Vote.UPVOTE,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await tx.questionsQuestionComment.update({
|
|
|
|
|
data: {
|
|
|
|
|
upvotes: {
|
|
|
|
|
increment: 1,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
where: {
|
|
|
|
|
id: questionCommentId,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return createdVote
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (vote!.userId !== userId) {
|
|
|
|
|
throw new TRPCError({
|
|
|
|
|
code: 'UNAUTHORIZED',
|
|
|
|
|
message: 'User have no authorization to record.',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (vote!.vote === Vote.UPVOTE) {
|
|
|
|
|
return vote;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (vote.vote === Vote.DOWNVOTE) {
|
|
|
|
|
tx.questionsQuestionCommentVote.delete({
|
|
|
|
|
where: {
|
|
|
|
|
id: vote.id,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const createdVote = await tx.questionsQuestionCommentVote.create({
|
|
|
|
|
data: {
|
|
|
|
|
questionCommentId,
|
|
|
|
|
userId,
|
|
|
|
|
vote: Vote.UPVOTE,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await tx.questionsQuestionComment.update({
|
|
|
|
|
data: {
|
|
|
|
|
upvotes: {
|
|
|
|
|
increment: 2,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
where: {
|
|
|
|
|
id: questionCommentId,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return createdVote
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
.mutation('updateVote', {
|
|
|
|
|
.mutation('setDownVote', {
|
|
|
|
|
input: z.object({
|
|
|
|
|
id: z.string(),
|
|
|
|
|
vote: z.nativeEnum(Vote),
|
|
|
|
|
questionCommentId: z.string(),
|
|
|
|
|
}),
|
|
|
|
|
async resolve({ ctx, input }) {
|
|
|
|
|
const userId = ctx.session?.user?.id;
|
|
|
|
|
const { id, vote } = input;
|
|
|
|
|
const { questionCommentId } = input;
|
|
|
|
|
|
|
|
|
|
const voteToUpdate =
|
|
|
|
|
await ctx.prisma.questionsQuestionCommentVote.findUnique({
|
|
|
|
|
return await ctx.prisma.$transaction(async (tx) => {
|
|
|
|
|
const vote = await tx.questionsQuestionCommentVote.findUnique({
|
|
|
|
|
where: {
|
|
|
|
|
id: input.id,
|
|
|
|
|
questionCommentId_userId: { questionCommentId, userId },
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (voteToUpdate?.userId !== userId) {
|
|
|
|
|
throw new TRPCError({
|
|
|
|
|
code: 'UNAUTHORIZED',
|
|
|
|
|
message: 'User have no authorization to record.',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (vote === null) {
|
|
|
|
|
const createdVote = await tx.questionsQuestionCommentVote.create({
|
|
|
|
|
data: {
|
|
|
|
|
questionCommentId,
|
|
|
|
|
userId,
|
|
|
|
|
vote: Vote.DOWNVOTE,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const incrementValue = vote === Vote.UPVOTE ? 2 : -2;
|
|
|
|
|
await tx.questionsQuestionComment.update({
|
|
|
|
|
data: {
|
|
|
|
|
upvotes: {
|
|
|
|
|
increment: -1,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
where: {
|
|
|
|
|
id: questionCommentId,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const [questionCommentVote] = await ctx.prisma.$transaction([
|
|
|
|
|
ctx.prisma.questionsQuestionCommentVote.update({
|
|
|
|
|
data: {
|
|
|
|
|
vote,
|
|
|
|
|
},
|
|
|
|
|
where: {
|
|
|
|
|
id,
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
ctx.prisma.questionsQuestionComment.update({
|
|
|
|
|
data: {
|
|
|
|
|
upvotes: {
|
|
|
|
|
increment: incrementValue,
|
|
|
|
|
return createdVote
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (vote!.userId !== userId) {
|
|
|
|
|
throw new TRPCError({
|
|
|
|
|
code: 'UNAUTHORIZED',
|
|
|
|
|
message: 'User have no authorization to record.',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (vote!.vote === Vote.DOWNVOTE) {
|
|
|
|
|
return vote;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (vote.vote === Vote.UPVOTE) {
|
|
|
|
|
tx.questionsQuestionCommentVote.delete({
|
|
|
|
|
where: {
|
|
|
|
|
id: vote.id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
where: {
|
|
|
|
|
id: voteToUpdate.questionCommentId,
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const createdVote = await tx.questionsQuestionCommentVote.create({
|
|
|
|
|
data: {
|
|
|
|
|
questionCommentId,
|
|
|
|
|
userId,
|
|
|
|
|
vote: Vote.DOWNVOTE,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await tx.questionsQuestionComment.update({
|
|
|
|
|
data: {
|
|
|
|
|
upvotes: {
|
|
|
|
|
increment: -2,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
where: {
|
|
|
|
|
id: questionCommentId,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return questionCommentVote;
|
|
|
|
|
return createdVote
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
.mutation('deleteVote', {
|
|
|
|
|
.mutation('setNoVote', {
|
|
|
|
|
input: z.object({
|
|
|
|
|
id: z.string(),
|
|
|
|
|
questionCommentId: z.string(),
|
|
|
|
|
}),
|
|
|
|
|
async resolve({ ctx, input }) {
|
|
|
|
|
const userId = ctx.session?.user?.id;
|
|
|
|
|
const { questionCommentId } = input;
|
|
|
|
|
|
|
|
|
|
const voteToDelete =
|
|
|
|
|
await ctx.prisma.questionsQuestionCommentVote.findUnique({
|
|
|
|
|
return await ctx.prisma.$transaction(async (tx) => {
|
|
|
|
|
const voteToDelete = await tx.questionsQuestionCommentVote.findUnique({
|
|
|
|
|
where: {
|
|
|
|
|
id: input.id,
|
|
|
|
|
questionCommentId_userId: { questionCommentId, userId },
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (voteToDelete?.userId !== userId) {
|
|
|
|
|
throw new TRPCError({
|
|
|
|
|
code: 'UNAUTHORIZED',
|
|
|
|
|
message: 'User have no authorization to record.',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (voteToDelete === null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (voteToDelete!.userId !== userId) {
|
|
|
|
|
throw new TRPCError({
|
|
|
|
|
code: 'UNAUTHORIZED',
|
|
|
|
|
message: 'User have no authorization to record.',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const incrementValue = voteToDelete.vote === Vote.UPVOTE ? -1 : 1;
|
|
|
|
|
const incrementValue = voteToDelete!.vote === Vote.UPVOTE ? -1 : 1;
|
|
|
|
|
|
|
|
|
|
const [questionCommentVote] = await ctx.prisma.$transaction([
|
|
|
|
|
ctx.prisma.questionsQuestionCommentVote.delete({
|
|
|
|
|
tx.questionsQuestionCommentVote.delete({
|
|
|
|
|
where: {
|
|
|
|
|
id: input.id,
|
|
|
|
|
id: voteToDelete.id,
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
ctx.prisma.questionsQuestionComment.update({
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await tx.questionsQuestionComment.update({
|
|
|
|
|
data: {
|
|
|
|
|
upvotes: {
|
|
|
|
|
increment: incrementValue,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
where: {
|
|
|
|
|
id: voteToDelete.questionCommentId,
|
|
|
|
|
id: questionCommentId,
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
]);
|
|
|
|
|
return questionCommentVote;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return voteToDelete;
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|