From 9144205dda8839420be1841cb4771c20b3b41119 Mon Sep 17 00:00:00 2001 From: Robin Munn Date: Tue, 23 Jul 2024 14:46:37 +0700 Subject: [PATCH] Add GraphQL query for projects in org by lang code --- backend/LexBoxApi/GraphQL/LexQueries.cs | 28 +++++++++++++++++++++++++ frontend/schema.graphql | 6 ++++++ 2 files changed, 34 insertions(+) diff --git a/backend/LexBoxApi/GraphQL/LexQueries.cs b/backend/LexBoxApi/GraphQL/LexQueries.cs index 3687cd4e4..2d0113618 100644 --- a/backend/LexBoxApi/GraphQL/LexQueries.cs +++ b/backend/LexBoxApi/GraphQL/LexQueries.cs @@ -54,6 +54,34 @@ public IQueryable DraftProjects(LexBoxDbContext context) return context.DraftProjects; } + public record ProjectsByLangCodeAndOrgInput(Guid OrgId, string LangCode); + [UseProjection] + [UseSorting] + public IQueryable ProjectsByLangCodeAndOrg(LoggedInContext loggedInContext, LexBoxDbContext context, IPermissionService permissionService, ProjectsByLangCodeAndOrgInput input) + { + var userId = loggedInContext.User.Id; + var authorized = loggedInContext.User.IsAdmin || permissionService.IsOrgMember(input.OrgId); + if (!authorized) throw new UnauthorizedAccessException(); + var query = context.Projects.Where(p => + p.Organizations.Any(o => o.Id == input.OrgId) && + p.FlexProjectMetadata != null && + p.FlexProjectMetadata.WritingSystems != null && + p.FlexProjectMetadata.WritingSystems.VernacularWss.Any(ws => + ws.IsActive && ( + ws.Tag == input.LangCode || + ws.Tag == $"qaa-x-{input.LangCode}" || + ws.Tag.StartsWith($"{input.LangCode}-") + ) + ) + ); + // Org admins can see all projects, everyone else can only see non-confidential + if (!permissionService.CanEditOrg(input.OrgId)) + { + query = query.Where(p => p.IsConfidential == false); + } + return query; + } + [UseSingleOrDefault] [UseProjection] public async Task> ProjectById(LexBoxDbContext context, IPermissionService permissionService, Guid projectId) diff --git a/frontend/schema.graphql b/frontend/schema.graphql index 6e9e8d91d..fe4aa7fcb 100644 --- a/frontend/schema.graphql +++ b/frontend/schema.graphql @@ -405,6 +405,7 @@ type Query { projects(withDeleted: Boolean! = false where: ProjectFilterInput orderBy: [ProjectSortInput!]): [Project!]! @authorize(policy: "AdminRequiredPolicy") myDraftProjects(orderBy: [DraftProjectSortInput!]): [DraftProject!]! draftProjects(where: DraftProjectFilterInput orderBy: [DraftProjectSortInput!]): [DraftProject!]! @authorize(policy: "AdminRequiredPolicy") + projectsByLangCodeAndOrg(input: ProjectsByLangCodeAndOrgInput! orderBy: [ProjectSortInput!]): [Project!]! projectById(projectId: UUID!): Project projectByCode(code: String!): Project draftProjectByCode(code: String!): DraftProject @authorize(policy: "AdminRequiredPolicy") @@ -907,6 +908,11 @@ input ProjectWritingSystemsFilterInput { analysisWss: ListFilterInputTypeOfFLExWsIdFilterInput } +input ProjectsByLangCodeAndOrgInput { + orgId: UUID! + langCode: String! +} + input RemoveProjectFromOrgInput { orgId: UUID! projectId: UUID!