Skip to content

Commit

Permalink
#9: improve retrieve filterDefinition
Browse files Browse the repository at this point in the history
  • Loading branch information
JoernBerkefeld committed Aug 29, 2023
1 parent 0e519db commit a742713
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 96 deletions.
24 changes: 7 additions & 17 deletions docs/dist/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -2951,17 +2951,16 @@ FilterDefinition MetadataType
**Extends**: [<code>MetadataType</code>](#MetadataType)

* [FilterDefinition](#FilterDefinition)[<code>MetadataType</code>](#MetadataType)
* [.retrieve(retrieveDir)](#FilterDefinition.retrieve) ⇒ <code>Promise.&lt;{metadata: TYPE.FilterDefinitionMap, type: string}&gt;</code>
* [.retrieve(retrieveDir, [_], [__], [key])](#FilterDefinition.retrieve) ⇒ <code>Promise.&lt;{metadata: TYPE.FilterDefinitionMap, type: string}&gt;</code>
* [.retrieveForCache()](#FilterDefinition.retrieveForCache) ⇒ <code>Promise.&lt;{metadata: TYPE.FilterDefinitionMap, type: string}&gt;</code>
* [.postRetrieveTasks(item)](#FilterDefinition.postRetrieveTasks) ⇒ <code>TYPE.FilterDefinitionItem</code>
* [.parseMetadata(metadata)](#FilterDefinition.parseMetadata) ⇒ <code>TYPE.FilterDefinitionItem</code>
* [.postRetrieveTasks(metadata)](#FilterDefinition.postRetrieveTasks) ⇒ <code>TYPE.FilterDefinitionItem</code>
* [.preDeployTasks(metadata)](#FilterDefinition.preDeployTasks) ⇒ <code>Promise.&lt;TYPE.FilterDefinitionItem&gt;</code>
* [.create(metadata)](#FilterDefinition.create) ⇒ <code>Promise.&lt;TYPE.FilterDefinitionItem&gt;</code>
* [.update(metadata)](#FilterDefinition.update) ⇒ <code>Promise.&lt;TYPE.FilterDefinitionItem&gt;</code>

<a name="FilterDefinition.retrieve"></a>

### FilterDefinition.retrieve(retrieveDir) ⇒ <code>Promise.&lt;{metadata: TYPE.FilterDefinitionMap, type: string}&gt;</code>
### FilterDefinition.retrieve(retrieveDir, [_], [__], [key]) ⇒ <code>Promise.&lt;{metadata: TYPE.FilterDefinitionMap, type: string}&gt;</code>
Retrieves all records and saves it to disk

**Kind**: static method of [<code>FilterDefinition</code>](#FilterDefinition)
Expand All @@ -2970,6 +2969,9 @@ Retrieves all records and saves it to disk
| Param | Type | Description |
| --- | --- | --- |
| retrieveDir | <code>string</code> | Directory where retrieved metadata directory will be saved |
| [_] | <code>void</code> | unused parameter |
| [__] | <code>void</code> | unused parameter |
| [key] | <code>string</code> | customer key of single item to retrieve |

<a name="FilterDefinition.retrieveForCache"></a>

Expand All @@ -2980,19 +2982,7 @@ Retrieves all records for caching
**Returns**: <code>Promise.&lt;{metadata: TYPE.FilterDefinitionMap, type: string}&gt;</code> - Promise of items
<a name="FilterDefinition.postRetrieveTasks"></a>

### FilterDefinition.postRetrieveTasks(item) ⇒ <code>TYPE.FilterDefinitionItem</code>
manages post retrieve steps

**Kind**: static method of [<code>FilterDefinition</code>](#FilterDefinition)
**Returns**: <code>TYPE.FilterDefinitionItem</code> - parsed metadata definition

| Param | Type | Description |
| --- | --- | --- |
| item | <code>TYPE.FilterDefinitionItem</code> | a single record |

<a name="FilterDefinition.parseMetadata"></a>

### FilterDefinition.parseMetadata(metadata) ⇒ <code>TYPE.FilterDefinitionItem</code>
### FilterDefinition.postRetrieveTasks(metadata) ⇒ <code>TYPE.FilterDefinitionItem</code>
parses retrieved Metadata before saving

**Kind**: static method of [<code>FilterDefinition</code>](#FilterDefinition)
Expand Down
140 changes: 84 additions & 56 deletions lib/metadataTypes/FilterDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,49 @@ class FilterDefinition extends MetadataType {
* Retrieves all records and saves it to disk
*
* @param {string} retrieveDir Directory where retrieved metadata directory will be saved
* @param {void} [_] unused parameter
* @param {void} [__] unused parameter
* @param {string} [key] customer key of single item to retrieve
* @returns {Promise.<{metadata: TYPE.FilterDefinitionMap, type: string}>} Promise of items
*/
static async retrieve(retrieveDir) {
static async retrieve(retrieveDir, _, __, key) {
// #1 get the list via SOAP cause the corresponding REST call has no BU filter apparently
// for reference the rest path: '/automation/v1/filterdefinitions?view=categoryinfo'

const soapFields = ['DataFilter', 'ObjectID', 'CustomerKey', 'Description', 'Name'];
const soapFields = ['DataFilter', 'ObjectID', 'CustomerKey', 'Name'];
let requestParams;
if (key) {
requestParams = {
filter: {
leftOperand: 'CustomerKey',
operator: 'equals',
rightOperand: key,
},
};
}

/**
* @type {TYPE.FilterDefinitionSOAPItemMap[]}
*/
const responseSOAP = await this.client.soap.retrieveBulk(this.definition.type, soapFields);
console.log('responseSOAP', responseSOAP); // eslint-disable-line no-console
const responseSOAP = await this.client.soap.retrieveBulk(
this.definition.type,
soapFields,
requestParams
);

// backup REST value of the keyField
const keyFieldBak = this.definition.keyField;
this.definition.keyField = 'CustomerKey';
const responseSOAPMap = this.parseResponseBody(responseSOAP);
const responseSOAPMap = this.parseResponseBody(responseSOAP, key);
// restore the keyField to its REST value
this.definition.keyField = keyFieldBak;
console.log('responseSOAPMap', responseSOAPMap); // eslint-disable-line no-console

/**
* @type {TYPE.FilterDefinitionSOAPItem[]}
*/
const responseSOAPList = responseSOAP.Results.filter((item) => {
if (item.ObjectState) {
Util.logger.debug(`Filtered filterDefinition ${item.name}: ${item.ObjectState}`);
Util.logger.debug(`Filtered filterDefinition ${item.Name}: ${item.ObjectState}`);
return false;
} else {
return true;
Expand All @@ -51,29 +67,28 @@ class FilterDefinition extends MetadataType {

// #2
// /automation/v1/filterdefinitions/<id>
const responseREST = (
await Promise.all(
responseSOAPList.map((item) =>
this.client.rest.get('/email/v1/filters/filterdefinition/' + item.ObjectID)
)
const metadataMap = (
await super.retrieveRESTcollection(
responseSOAPList.map((item) => ({
id: item.ObjectID,
uri: '/email/v1/filters/filterdefinition/' + item.ObjectID,
}))
)
).map((item) => {
).metadata;
for (const item of Object.values(metadataMap)) {
// description is not returned when empty
item.description ||= '';
// add extra info from XML
item.c__soap_DataFilter = responseSOAPMap[item.key].DataFilter;
return item;
});
console.log('responseREST', responseREST); // eslint-disable-line no-console
const results = this.parseResponseBody({ Results: responseREST });
}
if (retrieveDir) {
const savedMetadata = await this.saveResults(results, retrieveDir, null, null);
const savedMetadata = await this.saveResults(metadataMap, retrieveDir);
Util.logger.info(
`Downloaded: ${this.definition.type} (${Object.keys(savedMetadata).length})`
);
}

return { metadata: results, type: this.definition.type };
return { metadata: metadataMap, type: this.definition.type };
}
/**
* Retrieves all records for caching
Expand All @@ -84,50 +99,62 @@ class FilterDefinition extends MetadataType {
return this.retrieve(null);
}

/**
* manages post retrieve steps
*
* @param {TYPE.FilterDefinitionItem} item a single record
* @returns {TYPE.FilterDefinitionItem} parsed metadata definition
*/
static async postRetrieveTasks(item) {
return this.parseMetadata(item);
}
/**
* parses retrieved Metadata before saving
*
* @param {TYPE.FilterDefinitionItem} metadata a single record
* @returns {TYPE.FilterDefinitionItem} parsed metadata definition
*/
static async parseMetadata(metadata) {
static async postRetrieveTasks(metadata) {
if (metadata.derivedFromType > 4) {
// GUI only shows types 1,2,3,4; lets mimic that here.
// type 6 seems to be journey related. Maybe we need to change that again in the future
return;
}
try {
// folder
metadata.r__folder_Path = cache.searchForField(
'folder',
metadata.categoryId,
'ID',
'Path'
);
delete metadata.categoryId;

if (metadata.derivedFromType === 2) {
// DataExtension
metadata.r__dataExtension_CustomerKey = cache.searchForField(
'dataExtension',
metadata.derivedFromObjectId,
'ObjectID',
'CustomerKey'
);
this.setFolderPath(metadata);

switch (metadata.derivedFromType) {
case 1: {
// SubscriberAttributes
// TODO
break;
}
case 2: {
// DataExtension
metadata.r__dataExtension_CustomerKey = cache.searchForField(
'dataExtension',
metadata.derivedFromObjectId,
'ObjectID',
'CustomerKey'
);
delete metadata.derivedFromObjectId;
delete metadata.derivedFromType;
break;
}
case 3: {
// TODO
break;
}
case 4: {
// TODO
break;
}
case 5: {
// TODO
break;
}
case 6: {
// TODO
break;
}
}
metadata.del__derivedFromObjectId = metadata.derivedFromObjectId; // TEMP for DEBUGGING / remove before release
metadata.del__derivedFromType = metadata.derivedFromType; // TEMP for DEBUGGING / remove before release
delete metadata.derivedFromObjectId;
delete metadata.derivedFromType;

const xmlToJson = new XMLParser({ ignoreAttributes: false });
metadata.c__filterDefinition = xmlToJson.parse(metadata.filterDefinitionXml);

// TODO check if Condition ID needs to be resolved or can be ignored
// TODO map Condition ID to DataExtensionField ID
delete metadata.filterDefinitionXml;
} catch (ex) {
Util.logger.error(
`FilterDefinition '${metadata.name}' (${metadata.key}): ${ex.message}`
Expand All @@ -143,11 +170,10 @@ class FilterDefinition extends MetadataType {
*/
static async preDeployTasks(metadata) {
// folder
metadata.categoryId = cache.searchForField('folder', metadata.r__folder_Path, 'Path', 'ID');
delete metadata.r__folder_Path;
super.setFolderId(metadata);

if (metadata.derivedFromObjectTypeName === 'SubscriberAttributes') {
// List
// SubscriberAttributes
metadata.derivedFromType = 1;
metadata.derivedFromObjectId = '00000000-0000-0000-0000-000000000000';
} else {
Expand Down Expand Up @@ -190,8 +216,10 @@ class FilterDefinition extends MetadataType {
*/
static update(metadata) {
// TODO test the update
// TODO figure out how to get the ID on the fly
return super.updateREST(metadata, '/email/v1/filters/filterdefinition/' + metadata.Id);
return super.updateREST(
metadata,
'/email/v1/filters/filterdefinition/' + metadata[this.definition.idField]
);
}
}
// Assign definition to static attributes
Expand Down
Loading

0 comments on commit a742713

Please sign in to comment.