From 53cb3fea33dd60a1fed65da745e916058a95baad Mon Sep 17 00:00:00 2001 From: ramukaritik Date: Tue, 7 Nov 2023 14:12:32 +0530 Subject: [PATCH] created a common fuction to destructure attribute value --- src/web/client/dal/remoteFetchProvider.ts | 13 +++++----- src/web/client/telemetry/constants.ts | 3 +-- src/web/client/utilities/commonUtil.ts | 31 +++++------------------ 3 files changed, 13 insertions(+), 34 deletions(-) diff --git a/src/web/client/dal/remoteFetchProvider.ts b/src/web/client/dal/remoteFetchProvider.ts index 6e2e9dcd..6c6b9727 100644 --- a/src/web/client/dal/remoteFetchProvider.ts +++ b/src/web/client/dal/remoteFetchProvider.ts @@ -6,9 +6,8 @@ import * as vscode from "vscode"; import { convertContentToUint8Array, - GetFileContent, GetFileNameWithExtension, - getRootWebPageId, + getAttributeContent, getSanitizedFileName, isPortalVersionV1, isPortalVersionV2, @@ -337,7 +336,7 @@ async function processDataAndCreateFile( ); if (fileExtension === undefined) { - const expandedContent = GetFileContent(result, attributePath, entityName, entityId); + const expandedContent = getAttributeContent(result, attributePath, entityName, entityId); if (expandedContent !== Constants.NO_CONTENT) { await processExpandedData( @@ -360,10 +359,10 @@ async function processDataAndCreateFile( // Get rootpage id let rootWebPageId = undefined; - + if (rootWebPageIdAttribute) { const rootWebPageIdPath : IAttributePath = getAttributePath(rootWebPageIdAttribute); - rootWebPageId = getRootWebPageId(result, rootWebPageIdPath, entityName, entityId); + rootWebPageId = getAttributeContent(result, rootWebPageIdPath, entityName, entityId); } if (fileCreationValid) { @@ -457,7 +456,7 @@ async function createFile( mimeType = getMimeType(mappingContent); fileContent = getMappingEntityContent(entityName, mappingContent, attribute); } else { - fileContent = GetFileContent(result, attributePath, entityName, entityId); + fileContent = getAttributeContent(result, attributePath, entityName, entityId); } await createVirtualFile( @@ -580,7 +579,7 @@ export async function preprocessData( // eslint-disable-next-line @typescript-eslint/no-explicit-any data?.forEach((dataItem: any) => { - const webFormSteps = GetFileContent(dataItem, attributePath, entityType, fetchedFileId as string) as []; + const webFormSteps = getAttributeContent(dataItem, attributePath, entityType, fetchedFileId as string) as []; // eslint-disable-next-line @typescript-eslint/no-explicit-any const steps: any[] = []; diff --git a/src/web/client/telemetry/constants.ts b/src/web/client/telemetry/constants.ts index 81d00ca0..538adfe4 100644 --- a/src/web/client/telemetry/constants.ts +++ b/src/web/client/telemetry/constants.ts @@ -49,7 +49,7 @@ export enum telemetryEventNames { WEB_EXTENSION_CREATE_ENTITY_FOLDER_FAILED = "webExtensionCreateEntityFolderFailed", WEB_EXTENSION_PREPROCESS_DATA_FAILED = "webExtensionPreprocessDataFailed", WEB_EXTENSION_PREPROCESS_DATA_SUCCESS = "webExtensionPreprocessDataSuccess", - WEB_EXTENSION_GET_FILE_CONTENT_ERROR = "webExtensionGetFileContentError", + WEB_EXTENSION_ATTRIBUTE_CONTENT_ERROR = "webExtensionAttributeContentError", WEB_EXTENSION_SET_FILE_CONTENT_ERROR = "webExtensionSetFileContentError", WEB_EXTENSION_FAILED_TO_PREPARE_WORKSPACE = "webExtensionFailedToPrepareWorkspace", WEB_EXTENSION_BULKHEAD_QUEUE_FULL = "webExtensionBulkheadQueueFull", @@ -99,5 +99,4 @@ export enum telemetryEventNames { WEB_EXTENSION_PREVIEW_SITE_TRIGGERED = 'webExtensionPreviewSiteTriggered', WEB_EXTENSION_IMAGE_EDIT_SUPPORTED_FILE_EXTENSION = 'webExtensionImageEditSupportedFileExtension', WEB_EXTENSION_SAVE_IMAGE_FILE_TRIGGERED = 'webExtensionSaveImageFileTriggered', - WEB_EXTENSION_GET_ROOT_PAGE_ID_ERROR = 'webExtensionGetRootPageIdError', } diff --git a/src/web/client/utilities/commonUtil.ts b/src/web/client/utilities/commonUtil.ts index 9dc522d7..4296bf5a 100644 --- a/src/web/client/utilities/commonUtil.ts +++ b/src/web/client/utilities/commonUtil.ts @@ -62,23 +62,23 @@ export function isExtensionNeededInFileName(entity: string) { } // eslint-disable-next-line @typescript-eslint/no-explicit-any -export function GetFileContent(result: any, attributePath: IAttributePath, entityName: string, entityId: string) { - let fileContent = result[attributePath.source] ?? NO_CONTENT; +export function getAttributeContent(result: any, attributePath: IAttributePath, entityName: string, entityId: string) { + let value = result[attributePath.source] ?? NO_CONTENT; try { if (result[attributePath.source] && attributePath.relativePath.length) { - fileContent = + value = JSON.parse(result[attributePath.source])[attributePath.relativePath] ?? NO_CONTENT; } } catch (error) { const errorMsg = (error as Error)?.message; - WebExtensionContext.telemetry.sendErrorTelemetry(telemetryEventNames.WEB_EXTENSION_GET_FILE_CONTENT_ERROR, - GetFileContent.name, + WebExtensionContext.telemetry.sendErrorTelemetry(telemetryEventNames.WEB_EXTENSION_ATTRIBUTE_CONTENT_ERROR, + getAttributeContent.name, `For ${entityName} with entityId ${entityId} and attributePath ${JSON.stringify(attributePath)} error: ${errorMsg}`); } - return fileContent; + return value; } // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -264,22 +264,3 @@ export function getImageFileContent(fileFsPath: string, fileContent: Uint8Array) return webFileV2 ? fileContent : Buffer.from(fileContent).toString(BASE_64); } - -export function getRootWebPageId(result: any, attributePath: IAttributePath, entityName: string, entityId: string) { - let rootWebPageId = result[attributePath.source] ?? NO_CONTENT; - - try { - if (result[attributePath.source] && attributePath.relativePath.length) { - rootWebPageId = - JSON.parse(result[attributePath.source])[attributePath.relativePath] ?? NO_CONTENT; - } - } - catch (error) { - const errorMsg = (error as Error)?.message; - WebExtensionContext.telemetry.sendErrorTelemetry(telemetryEventNames.WEB_EXTENSION_GET_ROOT_PAGE_ID_ERROR, - getRootWebPageId.name, - `For ${entityName} with entityId ${entityId} and attributePath ${JSON.stringify(attributePath)} error: ${errorMsg}`); - } - - return rootWebPageId; -}