From 1b6aa084eb55d3f6632899fa4523f297451856f6 Mon Sep 17 00:00:00 2001 From: Gregory Noack Date: Tue, 17 Dec 2024 15:53:33 -0800 Subject: [PATCH 1/5] update resolver to query user by orcid or id --- .../openneuro-server/src/graphql/resolvers/user.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/openneuro-server/src/graphql/resolvers/user.ts b/packages/openneuro-server/src/graphql/resolvers/user.ts index 31589c64e..3f7e6efeb 100644 --- a/packages/openneuro-server/src/graphql/resolvers/user.ts +++ b/packages/openneuro-server/src/graphql/resolvers/user.ts @@ -3,8 +3,15 @@ */ import User from "../../models/user" -export const user = (obj, { id }) => { - return User.findOne({ id }).exec() + + +export const user = (obj, { id, orcid }) => { + if (id) { + return User.findOne(id).exec() + } + if (orcid) { + return User.findOne(orcid).exec() + } } export const users = (obj, args, { userInfo }) => { From 01ecc5390aae12ac70d80a1ecff24cfe15d11426 Mon Sep 17 00:00:00 2001 From: Gregory Noack Date: Thu, 19 Dec 2024 14:21:48 -0800 Subject: [PATCH 2/5] update resolver to allow for orcid as ID arg --- .../src/graphql/resolvers/user.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/openneuro-server/src/graphql/resolvers/user.ts b/packages/openneuro-server/src/graphql/resolvers/user.ts index 3f7e6efeb..6224d66ff 100644 --- a/packages/openneuro-server/src/graphql/resolvers/user.ts +++ b/packages/openneuro-server/src/graphql/resolvers/user.ts @@ -5,13 +5,18 @@ import User from "../../models/user" -export const user = (obj, { id, orcid }) => { - if (id) { - return User.findOne(id).exec() +export const user = (obj, { id }) => { + + function isValidOrcid(orcid: string): boolean { + return /^[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{3}[0-9X]$/.test(orcid || ''); } - if (orcid) { - return User.findOne(orcid).exec() + + if (isValidOrcid(id)) { + return User.findOne({ "orchid": id }).exec() + } else { + return User.findOne({ 'id': id }).exec() } + } export const users = (obj, args, { userInfo }) => { From 94bb1c58768c6ab59cb74e9da308e925d8cd4f06 Mon Sep 17 00:00:00 2001 From: Gregory Noack Date: Thu, 19 Dec 2024 14:30:31 -0800 Subject: [PATCH 3/5] copy non admin test for orcid --- .../src/graphql/resolvers/__tests__/user.spec.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/openneuro-server/src/graphql/resolvers/__tests__/user.spec.ts b/packages/openneuro-server/src/graphql/resolvers/__tests__/user.spec.ts index f06e1e8e0..67c25e0a7 100644 --- a/packages/openneuro-server/src/graphql/resolvers/__tests__/user.spec.ts +++ b/packages/openneuro-server/src/graphql/resolvers/__tests__/user.spec.ts @@ -13,6 +13,15 @@ describe("user resolvers", () => { { userInfo: {} }, ), ).rejects.toEqual(new Error("You must be a site admin to retrieve users")) - }) - }) -}) + }); + it("throws an error for non-admins", () => { + expect( + users( + null, + { id: "0000-0000-0000-000X" }, + { userInfo: {} }, + ), + ).rejects.toEqual(new Error("You must be a site admin to retrieve users")) + }); + }); +}); \ No newline at end of file From 5d293c275bb0fec2f180a194283d8728d0e8349b Mon Sep 17 00:00:00 2001 From: Gregory Noack Date: Thu, 19 Dec 2024 15:27:21 -0800 Subject: [PATCH 4/5] update user-query to use gql and url param --- .../src/scripts/users/user-query.tsx | 69 ++++++------------- 1 file changed, 22 insertions(+), 47 deletions(-) diff --git a/packages/openneuro-app/src/scripts/users/user-query.tsx b/packages/openneuro-app/src/scripts/users/user-query.tsx index 8984d2424..7864f5b55 100644 --- a/packages/openneuro-app/src/scripts/users/user-query.tsx +++ b/packages/openneuro-app/src/scripts/users/user-query.tsx @@ -1,49 +1,20 @@ -// UserQuery.tsx - import React from 'react'; import { useParams } from 'react-router-dom'; import { UserRoutes } from './user-routes'; import FourOFourPage from '../errors/404page'; import { isValidOrcid } from "../utils/validationUtils"; - - - -// Dummy user data -const dummyUsers: Record = { - '0000-0001-6755-0259': { - id: '1', - name: 'Gregory Noack', - location: 'Stanford, CA', - github: 'thinknoack', - institution: 'Stanford University', - email: 'gregorynoack@thinknoack.com', - avatar: 'https://dummyimage.com/200x200/000/fff', - orcid: '0000-0001-6755-0259', - links: ['onelink.com', 'https://www.twolink.com'], - }, - '0000-0002-1234-5678': { - id: '2', - name: 'Jane Doe', - location: 'Stanford, CA', - institution: 'Stanford University', - email: 'janedoe@example.com', - avatar: 'https://dummyimage.com/200x200/000/fff', - orcid: '0000-0002-1234-5678', - links: ['onelink.com', 'https://www.twolink.com'], - }, - '0000-0003-2345-6789': { - id: '3', - name: 'John Smith', - location: 'Stanford, CA', - institution: 'Stanford University', - email: 'johnsmith@example.com', - avatar: 'https://dummyimage.com/200x200/000/fff', - orcid: '0000-0003-2345-6789', - links: ['onelink.com', 'https://www.twolink.com'], - }, -}; - - +import { gql, useQuery } from "@apollo/client"; + +// GraphQL query to fetch user by ORCID +const GET_USER_BY_ORCID = gql` + query User($userId: ID!) { + user(id: $userId) { + id + name + orcid + } + } +`; export interface User { id: string; @@ -65,16 +36,20 @@ export const UserQuery: React.FC = () => { return ; } - // Check if the user exists in the dummyUsers data - const user = dummyUsers[orcid]; + // Fetch user data using GraphQL + const { data, loading, error } = useQuery(GET_USER_BY_ORCID, { + variables: { userId: orcid } + }); + + if (loading) return
Loading...
; + if (error) return ; // Handle error as 404 page - if (!user) { - // If user is not found, render 404 page + if (!data?.user) { return ; } - // Mocked for now + // Assuming hasEdit is a static boolean value for now const hasEdit = true; - return ; + return ; }; From 4db079b5ab22e825d9f6af679bc3bf4e32563dd5 Mon Sep 17 00:00:00 2001 From: Gregory Noack Date: Thu, 19 Dec 2024 16:42:35 -0800 Subject: [PATCH 5/5] chaning comment --- packages/openneuro-app/src/scripts/users/user-query.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/openneuro-app/src/scripts/users/user-query.tsx b/packages/openneuro-app/src/scripts/users/user-query.tsx index 7864f5b55..0a85d5a7a 100644 --- a/packages/openneuro-app/src/scripts/users/user-query.tsx +++ b/packages/openneuro-app/src/scripts/users/user-query.tsx @@ -48,7 +48,7 @@ export const UserQuery: React.FC = () => { return ; } - // Assuming hasEdit is a static boolean value for now + // Assuming hasEdit is a static boolean value for now need to check against logged in user const hasEdit = true; return ;