Skip to content

Commit

Permalink
created a common fuction to destructure attribute value
Browse files Browse the repository at this point in the history
  • Loading branch information
ritikramuka committed Nov 7, 2023
1 parent b5884c4 commit 53cb3fe
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 34 deletions.
13 changes: 6 additions & 7 deletions src/web/client/dal/remoteFetchProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
import * as vscode from "vscode";
import {
convertContentToUint8Array,
GetFileContent,
GetFileNameWithExtension,
getRootWebPageId,
getAttributeContent,
getSanitizedFileName,
isPortalVersionV1,
isPortalVersionV2,
Expand Down Expand Up @@ -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(
Expand All @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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[] = [];
Expand Down
3 changes: 1 addition & 2 deletions src/web/client/telemetry/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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',
}
31 changes: 6 additions & 25 deletions src/web/client/utilities/commonUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

0 comments on commit 53cb3fe

Please sign in to comment.