Skip to content

Commit

Permalink
Merge pull request #19 from RADAR-base/mp-verify
Browse files Browse the repository at this point in the history
  • Loading branch information
this-Aditya authored Aug 26, 2024
2 parents ace41bb + cf22a8f commit 5f85e34
Show file tree
Hide file tree
Showing 14 changed files with 665 additions and 423 deletions.
43 changes: 28 additions & 15 deletions config/github-config.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
export const GITHUB_CONFIG = {
ORGANIZATION_NAME: 'RADAR-base',
REPOSITORY_NAME: process.env.GITHUB_REPO_NAME || 'radar-self-enrolment-definitions',
DEFINITIONS_BRANCH: process.env.GITHUB_REPO_BRANCH_NAME || 'test',
MAX_CONTENT_LENGTH: parseInt(process.env.GITHUB_RESPONSE_CONTENT_LENGTH || '1000000', 10),
CACHE_DURATION: parseInt(process.env.GITHUB_RESPONSE_CACHE_DURATION || '180000', 10),
CACHE_SIZE: parseInt(process.env.GITHUB_RESPONSE_CACHE_SIZE || '50', 10),
API_URL: 'https://api.github.com',
ACCEPT_HEADER: 'application/vnd.github.v3+json',
ORGANIZATION_NAME: "RADAR-base",
REPOSITORY_NAME:
process.env.GITHUB_REPO_NAME || "radar-self-enrolment-definitions",
DEFINITIONS_BRANCH: process.env.GITHUB_REPO_BRANCH_NAME || "test",
MAX_CONTENT_LENGTH: parseInt(
process.env.GITHUB_RESPONSE_CONTENT_LENGTH || "1000000",
10,
),
CACHE_DURATION: parseInt(
process.env.GITHUB_RESPONSE_CACHE_DURATION || "180000",
10,
),
CACHE_SIZE: parseInt(process.env.GITHUB_RESPONSE_CACHE_SIZE || "50", 10),
API_URL: "https://api.github.com",
ACCEPT_HEADER: "application/vnd.github.v3+json",
}

export const GITHUB_AUTH_CONFIG = {
GITHUB_AUTH_TOKEN: process.env.GITHUB_AUTH_TOKEN || '',
GITHUB_AUTH_TOKEN: process.env.GITHUB_AUTH_TOKEN || "",
}

export const REMOTE_DEFINITIONS_CONFIG = {
CONSENT_VERSION: 'v1',
ELIGIBILITY_VERSION: 'v1',
STUDY_INFO_VERSION: 'v1',
CONSENT_VERSION: "v1",
ELIGIBILITY_VERSION: "v1",
STUDY_INFO_VERSION: "v1",

CONSENT_DEFINITION_FILE_NAME_CONTENT: 'consent',
ELIGIBILITY_DEFINITION_FILE_NAME_CONTENT: 'eligibility',
STUDY_INFO_DEFINITION_FILE_NAME_CONTENT: 'study_info'
CONSENT_DEFINITION_FILE_NAME_CONTENT: "consent",
ELIGIBILITY_DEFINITION_FILE_NAME_CONTENT: "eligibility",
STUDY_INFO_DEFINITION_FILE_NAME_CONTENT: "study_info",
}

export const MP_CONFIG = {
BASE_URL:
process.env.MP_CONFIG_BASE_URL || "http://127.0.1.1:8080/managementportal",
PROJECTS_ENDPOINT: process.env.MP_PROJECTS_ENDPOINT || "api/public/projects",
}
32 changes: 19 additions & 13 deletions pages/eligibility.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { RegistrationFlow, UpdateRegistrationFlowBody } from "@ory/client"
import { AxiosError } from "axios"
import type {GetServerSideProps, NextPage} from "next"
import type { GetServerSideProps, NextPage } from "next"
import Head from "next/head"
import { useRouter } from "next/router"
import {MutableRefObject, useEffect, useState} from "react"
import { MutableRefObject, useEffect, useState } from "react"
import { toast } from "react-toastify"

import { REMOTE_DEFINITIONS_CONFIG } from "../config/github-config"
// Import render helpers
import { MarginCard, CardTitle, TextCenterButton } from "../pkg"
import githubService from "../services/github-service";
import {REMOTE_DEFINITIONS_CONFIG} from "../config/github-config";
import {Definition} from "../utils/structures";
import githubService from "../services/github-service"
import { Definition } from "../utils/structures"

interface EligibilityFormProps {
questions: any[]
Expand All @@ -22,11 +22,13 @@ interface EligibilityPageProps {
}

// Renders the eligibility page
const Eligibility: NextPage<EligibilityPageProps> = ({definitions}) => {
const Eligibility: NextPage<EligibilityPageProps> = ({ definitions }) => {
const IS_ELIGIBLE = "yes"
const router = useRouter()
const [eligibility, setEligibility] = useState<boolean>()
const [eligibilityQuestions, setEligibilityQuestions] = useState<Definition[]>([])
const [eligibilityQuestions, setEligibilityQuestions] = useState<
Definition[]
>([])

const checkEligibility = async (values: any) => {
// Eligibility check
Expand Down Expand Up @@ -113,20 +115,24 @@ const EligibilityForm: React.FC<EligibilityFormProps> = ({
)

export const getServerSideProps: GetServerSideProps = async (context) => {
const {projectId} = context.query
const { projectId } = context.query

if (typeof projectId === "string") {
const consentDefinitions: string | undefined = await githubService.initiateFetch(projectId,
REMOTE_DEFINITIONS_CONFIG.ELIGIBILITY_DEFINITION_FILE_NAME_CONTENT ,REMOTE_DEFINITIONS_CONFIG.ELIGIBILITY_VERSION)
const consentDefinitions: string | undefined =
await githubService.initiateFetch(
projectId,
REMOTE_DEFINITIONS_CONFIG.ELIGIBILITY_DEFINITION_FILE_NAME_CONTENT,
REMOTE_DEFINITIONS_CONFIG.ELIGIBILITY_VERSION,
)

if (consentDefinitions == undefined) return {props: {}}
if (consentDefinitions == undefined) return { props: {} }

return {
props: {
definitions: consentDefinitions,
}
},
}
} else return {props: {}}
} else return { props: {} }
}

export default Eligibility
28 changes: 16 additions & 12 deletions pages/study-consent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import {
} from "@ory/client"
import { H3, P } from "@ory/themes"
import { AxiosError } from "axios"
import type {GetServerSideProps, NextPage} from "next"
import type { GetServerSideProps, NextPage } from "next"
import Head from "next/head"
import Link from "next/link"
import { useRouter } from "next/router"
import {MutableRefObject, ReactNode, useEffect, useState} from "react"
import { MutableRefObject, ReactNode, useEffect, useState } from "react"

import { REMOTE_DEFINITIONS_CONFIG } from "../config/github-config"
import {
ActionCard,
CenterLink,
Expand All @@ -23,9 +24,8 @@ import {
} from "../pkg"
import { handleFlowError } from "../pkg/errors"
import ory from "../pkg/sdk"
import githubService from "../services/github-service";
import {Definition} from "../utils/structures";
import {REMOTE_DEFINITIONS_CONFIG} from "../config/github-config";
import githubService from "../services/github-service"
import { Definition } from "../utils/structures"

interface Props {
flow?: SettingsFlow
Expand All @@ -44,7 +44,7 @@ function StudyConsentCard({ children }: Props & { children: ReactNode }) {
)
}

const StudyConsent: NextPage<StudyConsentPageProps> = ({definitions}) => {
const StudyConsent: NextPage<StudyConsentPageProps> = ({ definitions }) => {
const [flow, setFlow] = useState<SettingsFlow>()

// Get ?flow=... from the URL
Expand Down Expand Up @@ -222,20 +222,24 @@ const ConsentForm: React.FC<any> = ({
}

export const getServerSideProps: GetServerSideProps = async (context) => {
const {projectId} = context.query
const { projectId } = context.query

if (typeof projectId === "string") {
const consentDefinitions: string | undefined = await githubService.initiateFetch(projectId,
REMOTE_DEFINITIONS_CONFIG.CONSENT_DEFINITION_FILE_NAME_CONTENT ,REMOTE_DEFINITIONS_CONFIG.CONSENT_VERSION)
const consentDefinitions: string | undefined =
await githubService.initiateFetch(
projectId,
REMOTE_DEFINITIONS_CONFIG.CONSENT_DEFINITION_FILE_NAME_CONTENT,
REMOTE_DEFINITIONS_CONFIG.CONSENT_VERSION,
)

if (consentDefinitions == undefined) return {props: {}}
if (consentDefinitions == undefined) return { props: {} }

return {
props: {
definitions: consentDefinitions,
}
},
}
} else return {props: {}}
} else return { props: {} }
}

export default StudyConsent
108 changes: 93 additions & 15 deletions pages/study.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
import type {GetServerSideProps, NextPage} from "next"
import type { GetServerSideProps, NextPage } from "next"
import Head from "next/head"
import { useRouter } from "next/router"
import {MutableRefObject, useEffect, useRef, useState} from "react"
import { MutableRefObject, useEffect, useRef, useState } from "react"

import { REMOTE_DEFINITIONS_CONFIG } from "../config/github-config"
// Import render helpers
import { MarginCard, CardTitle, TextCenterButton, InnerCard } from "../pkg"
import githubService from "../services/github-service";
import {REMOTE_DEFINITIONS_CONFIG} from "../config/github-config";
import {Definition} from "../utils/structures";
import githubService from "../services/github-service"
import fetchProjectsFromMp from "../services/mp-projects-fetcher"
import { ContentLengthError } from "../utils/errors/ContentLengthError"
import { GithubApiError } from "../utils/errors/GithubApiError"
import { MPFetchError } from "../utils/errors/MPFetchError"
import { NoContentError } from "../utils/errors/NoContentError"
import { Definition, Project } from "../utils/structures"

interface StudyPageProps {
definitions: string
projectExists: boolean
exceptionMessage: string
exceptionStatusCode: number
}

// Renders the eligibility page
const Study: NextPage<StudyPageProps> = ({definitions}) => {
const Study: NextPage<StudyPageProps> = ({
definitions,
projectExists,
exceptionMessage,
exceptionStatusCode,
}) => {
const router = useRouter()
const [projectId, setProjectId] = useState<string | null>(null)
const studyInfo: MutableRefObject<Definition[]> = useRef([])

useEffect(() => {
// If the router is not ready yet, or we already have a flow, do nothing.
if (router.isReady) {
const {projectId} = router.query
const { projectId } = router.query
if (definitions != null) {
studyInfo.current = JSON.parse(definitions) as Definition[]
}
Expand All @@ -33,6 +46,31 @@ const Study: NextPage<StudyPageProps> = ({definitions}) => {
}
}, [router.query, router.isReady, definitions])

if (exceptionMessage) {
return (
<MarginCard>
<CardTitle>
An exception occurred while fetching the project or definitions
</CardTitle>
<p>{exceptionMessage}</p>
{exceptionStatusCode && <p>Status Code: {exceptionStatusCode}</p>}
</MarginCard>
)
}

if (projectExists === false) {
return (
<MarginCard>
<CardTitle>Project Not Found</CardTitle>
<p>
The project with the name {projectId} does not exist in the Management
Portal.
</p>
<p>Please enter a valid project name.</p>
</MarginCard>
)
}

return (
<>
<Head>
Expand Down Expand Up @@ -75,19 +113,59 @@ const StudyInfo: React.FC<any> = ({ questions }) => {
}

export const getServerSideProps: GetServerSideProps = async (context) => {
const {projectId} = context.query
const { projectId } = context.query
if (typeof projectId === "string") {
const studyDefinitions: string | undefined = await githubService.initiateFetch(projectId,
REMOTE_DEFINITIONS_CONFIG.STUDY_INFO_DEFINITION_FILE_NAME_CONTENT, REMOTE_DEFINITIONS_CONFIG.STUDY_INFO_VERSION)
try {
const fetchedProjects: Project[] = await fetchProjectsFromMp()
const mpProjectNames: string[] = fetchedProjects.map(
(project: Project) => project.projectName,
)
if (!mpProjectNames.includes(projectId)) {
console.log("No project found")
return {
props: {
projectExists: false,
},
}
}

if (studyDefinitions == undefined) return {props: {}}
const studyDefinitions: string | undefined =
await githubService.initiateFetch(
projectId,
REMOTE_DEFINITIONS_CONFIG.STUDY_INFO_DEFINITION_FILE_NAME_CONTENT,
REMOTE_DEFINITIONS_CONFIG.STUDY_INFO_VERSION,
)

return {
props: {
definitions: studyDefinitions,
return studyDefinitions === undefined
? { props: { projectExists: true } }
: { props: { definitions: studyDefinitions, projectExists: true } }
} catch (error: any) {
if (
error instanceof MPFetchError ||
error instanceof ContentLengthError ||
error instanceof NoContentError
) {
return {
props: {
exceptionMessage: error.message,
},
}
} else if (error instanceof GithubApiError) {
return {
props: {
exceptionMessage: error.message,
exceptionStatusCode: error.statusCode,
},
}
} else {
return {
props: {
exceptionMessage: error.message,
},
}
}
}
} else return {props: {}}
} else return { props: { projectExists: false } }
}

export default Study
Loading

0 comments on commit 5f85e34

Please sign in to comment.