Skip to content

Commit

Permalink
feat(trpc): provide starred and watching filters data
Browse files Browse the repository at this point in the history
  • Loading branch information
awinogradov committed May 31, 2023
1 parent 124f3da commit dcf58dc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/schema/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export const queryWithFiltersSchema = z.object({
project: z.array(z.string()).optional(),
sort: sortablePropertiesSchema,
query: z.string().optional(),
starred: z.boolean().optional(),
watching: z.boolean().optional(),
});

export type QueryWithFilters = z.infer<typeof queryWithFiltersSchema>;
24 changes: 23 additions & 1 deletion trpc/queries/goals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const defaultOrderBy = {
};

// TODO: make it much more type-safety
export const goalsFilter = (data: QueryWithFilters, extra: any = {}): any => {
export const goalsFilter = (data: QueryWithFilters, activityId: string, extra: any = {}): any => {
const priorityFilter = data.priority?.length ? { priority: { in: data.priority } } : {};

const statesFilter = data.state?.length
Expand Down Expand Up @@ -129,6 +129,26 @@ export const goalsFilter = (data: QueryWithFilters, extra: any = {}): any => {
});
}

const starredFilter = data.starred
? {
stargizers: {
some: {
id: activityId,
},
},
}
: {};

const watchingFilter = data.watching
? {
watchers: {
some: {
id: activityId,
},
},
}
: {};

return {
where: {
archived: false,
Expand Down Expand Up @@ -162,6 +182,8 @@ export const goalsFilter = (data: QueryWithFilters, extra: any = {}): any => {
},
},
],
...starredFilter,
...watchingFilter,
...priorityFilter,
...statesFilter,
...tagsFilter,
Expand Down
3 changes: 2 additions & 1 deletion trpc/router/goal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ export const goal = router({
sort: {},
query: '',
},
ctx.session.user.activityId,
{
...userDashboardGoals,
},
Expand All @@ -252,7 +253,7 @@ export const goal = router({
},
}),
prisma.goal.findMany({
...goalsFilter(input, {
...goalsFilter(input, ctx.session.user.activityId, {
...userDashboardGoals,
}),
include: {
Expand Down
3 changes: 2 additions & 1 deletion trpc/router/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export const project = router({
project: [],
query: '',
},
ctx.session.user.activityId,
{
AND: {
OR: [
Expand All @@ -161,7 +162,7 @@ export const project = router({
},
}),
prisma.goal.findMany({
...goalsFilter(input, {
...goalsFilter(input, ctx.session.user.activityId, {
AND: {
OR: [
{
Expand Down

0 comments on commit dcf58dc

Please sign in to comment.