mirror of
https://github.com/yangshun/tech-interview-handbook.git
synced 2025-07-28 12:43:12 +08:00
[questions][refactor]: refactor queries (#383)
This commit is contained in:
@ -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
|
||||
.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) => {
|
||||
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,
|
||||
},
|
||||
});
|
||||
},
|
||||
|
Reference in New Issue
Block a user