|
| 1 | +import { dynamicsRequest } from "./DynamicsRequest"; |
| 2 | +import { dynamicsBatch } from "./DynamicsBatch"; |
| 3 | +import { WebApiVersion } from "./Dynamics"; |
| 4 | +export default function dynamicsMetadata(accessToken) { |
| 5 | + return new DynamicsMetadataClient(accessToken); |
| 6 | +} |
| 7 | +const ExcludedAttributeFilters = { |
| 8 | + 'Uniqueidentifier': 'AttributeType', |
| 9 | + 'CalendarRules': 'AttributeType', |
| 10 | + 'EntityName': 'AttributeType', |
| 11 | + 'ManagedProperty': 'AttributeType', |
| 12 | + 'Owner': 'AttributeType', |
| 13 | + 'Virtual': 'AttributeType', |
| 14 | + 'Lookup': 'AttributeType', |
| 15 | + 'Picklist': 'AttributeType', |
| 16 | + 'Status': 'AttributeType', |
| 17 | + 'State': 'AttributeType', |
| 18 | + 'yomi': "contains(LogicalName,'yomi')", |
| 19 | + 'base': "endswith(LogicalName,'base')" |
| 20 | +}; |
| 21 | +class DynamicsMetadataClient { |
| 22 | + constructor(accessToken) { |
| 23 | + this.dynamicsHeaders = accessToken && { |
| 24 | + 'Authorization': 'Bearer ' + accessToken |
| 25 | + }; |
| 26 | + } |
| 27 | + attributes(entityName) { |
| 28 | + const properties = ["AttributeType", "DisplayName", "LogicalName", "SchemaName", "IsCustomAttribute"]; |
| 29 | + const filter = Object.keys(ExcludedAttributeFilters).map(v => (ExcludedAttributeFilters[v] == 'AttributeType' && `AttributeType ne Microsoft.Dynamics.CRM.AttributeTypeCode'${v}'`) || v).join(' and '); |
| 30 | + return dynamicsBatch(this.dynamicsHeaders) |
| 31 | + .requestAllUrls([ |
| 32 | + `/api/data/${WebApiVersion}/EntityDefinitions(LogicalName='${entityName}')/Attributes?$select=${properties}&$filter=${filter}`, |
| 33 | + `/api/data/${WebApiVersion}/EntityDefinitions(LogicalName='${entityName}')/Attributes/Microsoft.Dynamics.CRM.LookupAttributeMetadata?$select=${properties},Targets`, |
| 34 | + `/api/data/${WebApiVersion}/EntityDefinitions(LogicalName='${entityName}')/Attributes/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=${properties}&$expand=OptionSet($select=Options)`, |
| 35 | + `/api/data/${WebApiVersion}/EntityDefinitions(LogicalName='${entityName}')/Attributes/Microsoft.Dynamics.CRM.StatusAttributeMetadata?$select=${properties}&$expand=OptionSet($select=Options)`, |
| 36 | + `/api/data/${WebApiVersion}/EntityDefinitions(LogicalName='${entityName}')/Attributes/Microsoft.Dynamics.CRM.StateAttributeMetadata?$select=${properties}&$expand=OptionSet($select=Options)` |
| 37 | + ]) |
| 38 | + .execute() |
| 39 | + .then(data => this.flatten(data) |
| 40 | + .map((attribute) => ({ |
| 41 | + LogicalName: attribute.LogicalName, |
| 42 | + DisplayName: (attribute.DisplayName && attribute.DisplayName.UserLocalizedLabel && attribute.DisplayName.UserLocalizedLabel.Label) || attribute.LogicalName, |
| 43 | + Type: attribute.AttributeType, |
| 44 | + LookupEntityName: attribute.Targets && attribute.Targets[0], |
| 45 | + PicklistOptions: attribute.OptionSet && attribute.OptionSet.Options.map((opt) => ({ |
| 46 | + Label: (opt.Label && opt.Label.UserLocalizedLabel && opt.Label.UserLocalizedLabel.Label), |
| 47 | + Value: opt.Value |
| 48 | + })) |
| 49 | + }))); |
| 50 | + } |
| 51 | + entities() { |
| 52 | + return dynamicsRequest(`/api/data/${WebApiVersion}/EntityDefinitions?$select=EntitySetName,Description,DisplayName,LogicalName,PrimaryIdAttribute,PrimaryNameAttribute,IconSmallName,IsActivity,IsCustomEntity`, this.dynamicsHeaders) |
| 53 | + .then(data => data |
| 54 | + .map(entity => ({ |
| 55 | + LogicalName: entity.LogicalName, |
| 56 | + EntitySetName: entity.EntitySetName, |
| 57 | + DisplayName: (entity.DisplayName && entity.DisplayName.UserLocalizedLabel && entity.DisplayName.UserLocalizedLabel.Label) || entity.LogicalName, |
| 58 | + Description: (entity.Description && entity.Description.UserLocalizedLabel && entity.Description.UserLocalizedLabel.Label) || '' |
| 59 | + }))); |
| 60 | + } |
| 61 | + entity(entityName) { |
| 62 | + return dynamicsRequest(`/api/data/${WebApiVersion}/EntityDefinitions(LogicalName='${entityName}')?$select=EntitySetName,Description,DisplayName,LogicalName,PrimaryIdAttribute,PrimaryNameAttribute,IconSmallName,IsActivity,IsCustomEntity`, this.dynamicsHeaders) |
| 63 | + .then(entity => this.attributes(entityName) |
| 64 | + .then(attributes => ({ |
| 65 | + LogicalName: entity.LogicalName, |
| 66 | + EntitySetName: entity.EntitySetName, |
| 67 | + DisplayName: (entity.DisplayName && entity.DisplayName.UserLocalizedLabel && entity.DisplayName.UserLocalizedLabel.Label) || entity.LogicalName, |
| 68 | + Description: (entity.Description && entity.Description.UserLocalizedLabel && entity.Description.UserLocalizedLabel.Label) || '', |
| 69 | + Attributes: attributes |
| 70 | + }))); |
| 71 | + } |
| 72 | + flatten(values) { |
| 73 | + return [].concat(...values); |
| 74 | + } |
| 75 | +} |
0 commit comments