Skip to content

Commit

Permalink
Move projects I manager filter to GQL
Browse files Browse the repository at this point in the history
  • Loading branch information
myieye committed Oct 31, 2024
1 parent e83930d commit 4f9bb01
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
1 change: 1 addition & 0 deletions backend/LexBoxApi/GraphQL/LexQueries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class LexQueries
{
[UseProjection]
[UseSorting]
[UseFiltering]
public IQueryable<Project> MyProjects(LoggedInContext loggedInContext, LexBoxDbContext context)
{
var userId = loggedInContext.User.Id;
Expand Down
2 changes: 1 addition & 1 deletion frontend/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ type ProjectWritingSystems {
}

type Query {
myProjects(orderBy: [ProjectSortInput!] @cost(weight: "10")): [Project!]! @cost(weight: "10")
myProjects(orderBy: [ProjectSortInput!] @cost(weight: "10") where: ProjectFilterInput @cost(weight: "10")): [Project!]! @cost(weight: "10")
projects(withDeleted: Boolean! = false where: ProjectFilterInput @cost(weight: "10") orderBy: [ProjectSortInput!] @cost(weight: "10")): [Project!]! @authorize(policy: "AdminRequiredPolicy") @cost(weight: "10")
myDraftProjects(orderBy: [DraftProjectSortInput!] @cost(weight: "10")): [DraftProject!]! @cost(weight: "10")
draftProjects(where: DraftProjectFilterInput @cost(weight: "10") orderBy: [DraftProjectSortInput!] @cost(weight: "10")): [DraftProject!]! @authorize(policy: "AdminRequiredPolicy") @cost(weight: "10")
Expand Down
23 changes: 18 additions & 5 deletions frontend/src/routes/(authenticated)/org/[org_id]/+page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type {
} from '$lib/gql/types';
import {getClient, graphql} from '$lib/gql';

import type {LexAuthUser} from '$lib/user';
import type {OrgTabId} from './OrgTabs.svelte';
import type {PageLoadEvent} from './$types';
import type {UUID} from 'crypto';
Expand Down Expand Up @@ -315,14 +316,26 @@ export async function _deleteOrg(orgId: string): $OpResult<DeleteOrgMutation> {
return result;
}

export async function _getMyProjects(): Promise<LoadMyProjectsQuery['myProjects']> {
export async function _getProjectsIManage(user: LexAuthUser): Promise<LoadMyProjectsQuery['myProjects']> {
const client = getClient();

//language=GraphQL
const results = await client.query(graphql(`
query loadMyProjects {
myProjects(orderBy: [
query loadMyProjects($userId: UUID!) {
myProjects(
orderBy: [
{name: ASC }
]) {
],
where: {
users: {
some: {
and: [
{userId: {eq: $userId}},
{role: {eq: MANAGER}}
]
}
}
}) {
code
id
name
Expand All @@ -333,7 +346,7 @@ export async function _getMyProjects(): Promise<LoadMyProjectsQuery['myProjects'
}
}
}
`), {});
`), { userId: user.id });
return results.data?.myProjects ?? [];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import t from '$lib/i18n';
import {type LexAuthUser} from '$lib/user';
import {z} from 'zod';
import {_addProjectsToOrg, _getMyProjects, type Org} from './+page';
import {_addProjectsToOrg, _getProjectsIManage, type Org} from './+page';
import {ProjectRole} from '$lib/gql/types';
import {useNotifications} from '$lib/notify';
import {type UUID} from 'crypto';
Expand All @@ -19,25 +19,22 @@
let formModal: FormModal<typeof schema>;
let newProjects: Project[] = [];
let alreadyAddedProjects: Project[] = [];
let alreadyAddedProjects: number = 0;
let selectedProjects: string[] = [];
async function openModal(): Promise<void> {
const myProjects = await _getMyProjects();
const projectsIManage = myProjects.map((project) => ({
id: project.id,
name: project.name,
code: project.code,
memberRole: project.users.find(projUser => projUser.userId === user.id)?.role ?? ProjectRole.Editor,
})).filter(p => p.memberRole === ProjectRole.Manager);
const projectsIManage = await _getProjectsIManage(user);
newProjects = [];
alreadyAddedProjects = [];
alreadyAddedProjects = 0;
projectsIManage.forEach(proj => {
if (org.projects.find(p => p.id === proj.id)) {
alreadyAddedProjects.push(proj);
alreadyAddedProjects++;
} else {
newProjects.push(proj);
newProjects.push({
...proj,
memberRole: ProjectRole.Manager,
});
}
});
Expand Down Expand Up @@ -66,9 +63,9 @@
</span>
{#if newProjects.length}
<UserProjects projects={newProjects} bind:selectedProjects hideRoleColumn />
{:else if alreadyAddedProjects.length}
{:else if alreadyAddedProjects}
<span class="text-secondary">
{$t('org_page.add_my_projects.all_projects_already_added', { count: alreadyAddedProjects.length })}
{$t('org_page.add_my_projects.all_projects_already_added', { count: alreadyAddedProjects })}
</span>
{:else}
<span class="text-secondary">
Expand Down

0 comments on commit 4f9bb01

Please sign in to comment.