Skip to content

Commit 397930e

Browse files
authored
feat: confirm registration via graphql api (#1159)
* feat: confirm registration via graphql api (#288) * refactor: remove all graphql related stuff * chore: remove unused code * feat: allow graphql type hints * feat: allow confirming registration via the new graphql api * refactor: use graphql comment so no package needed * refactor: use graphql comment in the last query as well * refactor: throw if the composition doesn't have an id * fix: throw if the id is not found as the function is _get_ * fix: add required env variables * fix: use identifiers instead of childIdentifiers * fix: remove posting tracking id * revert country data changes
1 parent 4d33096 commit 397930e

File tree

14 files changed

+1039
-1056
lines changed

14 files changed

+1039
-1056
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ build
2626
src/features/employees/tmp/login-details.json
2727
src/tests/locations.json
2828

29-
3029
graphql.schema.json
30+
3131
*.tar.gz
3232
.secrets
3333
.env*

.vscode/extensions.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"recommendations": [
3+
"apollographql.vscode-apollo"
4+
]
5+
}

apollo.config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"client": {
3+
"service": {
4+
"name": "@opencrvs/gateway",
5+
"url": "http://localhost:7070/graphql"
6+
}
7+
}
8+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
"csv2json": "^1.4.2",
9898
"date-fns": "^2.28.0",
9999
"dotenv": "^16.4.5",
100+
"envalid": "^8.0.0",
100101
"esbuild": "^0.18.9",
101102
"google-libphonenumber": "^3.2.32",
102103
"graphql": "^16.3.0",

src/api/event-registration/handler.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
1010
*/
1111
import * as Hapi from '@hapi/hapi'
12-
import fetch from 'node-fetch'
1312
import { logger } from '@countryconfig/logger'
14-
import { CONFIRM_REGISTRATION_URL } from '@countryconfig/constants'
1513
import { createUniqueRegistrationNumberFromBundle } from '@countryconfig/api/event-registration/service'
1614
import { badImplementation } from '@hapi/boom'
15+
import {
16+
confirmRegistration
17+
// rejectRegistration
18+
} from '@countryconfig/utils/gateway-api'
1719

1820
export async function eventRegistrationHandler(
1921
request: Hapi.Request,
@@ -33,15 +35,28 @@ export async function eventRegistrationHandler(
3335
const bundle = request.payload as fhir.Bundle
3436

3537
const eventRegistrationIdentifiersResponse =
36-
await createUniqueRegistrationNumberFromBundle(bundle)
38+
createUniqueRegistrationNumberFromBundle(bundle)
3739

38-
await fetch(CONFIRM_REGISTRATION_URL, {
39-
method: 'POST',
40-
body: JSON.stringify(eventRegistrationIdentifiersResponse),
41-
headers: request.headers
42-
})
40+
await confirmRegistration(
41+
eventRegistrationIdentifiersResponse.compositionId,
42+
eventRegistrationIdentifiersResponse,
43+
{
44+
headers: request.headers
45+
}
46+
)
4347
} catch (err) {
44-
// IF ANY ERROR OCCURS IN THIS API, THE REGISTRATION WILL BE REJECTED AND MUST BE RE-SUBMITTED BY A REGISTRAR ONCE THE ISSUE IS RESOLVED
48+
// If the confirm registration endpoint throws an error, the registration will be retried through country-config
49+
// If you don't want the registration to retry, you can call `rejectRegistration` from here and return 202 Accepted
50+
51+
// await confirmRegistration(
52+
// eventRegistrationIdentifiersResponse.compositionId,
53+
// {
54+
// reason: 'other', // Refer to the GraphQL schema for other options
55+
// comment: 'The comment that will be visible on audit log.'
56+
// },
57+
// { headers: request.headers }
58+
// )
59+
4560
logger.error(err)
4661

4762
const boomError = badImplementation()

src/api/event-registration/service.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ function generateRegistrationNumber(trackingId: string): string {
2424
return brn
2525
}
2626

27-
export async function createUniqueRegistrationNumberFromBundle(
28-
bundle: fhir.Bundle
29-
) {
27+
export function createUniqueRegistrationNumberFromBundle(bundle: fhir.Bundle) {
3028
const taskResource = getTaskResource(bundle)
3129

3230
if (!taskResource || !taskResource.extension) {
@@ -36,11 +34,12 @@ export async function createUniqueRegistrationNumberFromBundle(
3634
}
3735

3836
const trackingId = getTrackingIdFromTaskResource(taskResource)
37+
const compositionId = getCompositionId(bundle)
3938

4039
return {
4140
trackingId,
41+
compositionId,
4242
registrationNumber: generateRegistrationNumber(trackingId),
43-
compositionId: getCompositionId(bundle),
4443
...(taskResource.code?.coding?.[0].code === 'BIRTH' && {
4544
// Some countries desire to create multiple identifiers for citizens at the point of birth registration using external systems.
4645
// OpenCRVS supports up to 3 additional, custom identifiers that can be created

src/constants.ts

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,25 @@
88
*
99
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
1010
*/
11+
import { env } from './environment'
12+
1113
export const TEST_SOURCE = `${process.cwd()}/src/tests/`
12-
export const HOSTNAME = process.env.DOMAIN || '*'
13-
export const DOMAIN = process.env.DOMAIN || '*'
14-
export const GATEWAY_URL = process.env.GATEWAY_URL || 'http://localhost:7070'
15-
export const LOGIN_URL = process.env.LOGIN_URL || 'http://localhost:3020/'
16-
export const CLIENT_APP_URL =
17-
process.env.CLIENT_APP_URL || 'http://localhost:3000/'
18-
export const FHIR_URL = process.env.FHIR_URL || 'http://localhost:3447/fhir'
19-
export const ORG_URL = 'http://opencrvs.org'
20-
export const COUNTRY_CONFIG_HOST = process.env.COUNTRY_CONFIG_HOST || '0.0.0.0'
21-
export const COUNTRY_CONFIG_PORT = process.env.COUNTRY_CONFIG_PORT || 3040
22-
export const AUTH_URL = process.env.AUTH_URL || 'http://localhost:4040'
23-
export const COUNTRY_CONFIG_URL =
24-
process.env.COUNTRY_CONFIG_URL || 'http://localhost:3040'
25-
export const APPLICATION_CONFIG_URL =
26-
process.env.APPLICATION_CONFIG_URL || 'http://localhost:2021/'
27-
export const SENTRY_DSN = process.env.SENTRY_DSN
28-
// Check if the token has been invalided in the auth service before it has expired
29-
// This needs to be a string to make it easy to pass as an ENV var.
30-
export const CHECK_INVALID_TOKEN = process.env.CHECK_INVALID_TOKEN || 'false'
31-
export const CONFIRM_REGISTRATION_URL =
32-
process.env.CONFIRM_REGISTRATION_URL ||
33-
'http://localhost:5050/confirm/registration'
3414
export const DEFAULT_TIMEOUT = 600000
35-
export const PRODUCTION = process.env.NODE_ENV === 'production'
36-
export const QA_ENV = process.env.QA_ENV || false
15+
16+
export const DOMAIN = env.DOMAIN
17+
export const GATEWAY_URL = env.GATEWAY_URL
18+
export const LOGIN_URL = env.LOGIN_URL
19+
export const CLIENT_APP_URL = env.CLIENT_APP_URL
20+
export const FHIR_URL = env.FHIR_URL
21+
22+
export const COUNTRY_CONFIG_HOST = env.COUNTRY_CONFIG_HOST
23+
export const COUNTRY_CONFIG_PORT = env.COUNTRY_CONFIG_PORT
24+
export const AUTH_URL = env.AUTH_URL
25+
export const COUNTRY_CONFIG_URL = env.COUNTRY_CONFIG_URL
26+
export const APPLICATION_CONFIG_URL = env.APPLICATION_CONFIG_URL
27+
28+
export const SENTRY_DSN = env.SENTRY_DSN
29+
export const CHECK_INVALID_TOKEN = env.CHECK_INVALID_TOKEN
30+
31+
export const PRODUCTION = env.isProd
32+
export const QA_ENV = env.QA_ENV

src/environment.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
5+
*
6+
* OpenCRVS is also distributed under the terms of the Civil Registration
7+
* & Healthcare Disclaimer located at http://opencrvs.org/license.
8+
*
9+
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
10+
*/
11+
import { bool, cleanEnv, port, str, url } from 'envalid'
12+
13+
export const env = cleanEnv(process.env, {
14+
DOMAIN: str({ devDefault: '*' }),
15+
GATEWAY_URL: url({ devDefault: 'http://localhost:7070' }),
16+
LOGIN_URL: url({ devDefault: 'http://localhost:3020/' }),
17+
CLIENT_APP_URL: url({ devDefault: 'http://localhost:3000/' }),
18+
FHIR_URL: url({ devDefault: 'http://localhost:3447/fhir' }),
19+
COUNTRY_CONFIG_HOST: str({ default: '0.0.0.0' }),
20+
COUNTRY_CONFIG_PORT: port({ default: 3040 }),
21+
AUTH_URL: url({ devDefault: 'http://localhost:4040' }),
22+
COUNTRY_CONFIG_URL: url({ devDefault: 'http://localhost:3040' }),
23+
APPLICATION_CONFIG_URL: url({ devDefault: 'http://localhost:2021/' }),
24+
SENTRY_DSN: str({ default: undefined }),
25+
CHECK_INVALID_TOKEN: bool({
26+
default: true,
27+
devDefault: false,
28+
desc: 'Check if the token has been invalidated in the auth service before it has expired'
29+
}),
30+
CONFIRM_REGISTRATION_URL: url({
31+
devDefault: 'http://localhost:5050/confirm/registration'
32+
}),
33+
QA_ENV: bool({ default: false })
34+
})

src/form/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ import { birthForm } from './birth'
1414
import { deathForm } from './death'
1515
import { marriageForm } from './marriage'
1616
import { IForms, Event } from './types/types'
17-
import { fetchUserLocationHierarchy } from '@countryconfig/utils/users'
17+
import { fetchUserLocationHierarchy } from '@countryconfig/utils/gateway-api'
1818

1919
export async function formHandler(req: Request): Promise<IForms> {
2020
const addressHierarchy = await fetchUserLocationHierarchy(
21-
req.headers.authorization,
22-
req.auth.credentials.sub as string
21+
req.auth.credentials.sub as string,
22+
{ headers: req.headers }
2323
)
2424
// ====================== NOTE REGARDING MIGRATING FROM OPNCRVS v1.2 OR EARLIER ======================
2525

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,18 +142,18 @@ export const verifyToken = async (token: string, authUrl: string) => {
142142
const validateFunc = async (
143143
payload: any,
144144
request: Hapi.Request,
145-
checkInvalidToken: string,
145+
checkInvalidToken: boolean,
146146
authUrl: string
147147
) => {
148148
let valid
149-
if (checkInvalidToken === 'true') {
149+
if (checkInvalidToken) {
150150
valid = await verifyToken(
151151
request.headers.authorization.replace('Bearer ', ''),
152152
authUrl
153153
)
154154
}
155155

156-
if (valid === true || checkInvalidToken !== 'true') {
156+
if (valid === true || !checkInvalidToken) {
157157
return {
158158
isValid: true,
159159
credentials: payload

0 commit comments

Comments
 (0)