[question][feat] error handling

pull/457/head
hpkoh 3 years ago
parent 10b081dc95
commit 9f10260f32

@ -109,6 +109,20 @@ export const questionsAnswerCommentUserRouter = createProtectedRouter()
const { answerCommentId } = input; const { answerCommentId } = input;
return await ctx.prisma.$transaction(async (tx) => { return await ctx.prisma.$transaction(async (tx) => {
const answerCommentToUpdate =
await tx.questionsAnswerComment.findUnique({
where: {
id: answerCommentId,
},
});
if (answerCommentToUpdate === null) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'Answer Comment do not exist.',
});
}
const vote = await tx.questionsAnswerCommentVote.findUnique({ const vote = await tx.questionsAnswerCommentVote.findUnique({
where: { where: {
answerCommentId_userId: { answerCommentId, userId }, answerCommentId_userId: { answerCommentId, userId },
@ -189,6 +203,20 @@ export const questionsAnswerCommentUserRouter = createProtectedRouter()
const { answerCommentId } = input; const { answerCommentId } = input;
return await ctx.prisma.$transaction(async (tx) => { return await ctx.prisma.$transaction(async (tx) => {
const answerCommentToUpdate =
await tx.questionsAnswerComment.findUnique({
where: {
id: answerCommentId,
},
});
if (answerCommentToUpdate === null) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'Answer Comment do not exist.',
});
}
const vote = await tx.questionsAnswerCommentVote.findUnique({ const vote = await tx.questionsAnswerCommentVote.findUnique({
where: { where: {
answerCommentId_userId: { answerCommentId, userId }, answerCommentId_userId: { answerCommentId, userId },
@ -269,6 +297,20 @@ export const questionsAnswerCommentUserRouter = createProtectedRouter()
const { answerCommentId } = input; const { answerCommentId } = input;
return await ctx.prisma.$transaction(async (tx) => { return await ctx.prisma.$transaction(async (tx) => {
const answerCommentToUpdate =
await tx.questionsAnswerComment.findUnique({
where: {
id: answerCommentId,
},
});
if (answerCommentToUpdate === null) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'Answer Comment do not exist.',
});
}
const voteToDelete = await tx.questionsAnswerCommentVote.findUnique({ const voteToDelete = await tx.questionsAnswerCommentVote.findUnique({
where: { where: {
answerCommentId_userId: { answerCommentId, userId }, answerCommentId_userId: { answerCommentId, userId },

@ -39,7 +39,7 @@ export const questionsAnswerUserRouter = createProtectedRouter()
}, },
}); });
if (answerToUpdate?.id !== userId) { if (answerToUpdate?.userId !== userId) {
throw new TRPCError({ throw new TRPCError({
code: 'UNAUTHORIZED', code: 'UNAUTHORIZED',
message: 'User have no authorization to record.', message: 'User have no authorization to record.',
@ -69,7 +69,7 @@ export const questionsAnswerUserRouter = createProtectedRouter()
}, },
}); });
if (answerToDelete?.id !== userId) { if (answerToDelete?.userId !== userId) {
throw new TRPCError({ throw new TRPCError({
code: 'UNAUTHORIZED', code: 'UNAUTHORIZED',
message: 'User have no authorization to record.', message: 'User have no authorization to record.',
@ -107,6 +107,20 @@ export const questionsAnswerUserRouter = createProtectedRouter()
const { answerId } = input; const { answerId } = input;
return await ctx.prisma.$transaction(async (tx) => { return await ctx.prisma.$transaction(async (tx) => {
const answerToUpdate =
await tx.questionsAnswer.findUnique({
where: {
id: answerId,
},
});
if (answerToUpdate === null) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'Answer do not exist.',
});
}
const vote = await tx.questionsAnswerVote.findUnique({ const vote = await tx.questionsAnswerVote.findUnique({
where: { where: {
answerId_userId: { answerId, userId }, answerId_userId: { answerId, userId },
@ -187,6 +201,20 @@ export const questionsAnswerUserRouter = createProtectedRouter()
const { answerId } = input; const { answerId } = input;
return await ctx.prisma.$transaction(async (tx) => { return await ctx.prisma.$transaction(async (tx) => {
const answerToUpdate =
await tx.questionsAnswer.findUnique({
where: {
id: answerId,
},
});
if (answerToUpdate === null) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'Answer do not exist.',
});
}
const vote = await tx.questionsAnswerVote.findUnique({ const vote = await tx.questionsAnswerVote.findUnique({
where: { where: {
answerId_userId: { answerId, userId }, answerId_userId: { answerId, userId },
@ -267,6 +295,20 @@ export const questionsAnswerUserRouter = createProtectedRouter()
const { answerId } = input; const { answerId } = input;
return await ctx.prisma.$transaction(async (tx) => { return await ctx.prisma.$transaction(async (tx) => {
const answerToUpdate =
await tx.questionsAnswer.findUnique({
where: {
id: answerId,
},
});
if (answerToUpdate === null) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'Answer do not exist.',
});
}
const voteToDelete = await tx.questionsAnswerVote.findUnique({ const voteToDelete = await tx.questionsAnswerVote.findUnique({
where: { where: {
answerId_userId: { answerId, userId }, answerId_userId: { answerId, userId },

@ -110,6 +110,20 @@ export const questionsQuestionCommentUserRouter = createProtectedRouter()
const { questionCommentId } = input; const { questionCommentId } = input;
return await ctx.prisma.$transaction(async (tx) => { return await ctx.prisma.$transaction(async (tx) => {
const questionCommentToUpdate =
await tx.questionsQuestionComment.findUnique({
where: {
id: questionCommentId,
},
});
if (questionCommentToUpdate === null) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'Question Comment do not exist.',
});
}
const vote = await tx.questionsQuestionCommentVote.findUnique({ const vote = await tx.questionsQuestionCommentVote.findUnique({
where: { where: {
questionCommentId_userId: { questionCommentId, userId }, questionCommentId_userId: { questionCommentId, userId },
@ -190,6 +204,20 @@ export const questionsQuestionCommentUserRouter = createProtectedRouter()
const { questionCommentId } = input; const { questionCommentId } = input;
return await ctx.prisma.$transaction(async (tx) => { return await ctx.prisma.$transaction(async (tx) => {
const questionCommentToUpdate =
await tx.questionsQuestionComment.findUnique({
where: {
id: questionCommentId,
},
});
if (questionCommentToUpdate === null) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'Question Comment do not exist.',
});
}
const vote = await tx.questionsQuestionCommentVote.findUnique({ const vote = await tx.questionsQuestionCommentVote.findUnique({
where: { where: {
questionCommentId_userId: { questionCommentId, userId }, questionCommentId_userId: { questionCommentId, userId },
@ -270,6 +298,20 @@ export const questionsQuestionCommentUserRouter = createProtectedRouter()
const { questionCommentId } = input; const { questionCommentId } = input;
return await ctx.prisma.$transaction(async (tx) => { return await ctx.prisma.$transaction(async (tx) => {
const questionCommentToUpdate =
await tx.questionsQuestionComment.findUnique({
where: {
id: questionCommentId,
},
});
if (questionCommentToUpdate === null) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'Question Comment do not exist.',
});
}
const voteToDelete = await tx.questionsQuestionCommentVote.findUnique({ const voteToDelete = await tx.questionsQuestionCommentVote.findUnique({
where: { where: {
questionCommentId_userId: { questionCommentId, userId }, questionCommentId_userId: { questionCommentId, userId },

@ -132,6 +132,20 @@ export const questionsQuestionUserRouter = createProtectedRouter()
const { questionId } = input; const { questionId } = input;
return await ctx.prisma.$transaction(async (tx) => { return await ctx.prisma.$transaction(async (tx) => {
const questionToUpdate =
await tx.questionsQuestion.findUnique({
where: {
id: questionId,
},
});
if (questionToUpdate === null) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'Question do not exist.',
});
}
const vote = await tx.questionsQuestionVote.findUnique({ const vote = await tx.questionsQuestionVote.findUnique({
where: { where: {
questionId_userId: { questionId, userId }, questionId_userId: { questionId, userId },
@ -212,6 +226,20 @@ export const questionsQuestionUserRouter = createProtectedRouter()
const { questionId } = input; const { questionId } = input;
return await ctx.prisma.$transaction(async (tx) => { return await ctx.prisma.$transaction(async (tx) => {
const questionToUpdate =
await tx.questionsQuestion.findUnique({
where: {
id: questionId,
},
});
if (questionToUpdate === null) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'Question do not exist.',
});
}
const vote = await tx.questionsQuestionVote.findUnique({ const vote = await tx.questionsQuestionVote.findUnique({
where: { where: {
questionId_userId: { questionId, userId }, questionId_userId: { questionId, userId },
@ -292,6 +320,20 @@ export const questionsQuestionUserRouter = createProtectedRouter()
const { questionId } = input; const { questionId } = input;
return await ctx.prisma.$transaction(async (tx) => { return await ctx.prisma.$transaction(async (tx) => {
const questionToUpdate =
await tx.questionsQuestion.findUnique({
where: {
id: questionId,
},
});
if (questionToUpdate === null) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'Question do not exist.',
});
}
const voteToDelete = await tx.questionsQuestionVote.findUnique({ const voteToDelete = await tx.questionsQuestionVote.findUnique({
where: { where: {
questionId_userId: { questionId, userId }, questionId_userId: { questionId, userId },

Loading…
Cancel
Save