[questions][refactor]: refactor queries (#383)

pull/394/head
hpkoh 2 years ago committed by GitHub
parent 3209f8ef7e
commit 925ba937b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -27,7 +27,7 @@ export const questionsAnswerCommentRouter = createProtectedRouter()
createdAt: 'desc',
},
where: {
...input,
answerId : input.answerId,
},
});
return questionAnswerCommentsData.map((data) => {
@ -69,9 +69,12 @@ export const questionsAnswerCommentRouter = createProtectedRouter()
async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id;
const { answerId, content } = input;
return await ctx.prisma.questionsAnswerComment.create({
data: {
...input,
answerId,
content,
userId,
},
});
@ -99,9 +102,10 @@ export const questionsAnswerCommentRouter = createProtectedRouter()
});
}
const { content } = input;
return await ctx.prisma.questionsAnswerComment.update({
data: {
...input,
content,
},
where: {
id: input.id,
@ -160,10 +164,13 @@ export const questionsAnswerCommentRouter = createProtectedRouter()
async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id;
const { answerCommentId, vote } = input;
return await ctx.prisma.questionsAnswerCommentVote.create({
data: {
...input,
answerCommentId,
userId,
vote,
},
});
},

@ -12,6 +12,8 @@ export const questionsAnswerRouter = createProtectedRouter()
questionId: z.string(),
}),
async resolve({ ctx, input }) {
const { questionId } = input;
const answersData = await ctx.prisma.questionsAnswer.findMany({
include: {
_count: {
@ -31,7 +33,7 @@ export const questionsAnswerRouter = createProtectedRouter()
createdAt: 'desc',
},
where: {
...input,
questionId,
},
});
return answersData.map((data) => {
@ -132,9 +134,12 @@ export const questionsAnswerRouter = createProtectedRouter()
async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id;
const { content, questionId } = input;
return await ctx.prisma.questionsAnswer.create({
data: {
...input,
content,
questionId,
userId,
},
});
@ -222,10 +227,13 @@ export const questionsAnswerRouter = createProtectedRouter()
async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id;
const { answerId, vote } = input;
return await ctx.prisma.questionsAnswerVote.create({
data: {
...input,
answerId,
userId,
vote,
},
});
},

@ -12,6 +12,7 @@ export const questionsQuestionCommentRouter = createProtectedRouter()
questionId: z.string(),
}),
async resolve({ ctx, input }) {
const { questionId } = input;
const questionCommentsData =
await ctx.prisma.questionsQuestionComment.findMany({
include: {
@ -27,7 +28,7 @@ export const questionsQuestionCommentRouter = createProtectedRouter()
createdAt: 'desc',
},
where: {
...input,
questionId,
},
});
return questionCommentsData.map((data) => {
@ -68,9 +69,12 @@ export const questionsQuestionCommentRouter = createProtectedRouter()
async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id;
const { content, questionId } = input;
return await ctx.prisma.questionsQuestionComment.create({
data: {
...input,
content,
questionId,
userId,
},
});
@ -84,6 +88,8 @@ export const questionsQuestionCommentRouter = createProtectedRouter()
async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id;
const { content } = input;
const questionCommentToUpdate =
await ctx.prisma.questionsQuestionComment.findUnique({
where: {
@ -100,7 +106,7 @@ export const questionsQuestionCommentRouter = createProtectedRouter()
return await ctx.prisma.questionsQuestionComment.update({
data: {
...input,
content,
},
where: {
id: input.id,
@ -158,11 +164,13 @@ export const questionsQuestionCommentRouter = createProtectedRouter()
}),
async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id;
const { questionCommentId, vote } = input;
return await ctx.prisma.questionsQuestionCommentVote.create({
data: {
...input,
questionCommentId,
userId,
vote,
},
});
},

@ -51,29 +51,34 @@ export const questionsQuestionRouter = createProtectedRouter()
},
}
: {}),
encounters : {
some: {
...(input.companies.length > 0
? {
company : {
in : input.companies
}
}
: {}),
...(input.locations.length > 0
? {
location: {
in: input.locations
},
});
return questionsData
.filter((data) => {
for (let i = 0; i < data.encounters.length; i++) {
const encounter = data.encounters[i];
const matchCompany =
input.companies.length === 0 ||
input.companies.includes(encounter.company);
const matchLocation =
input.locations.length === 0 ||
input.locations.includes(encounter.location);
const matchRole =
input.roles.length === 0 || input.roles.includes(encounter.role);
const matchDate =
(!input.startDate || encounter.seenAt >= input.startDate) &&
encounter.seenAt <= input.endDate;
if (matchCompany && matchLocation && matchRole && matchDate) {
return true;
}
: {}),
...(input.roles.length > 0
? {
role : {
in: input.roles
}
return false;
})
}
: {}),
}
}
},
});
return questionsData
.map((data) => {
const votes: number = data.votes.reduce(
(previousValue: number, currentValue) => {
@ -251,9 +256,13 @@ export const questionsQuestionRouter = createProtectedRouter()
});
}
const { content, questionType } = input;
return await ctx.prisma.questionsQuestion.update({
data: {
...input,
content,
questionType,
},
where: {
id: input.id,
@ -311,11 +320,13 @@ export const questionsQuestionRouter = createProtectedRouter()
}),
async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id;
const { questionId, vote } = input;
return await ctx.prisma.questionsQuestionVote.create({
data: {
...input,
questionId,
userId,
vote,
},
});
},

Loading…
Cancel
Save