From eb7a3cbff0e6648de03bb71a858cbb3d6d009f5e Mon Sep 17 00:00:00 2001 From: "Md. Ashikul Alam" Date: Thu, 24 Oct 2024 17:13:13 +0600 Subject: [PATCH] chore: amend workflow scopes and remove redundant functions --- .../registration/fhir/fhir-bundle-modifier.ts | 68 +------------------ .../features/registration/fhir/fhir-utils.ts | 19 +----- packages/workflow/src/utils/auth-utils.ts | 15 +--- 3 files changed, 5 insertions(+), 97 deletions(-) diff --git a/packages/workflow/src/features/registration/fhir/fhir-bundle-modifier.ts b/packages/workflow/src/features/registration/fhir/fhir-bundle-modifier.ts index c1f6bd98650..272513dd20b 100644 --- a/packages/workflow/src/features/registration/fhir/fhir-bundle-modifier.ts +++ b/packages/workflow/src/features/registration/fhir/fhir-bundle-modifier.ts @@ -15,7 +15,6 @@ import { Composition, Patient, Practitioner, - RegistrationStatus, ResourceIdentifier, Saved, Task, @@ -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( @@ -64,39 +57,6 @@ export async function invokeRegistrationValidation( return bundle } -export async function setupRegistrationWorkflow( - taskResource: Task, - tokenpayload: ITokenPayload, - defaultStatus?: RegistrationStatus -): Promise { - 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( taskResource: T, practitionerOfficeId: UUID @@ -140,32 +100,6 @@ export function setupLastRegUser( 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, diff --git a/packages/workflow/src/features/registration/fhir/fhir-utils.ts b/packages/workflow/src/features/registration/fhir/fhir-utils.ts index cb7df67a276..2b73bf8e4ab 100644 --- a/packages/workflow/src/features/registration/fhir/fhir-utils.ts +++ b/packages/workflow/src/features/registration/fhir/fhir-utils.ts @@ -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, @@ -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) { @@ -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 && diff --git a/packages/workflow/src/utils/auth-utils.ts b/packages/workflow/src/utils/auth-utils.ts index abc2516490f..14faa16b11c 100644 --- a/packages/workflow/src/utils/auth-utils.ts +++ b/packages/workflow/src/utils/auth-utils.ts @@ -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 @@ -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 ) }