Skip to content

Commit

Permalink
#889: remove searchForMobileKeyword; reference c__codeKeyword across …
Browse files Browse the repository at this point in the history
…other types
  • Loading branch information
JoernBerkefeld committed May 26, 2023
1 parent 751aee3 commit 84733d5
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 102 deletions.
21 changes: 11 additions & 10 deletions lib/metadataTypes/Journey.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,12 +542,13 @@ class Journey extends MetadataType {
try {
// mobileKeyword
if (activity.configurationArguments?.keywordId) {
activity.configurationArguments.c__mobileKeyword = cache.searchForField(
'mobileKeyword',
activity.configurationArguments.keywordId,
'id',
'keyword'
);
activity.configurationArguments.r__mobileKeyword_codeKeyword =
cache.searchForField(
'mobileKeyword',
activity.configurationArguments.keywordId,
'id',
'c__codeKeyword'
);
delete activity.configurationArguments.keywordId;
}
} catch (ex) {
Expand Down Expand Up @@ -833,14 +834,14 @@ class Journey extends MetadataType {
delete activity.configurationArguments.c__mobileMessage_id;
}
// mobileKeyword
if (activity.configurationArguments?.c__mobileKeyword) {
if (activity.configurationArguments?.r__mobileKeyword_codeKeyword) {
activity.configurationArguments.keywordId = cache.searchForField(
'mobileKeyword',
activity.c__mobileKeyword,
'keyword',
activity.r__mobileKeyword_codeKeyword,
'c__codeKeyword',
'id'
);
delete activity.configurationArguments.c__mobileKeyword;
delete activity.configurationArguments.r__mobileKeyword_codeKeyword;
}
if (activity.configurationArguments?.c__next_mobileKeyword) {
activity.configurationArguments.nextKeywordId = cache.searchForField(
Expand Down
54 changes: 20 additions & 34 deletions lib/metadataTypes/MobileMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,17 @@ class MobileMessage extends MetadataType {

// mobileKeyword
try {
if (metadata.keyword?.keyword) {
cache.searchForMobileKeyword(
metadata.code.code,
metadata.keyword.keyword,
'keyword'
);
}
if (metadata.subscriptionKeyword?.keyword) {
cache.searchForMobileKeyword(
metadata.code.code,
metadata.subscriptionKeyword.keyword,
'keyword'
);
for (const attr of ['keyword', 'subscriptionKeyword', 'nextKeyword']) {
if (metadata[attr]?.id) {
metadata[attr] = {
c__codeKeyword: cache.searchForField(
'mobileKeyword',
metadata[attr].id,
'id',
'c__codeKeyword'
),
};
}
}
} catch (ex) {
Util.logger.warn(
Expand Down Expand Up @@ -263,28 +261,16 @@ class MobileMessage extends MetadataType {
metadata.code = code;

// mobileKeyword
if (metadata.keyword?.keyword) {
const keyword = cache.searchForMobileKeyword(
metadata.code.code,
metadata.keyword.keyword,
'keyword'
);
if (!keyword) {
throw new Error(`mobileKeyword ${metadata.keyword.keyword} not found in cache`);
}
metadata.keyword = keyword;
}
if (metadata.subscriptionKeyword?.keyword) {
const keyword = cache.searchForMobileKeyword(
metadata.code.code,
metadata.subscriptionKeyword.keyword,
'keyword'
);
if (!keyword) {
throw new Error(`mobileKeyword ${metadata.keyword.keyword} not found in cache`);
for (const attr of ['keyword', 'subscriptionKeyword', 'nextKeyword']) {
if (metadata[attr]?.c__codeKeyword) {
const keywordObj = cache.getByKey('mobileKeyword', metadata[attr].c__codeKeyword);
if (!keywordObj) {
throw new Error(
`mobileKeyword ${metadata[attr].c__codeKeyword} not found in cache`
);
}
metadata[attr] = keywordObj;
}

metadata.subscriptionKeyword.keyword = keyword;
}

// campaign
Expand Down
18 changes: 10 additions & 8 deletions lib/metadataTypes/TransactionalSMS.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ class TransactionalSMS extends TransactionalMessage {
// subscriptions: mobileKeyword
if (metadata.subscriptions?.keyword) {
// we merely want to be able to show an error if it does not exist
cache.searchForMobileKeyword(
metadata.subscriptions.shortCode,
metadata.subscriptions.keyword,
'keyword'
cache.searchForField(
'mobileKeyword',
metadata.subscriptions.shortCode + '.' + metadata.subscriptions.keyword,
'c__codeKeyword',
'id'
);
}
return metadata;
Expand Down Expand Up @@ -139,10 +140,11 @@ class TransactionalSMS extends TransactionalMessage {
if (metadata.subscriptions?.keyword) {
try {
// we merely want to be able to show a warning if it does not exist
cache.searchForMobileKeyword(
metadata.subscriptions.shortCode,
metadata.subscriptions.keyword,
'keyword'
cache.searchForField(
'mobileKeyword',
metadata.subscriptions.shortCode + '.' + metadata.subscriptions.keyword,
'keyword',
'id'
);
} catch {
Util.logger.warn(
Expand Down
58 changes: 50 additions & 8 deletions lib/metadataTypes/definitions/MobileMessage.definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,12 @@ module.exports = {
'keyword.keyword': {
isCreateable: true,
isUpdateable: true,
retrieving: false,
template: false,
},
'keyword.c__codeKeyword': {
isCreateable: false,
isUpdateable: false,
retrieving: true,
template: true,
},
Expand Down Expand Up @@ -620,27 +626,63 @@ module.exports = {
'subscriptionKeyword.id': {
isCreatable: true,
isUpdatable: true,
retrieving: true,
template: true,
retrieving: false,
template: false,
},
'subscriptionKeyword.keyword': {
isCreatable: true,
isUpdatable: true,
isCreatable: false,
isUpdatable: false,
retrieving: false,
template: false,
},
'subscriptionKeyword.c__codeKeyword': {
isCreateable: false,
isUpdateable: false,
retrieving: true,
template: true,
},
'subscriptionKeyword.restriction': {
isCreatable: true,
isUpdatable: true,
retrieving: true,
template: true,
isCreatable: false,
isUpdatable: false,
retrieving: false,
template: false,
},
'subscriptionKeyword.isInherited': {
isCreatable: false,
isUpdatable: false,
retrieving: false,
template: false,
},
'nextKeyword.id': {
isCreatable: true,
isUpdatable: true,
retrieving: false,
template: false,
},
'nextKeyword.keyword': {
isCreatable: false,
isUpdatable: false,
retrieving: false,
template: false,
},
'nextKeyword.c__codeKeyword': {
isCreateable: false,
isUpdateable: false,
retrieving: true,
template: true,
},
'nextKeyword.restriction': {
isCreatable: false,
isUpdatable: false,
retrieving: false,
template: false,
},
'nextKeyword.isInherited': {
isCreatable: false,
isUpdatable: false,
retrieving: false,
template: false,
},
subscriberResponseMessage: {
isCreatable: true,
isUpdatable: true,
Expand Down
38 changes: 0 additions & 38 deletions lib/util/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,44 +120,6 @@ module.exports = {
`Dependent ${metadataType} with ${searchField}='${searchValue}' was not found on your BU`
);
},
/**
* standardized method for getting data from cache.
*
* @param {string} code mobileCode
* @param {string} keyword mobileKeyword
* @param {string} returnField field which should be returned
* @returns {string|number|boolean} value of specified field. Error is thrown if not found
*/
searchForMobileKeyword(code, keyword, returnField) {
const metadataType = 'mobileKeyword';
for (const key in dataStore[currentMID]?.[metadataType]) {
if (
Util.resolveObjPath('code.code', dataStore[currentMID][metadataType][key]) ==
code &&
Util.resolveObjPath('keyword', dataStore[currentMID][metadataType][key]) == keyword
) {
try {
if (
Util.resolveObjPath(returnField, dataStore[currentMID][metadataType][key])
) {
return Util.resolveObjPath(
returnField,
dataStore[currentMID][metadataType][key]
);
} else {
throw new Error(); // eslint-disable-line unicorn/error-message
}
} catch {
throw new Error(
`${metadataType} with code ${code} and keyword ${keyword} does not have field '${returnField}'`
);
}
}
}
throw new Error(
`Dependent ${metadataType} with code ${code} and keyword ${keyword} was not found on your BU`
);
},
/**
* standardized method for getting data from cache - adapted for special case of lists
* ! keeping this in util/cache.js rather than in metadataTypes/List.js to avoid potential circular dependencies
Expand Down
2 changes: 1 addition & 1 deletion test/resources/9999999/journey/build-expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@
},
"configurationArguments": {
"c__mobileMessage_id": "NTIzOjc4OjA",
"c__mobileKeyword": "testTemplated_keyword",
"r__mobileKeyword_codeKeyword": "4912312345678.TESTTEMPLATED_KEYWORD",
"isOptIn": false,
"honorBlackoutWindowEnum": 0,
"mobileBlackoutWindowStartTime": "",
Expand Down
2 changes: 1 addition & 1 deletion test/resources/9999999/journey/get-expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@
},
"configurationArguments": {
"c__mobileMessage_id": "NTIzOjc4OjA",
"c__mobileKeyword": "testExisting_keyword",
"r__mobileKeyword_codeKeyword": "4912312345678.TESTEXISTING_KEYWORD",
"isOptIn": false,
"honorBlackoutWindowEnum": 0,
"mobileBlackoutWindowStartTime": "",
Expand Down
2 changes: 1 addition & 1 deletion test/resources/9999999/journey/put-expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@
},
"configurationArguments": {
"c__mobileMessage_id": "NTIzOjc4OjA",
"c__mobileKeyword": "testExisting_keyword",
"r__mobileKeyword_codeKeyword": "4912312345678.TESTEXISTING_KEYWORD",
"isOptIn": false,
"honorBlackoutWindowEnum": 0,
"mobileBlackoutWindowStartTime": "",
Expand Down
2 changes: 1 addition & 1 deletion test/resources/9999999/journey/template-expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@
},
"configurationArguments": {
"c__mobileMessage_id": "NTIzOjc4OjA",
"c__mobileKeyword": "{{{prefix}}}keyword",
"r__mobileKeyword_codeKeyword": "4912312345678.{{{prefixUpper}}}KEYWORD",
"isOptIn": false,
"honorBlackoutWindowEnum": 0,
"mobileBlackoutWindowStartTime": "",
Expand Down

0 comments on commit 84733d5

Please sign in to comment.