Skip to content

Commit

Permalink
Merge pull request #1970 from Accenture/task/1923-support-transaction…
Browse files Browse the repository at this point in the history
…al-send-journeys-for-deploy-refresh-and-auto-refresh-for-publish

Task/1923 support transactional send journeys for deploy refresh and auto refresh for publish
  • Loading branch information
JoernBerkefeld authored Jan 9, 2025
2 parents 737ae48 + 9cedea6 commit 65c9aa7
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 28 deletions.
3 changes: 1 addition & 2 deletions @types/lib/metadataTypes/Journey.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion @types/lib/metadataTypes/Journey.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 51 additions & 20 deletions lib/metadataTypes/Journey.js
Original file line number Diff line number Diff line change
Expand Up @@ -1834,21 +1834,41 @@ class Journey extends MetadataType {
* @param {MetadataTypeMap} upsertResults metadata mapped by their keyField as returned by update/create
*/
static async postDeployTasks(upsertResults) {
let postDeployFlags = 0;
if (Util.OPTIONS.publish) {
postDeployFlags++;
}
if (Util.OPTIONS.validate) {
postDeployFlags++;
}
if (Util.OPTIONS.refresh) {
postDeployFlags++;
}
if (postDeployFlags > 1) {
Util.logger.warn(
`Please provide only one of the following options (--publish, --validate, --refresh). Flags are processed in this order and only the first one found is executed.`
);
}

if (Util.OPTIONS.publish) {
Util.logger.info(`Publishing: ${this.definition.type}`);
// pubslih
const idArr = Object.values(upsertResults).map(
(item) => 'id:' + item.id + '/' + item.version
);
await this.publish(idArr);
}
if (Util.OPTIONS.validate) {
} else if (Util.OPTIONS.validate) {
Util.logger.info(`Validating: ${this.definition.type}`);
// pubslih
const idArr = Object.values(upsertResults).map(
(item) => 'id:' + item.id + '/' + item.version
);
await this.validate(idArr);
} else if (Util.OPTIONS.refresh) {
Util.logger.info(`Refreshing: ${this.definition.type}`);
// refresh
const keyArr = Object.values(upsertResults).map((item) => item.key);
await this.refresh(keyArr);
}
}

Expand All @@ -1863,6 +1883,7 @@ class Journey extends MetadataType {
// works only with objectId
const statusUrls = [];
const executedKeyArr = [];
const refreshTransactionalKeys = [];
const metadataMap = await this.retrieveForCache();
const spinnerTransactional = yoctoSpinner({
text: `Publishing transactional journey…`,
Expand Down Expand Up @@ -1927,11 +1948,20 @@ class Journey extends MetadataType {
}
if (journey.status === 'Published') {
// api would return error code 30000 and ask to open a support case when in fact we simply already have a transactionalEmail created based on this status
Util.logger.error(
` ☇ skipping ${this.definition.type} ${
journey[this.definition.nameField]
} (${journey[this.definition.keyField]}): already published`
);
if (journey.definitionType === 'Transactional') {
Util.logger.info(
` ☇ skipping ${this.definition.type} ${
journey[this.definition.nameField]
} (${journey[this.definition.keyField]}): already published. Queueing for refresh.`
);
refreshTransactionalKeys.push(journey.key);
} else {
Util.logger.warn(
` ☇ skipping ${this.definition.type} ${
journey[this.definition.nameField]
} (${journey[this.definition.keyField]}): already published.`
);
}
continue;
}

Expand Down Expand Up @@ -2121,6 +2151,11 @@ class Journey extends MetadataType {
Util.logger.info(
`Published ${executedKeyArr.filter(Boolean).length} of ${keyArr.length} items`
);

if (refreshTransactionalKeys.length) {
// in case we tried to publish a transactional journey that was already published we will instead run a refresh for those
executedKeyArr.push(...(await this.refresh(refreshTransactionalKeys)));
}
return executedKeyArr.filter(Boolean);
}

Expand Down Expand Up @@ -2675,21 +2710,17 @@ class Journey extends MetadataType {
* TSD-specific refresh method that finds active TSDs and refreshes them
*
* @param {string[]} keyArr metadata keys
* @param {boolean} [checkKey] whether to check if the key is valid
* @returns {Promise.<string[]>} Returns list of keys that were refreshed
*/
static async refresh(keyArr, checkKey = true) {
static async refresh(keyArr) {
console.time('Time'); // eslint-disable-line no-console
if (!keyArr) {
if (!Array.isArray(keyArr) || !keyArr.length) {
Util.logger.error('No refresh-keys provided');
return [];
// keyArr = await this.getKeysForValidTSDs((await this.findRefreshableItems()).metadata);
// checkKey = false;
}
let journeyCache;
if (checkKey) {
journeyCache = await this.retrieveForCache();
}
const journeyCache = await this.retrieveForCache();
// then executes pause, publish, start on them.
Util.logger.info(`Refreshing ${keyArr.length} ${this.definition.typeName}...`);
Util.logger.debug(`Refreshing keys: ${keyArr.join(', ')}`);
Expand All @@ -2699,23 +2730,23 @@ class Journey extends MetadataType {
await Promise.all(
keyArr.map((key) =>
rateLimit(async () => {
if (checkKey && !journeyCache.metadata[key]) {
if (!journeyCache.metadata[key]) {
Util.logger.error(
` ☇ skipping refresh of ${this.definition.type} ${key}: not found on server`
);
return;
}
switch (journeyCache.metadata[key].definitionType) {
case 'Transactional': {
if (checkKey && journeyCache.metadata[key]?.status !== 'Published') {
Util.logger.error(
` ☇ skipping refresh of ${this.definition.type} ${key}: Can only refresh journeys with status 'Published'. Found status: ${journeyCache.metadata[key]?.status}`
);
} else {
if (journeyCache.metadata[key]?.status === 'Published') {
const result = await this._refreshItem(key, journeyCache);
if (result) {
refreshedKeyArr.push(key);
}
} else {
Util.logger.error(
` ☇ skipping refresh of ${this.definition.type} ${key}: Can only refresh journeys with status 'Published'. Found status: ${journeyCache.metadata[key]?.status}`
);
}
break;
}
Expand Down
34 changes: 29 additions & 5 deletions test/type.journey.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ describe('type: journey', () => {
});

describe('Publish ================', () => {
it(`Should not publish a transactional journey by key that is already published`, async () => {
it(`Should not publish a transactional journey by key that is already published but instead trigger a refresh`, async () => {
handler.setOptions({ skipStatusCheck: true });
// WHEN
const publish = await handler.publish(
Expand All @@ -533,17 +533,41 @@ describe('type: journey', () => {
['testExisting_temail']
);
// THEN
assert.equal(process.exitCode, 1, 'publish should have thrown an error');
assert.equal(process.exitCode, 0, 'publish should not have thrown an error');

assert.deepEqual(
publish['testInstance/testBU']?.journey,
[],
'should have not have published any journey'
['testExisting_temail'],
'should not have published any journey but instead triggered a refresh'
);

// get callouts
const pauseCallout = testUtils.getRestCallout(
'post',
'/interaction/v1/interactions/transactional/pause'
);
const resumeCallout = testUtils.getRestCallout(
'post',
'/interaction/v1/interactions/transactional/resume'
);
const pauseResumeResponse = {
definitionId: 'dsfdsafdsa-922c-4568-85a5-e5cc77efc3be',
};
// confirm responses
assert.deepEqual(
pauseCallout,
pauseResumeResponse,
'pause-payload JSON was not equal expected'
);
assert.deepEqual(
resumeCallout,
pauseResumeResponse,
'resume-payload JSON was not equal expected'
);

assert.equal(
testUtils.getAPIHistoryLength(),
1,
4,
'Unexpected number of requests made. Run testUtils.logAPIHistoryDebug() to see the requests'
);
return;
Expand Down

0 comments on commit 65c9aa7

Please sign in to comment.