Skip to content

Commit

Permalink
#58: normalize cross-type references with r__ for transactionalSMS
Browse files Browse the repository at this point in the history
  • Loading branch information
JoernBerkefeld committed May 21, 2024
1 parent e5ee9c3 commit 4661aa1
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 43 deletions.
79 changes: 50 additions & 29 deletions lib/metadataTypes/TransactionalSMS.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,34 @@ class TransactionalSMS extends TransactionalMessage {
);
}

// subscriptions: mobileCode
if (metadata.subscriptions?.shortCode) {
// subscriptions: mobileKeyword
if (metadata.subscriptions?.r__mobileKeyword_c__codeKeyword) {
// we merely want to be able to show an error if it does not exist
cache.searchForField(
'mobileKeyword',
metadata.subscriptions.r__mobileKeyword_c__codeKeyword,
'c__codeKeyword',
'c__codeKeyword'
);
const codeKeywordArr =
metadata.subscriptions.r__mobileKeyword_c__codeKeyword.split('.');
metadata.subscriptions.shortCode = codeKeywordArr[0];
metadata.subscriptions.keyword = codeKeywordArr[1];
// make sure the code actually exists
cache.searchForField('mobileCode', metadata.subscriptions.shortCode, 'code', 'code');
// subscriptions: mobileKeyword
if (metadata.subscriptions?.keyword) {
// we merely want to be able to show an error if it does not exist
cache.searchForField(
'mobileKeyword',
metadata.subscriptions.shortCode + '.' + metadata.subscriptions.keyword,
'c__codeKeyword',
'id'
);
}
} else if (metadata.subscriptions?.r__mobileCode_code) {
// subscriptions: mobileCode
// in case we dont have r__mobileKeyword_c__codeKeyword set
metadata.subscriptions.shortCode = cache.searchForField(
'mobileCode',
metadata.subscriptions.r__mobileCode_code,
'code',
'code'
);
}
delete metadata.subscriptions.r__mobileKeyword_c__codeKeyword;
delete metadata.subscriptions.r__mobileCode_code;

return metadata;
}
/**
Expand Down Expand Up @@ -139,30 +152,38 @@ class TransactionalSMS extends TransactionalMessage {
'code',
'code'
);
metadata.subscriptions.r__mobileCode_code = metadata.subscriptions.shortCode;
delete metadata.subscriptions.shortCode;
} catch {
Util.logger.warn(
` - ${this.definition.type} ${metadata[this.definition.nameField]} (${
metadata[this.definition.keyField]
}): Could not find mobileCode ${metadata.subscriptions.shortCode}.`
);
}
}
// subscriptions: mobileKeyword
if (metadata.subscriptions?.keyword) {
try {
// we merely want to be able to show a warning if it does not exist
cache.searchForField(
'mobileKeyword',
metadata.subscriptions.shortCode + '.' + metadata.subscriptions.keyword,
'c__codeKeyword',
'id'
);
} catch {
Util.logger.warn(
` - ${this.definition.type} ${metadata[this.definition.nameField]} (${
metadata[this.definition.keyField]
}): Could not find mobileKeyword ${metadata.subscriptions.keyword}.`
);
// subscriptions: mobileKeyword
if (metadata.subscriptions?.keyword) {
try {
// we merely want to be able to show a warning if it does not exist
metadata.subscriptions.r__mobileKeyword_c__codeKeyword = cache.searchForField(
'mobileKeyword',
(metadata.subscriptions.shortCode ||
metadata.subscriptions.r__mobileCode_code) +
'.' +
metadata.subscriptions.keyword,
'c__codeKeyword',
'c__codeKeyword'
);
delete metadata.subscriptions.r__mobileCode_code;
delete metadata.subscriptions.shortCode;
delete metadata.subscriptions.keyword;
} catch {
Util.logger.warn(
` - ${this.definition.type} ${metadata[this.definition.nameField]} (${
metadata[this.definition.keyField]
}): Could not find mobileKeyword ${metadata.subscriptions.keyword}.`
);
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions lib/metadataTypes/definitions/TransactionalSMS.definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,11 @@ export default {
retrieving: true,
template: true,
},
'subscriptions.r__mobileKeyword_c__codeKeyword': {
isCreateable: false,
isUpdateable: false,
retrieving: true,
template: true,
},
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
"createdDate": "2022-11-07T02:24:00",
"modifiedDate": "2022-11-07T02:25:00",
"subscriptions": {
"shortCode": "4912312345678",
"countryCode": "",
"autoAddSubscriber": true,
"updateSubscriber": true,
"keyword": "TESTEXISTING_KEYWORD"
"r__mobileKeyword_c__codeKeyword": "4912312345678.TESTEXISTING_KEYWORD"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
"createdDate": "2022-11-07T02:24:00",
"modifiedDate": "2022-11-07T02:25:00",
"subscriptions": {
"shortCode": "4912312345678",
"countryCode": "",
"autoAddSubscriber": true,
"updateSubscriber": true,
"keyword": "TESTEXISTING_KEYWORD"
"r__mobileKeyword_c__codeKeyword": "4912312345678.TESTEXISTING_KEYWORD"
}
}
3 changes: 1 addition & 2 deletions test/resources/9999999/transactionalSMS/build-expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
"description": "foobar",
"status": "Active",
"subscriptions": {
"shortCode": "4912312345678",
"countryCode": "",
"autoAddSubscriber": true,
"updateSubscriber": true,
"keyword": "TESTTEMPLATED_KEYWORD"
"r__mobileKeyword_c__codeKeyword": "4912312345678.TESTTEMPLATED_KEYWORD"
}
}
3 changes: 1 addition & 2 deletions test/resources/9999999/transactionalSMS/get-expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
"createdDate": "2022-11-07T02:24:00",
"modifiedDate": "2022-11-07T02:25:00",
"subscriptions": {
"shortCode": "4912312345678",
"countryCode": "",
"autoAddSubscriber": true,
"updateSubscriber": true,
"keyword": "TESTEXISTING_KEYWORD"
"r__mobileKeyword_c__codeKeyword": "4912312345678.TESTEXISTING_KEYWORD"
}
}
3 changes: 1 addition & 2 deletions test/resources/9999999/transactionalSMS/patch-expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
"createdDate": "2022-11-07T02:24:00",
"modifiedDate": "2022-11-07T02:25:00",
"subscriptions": {
"shortCode": "4912312345678",
"countryCode": "",
"autoAddSubscriber": true,
"updateSubscriber": true,
"keyword": "TESTEXISTING_KEYWORD"
"r__mobileKeyword_c__codeKeyword": "4912312345678.TESTEXISTING_KEYWORD"
}
}
3 changes: 1 addition & 2 deletions test/resources/9999999/transactionalSMS/post-expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
"createdDate": "2022-11-07T02:24:00",
"modifiedDate": "2022-11-07T02:25:00",
"subscriptions": {
"shortCode": "4912312345678",
"countryCode": "",
"autoAddSubscriber": true,
"updateSubscriber": true,
"keyword": "TESTEXISTING_KEYWORD"
"r__mobileKeyword_c__codeKeyword": "4912312345678.TESTEXISTING_KEYWORD"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
"description": "{{{description}}}",
"status": "Active",
"subscriptions": {
"shortCode": "4912312345678",
"countryCode": "",
"autoAddSubscriber": true,
"updateSubscriber": true,
"keyword": "{{{prefixUpper}}}KEYWORD"
"r__mobileKeyword_c__codeKeyword": "4912312345678.{{{prefixUpper}}}KEYWORD"
}
}

0 comments on commit 4661aa1

Please sign in to comment.