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

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

@ -12,6 +12,7 @@ export const questionsQuestionCommentRouter = createProtectedRouter()
questionId: z.string(), questionId: z.string(),
}), }),
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
const { questionId } = input;
const questionCommentsData = const questionCommentsData =
await ctx.prisma.questionsQuestionComment.findMany({ await ctx.prisma.questionsQuestionComment.findMany({
include: { include: {
@ -27,7 +28,7 @@ export const questionsQuestionCommentRouter = createProtectedRouter()
createdAt: 'desc', createdAt: 'desc',
}, },
where: { where: {
...input, questionId,
}, },
}); });
return questionCommentsData.map((data) => { return questionCommentsData.map((data) => {
@ -68,9 +69,12 @@ export const questionsQuestionCommentRouter = createProtectedRouter()
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id; const userId = ctx.session?.user?.id;
const { content, questionId } = input;
return await ctx.prisma.questionsQuestionComment.create({ return await ctx.prisma.questionsQuestionComment.create({
data: { data: {
...input, content,
questionId,
userId, userId,
}, },
}); });
@ -84,6 +88,8 @@ export const questionsQuestionCommentRouter = createProtectedRouter()
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id; const userId = ctx.session?.user?.id;
const { content } = input;
const questionCommentToUpdate = const questionCommentToUpdate =
await ctx.prisma.questionsQuestionComment.findUnique({ await ctx.prisma.questionsQuestionComment.findUnique({
where: { where: {
@ -100,7 +106,7 @@ export const questionsQuestionCommentRouter = createProtectedRouter()
return await ctx.prisma.questionsQuestionComment.update({ return await ctx.prisma.questionsQuestionComment.update({
data: { data: {
...input, content,
}, },
where: { where: {
id: input.id, id: input.id,
@ -158,11 +164,13 @@ export const questionsQuestionCommentRouter = createProtectedRouter()
}), }),
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id; const userId = ctx.session?.user?.id;
const { questionCommentId, vote } = input;
return await ctx.prisma.questionsQuestionCommentVote.create({ return await ctx.prisma.questionsQuestionCommentVote.create({
data: { data: {
...input, questionCommentId,
userId, 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
},
}
: {}),
...(input.roles.length > 0
? {
role : {
in: input.roles
}
}
: {}),
}
}
}, },
}); });
return questionsData 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;
}
}
return false;
})
.map((data) => { .map((data) => {
const votes: number = data.votes.reduce( const votes: number = data.votes.reduce(
(previousValue: number, currentValue) => { (previousValue: number, currentValue) => {
@ -251,9 +256,13 @@ export const questionsQuestionRouter = createProtectedRouter()
}); });
} }
const { content, questionType } = input;
return await ctx.prisma.questionsQuestion.update({ return await ctx.prisma.questionsQuestion.update({
data: { data: {
...input, content,
questionType,
}, },
where: { where: {
id: input.id, id: input.id,
@ -311,11 +320,13 @@ export const questionsQuestionRouter = createProtectedRouter()
}), }),
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id; const userId = ctx.session?.user?.id;
const { questionId, vote } = input;
return await ctx.prisma.questionsQuestionVote.create({ return await ctx.prisma.questionsQuestionVote.create({
data: { data: {
...input, questionId,
userId, userId,
vote,
}, },
}); });
}, },

Loading…
Cancel
Save