Skip to content

Commit

Permalink
Fix: Signature not showing (#8235)
Browse files Browse the repository at this point in the history
* fix: use existing signature attachment if signature was not uploaded

* fix: use minio regex from commons in client service worker

* fix: expose documents to client in commons

* fix: datasource url
  • Loading branch information
jamil314 authored Jan 8, 2025
1 parent 1ff163d commit 689146f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 18 deletions.
8 changes: 2 additions & 6 deletions packages/client/src/src-sw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import { registerRoute, NavigationRoute } from 'workbox-routing'
import { NetworkFirst, CacheFirst } from 'workbox-strategies'
import { clientsClaim } from 'workbox-core'
import { MINIO_REGEX } from '@opencrvs/commons/client'

// eslint-disable-next-line @typescript-eslint/no-use-before-define
self.__WB_DISABLE_DEV_LOGS = true
Expand Down Expand Up @@ -82,12 +83,7 @@ registerRoute(/http(.+)conditionals\.js$/, new NetworkFirst())
registerRoute(/http(.+)config$/, new NetworkFirst())

// This caches the minio urls
registerRoute(
import.meta.env.DEV
? /http(.+)localhost:3535\/ocrvs\/.+/
: /http(.+)minio\.(.+)\/.*ocrvs.*\/.+/,
new CacheFirst()
)
registerRoute(MINIO_REGEX, new CacheFirst())

/*
* Alternate for navigateFallback & navigateFallbackBlacklist
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ import {
import { useNavigate } from 'react-router-dom'
import { ICertificateData } from '@client/utils/referenceApi'
import { fetchImageAsBase64 } from '@client/utils/imageUtils'
import { isMinioUrl } from '@opencrvs/commons/client'

async function replaceMinioUrlWithBase64(template: Record<string, any>) {
const regex = /\/[^\/?]+\.(jpg|png|jpeg|svg)(?=\?|$)/i

async function recursiveTransform(obj: any) {
if (typeof obj !== 'object' || obj === null) {
return obj
Expand All @@ -63,7 +62,7 @@ async function replaceMinioUrlWithBase64(template: Record<string, any>) {

for (const key in obj) {
const value = obj[key]
if (typeof value === 'string' && regex.test(value)) {
if (typeof value === 'string' && isMinioUrl(value)) {
transformedObject[key] = await fetchImageAsBase64(value)
} else if (typeof value === 'object') {
transformedObject[key] = await recursiveTransform(value)
Expand Down
1 change: 1 addition & 0 deletions packages/commons/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ export * from './search'
export * from './events'
export * from './conditionals/conditionals'
export * from './conditionals/validate'
export * from './documents'

export type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>
5 changes: 5 additions & 0 deletions packages/commons/src/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@
*
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
*/

export const MINIO_REGEX = /\/[^\/?]+\.(jpg|png|jpeg|svg)(?=\?|$)/i

export function isBase64FileString(str: string) {
if (str === '' || str.trim() === '') {
return false
}
const strSplit = str.split(':')
return strSplit.length > 0 && strSplit[0] === 'data'
}

export const isMinioUrl = (url: string) => MINIO_REGEX.test(url)
4 changes: 2 additions & 2 deletions packages/gateway/src/features/fhir/minioAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { DOCUMENTS_URL } from '@gateway/constants'
import { OpenCRVSRESTDataSource } from '@gateway/graphql/data-source'

export default class MinioAPI extends OpenCRVSRESTDataSource {
override baseURL = `${DOCUMENTS_URL}/presigned-url`
override baseURL = `${DOCUMENTS_URL}`

override willSendRequest(
_path: string,
Expand All @@ -25,6 +25,6 @@ export default class MinioAPI extends OpenCRVSRESTDataSource {
}

getStaticData(fileUri: string) {
return this.get(`${fileUri}`)
return this.get(`/presigned-url${fileUri}`)
}
}
27 changes: 20 additions & 7 deletions packages/user-mgnt/src/features/updateUser/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ import {
postFhir,
uploadSignatureToMinio
} from '@user-mgnt/features/createUser/service'
import { logger } from '@opencrvs/commons'
import { isBase64FileString, logger } from '@opencrvs/commons'
import User, { IUser, IUserModel } from '@user-mgnt/model/user'
import { getUserId } from '@user-mgnt/utils/userUtils'
import { QA_ENV } from '@user-mgnt/constants'
import * as Hapi from '@hapi/hapi'
import * as _ from 'lodash'
import { postUserActionToMetrics } from '@user-mgnt/features/changePhone/handler'
import { userRoleScopes } from '@opencrvs/commons/authentication'
import { Practitioner } from '@opencrvs/commons/types'
import {
findExtension,
OPENCRVS_SPECIFICATION_URL,
Practitioner
} from '@opencrvs/commons/types'

export default async function updateUser(
request: Hapi.Request,
Expand Down Expand Up @@ -93,11 +97,20 @@ export default async function updateUser(
throw new Error('Location can be changed only by National System Admin')
}
}
const signatureAttachment = user.signature && {
contentType: user.signature.type,
url: await uploadSignatureToMinio(token, user.signature),
creation: new Date().getTime().toString()
}

const existingSignatureAttachment = findExtension(
`${OPENCRVS_SPECIFICATION_URL}extension/employee-signature`,
existingPractitioner.extension || []
)?.valueAttachment

const signatureAttachment =
(user.signature &&
isBase64FileString(user.signature.data) && {
contentType: user.signature.type,
url: await uploadSignatureToMinio(token, user.signature),
creation: new Date().getTime().toString()
}) ||
existingSignatureAttachment

const practitioner = createFhirPractitioner(
existingUser,
Expand Down

0 comments on commit 689146f

Please sign in to comment.