Skip to content

Commit

Permalink
Fetching logical and display name for attributes (#905)
Browse files Browse the repository at this point in the history
Co-authored-by: amitjoshi <[email protected]>
  • Loading branch information
amitjoshi438 and amitjoshi authored Apr 12, 2024
1 parent 06040fb commit 7e0d326
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/common/copilot/dataverseMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@ import { DOMParser } from "@xmldom/xmldom";
import { ATTRIBUTE_CLASSID, ATTRIBUTE_DATAFIELD_NAME, ATTRIBUTE_DESCRIPTION, ControlClassIdMap, SYSTEFORMS_API_PATH } from "./constants";


interface Attribute {
LogicalName: string;
}

declare const IS_DESKTOP: string | undefined;

export async function getEntityColumns(entityName: string, orgUrl: string, apiToken: string, telemetry: ITelemetry, sessionID: string): Promise<string[]> {
try {
const dataverseURL = `${orgUrl.endsWith('/') ? orgUrl : orgUrl.concat('/')}api/data/v9.2/EntityDefinitions(LogicalName='${entityName}')?$expand=Attributes($select=LogicalName)`;
const dataverseURL = `${orgUrl.endsWith('/') ? orgUrl : orgUrl.concat('/')}api/data/v9.2/EntityDefinitions(LogicalName='${entityName}')?$expand=Attributes`;
const requestInit: RequestInit = {
method: "GET",
headers: {
Expand All @@ -36,10 +32,10 @@ export async function getEntityColumns(entityName: string, orgUrl: string, apiTo
const jsonResponse = await fetchJsonResponse(dataverseURL, requestInit);
const endTime = performance.now();
const responseTime = endTime - startTime || 0;
const attributes = getAttributesFromResponse(jsonResponse);
const attributes = getAttributesFromResponse(jsonResponse); //Display name and logical name fetching from response

sendTelemetryEvent(telemetry, { eventName: CopilotDataverseMetadataSuccessEvent, copilotSessionId: sessionID, durationInMills: responseTime, orgUrl: orgUrl })
return attributes.map((attribute: Attribute) => attribute.LogicalName);
return attributes

} catch (error) {
sendTelemetryEvent(telemetry, { eventName: CopilotDataverseMetadataFailureEvent, copilotSessionId: sessionID, error: error as Error, orgUrl: orgUrl })
Expand Down Expand Up @@ -92,9 +88,20 @@ async function fetchJsonResponse(url: string, requestInit: RequestInit): Promise
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function getAttributesFromResponse(jsonResponse: any): Attribute[] {
function getAttributesFromResponse(jsonResponse: any): string[] {
if (jsonResponse.Attributes && Array.isArray(jsonResponse.Attributes) && jsonResponse.Attributes.length > 0) {
return jsonResponse.Attributes;
const attributes = jsonResponse.Attributes;
const logicalNameDisplayName: string[] = [];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
attributes.forEach((attr:any) => {
const attrDisplayName = attr.DisplayName?.UserLocalizedLabel?.Label; // Optional chaining for handling missing values
if (attrDisplayName) {
logicalNameDisplayName.push(attrDisplayName)
logicalNameDisplayName.push(attr.LogicalName)
}
})

return logicalNameDisplayName
}

return [];
Expand Down

0 comments on commit 7e0d326

Please sign in to comment.