Skip to content

Commit

Permalink
chore: amend workflow scopes and remove redundant functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Nil20 committed Oct 24, 2024
1 parent 5c37d29 commit eb7a3cb
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
Composition,
Patient,
Practitioner,
RegistrationStatus,
ResourceIdentifier,
Saved,
Task,
Expand All @@ -26,20 +25,14 @@ import {
SupportedPatientIdentifierCode
} from '@opencrvs/commons/types'
import { APPLICATION_CONFIG_URL, COUNTRY_CONFIG_URL } from '@workflow/constants'
import {
OPENCRVS_SPECIFICATION_URL,
RegStatus
} from '@workflow/features/registration/fhir/constants'
import { OPENCRVS_SPECIFICATION_URL } from '@workflow/features/registration/fhir/constants'
import { getSectionEntryBySectionCode } from '@workflow/features/registration/fhir/fhir-template'
import {
fetchExistingRegStatusCode,
getFromFhir,
getRegStatusCode,
updateResourceInHearth
} from '@workflow/features/registration/fhir/fhir-utils'
import { getMosipUINToken } from '@workflow/features/registration/utils'
import { getPractitionerRef } from '@workflow/features/user/utils'
import { ITokenPayload } from '@workflow/utils/auth-utils'
import fetch from 'node-fetch'

export async function invokeRegistrationValidation(
Expand All @@ -64,39 +57,6 @@ export async function invokeRegistrationValidation(
return bundle
}

export async function setupRegistrationWorkflow(
taskResource: Task,
tokenpayload: ITokenPayload,
defaultStatus?: RegistrationStatus
): Promise<Task> {
const regStatusCodeString = defaultStatus
? defaultStatus
: getRegStatusCode(tokenpayload)

if (!taskResource.businessStatus) {
taskResource.businessStatus = {} as Task['businessStatus']
}
if (!taskResource.businessStatus.coding) {
taskResource.businessStatus.coding = []
}
const regStatusCode = taskResource.businessStatus.coding.find((code) => {
return code.system === `${OPENCRVS_SPECIFICATION_URL}reg-status`
})

if (regStatusCode) {
regStatusCode.code = regStatusCodeString
} else {
taskResource.businessStatus.coding.push({
system: `${OPENCRVS_SPECIFICATION_URL}reg-status`,
code: regStatusCodeString
})
}
// Checking for duplicate status update
await checkForDuplicateStatusUpdate(taskResource)

return taskResource
}

export function setupLastRegOffice<T extends Task>(
taskResource: T,
practitionerOfficeId: UUID
Expand Down Expand Up @@ -140,32 +100,6 @@ export function setupLastRegUser<T extends Task>(
return taskResource
}

async function checkForDuplicateStatusUpdate(taskResource: Task) {
const regStatusCode =
taskResource &&
taskResource.businessStatus &&
taskResource.businessStatus.coding &&
taskResource.businessStatus.coding.find((code) => {
return code.system === `${OPENCRVS_SPECIFICATION_URL}reg-status`
})

if (
!taskResource ||
!taskResource.id ||
!regStatusCode ||
regStatusCode.code === RegStatus.CERTIFIED
) {
return
}
const existingRegStatusCode = await fetchExistingRegStatusCode(
taskResource.id
)
if (existingRegStatusCode?.code === regStatusCode.code) {
logger.error(`Declaration is already in ${regStatusCode} state`)
throw new Error(`Declaration is already in ${regStatusCode} state`)
}
}

export function updatePatientIdentifierWithRN(
record: WaitingForValidationRecord,
composition: Composition,
Expand Down
19 changes: 1 addition & 18 deletions packages/workflow/src/features/registration/fhir/fhir-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ import {
CHILD_SECTION_CODE,
DECEASED_SECTION_CODE,
EVENT_TYPE,
OPENCRVS_SPECIFICATION_URL,
RegStatus
OPENCRVS_SPECIFICATION_URL
} from '@workflow/features/registration/fhir/constants'
import {
getSectionEntryBySectionCode,
Expand All @@ -41,7 +40,6 @@ import {
} from '@workflow/features/registration/utils'
import { getTaskEventType } from '@workflow/features/task/fhir/utils'
import { logger } from '@opencrvs/commons'
import { ITokenPayload, USER_SCOPE } from '@workflow/utils/auth-utils'
import fetch, { RequestInit } from 'node-fetch'

export async function getSharedContactMsisdn(fhirBundle: Bundle) {
Expand Down Expand Up @@ -164,21 +162,6 @@ export function getPaperFormID(taskResource: Task) {
return paperFormIdentifier.value
}

export function getRegStatusCode(tokenPayload: ITokenPayload) {
if (!tokenPayload.scope) {
throw new Error('No scope found on token')
}
if (tokenPayload.scope.indexOf(USER_SCOPE.REGISTER.toString()) > -1) {
return RegStatus.REGISTERED
} else if (tokenPayload.scope.indexOf(USER_SCOPE.DECLARE.toString()) > -1) {
return RegStatus.DECLARED
} else if (tokenPayload.scope.indexOf(USER_SCOPE.VALIDATE.toString()) > -1) {
return RegStatus.VALIDATED
} else {
throw new Error('No valid scope found on token')
}
}

export function getEntryId(fhirBundle: Bundle) {
const composition =
fhirBundle &&
Expand Down
15 changes: 3 additions & 12 deletions packages/workflow/src/utils/auth-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,7 @@
*/
import * as decode from 'jwt-decode'
import * as Hapi from '@hapi/hapi'
import { hasScope } from '@opencrvs/commons/authentication'

export enum USER_SCOPE {
DECLARE = 'declare',
VALIDATE = 'validate',
REGISTER = 'register',
CERTIFY = 'certify',
RECORD_SEARCH = 'recordsearch',
VERIFY = 'verify'
}
import { hasScope, SCOPES } from '@opencrvs/commons/authentication'

export interface ITokenPayload {
sub: string
Expand Down Expand Up @@ -51,13 +42,13 @@ export const getToken = (request: Hapi.Request): string => {
export function hasRegisterScope(request: Hapi.Request): boolean {
return hasScope(
{ Authorization: request.headers.authorization },
USER_SCOPE.REGISTER
SCOPES.RECORD_REGISTER
)
}

export function hasValidateScope(request: Hapi.Request): boolean {
return hasScope(
{ Authorization: request.headers.authorization },
USER_SCOPE.VALIDATE
SCOPES.RECORD_SUBMIT_FOR_APPROVAL
)
}

0 comments on commit eb7a3cb

Please sign in to comment.