-
Notifications
You must be signed in to change notification settings - Fork 36
/
TransactionalMessage.js
121 lines (114 loc) · 4.23 KB
/
TransactionalMessage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
'use strict';
import TYPE from '../../types/mcdev.d.js';
import MetadataType from './MetadataType.js';
import { Util } from '../util/util.js';
/**
* TransactionalMessage MetadataType
*
* @augments MetadataType
*/
class TransactionalMessage extends MetadataType {
// define this.subType as string here for intellisense; requires to be redefined in child class
static subType;
/**
* Retrieves Metadata
*
* @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.<TYPE.MetadataTypeMapObj>} Promise of metadata
*/
static async retrieve(retrieveDir, _, __, key) {
let keyList;
const baseUri = '/messaging/v1/' + this.subType + '/definitions/';
if (key) {
// Retrieve single
keyList = [key];
} else {
// Retrieve all
// * keep deleted items for caching (and to decide on update vs create)
const parsed = (
await this.retrieveREST(
null,
baseUri + (retrieveDir ? '?$filter=status%20neq%20deleted' : '')
)
).metadata;
keyList = Object.keys(parsed);
}
// get all transactionalX items with additional details not given by the list endpoint
const details = (
await Promise.all(
keyList.map(async (key) => {
try {
return await this.client.rest.get(baseUri + (key || ''));
} catch {
return null;
}
})
)
).filter(Boolean);
const parsed = this.parseResponseBody({ definitions: details });
let savedMetadata;
if (retrieveDir) {
// * retrieveDir is mandatory in this method as it is not used for caching (there is a seperate method for that)
savedMetadata = await this.saveResults(parsed, retrieveDir, null, null);
Util.logger.info(
`Downloaded: ${this.definition.type} (${Object.keys(savedMetadata).length})` +
Util.getKeysString(key)
);
}
return { metadata: savedMetadata || parsed, type: this.definition.type };
}
/**
* Retrieves event definition metadata for caching
*
* @param {string} [key] customer key of single item to cache
* @returns {Promise.<TYPE.MetadataTypeMapObj>} Promise of metadata
*/
static retrieveForCache(key) {
// the call to /messaging/v1/email/definitions/ does not return definitionId
// definitionId is required for resolving dependencies on interactions.
// we should therefore use the already defined retrieve method
return this.retrieve(undefined, undefined, undefined, key);
}
/**
* Updates a single item
*
* @param {TYPE.MetadataTypeItem} metadata a single item
* @returns {Promise} Promise
*/
static update(metadata) {
return super.updateREST(
metadata,
'/messaging/v1/' + this.subType + '/definitions/' + metadata[this.definition.keyField]
);
}
/**
* Creates a single item
*
* @param {TYPE.MetadataTypeItem} metadata a single item
* @returns {Promise} Promise
*/
static create(metadata) {
return super.createREST(metadata, '/messaging/v1/' + this.subType + '/definitions');
}
/**
* Delete a metadata item from the specified business unit
*
* @param {string} key Identifier of item
* @returns {Promise.<boolean>} deletion success status
*/
static deleteByKey(key) {
return super.deleteByKeyREST(
'/messaging/v1/' + this.subType + '/definitions/' + key,
key,
false
);
}
}
// Assign definition to static attributes
// ! using SMS definitions here as placeholder to have auto completion
import MetadataTypeDefinitions from '../MetadataTypeDefinitions.js';
TransactionalMessage.definition = MetadataTypeDefinitions.transactionalSMS;
export default TransactionalMessage;