Skip to content

Commit

Permalink
fix(Dashboard): rewrite selects into two queries
Browse files Browse the repository at this point in the history
  • Loading branch information
9teen90nine committed Aug 15, 2023
1 parent 691dbaa commit 78e4cc1
Showing 1 changed file with 58 additions and 5 deletions.
63 changes: 58 additions & 5 deletions trpc/router/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,30 @@ export const project = router({
];
};

const requestSchemaWithoutOwner = requestSchema({ withOwner: false });
const projectIds = await prisma.project.findMany({
where: {
OR: [
...requestSchemaWithoutOwner,
{
parent: {
some: {
OR: requestSchemaWithoutOwner,
},
},
},
],
},
select: {
id: true,
},
});
const projectIdsArray = projectIds.map(({ id }) => id);

const nonArchived = {
archived: false,
};

const res = await prisma.project
.findMany({
orderBy: {
Expand All @@ -168,24 +192,53 @@ export const project = router({
goals: {
// all goals with filters
where: {
AND: [input ? { ...goalsFilter(input, activityId).where } : {}],
AND: [
input ? { ...goalsFilter(input, activityId).where } : {},
{
OR: [
...requestSchema({ withOwner: true }),
{
projectId: {
in: projectIdsArray,
},
},
],
},
nonArchived,
],
},
include: goalDeepQuery,
},
_count: {
select: {
// all goals without filters to count the total goals
goals: {
where: {
archived: false,
},
where: nonArchived,
},
},
},
},
where: {
// all projects where the user is a participant / watcher / issuer / stargizer
OR: requestSchema({ withOwner: false }),
OR: [
{
id: {
in: projectIdsArray,
},
},
{
goals: {
some: {
AND: [
{
OR: requestSchema({ withOwner: true }),
},
nonArchived,
],
},
},
},
],
},
})
.then((res) => ({
Expand Down

0 comments on commit 78e4cc1

Please sign in to comment.