mirror of
https://github.com/yangshun/tech-interview-handbook.git
synced 2025-08-01 09:13:27 +08:00
[questions][chore] Refactor routers (#435)
Co-authored-by: Jeff Sieu <jeffsy00@gmail.com>
This commit is contained in:
@ -42,7 +42,7 @@ export default function QuestionPage() {
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
const { mutate: addComment } = trpc.useMutation(
|
const { mutate: addComment } = trpc.useMutation(
|
||||||
'questions.answers.comments.create',
|
'questions.answers.comments.user.create',
|
||||||
{
|
{
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
utils.invalidateQueries([
|
utils.invalidateQueries([
|
||||||
|
@ -60,7 +60,7 @@ export default function QuestionPage() {
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
const { mutate: addComment } = trpc.useMutation(
|
const { mutate: addComment } = trpc.useMutation(
|
||||||
'questions.questions.comments.create',
|
'questions.questions.comments.user.create',
|
||||||
{
|
{
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
utils.invalidateQueries(
|
utils.invalidateQueries(
|
||||||
@ -75,14 +75,17 @@ export default function QuestionPage() {
|
|||||||
{ questionId: questionId as string },
|
{ questionId: questionId as string },
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const { mutate: addAnswer } = trpc.useMutation('questions.answers.create', {
|
const { mutate: addAnswer } = trpc.useMutation(
|
||||||
onSuccess: () => {
|
'questions.answers.user.create',
|
||||||
utils.invalidateQueries('questions.answers.getAnswers');
|
{
|
||||||
|
onSuccess: () => {
|
||||||
|
utils.invalidateQueries('questions.answers.getAnswers');
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
);
|
||||||
|
|
||||||
const { mutate: addEncounter } = trpc.useMutation(
|
const { mutate: addEncounter } = trpc.useMutation(
|
||||||
'questions.questions.encounters.create',
|
'questions.questions.encounters.user.create',
|
||||||
{
|
{
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
utils.invalidateQueries(
|
utils.invalidateQueries(
|
||||||
|
@ -183,7 +183,7 @@ export default function QuestionsBrowsePage() {
|
|||||||
|
|
||||||
const utils = trpc.useContext();
|
const utils = trpc.useContext();
|
||||||
const { mutate: createQuestion } = trpc.useMutation(
|
const { mutate: createQuestion } = trpc.useMutation(
|
||||||
'questions.questions.create',
|
'questions.questions.user.create',
|
||||||
{
|
{
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
utils.invalidateQueries('questions.questions.getQuestionsByFilter');
|
utils.invalidateQueries('questions.questions.getQuestionsByFilter');
|
||||||
|
@ -7,12 +7,17 @@ import { offersAnalysisRouter } from './offers/offers-analysis-router';
|
|||||||
import { offersCommentsRouter } from './offers/offers-comments-router';
|
import { offersCommentsRouter } from './offers/offers-comments-router';
|
||||||
import { offersProfileRouter } from './offers/offers-profile-router';
|
import { offersProfileRouter } from './offers/offers-profile-router';
|
||||||
import { protectedExampleRouter } from './protected-example-router';
|
import { protectedExampleRouter } from './protected-example-router';
|
||||||
import { questionsAnswerCommentRouter } from './questions-answer-comment-router';
|
import { questionsAnswerCommentRouter } from './questions/questions-answer-comment-router';
|
||||||
import { questionsAnswerRouter } from './questions-answer-router';
|
import { questionsAnswerCommentUserRouter } from './questions/questions-answer-comment-user-router';
|
||||||
import { questionListRouter } from './questions-list-router';
|
import { questionsAnswerRouter } from './questions/questions-answer-router';
|
||||||
import { questionsQuestionCommentRouter } from './questions-question-comment-router';
|
import { questionsAnswerUserRouter } from './questions/questions-answer-user-router';
|
||||||
import { questionsQuestionEncounterRouter } from './questions-question-encounter-router';
|
import { questionsListRouter } from './questions/questions-list-router';
|
||||||
import { questionsQuestionRouter } from './questions-question-router';
|
import { questionsQuestionCommentRouter } from './questions/questions-question-comment-router';
|
||||||
|
import { questionsQuestionCommentUserRouter } from './questions/questions-question-comment-user-router';
|
||||||
|
import { questionsQuestionEncounterRouter } from './questions/questions-question-encounter-router';
|
||||||
|
import { questionsQuestionEncounterUserRouter } from './questions/questions-question-encounter-user-router';
|
||||||
|
import { questionsQuestionRouter } from './questions/questions-question-router';
|
||||||
|
import { questionsQuestionUserRouter } from './questions/questions-question-user-router';
|
||||||
import { resumeCommentsRouter } from './resumes/resumes-comments-router';
|
import { resumeCommentsRouter } from './resumes/resumes-comments-router';
|
||||||
import { resumesCommentsUserRouter } from './resumes/resumes-comments-user-router';
|
import { resumesCommentsUserRouter } from './resumes/resumes-comments-user-router';
|
||||||
import { resumesCommentsVotesRouter } from './resumes/resumes-comments-votes-router';
|
import { resumesCommentsVotesRouter } from './resumes/resumes-comments-votes-router';
|
||||||
@ -40,11 +45,22 @@ export const appRouter = createRouter()
|
|||||||
.merge('resumes.comments.votes.', resumesCommentsVotesRouter)
|
.merge('resumes.comments.votes.', resumesCommentsVotesRouter)
|
||||||
.merge('resumes.comments.votes.user.', resumesCommentsVotesUserRouter)
|
.merge('resumes.comments.votes.user.', resumesCommentsVotesUserRouter)
|
||||||
.merge('questions.answers.comments.', questionsAnswerCommentRouter)
|
.merge('questions.answers.comments.', questionsAnswerCommentRouter)
|
||||||
|
.merge('questions.answers.comments.user.', questionsAnswerCommentUserRouter)
|
||||||
.merge('questions.answers.', questionsAnswerRouter)
|
.merge('questions.answers.', questionsAnswerRouter)
|
||||||
.merge('questions.lists.', questionListRouter)
|
.merge('questions.answers.user.', questionsAnswerUserRouter)
|
||||||
|
.merge('questions.lists.', questionsListRouter)
|
||||||
.merge('questions.questions.comments.', questionsQuestionCommentRouter)
|
.merge('questions.questions.comments.', questionsQuestionCommentRouter)
|
||||||
|
.merge(
|
||||||
|
'questions.questions.comments.user.',
|
||||||
|
questionsQuestionCommentUserRouter,
|
||||||
|
)
|
||||||
.merge('questions.questions.encounters.', questionsQuestionEncounterRouter)
|
.merge('questions.questions.encounters.', questionsQuestionEncounterRouter)
|
||||||
|
.merge(
|
||||||
|
'questions.questions.encounters.user.',
|
||||||
|
questionsQuestionEncounterUserRouter,
|
||||||
|
)
|
||||||
.merge('questions.questions.', questionsQuestionRouter)
|
.merge('questions.questions.', questionsQuestionRouter)
|
||||||
|
.merge('questions.questions.user.', questionsQuestionUserRouter)
|
||||||
.merge('offers.', offersRouter)
|
.merge('offers.', offersRouter)
|
||||||
.merge('offers.profile.', offersProfileRouter)
|
.merge('offers.profile.', offersProfileRouter)
|
||||||
.merge('offers.analysis.', offersAnalysisRouter)
|
.merge('offers.analysis.', offersAnalysisRouter)
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { TRPCError } from '@trpc/server';
|
import { TRPCError } from '@trpc/server';
|
||||||
|
|
||||||
import createQuestionWithAggregateData from '~/utils/questions/server/createQuestionWithAggregateData';
|
import { createQuestionWithAggregateData } from '~/utils/questions/server/aggregate-encounters';
|
||||||
|
|
||||||
import { createProtectedRouter } from './context';
|
import { createProtectedRouter } from './context';
|
||||||
|
|
||||||
export const questionListRouter = createProtectedRouter()
|
export const questionsListRouter = createProtectedRouter()
|
||||||
.query('getListsByUser', {
|
.query('getListsByUser', {
|
||||||
async resolve({ ctx }) {
|
async resolve({ ctx }) {
|
||||||
const userId = ctx.session?.user?.id;
|
const userId = ctx.session?.user?.id;
|
||||||
|
@ -2,7 +2,7 @@ import { z } from 'zod';
|
|||||||
import { QuestionsQuestionType, Vote } from '@prisma/client';
|
import { QuestionsQuestionType, Vote } from '@prisma/client';
|
||||||
import { TRPCError } from '@trpc/server';
|
import { TRPCError } from '@trpc/server';
|
||||||
|
|
||||||
import createQuestionWithAggregateData from '~/utils/questions/server/createQuestionWithAggregateData';
|
import { createQuestionWithAggregateData } from '~/utils/questions/server/aggregate-encounters';
|
||||||
|
|
||||||
import { createProtectedRouter } from './context';
|
import { createProtectedRouter } from './context';
|
||||||
|
|
||||||
|
@ -0,0 +1,64 @@
|
|||||||
|
import { z } from 'zod';
|
||||||
|
import { Vote } from '@prisma/client';
|
||||||
|
|
||||||
|
import { createRouter } from '../context';
|
||||||
|
|
||||||
|
import type { AnswerComment } from '~/types/questions';
|
||||||
|
|
||||||
|
export const questionsAnswerCommentRouter = createRouter().query(
|
||||||
|
'getAnswerComments',
|
||||||
|
{
|
||||||
|
input: z.object({
|
||||||
|
answerId: z.string(),
|
||||||
|
}),
|
||||||
|
async resolve({ ctx, input }) {
|
||||||
|
const questionAnswerCommentsData =
|
||||||
|
await ctx.prisma.questionsAnswerComment.findMany({
|
||||||
|
include: {
|
||||||
|
user: {
|
||||||
|
select: {
|
||||||
|
image: true,
|
||||||
|
name: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
votes: true,
|
||||||
|
},
|
||||||
|
orderBy: {
|
||||||
|
createdAt: 'desc',
|
||||||
|
},
|
||||||
|
where: {
|
||||||
|
answerId: input.answerId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
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,
|
||||||
|
);
|
||||||
|
|
||||||
|
const answerComment: AnswerComment = {
|
||||||
|
content: data.content,
|
||||||
|
createdAt: data.createdAt,
|
||||||
|
id: data.id,
|
||||||
|
numVotes: votes,
|
||||||
|
updatedAt: data.updatedAt,
|
||||||
|
user: data.user?.name ?? '',
|
||||||
|
userImage: data.user?.image ?? '',
|
||||||
|
};
|
||||||
|
return answerComment;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
@ -2,65 +2,9 @@ import { z } from 'zod';
|
|||||||
import { Vote } from '@prisma/client';
|
import { Vote } from '@prisma/client';
|
||||||
import { TRPCError } from '@trpc/server';
|
import { TRPCError } from '@trpc/server';
|
||||||
|
|
||||||
import { createProtectedRouter } from './context';
|
import { createProtectedRouter } from '../context';
|
||||||
|
|
||||||
import type { AnswerComment } from '~/types/questions';
|
export const questionsAnswerCommentUserRouter = createProtectedRouter()
|
||||||
|
|
||||||
export const questionsAnswerCommentRouter = createProtectedRouter()
|
|
||||||
.query('getAnswerComments', {
|
|
||||||
input: z.object({
|
|
||||||
answerId: z.string(),
|
|
||||||
}),
|
|
||||||
async resolve({ ctx, input }) {
|
|
||||||
const questionAnswerCommentsData =
|
|
||||||
await ctx.prisma.questionsAnswerComment.findMany({
|
|
||||||
include: {
|
|
||||||
user: {
|
|
||||||
select: {
|
|
||||||
image: true,
|
|
||||||
name: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
votes: true,
|
|
||||||
},
|
|
||||||
orderBy: {
|
|
||||||
createdAt: 'desc',
|
|
||||||
},
|
|
||||||
where: {
|
|
||||||
answerId: input.answerId,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
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,
|
|
||||||
);
|
|
||||||
|
|
||||||
const answerComment: AnswerComment = {
|
|
||||||
content: data.content,
|
|
||||||
createdAt: data.createdAt,
|
|
||||||
id: data.id,
|
|
||||||
numVotes: votes,
|
|
||||||
updatedAt: data.updatedAt,
|
|
||||||
user: data.user?.name ?? '',
|
|
||||||
userImage: data.user?.image ?? '',
|
|
||||||
};
|
|
||||||
return answerComment;
|
|
||||||
});
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.mutation('create', {
|
.mutation('create', {
|
||||||
input: z.object({
|
input: z.object({
|
||||||
answerId: z.string(),
|
answerId: z.string(),
|
@ -0,0 +1,128 @@
|
|||||||
|
import { z } from 'zod';
|
||||||
|
import { Vote } from '@prisma/client';
|
||||||
|
import { TRPCError } from '@trpc/server';
|
||||||
|
|
||||||
|
import { createRouter } from '../context';
|
||||||
|
|
||||||
|
import type { Answer } from '~/types/questions';
|
||||||
|
|
||||||
|
export const questionsAnswerRouter = createRouter()
|
||||||
|
.query('getAnswers', {
|
||||||
|
input: z.object({
|
||||||
|
questionId: z.string(),
|
||||||
|
}),
|
||||||
|
async resolve({ ctx, input }) {
|
||||||
|
const { questionId } = input;
|
||||||
|
|
||||||
|
const answersData = await ctx.prisma.questionsAnswer.findMany({
|
||||||
|
include: {
|
||||||
|
_count: {
|
||||||
|
select: {
|
||||||
|
comments: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
user: {
|
||||||
|
select: {
|
||||||
|
image: true,
|
||||||
|
name: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
votes: true,
|
||||||
|
},
|
||||||
|
orderBy: {
|
||||||
|
createdAt: 'desc',
|
||||||
|
},
|
||||||
|
where: {
|
||||||
|
questionId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return answersData.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,
|
||||||
|
);
|
||||||
|
|
||||||
|
const answer: Answer = {
|
||||||
|
content: data.content,
|
||||||
|
createdAt: data.createdAt,
|
||||||
|
id: data.id,
|
||||||
|
numComments: data._count.comments,
|
||||||
|
numVotes: votes,
|
||||||
|
user: data.user?.name ?? '',
|
||||||
|
userImage: data.user?.image ?? '',
|
||||||
|
};
|
||||||
|
return answer;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.query('getAnswerById', {
|
||||||
|
input: z.object({
|
||||||
|
answerId: z.string(),
|
||||||
|
}),
|
||||||
|
async resolve({ ctx, input }) {
|
||||||
|
const answerData = await ctx.prisma.questionsAnswer.findUnique({
|
||||||
|
include: {
|
||||||
|
_count: {
|
||||||
|
select: {
|
||||||
|
comments: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
user: {
|
||||||
|
select: {
|
||||||
|
image: true,
|
||||||
|
name: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
votes: true,
|
||||||
|
},
|
||||||
|
where: {
|
||||||
|
id: input.answerId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (!answerData) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: 'NOT_FOUND',
|
||||||
|
message: 'Answer not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
const votes: number = answerData.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,
|
||||||
|
);
|
||||||
|
|
||||||
|
const answer: Answer = {
|
||||||
|
content: answerData.content,
|
||||||
|
createdAt: answerData.createdAt,
|
||||||
|
id: answerData.id,
|
||||||
|
numComments: answerData._count.comments,
|
||||||
|
numVotes: votes,
|
||||||
|
user: answerData.user?.name ?? '',
|
||||||
|
userImage: answerData.user?.image ?? '',
|
||||||
|
};
|
||||||
|
return answer;
|
||||||
|
},
|
||||||
|
});
|
@ -2,130 +2,9 @@ import { z } from 'zod';
|
|||||||
import { Vote } from '@prisma/client';
|
import { Vote } from '@prisma/client';
|
||||||
import { TRPCError } from '@trpc/server';
|
import { TRPCError } from '@trpc/server';
|
||||||
|
|
||||||
import { createProtectedRouter } from './context';
|
import { createProtectedRouter } from '../context';
|
||||||
|
|
||||||
import type { Answer } from '~/types/questions';
|
export const questionsAnswerUserRouter = createProtectedRouter()
|
||||||
|
|
||||||
export const questionsAnswerRouter = createProtectedRouter()
|
|
||||||
.query('getAnswers', {
|
|
||||||
input: z.object({
|
|
||||||
questionId: z.string(),
|
|
||||||
}),
|
|
||||||
async resolve({ ctx, input }) {
|
|
||||||
const { questionId } = input;
|
|
||||||
|
|
||||||
const answersData = await ctx.prisma.questionsAnswer.findMany({
|
|
||||||
include: {
|
|
||||||
_count: {
|
|
||||||
select: {
|
|
||||||
comments: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
user: {
|
|
||||||
select: {
|
|
||||||
image: true,
|
|
||||||
name: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
votes: true,
|
|
||||||
},
|
|
||||||
orderBy: {
|
|
||||||
createdAt: 'desc',
|
|
||||||
},
|
|
||||||
where: {
|
|
||||||
questionId,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
return answersData.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,
|
|
||||||
);
|
|
||||||
|
|
||||||
const answer: Answer = {
|
|
||||||
content: data.content,
|
|
||||||
createdAt: data.createdAt,
|
|
||||||
id: data.id,
|
|
||||||
numComments: data._count.comments,
|
|
||||||
numVotes: votes,
|
|
||||||
user: data.user?.name ?? '',
|
|
||||||
userImage: data.user?.image ?? '',
|
|
||||||
};
|
|
||||||
return answer;
|
|
||||||
});
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.query('getAnswerById', {
|
|
||||||
input: z.object({
|
|
||||||
answerId: z.string(),
|
|
||||||
}),
|
|
||||||
async resolve({ ctx, input }) {
|
|
||||||
const answerData = await ctx.prisma.questionsAnswer.findUnique({
|
|
||||||
include: {
|
|
||||||
_count: {
|
|
||||||
select: {
|
|
||||||
comments: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
user: {
|
|
||||||
select: {
|
|
||||||
image: true,
|
|
||||||
name: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
votes: true,
|
|
||||||
},
|
|
||||||
where: {
|
|
||||||
id: input.answerId,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
if (!answerData) {
|
|
||||||
throw new TRPCError({
|
|
||||||
code: 'NOT_FOUND',
|
|
||||||
message: 'Answer not found',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
const votes: number = answerData.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,
|
|
||||||
);
|
|
||||||
|
|
||||||
const answer: Answer = {
|
|
||||||
content: answerData.content,
|
|
||||||
createdAt: answerData.createdAt,
|
|
||||||
id: answerData.id,
|
|
||||||
numComments: answerData._count.comments,
|
|
||||||
numVotes: votes,
|
|
||||||
user: answerData.user?.name ?? '',
|
|
||||||
userImage: answerData.user?.image ?? '',
|
|
||||||
};
|
|
||||||
return answer;
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.mutation('create', {
|
.mutation('create', {
|
||||||
input: z.object({
|
input: z.object({
|
||||||
content: z.string(),
|
content: z.string(),
|
275
apps/portal/src/server/router/questions/questions-list-router.ts
Normal file
275
apps/portal/src/server/router/questions/questions-list-router.ts
Normal file
@ -0,0 +1,275 @@
|
|||||||
|
import { z } from 'zod';
|
||||||
|
import { TRPCError } from '@trpc/server';
|
||||||
|
|
||||||
|
import { createQuestionWithAggregateData } from '~/utils/questions/server/aggregate-encounters';
|
||||||
|
|
||||||
|
import { createProtectedRouter } from '../context';
|
||||||
|
|
||||||
|
export const questionsListRouter = createProtectedRouter()
|
||||||
|
.query('getListsByUser', {
|
||||||
|
async resolve({ ctx }) {
|
||||||
|
const userId = ctx.session?.user?.id;
|
||||||
|
|
||||||
|
// TODO: Optimize by not returning question entries
|
||||||
|
const questionsLists = await ctx.prisma.questionsList.findMany({
|
||||||
|
include: {
|
||||||
|
questionEntries: {
|
||||||
|
include: {
|
||||||
|
question: {
|
||||||
|
include: {
|
||||||
|
_count: {
|
||||||
|
select: {
|
||||||
|
answers: true,
|
||||||
|
comments: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
encounters: {
|
||||||
|
select: {
|
||||||
|
company: true,
|
||||||
|
location: true,
|
||||||
|
role: true,
|
||||||
|
seenAt: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
user: {
|
||||||
|
select: {
|
||||||
|
name: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
votes: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
orderBy: {
|
||||||
|
createdAt: 'asc',
|
||||||
|
},
|
||||||
|
where: {
|
||||||
|
userId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const lists = questionsLists.map((list) => ({
|
||||||
|
...list,
|
||||||
|
questionEntries: list.questionEntries.map((entry) => ({
|
||||||
|
...entry,
|
||||||
|
question: createQuestionWithAggregateData(entry.question),
|
||||||
|
})),
|
||||||
|
}));
|
||||||
|
|
||||||
|
return lists;
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.query('getListById', {
|
||||||
|
input: z.object({
|
||||||
|
listId: z.string(),
|
||||||
|
}),
|
||||||
|
async resolve({ ctx, input }) {
|
||||||
|
const userId = ctx.session?.user?.id;
|
||||||
|
const { listId } = input;
|
||||||
|
|
||||||
|
const questionList = await ctx.prisma.questionsList.findFirst({
|
||||||
|
include: {
|
||||||
|
questionEntries: {
|
||||||
|
include: {
|
||||||
|
question: {
|
||||||
|
include: {
|
||||||
|
_count: {
|
||||||
|
select: {
|
||||||
|
answers: true,
|
||||||
|
comments: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
encounters: {
|
||||||
|
select: {
|
||||||
|
company: true,
|
||||||
|
location: true,
|
||||||
|
role: true,
|
||||||
|
seenAt: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
user: {
|
||||||
|
select: {
|
||||||
|
name: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
votes: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
orderBy: {
|
||||||
|
createdAt: 'asc',
|
||||||
|
},
|
||||||
|
where: {
|
||||||
|
id: listId,
|
||||||
|
userId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!questionList) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: 'NOT_FOUND',
|
||||||
|
message: 'Question list not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...questionList,
|
||||||
|
questionEntries: questionList.questionEntries.map((questionEntry) => ({
|
||||||
|
...questionEntry,
|
||||||
|
question: createQuestionWithAggregateData(questionEntry.question),
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.mutation('create', {
|
||||||
|
input: z.object({
|
||||||
|
name: z.string(),
|
||||||
|
}),
|
||||||
|
async resolve({ ctx, input }) {
|
||||||
|
const userId = ctx.session?.user?.id;
|
||||||
|
|
||||||
|
const { name } = input;
|
||||||
|
|
||||||
|
return await ctx.prisma.questionsList.create({
|
||||||
|
data: {
|
||||||
|
name,
|
||||||
|
userId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.mutation('update', {
|
||||||
|
input: z.object({
|
||||||
|
id: z.string(),
|
||||||
|
name: z.string().optional(),
|
||||||
|
}),
|
||||||
|
async resolve({ ctx, input }) {
|
||||||
|
const userId = ctx.session?.user?.id;
|
||||||
|
const { name, id } = input;
|
||||||
|
|
||||||
|
const listToUpdate = await ctx.prisma.questionsList.findUnique({
|
||||||
|
where: {
|
||||||
|
id: input.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (listToUpdate?.id !== userId) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: 'UNAUTHORIZED',
|
||||||
|
message: 'User have no authorization to record.',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return await ctx.prisma.questionsList.update({
|
||||||
|
data: {
|
||||||
|
name,
|
||||||
|
},
|
||||||
|
where: {
|
||||||
|
id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.mutation('delete', {
|
||||||
|
input: z.object({
|
||||||
|
id: z.string(),
|
||||||
|
}),
|
||||||
|
async resolve({ ctx, input }) {
|
||||||
|
const userId = ctx.session?.user?.id;
|
||||||
|
|
||||||
|
const listToDelete = await ctx.prisma.questionsList.findUnique({
|
||||||
|
where: {
|
||||||
|
id: input.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (listToDelete?.userId !== userId) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: 'UNAUTHORIZED',
|
||||||
|
message: 'User have no authorization to record.',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return await ctx.prisma.questionsList.delete({
|
||||||
|
where: {
|
||||||
|
id: input.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.mutation('createQuestionEntry', {
|
||||||
|
input: z.object({
|
||||||
|
listId: z.string(),
|
||||||
|
questionId: z.string(),
|
||||||
|
}),
|
||||||
|
async resolve({ ctx, input }) {
|
||||||
|
const userId = ctx.session?.user?.id;
|
||||||
|
|
||||||
|
const listToAugment = await ctx.prisma.questionsList.findUnique({
|
||||||
|
where: {
|
||||||
|
id: input.listId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (listToAugment?.userId !== userId) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: 'UNAUTHORIZED',
|
||||||
|
message: 'User have no authorization to record.',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const { questionId, listId } = input;
|
||||||
|
|
||||||
|
return await ctx.prisma.questionsListQuestionEntry.create({
|
||||||
|
data: {
|
||||||
|
listId,
|
||||||
|
questionId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.mutation('deleteQuestionEntry', {
|
||||||
|
input: z.object({
|
||||||
|
id: z.string(),
|
||||||
|
}),
|
||||||
|
async resolve({ ctx, input }) {
|
||||||
|
const userId = ctx.session?.user?.id;
|
||||||
|
|
||||||
|
const entryToDelete =
|
||||||
|
await ctx.prisma.questionsListQuestionEntry.findUnique({
|
||||||
|
where: {
|
||||||
|
id: input.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (entryToDelete === null) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: 'NOT_FOUND',
|
||||||
|
message: 'Entry not found.',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const listToAugment = await ctx.prisma.questionsList.findUnique({
|
||||||
|
where: {
|
||||||
|
id: entryToDelete.listId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (listToAugment?.userId !== userId) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: 'UNAUTHORIZED',
|
||||||
|
message: 'User have no authorization to record.',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return await ctx.prisma.questionsListQuestionEntry.delete({
|
||||||
|
where: {
|
||||||
|
id: input.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
@ -0,0 +1,64 @@
|
|||||||
|
import { z } from 'zod';
|
||||||
|
import { Vote } from '@prisma/client';
|
||||||
|
|
||||||
|
import { createRouter } from '../context';
|
||||||
|
|
||||||
|
import type { QuestionComment } from '~/types/questions';
|
||||||
|
|
||||||
|
export const questionsQuestionCommentRouter = createRouter().query(
|
||||||
|
'getQuestionComments',
|
||||||
|
{
|
||||||
|
input: z.object({
|
||||||
|
questionId: z.string(),
|
||||||
|
}),
|
||||||
|
async resolve({ ctx, input }) {
|
||||||
|
const { questionId } = input;
|
||||||
|
const questionCommentsData =
|
||||||
|
await ctx.prisma.questionsQuestionComment.findMany({
|
||||||
|
include: {
|
||||||
|
user: {
|
||||||
|
select: {
|
||||||
|
image: true,
|
||||||
|
name: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
votes: true,
|
||||||
|
},
|
||||||
|
orderBy: {
|
||||||
|
createdAt: 'desc',
|
||||||
|
},
|
||||||
|
where: {
|
||||||
|
questionId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return questionCommentsData.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,
|
||||||
|
);
|
||||||
|
|
||||||
|
const questionComment: QuestionComment = {
|
||||||
|
content: data.content,
|
||||||
|
createdAt: data.createdAt,
|
||||||
|
id: data.id,
|
||||||
|
numVotes: votes,
|
||||||
|
user: data.user?.name ?? '',
|
||||||
|
userImage: data.user?.image ?? '',
|
||||||
|
};
|
||||||
|
return questionComment;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
@ -2,65 +2,9 @@ import { z } from 'zod';
|
|||||||
import { Vote } from '@prisma/client';
|
import { Vote } from '@prisma/client';
|
||||||
import { TRPCError } from '@trpc/server';
|
import { TRPCError } from '@trpc/server';
|
||||||
|
|
||||||
import { createProtectedRouter } from './context';
|
import { createProtectedRouter } from '../context';
|
||||||
|
|
||||||
import type { QuestionComment } from '~/types/questions';
|
export const questionsQuestionCommentUserRouter = createProtectedRouter()
|
||||||
|
|
||||||
export const questionsQuestionCommentRouter = createProtectedRouter()
|
|
||||||
.query('getQuestionComments', {
|
|
||||||
input: z.object({
|
|
||||||
questionId: z.string(),
|
|
||||||
}),
|
|
||||||
async resolve({ ctx, input }) {
|
|
||||||
const { questionId } = input;
|
|
||||||
const questionCommentsData =
|
|
||||||
await ctx.prisma.questionsQuestionComment.findMany({
|
|
||||||
include: {
|
|
||||||
user: {
|
|
||||||
select: {
|
|
||||||
image: true,
|
|
||||||
name: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
votes: true,
|
|
||||||
},
|
|
||||||
orderBy: {
|
|
||||||
createdAt: 'desc',
|
|
||||||
},
|
|
||||||
where: {
|
|
||||||
questionId,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
return questionCommentsData.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,
|
|
||||||
);
|
|
||||||
|
|
||||||
const questionComment: QuestionComment = {
|
|
||||||
content: data.content,
|
|
||||||
createdAt: data.createdAt,
|
|
||||||
id: data.id,
|
|
||||||
numVotes: votes,
|
|
||||||
user: data.user?.name ?? '',
|
|
||||||
userImage: data.user?.image ?? '',
|
|
||||||
};
|
|
||||||
return questionComment;
|
|
||||||
});
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.mutation('create', {
|
.mutation('create', {
|
||||||
input: z.object({
|
input: z.object({
|
||||||
content: z.string(),
|
content: z.string(),
|
@ -0,0 +1,61 @@
|
|||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
import { createRouter } from '../context';
|
||||||
|
|
||||||
|
import type { AggregatedQuestionEncounter } from '~/types/questions';
|
||||||
|
|
||||||
|
export const questionsQuestionEncounterRouter = createRouter().query(
|
||||||
|
'getAggregatedEncounters',
|
||||||
|
{
|
||||||
|
input: z.object({
|
||||||
|
questionId: z.string(),
|
||||||
|
}),
|
||||||
|
async resolve({ ctx, input }) {
|
||||||
|
const questionEncountersData =
|
||||||
|
await ctx.prisma.questionsQuestionEncounter.findMany({
|
||||||
|
include: {
|
||||||
|
company: true,
|
||||||
|
},
|
||||||
|
where: {
|
||||||
|
...input,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const companyCounts: Record<string, number> = {};
|
||||||
|
const locationCounts: Record<string, number> = {};
|
||||||
|
const roleCounts: Record<string, number> = {};
|
||||||
|
|
||||||
|
let latestSeenAt = questionEncountersData[0].seenAt;
|
||||||
|
|
||||||
|
for (let i = 0; i < questionEncountersData.length; i++) {
|
||||||
|
const encounter = questionEncountersData[i];
|
||||||
|
|
||||||
|
latestSeenAt =
|
||||||
|
latestSeenAt < encounter.seenAt ? encounter.seenAt : latestSeenAt;
|
||||||
|
|
||||||
|
if (!(encounter.company!.name in companyCounts)) {
|
||||||
|
companyCounts[encounter.company!.name] = 1;
|
||||||
|
}
|
||||||
|
companyCounts[encounter.company!.name] += 1;
|
||||||
|
|
||||||
|
if (!(encounter.location in locationCounts)) {
|
||||||
|
locationCounts[encounter.location] = 1;
|
||||||
|
}
|
||||||
|
locationCounts[encounter.location] += 1;
|
||||||
|
|
||||||
|
if (!(encounter.role in roleCounts)) {
|
||||||
|
roleCounts[encounter.role] = 1;
|
||||||
|
}
|
||||||
|
roleCounts[encounter.role] += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const questionEncounter: AggregatedQuestionEncounter = {
|
||||||
|
companyCounts,
|
||||||
|
latestSeenAt,
|
||||||
|
locationCounts,
|
||||||
|
roleCounts,
|
||||||
|
};
|
||||||
|
return questionEncounter;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
@ -1,12 +1,13 @@
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { TRPCError } from '@trpc/server';
|
import { TRPCError } from '@trpc/server';
|
||||||
|
|
||||||
import { createProtectedRouter } from './context';
|
import { createAggregatedQuestionEncounter } from '~/utils/questions/server/aggregate-encounters';
|
||||||
|
|
||||||
|
import { createProtectedRouter } from '../context';
|
||||||
|
|
||||||
import type { AggregatedQuestionEncounter } from '~/types/questions';
|
|
||||||
import { SortOrder } from '~/types/questions.d';
|
import { SortOrder } from '~/types/questions.d';
|
||||||
|
|
||||||
export const questionsQuestionEncounterRouter = createProtectedRouter()
|
export const questionsQuestionEncounterUserRouter = createProtectedRouter()
|
||||||
.query('getAggregatedEncounters', {
|
.query('getAggregatedEncounters', {
|
||||||
input: z.object({
|
input: z.object({
|
||||||
questionId: z.string(),
|
questionId: z.string(),
|
||||||
@ -22,41 +23,7 @@ export const questionsQuestionEncounterRouter = createProtectedRouter()
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const companyCounts: Record<string, number> = {};
|
return createAggregatedQuestionEncounter(questionEncountersData);
|
||||||
const locationCounts: Record<string, number> = {};
|
|
||||||
const roleCounts: Record<string, number> = {};
|
|
||||||
|
|
||||||
let latestSeenAt = questionEncountersData[0].seenAt;
|
|
||||||
|
|
||||||
for (let i = 0; i < questionEncountersData.length; i++) {
|
|
||||||
const encounter = questionEncountersData[i];
|
|
||||||
|
|
||||||
latestSeenAt =
|
|
||||||
latestSeenAt < encounter.seenAt ? encounter.seenAt : latestSeenAt;
|
|
||||||
|
|
||||||
if (!(encounter.company!.name in companyCounts)) {
|
|
||||||
companyCounts[encounter.company!.name] = 0;
|
|
||||||
}
|
|
||||||
companyCounts[encounter.company!.name] += 1;
|
|
||||||
|
|
||||||
if (!(encounter.location in locationCounts)) {
|
|
||||||
locationCounts[encounter.location] = 0;
|
|
||||||
}
|
|
||||||
locationCounts[encounter.location] += 1;
|
|
||||||
|
|
||||||
if (!(encounter.role in roleCounts)) {
|
|
||||||
roleCounts[encounter.role] = 0;
|
|
||||||
}
|
|
||||||
roleCounts[encounter.role] += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
const questionEncounter: AggregatedQuestionEncounter = {
|
|
||||||
companyCounts,
|
|
||||||
latestSeenAt,
|
|
||||||
locationCounts,
|
|
||||||
roleCounts,
|
|
||||||
};
|
|
||||||
return questionEncounter;
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
.mutation('create', {
|
.mutation('create', {
|
@ -0,0 +1,196 @@
|
|||||||
|
import { z } from 'zod';
|
||||||
|
import { QuestionsQuestionType } from '@prisma/client';
|
||||||
|
import { TRPCError } from '@trpc/server';
|
||||||
|
|
||||||
|
import { createQuestionWithAggregateData } from '~/utils/questions/server/aggregate-encounters';
|
||||||
|
|
||||||
|
import { createRouter } from '../context';
|
||||||
|
|
||||||
|
import { SortOrder, SortType } from '~/types/questions.d';
|
||||||
|
|
||||||
|
export const questionsQuestionRouter = createRouter()
|
||||||
|
.query('getQuestionsByFilter', {
|
||||||
|
input: z.object({
|
||||||
|
companyNames: z.string().array(),
|
||||||
|
cursor: z
|
||||||
|
.object({
|
||||||
|
idCursor: z.string().optional(),
|
||||||
|
lastSeenCursor: z.date().nullish().optional(),
|
||||||
|
upvoteCursor: z.number().optional(),
|
||||||
|
})
|
||||||
|
.nullish(),
|
||||||
|
endDate: z.date().default(new Date()),
|
||||||
|
limit: z.number().min(1).default(50),
|
||||||
|
locations: z.string().array(),
|
||||||
|
questionTypes: z.nativeEnum(QuestionsQuestionType).array(),
|
||||||
|
roles: z.string().array(),
|
||||||
|
sortOrder: z.nativeEnum(SortOrder),
|
||||||
|
sortType: z.nativeEnum(SortType),
|
||||||
|
startDate: z.date().optional(),
|
||||||
|
}),
|
||||||
|
async resolve({ ctx, input }) {
|
||||||
|
const { cursor } = input;
|
||||||
|
|
||||||
|
const sortCondition =
|
||||||
|
input.sortType === SortType.TOP
|
||||||
|
? [
|
||||||
|
{
|
||||||
|
upvotes: input.sortOrder,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: input.sortOrder,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
: [
|
||||||
|
{
|
||||||
|
lastSeenAt: input.sortOrder,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: input.sortOrder,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const questionsData = await ctx.prisma.questionsQuestion.findMany({
|
||||||
|
cursor:
|
||||||
|
cursor !== undefined
|
||||||
|
? {
|
||||||
|
id: cursor ? cursor!.idCursor : undefined,
|
||||||
|
}
|
||||||
|
: undefined,
|
||||||
|
include: {
|
||||||
|
_count: {
|
||||||
|
select: {
|
||||||
|
answers: true,
|
||||||
|
comments: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
encounters: {
|
||||||
|
select: {
|
||||||
|
company: true,
|
||||||
|
location: true,
|
||||||
|
role: true,
|
||||||
|
seenAt: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
user: {
|
||||||
|
select: {
|
||||||
|
name: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
votes: true,
|
||||||
|
},
|
||||||
|
orderBy: sortCondition,
|
||||||
|
take: input.limit + 1,
|
||||||
|
where: {
|
||||||
|
...(input.questionTypes.length > 0
|
||||||
|
? {
|
||||||
|
questionType: {
|
||||||
|
in: input.questionTypes,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
: {}),
|
||||||
|
encounters: {
|
||||||
|
some: {
|
||||||
|
seenAt: {
|
||||||
|
gte: input.startDate,
|
||||||
|
lte: input.endDate,
|
||||||
|
},
|
||||||
|
...(input.companyNames.length > 0
|
||||||
|
? {
|
||||||
|
company: {
|
||||||
|
name: {
|
||||||
|
in: input.companyNames,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
: {}),
|
||||||
|
...(input.locations.length > 0
|
||||||
|
? {
|
||||||
|
location: {
|
||||||
|
in: input.locations,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
: {}),
|
||||||
|
...(input.roles.length > 0
|
||||||
|
? {
|
||||||
|
role: {
|
||||||
|
in: input.roles,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
: {}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const processedQuestionsData = questionsData.map(
|
||||||
|
createQuestionWithAggregateData,
|
||||||
|
);
|
||||||
|
|
||||||
|
let nextCursor: typeof cursor | undefined = undefined;
|
||||||
|
|
||||||
|
if (questionsData.length > input.limit) {
|
||||||
|
const nextItem = questionsData.pop()!;
|
||||||
|
processedQuestionsData.pop();
|
||||||
|
|
||||||
|
const nextIdCursor: string | undefined = nextItem.id;
|
||||||
|
const nextLastSeenCursor =
|
||||||
|
input.sortType === SortType.NEW ? nextItem.lastSeenAt : undefined;
|
||||||
|
const nextUpvoteCursor =
|
||||||
|
input.sortType === SortType.TOP ? nextItem.upvotes : undefined;
|
||||||
|
|
||||||
|
nextCursor = {
|
||||||
|
idCursor: nextIdCursor,
|
||||||
|
lastSeenCursor: nextLastSeenCursor,
|
||||||
|
upvoteCursor: nextUpvoteCursor,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
data: processedQuestionsData,
|
||||||
|
nextCursor,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.query('getQuestionById', {
|
||||||
|
input: z.object({
|
||||||
|
id: z.string(),
|
||||||
|
}),
|
||||||
|
async resolve({ ctx, input }) {
|
||||||
|
const questionData = await ctx.prisma.questionsQuestion.findUnique({
|
||||||
|
include: {
|
||||||
|
_count: {
|
||||||
|
select: {
|
||||||
|
answers: true,
|
||||||
|
comments: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
encounters: {
|
||||||
|
select: {
|
||||||
|
company: true,
|
||||||
|
location: true,
|
||||||
|
role: true,
|
||||||
|
seenAt: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
user: {
|
||||||
|
select: {
|
||||||
|
name: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
votes: true,
|
||||||
|
},
|
||||||
|
where: {
|
||||||
|
id: input.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (!questionData) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: 'NOT_FOUND',
|
||||||
|
message: 'Question not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return createQuestionWithAggregateData(questionData);
|
||||||
|
},
|
||||||
|
});
|
@ -0,0 +1,248 @@
|
|||||||
|
import { z } from 'zod';
|
||||||
|
import { QuestionsQuestionType, Vote } from '@prisma/client';
|
||||||
|
import { TRPCError } from '@trpc/server';
|
||||||
|
|
||||||
|
import { createProtectedRouter } from '../context';
|
||||||
|
|
||||||
|
export const questionsQuestionUserRouter = createProtectedRouter()
|
||||||
|
.mutation('create', {
|
||||||
|
input: z.object({
|
||||||
|
companyId: z.string(),
|
||||||
|
content: z.string(),
|
||||||
|
location: z.string(),
|
||||||
|
questionType: z.nativeEnum(QuestionsQuestionType),
|
||||||
|
role: z.string(),
|
||||||
|
seenAt: z.date(),
|
||||||
|
}),
|
||||||
|
async resolve({ ctx, input }) {
|
||||||
|
const userId = ctx.session?.user?.id;
|
||||||
|
|
||||||
|
return await ctx.prisma.questionsQuestion.create({
|
||||||
|
data: {
|
||||||
|
content: input.content,
|
||||||
|
encounters: {
|
||||||
|
create: {
|
||||||
|
company: {
|
||||||
|
connect: {
|
||||||
|
id: input.companyId,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
location: input.location,
|
||||||
|
role: input.role,
|
||||||
|
seenAt: input.seenAt,
|
||||||
|
user: {
|
||||||
|
connect: {
|
||||||
|
id: userId,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
lastSeenAt: input.seenAt,
|
||||||
|
questionType: input.questionType,
|
||||||
|
userId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.mutation('update', {
|
||||||
|
input: z.object({
|
||||||
|
content: z.string().optional(),
|
||||||
|
id: z.string(),
|
||||||
|
questionType: z.nativeEnum(QuestionsQuestionType).optional(),
|
||||||
|
}),
|
||||||
|
async resolve({ ctx, input }) {
|
||||||
|
const userId = ctx.session?.user?.id;
|
||||||
|
|
||||||
|
const questionToUpdate = await ctx.prisma.questionsQuestion.findUnique({
|
||||||
|
where: {
|
||||||
|
id: input.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (questionToUpdate?.id !== userId) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: 'UNAUTHORIZED',
|
||||||
|
message: 'User have no authorization to record.',
|
||||||
|
// Optional: pass the original error to retain stack trace
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const { content, questionType } = input;
|
||||||
|
|
||||||
|
return await ctx.prisma.questionsQuestion.update({
|
||||||
|
data: {
|
||||||
|
content,
|
||||||
|
questionType,
|
||||||
|
},
|
||||||
|
where: {
|
||||||
|
id: input.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.mutation('delete', {
|
||||||
|
input: z.object({
|
||||||
|
id: z.string(),
|
||||||
|
}),
|
||||||
|
async resolve({ ctx, input }) {
|
||||||
|
const userId = ctx.session?.user?.id;
|
||||||
|
|
||||||
|
const questionToDelete = await ctx.prisma.questionsQuestion.findUnique({
|
||||||
|
where: {
|
||||||
|
id: input.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (questionToDelete?.id !== userId) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: 'UNAUTHORIZED',
|
||||||
|
message: 'User have no authorization to record.',
|
||||||
|
// Optional: pass the original error to retain stack trace
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return await ctx.prisma.questionsQuestion.delete({
|
||||||
|
where: {
|
||||||
|
id: input.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.query('getVote', {
|
||||||
|
input: z.object({
|
||||||
|
questionId: z.string(),
|
||||||
|
}),
|
||||||
|
async resolve({ ctx, input }) {
|
||||||
|
const userId = ctx.session?.user?.id;
|
||||||
|
const { questionId } = input;
|
||||||
|
|
||||||
|
return await ctx.prisma.questionsQuestionVote.findUnique({
|
||||||
|
where: {
|
||||||
|
questionId_userId: { questionId, userId },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.mutation('createVote', {
|
||||||
|
input: z.object({
|
||||||
|
questionId: z.string(),
|
||||||
|
vote: z.nativeEnum(Vote),
|
||||||
|
}),
|
||||||
|
async resolve({ ctx, input }) {
|
||||||
|
const userId = ctx.session?.user?.id;
|
||||||
|
const { questionId, vote } = input;
|
||||||
|
|
||||||
|
const incrementValue = vote === Vote.UPVOTE ? 1 : -1;
|
||||||
|
|
||||||
|
const [questionVote] = await ctx.prisma.$transaction([
|
||||||
|
ctx.prisma.questionsQuestionVote.create({
|
||||||
|
data: {
|
||||||
|
questionId,
|
||||||
|
userId,
|
||||||
|
vote,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
ctx.prisma.questionsQuestion.update({
|
||||||
|
data: {
|
||||||
|
upvotes: {
|
||||||
|
increment: incrementValue,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
where: {
|
||||||
|
id: questionId,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
return questionVote;
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.mutation('updateVote', {
|
||||||
|
input: z.object({
|
||||||
|
id: z.string(),
|
||||||
|
vote: z.nativeEnum(Vote),
|
||||||
|
}),
|
||||||
|
async resolve({ ctx, input }) {
|
||||||
|
const userId = ctx.session?.user?.id;
|
||||||
|
const { id, vote } = input;
|
||||||
|
|
||||||
|
const voteToUpdate = await ctx.prisma.questionsQuestionVote.findUnique({
|
||||||
|
where: {
|
||||||
|
id: input.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (voteToUpdate?.userId !== userId) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: 'UNAUTHORIZED',
|
||||||
|
message: 'User have no authorization to record.',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const incrementValue = vote === Vote.UPVOTE ? 2 : -2;
|
||||||
|
|
||||||
|
const [questionVote] = await ctx.prisma.$transaction([
|
||||||
|
ctx.prisma.questionsQuestionVote.update({
|
||||||
|
data: {
|
||||||
|
vote,
|
||||||
|
},
|
||||||
|
where: {
|
||||||
|
id,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
ctx.prisma.questionsQuestion.update({
|
||||||
|
data: {
|
||||||
|
upvotes: {
|
||||||
|
increment: incrementValue,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
where: {
|
||||||
|
id: voteToUpdate.questionId,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
|
||||||
|
return questionVote;
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.mutation('deleteVote', {
|
||||||
|
input: z.object({
|
||||||
|
id: z.string(),
|
||||||
|
}),
|
||||||
|
async resolve({ ctx, input }) {
|
||||||
|
const userId = ctx.session?.user?.id;
|
||||||
|
|
||||||
|
const voteToDelete = await ctx.prisma.questionsQuestionVote.findUnique({
|
||||||
|
where: {
|
||||||
|
id: input.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
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 [questionVote] = await ctx.prisma.$transaction([
|
||||||
|
ctx.prisma.questionsQuestionVote.delete({
|
||||||
|
where: {
|
||||||
|
id: input.id,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
ctx.prisma.questionsQuestion.update({
|
||||||
|
data: {
|
||||||
|
upvotes: {
|
||||||
|
increment: incrementValue,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
where: {
|
||||||
|
id: voteToDelete.questionId,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
return questionVote;
|
||||||
|
},
|
||||||
|
});
|
@ -5,26 +5,28 @@ import type {
|
|||||||
} from '@prisma/client';
|
} from '@prisma/client';
|
||||||
import { Vote } from '@prisma/client';
|
import { Vote } from '@prisma/client';
|
||||||
|
|
||||||
import type { Question } from '~/types/questions';
|
import type { AggregatedQuestionEncounter, Question } from '~/types/questions';
|
||||||
|
|
||||||
|
type AggregatableEncounters = Array<{
|
||||||
|
company: Company | null;
|
||||||
|
location: string;
|
||||||
|
role: string;
|
||||||
|
seenAt: Date;
|
||||||
|
}>;
|
||||||
|
|
||||||
type QuestionWithAggregatableData = QuestionsQuestion & {
|
type QuestionWithAggregatableData = QuestionsQuestion & {
|
||||||
_count: {
|
_count: {
|
||||||
answers: number;
|
answers: number;
|
||||||
comments: number;
|
comments: number;
|
||||||
};
|
};
|
||||||
encounters: Array<{
|
encounters: AggregatableEncounters;
|
||||||
company: Company | null;
|
|
||||||
location: string;
|
|
||||||
role: string;
|
|
||||||
seenAt: Date;
|
|
||||||
}>;
|
|
||||||
user: {
|
user: {
|
||||||
name: string | null;
|
name: string | null;
|
||||||
} | null;
|
} | null;
|
||||||
votes: Array<QuestionsQuestionVote>;
|
votes: Array<QuestionsQuestionVote>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function createQuestionWithAggregateData(
|
export function createQuestionWithAggregateData(
|
||||||
data: QuestionWithAggregatableData,
|
data: QuestionWithAggregatableData,
|
||||||
): Question {
|
): Question {
|
||||||
const votes: number = data.votes.reduce(
|
const votes: number = data.votes.reduce(
|
||||||
@ -44,13 +46,34 @@ export default function createQuestionWithAggregateData(
|
|||||||
0,
|
0,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const question: Question = {
|
||||||
|
aggregatedQuestionEncounters: createAggregatedQuestionEncounter(
|
||||||
|
data.encounters,
|
||||||
|
),
|
||||||
|
content: data.content,
|
||||||
|
id: data.id,
|
||||||
|
numAnswers: data._count.answers,
|
||||||
|
numComments: data._count.comments,
|
||||||
|
numVotes: votes,
|
||||||
|
receivedCount: data.encounters.length,
|
||||||
|
seenAt: data.encounters[0].seenAt,
|
||||||
|
type: data.questionType,
|
||||||
|
updatedAt: data.updatedAt,
|
||||||
|
user: data.user?.name ?? '',
|
||||||
|
};
|
||||||
|
return question;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createAggregatedQuestionEncounter(
|
||||||
|
encounters: AggregatableEncounters,
|
||||||
|
): AggregatedQuestionEncounter {
|
||||||
const companyCounts: Record<string, number> = {};
|
const companyCounts: Record<string, number> = {};
|
||||||
const locationCounts: Record<string, number> = {};
|
const locationCounts: Record<string, number> = {};
|
||||||
const roleCounts: Record<string, number> = {};
|
const roleCounts: Record<string, number> = {};
|
||||||
|
|
||||||
let latestSeenAt = data.encounters[0].seenAt;
|
let latestSeenAt = encounters[0].seenAt;
|
||||||
|
|
||||||
for (const encounter of data.encounters) {
|
for (const encounter of encounters) {
|
||||||
latestSeenAt =
|
latestSeenAt =
|
||||||
latestSeenAt < encounter.seenAt ? encounter.seenAt : latestSeenAt;
|
latestSeenAt < encounter.seenAt ? encounter.seenAt : latestSeenAt;
|
||||||
|
|
||||||
@ -70,23 +93,10 @@ export default function createQuestionWithAggregateData(
|
|||||||
roleCounts[encounter.role] += 1;
|
roleCounts[encounter.role] += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
const question: Question = {
|
return {
|
||||||
aggregatedQuestionEncounters: {
|
companyCounts,
|
||||||
companyCounts,
|
latestSeenAt,
|
||||||
latestSeenAt,
|
locationCounts,
|
||||||
locationCounts,
|
roleCounts,
|
||||||
roleCounts,
|
|
||||||
},
|
|
||||||
content: data.content,
|
|
||||||
id: data.id,
|
|
||||||
numAnswers: data._count.answers,
|
|
||||||
numComments: data._count.comments,
|
|
||||||
numVotes: votes,
|
|
||||||
receivedCount: data.encounters.length,
|
|
||||||
seenAt: data.encounters[0].seenAt,
|
|
||||||
type: data.questionType,
|
|
||||||
updatedAt: data.updatedAt,
|
|
||||||
user: data.user?.name ?? '',
|
|
||||||
};
|
};
|
||||||
return question;
|
|
||||||
}
|
}
|
@ -71,51 +71,51 @@ type QueryKey = Parameters<typeof trpc.useQuery>[0][0];
|
|||||||
|
|
||||||
export const useQuestionVote = (id: string) => {
|
export const useQuestionVote = (id: string) => {
|
||||||
return useVote(id, {
|
return useVote(id, {
|
||||||
create: 'questions.questions.createVote',
|
create: 'questions.questions.user.createVote',
|
||||||
deleteKey: 'questions.questions.deleteVote',
|
deleteKey: 'questions.questions.user.deleteVote',
|
||||||
idKey: 'questionId',
|
idKey: 'questionId',
|
||||||
invalidateKeys: [
|
invalidateKeys: [
|
||||||
'questions.questions.getQuestionsByFilter',
|
'questions.questions.getQuestionsByFilter',
|
||||||
'questions.questions.getQuestionById',
|
'questions.questions.getQuestionById',
|
||||||
],
|
],
|
||||||
query: 'questions.questions.getVote',
|
query: 'questions.questions.user.getVote',
|
||||||
update: 'questions.questions.updateVote',
|
update: 'questions.questions.user.updateVote',
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useAnswerVote = (id: string) => {
|
export const useAnswerVote = (id: string) => {
|
||||||
return useVote(id, {
|
return useVote(id, {
|
||||||
create: 'questions.answers.createVote',
|
create: 'questions.answers.user.createVote',
|
||||||
deleteKey: 'questions.answers.deleteVote',
|
deleteKey: 'questions.answers.user.deleteVote',
|
||||||
idKey: 'answerId',
|
idKey: 'answerId',
|
||||||
invalidateKeys: [
|
invalidateKeys: [
|
||||||
'questions.answers.getAnswers',
|
'questions.answers.getAnswers',
|
||||||
'questions.answers.getAnswerById',
|
'questions.answers.getAnswerById',
|
||||||
],
|
],
|
||||||
query: 'questions.answers.getVote',
|
query: 'questions.answers.user.getVote',
|
||||||
update: 'questions.answers.updateVote',
|
update: 'questions.answers.user.updateVote',
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useQuestionCommentVote = (id: string) => {
|
export const useQuestionCommentVote = (id: string) => {
|
||||||
return useVote(id, {
|
return useVote(id, {
|
||||||
create: 'questions.questions.comments.createVote',
|
create: 'questions.questions.comments.user.createVote',
|
||||||
deleteKey: 'questions.questions.comments.deleteVote',
|
deleteKey: 'questions.questions.comments.user.deleteVote',
|
||||||
idKey: 'questionCommentId',
|
idKey: 'questionCommentId',
|
||||||
invalidateKeys: ['questions.questions.comments.getQuestionComments'],
|
invalidateKeys: ['questions.questions.comments.getQuestionComments'],
|
||||||
query: 'questions.questions.comments.getVote',
|
query: 'questions.questions.comments.user.getVote',
|
||||||
update: 'questions.questions.comments.updateVote',
|
update: 'questions.questions.comments.user.updateVote',
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useAnswerCommentVote = (id: string) => {
|
export const useAnswerCommentVote = (id: string) => {
|
||||||
return useVote(id, {
|
return useVote(id, {
|
||||||
create: 'questions.answers.comments.createVote',
|
create: 'questions.answers.comments.user.createVote',
|
||||||
deleteKey: 'questions.answers.comments.deleteVote',
|
deleteKey: 'questions.answers.comments.user.deleteVote',
|
||||||
idKey: 'answerCommentId',
|
idKey: 'answerCommentId',
|
||||||
invalidateKeys: ['questions.answers.comments.getAnswerComments'],
|
invalidateKeys: ['questions.answers.comments.getAnswerComments'],
|
||||||
query: 'questions.answers.comments.getVote',
|
query: 'questions.answers.comments.user.getVote',
|
||||||
update: 'questions.answers.comments.updateVote',
|
update: 'questions.answers.comments.user.updateVote',
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user