Skip to content

Commit

Permalink
fix(): activity completion logic
Browse files Browse the repository at this point in the history
  • Loading branch information
nckhell committed Sep 11, 2024
1 parent e61d65e commit 52b0e59
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 86 deletions.
35 changes: 1 addition & 34 deletions pages/api/identity/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export default async function handler(

const { activity_id, pathway_id, dob } = bodyValidation.data

// TODO JB
const token = jwt.sign(
{
username: environment.apiGatewayConsumerName,
Expand All @@ -57,7 +56,7 @@ export default async function handler(

// LATER: we have an "i don't know my DOB" option then we can complete with failure... etc.
const input = { pathway_id, dob }
// TODO JB

const response = await fetch(environment.orchestrationApiUrl, {
method: 'POST',
headers: {
Expand All @@ -79,41 +78,9 @@ export default async function handler(
})

const resp = await response.json()
console.log(resp)
const { data, errors } = resp
const { is_verified } = data?.verify_identity

if (is_verified) {
await fetch(environment.orchestrationApiUrl, {
method: 'POST',
headers: {
authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: `
mutation CompleteExtensionActivity($input: CompleteExtensionActivityInput!) {
completeExtensionActivity(input: $input) {
code
success
}
}
`,
variables: {
input: {
activity_id,
data_points: [
{
key: 'success',
value: 'true',
},
],
},
},
}),
})
}

res.status(200).json({
success: is_verified,
...(errors && errors.length > 0 && { error: errors }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
HostedPageFooter,
InputField,
} from '@awell-health/ui-library'
import { isEmpty } from 'lodash'
import { mapActionFieldsToObject } from '../../../utils'
import { DobCheckActionFields } from './types'
import { useTranslation } from 'next-i18next'
Expand All @@ -23,60 +22,18 @@ export const DobCheck: FC<DobCheckProps> = ({ activityDetails }) => {
const { t } = useTranslation()

const [dobValue, setDobValue] = useState('')
const [loading, setLoading] = useState(false)
const { activity_id, fields, pathway_id } = activityDetails

const { onSubmit } = useDobCheck()
const { loading, onSubmit } = useDobCheck({
pathway_id,
activity_id,
})

const { label } = useMemo(
() => mapActionFieldsToObject<DobCheckActionFields>(fields),
[fields]
)

const handleActivityCompletion = useCallback(() => {
onSubmit({
activityId: activity_id,
})
}, [activity_id, onSubmit])

const handleDobCheck = useCallback(async () => {
if (isEmpty(dobValue)) {
// Prettify this later
alert('Please enter a date of birth')
return
}

try {
setLoading(true)
const response = await fetch('/api/identity/verify', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ dob: dobValue, pathway_id, activity_id }),
})

setLoading(false)

if (!response.ok) {
throw new Error('Failed to check dob')
}

const jsonRes = await response.json()

if (!jsonRes?.success) {
// Prettify this later
alert('No match')
return
}

handleActivityCompletion()
} catch (error) {
console.error('Error checking dob:', error)
throw error
}
}, [dobValue, handleActivityCompletion])

return (
<>
<main
Expand Down Expand Up @@ -108,7 +65,11 @@ export const DobCheck: FC<DobCheckProps> = ({ activityDetails }) => {
<div
className={`${activityClasses.button_wrapper} ${classes.container}`}
>
<Button variant="primary" onClick={handleDobCheck} disabled={loading}>
<Button
variant="primary"
onClick={() => onSubmit(dobValue)}
disabled={loading}
>
{t('activities.identity_verification.cta')}
</Button>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,73 @@
import { useCallback } from 'react'
import { useCallback, useState } from 'react'
import { isEmpty } from 'lodash'
import { useCompleteExtensionActivity } from '../../../types'

export const useDobCheck = () => {
type UseDobCheckHook = ({
pathway_id,
activity_id,
}: {
pathway_id: string
activity_id: string
}) => {
loading: boolean
isSubmitting: boolean
onSubmit: (dob: string) => Promise<void>
}

export const useDobCheck: UseDobCheckHook = ({ pathway_id, activity_id }) => {
const [loading, setLoading] = useState(false)
const { isSubmitting, onSubmit: _onSubmit } = useCompleteExtensionActivity()

const onSubmit = useCallback(
const handleActivityCompletion = useCallback(
async ({ activityId }: { activityId: string }) => {
return _onSubmit(activityId, [])
return _onSubmit(activityId, [{ key: 'success', value: String(true) }])
},
[_onSubmit]
)

const onSubmit = useCallback(
async (dob: string) => {
if (isEmpty(dob)) {
// Prettify this later
alert('Please enter a date of birth')
return
}

try {
setLoading(true)
const response = await fetch('/api/identity/verify', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ dob: dob, pathway_id, activity_id }),
})

setLoading(false)

if (!response.ok) {
throw new Error('Failed to check dob')
}

const jsonRes = await response.json()

if (!jsonRes?.success) {
// Prettify this later
alert('No match')
return
}

handleActivityCompletion({ activityId: activity_id })
} catch (error) {
console.error('Error checking dob:', error)
throw error
}
},
[pathway_id, activity_id, handleActivityCompletion]
)

return {
loading,
isSubmitting,
onSubmit,
}
Expand Down

0 comments on commit 52b0e59

Please sign in to comment.