Skip to content

Commit

Permalink
#1943: reretrieve journey and transactionalEmail after refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
JoernBerkefeld committed Jan 13, 2025
1 parent 47bfc31 commit 82baad1
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 67 deletions.
8 changes: 8 additions & 0 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.

157 changes: 91 additions & 66 deletions lib/metadataTypes/Journey.js
Original file line number Diff line number Diff line change
Expand Up @@ -2128,72 +2128,12 @@ class Journey extends MetadataType {
}
}

// reload published non-transactional journeys including their events
if (executedKeyArr.filter(Boolean).length) {
Util.logger.info('Re-retrieving published journeys');
const retriever = new Retriever(this.properties, this.buObject);
try {
// we need to retrieve the updated journeys and all dependencies
const updatedJourneyRetrieve = await retriever.retrieve(
['journey'],
executedKeyArr.filter(Boolean)
);

/** @type {MetadataTypeItem[]} */
const updatedJourneys =
updatedJourneyRetrieve?.journey?.length > 1
? Object.values(
updatedJourneyRetrieve?.journey.reduce(
(previousValue, currentValue) =>
Object.assign(previousValue, currentValue),
{}
)
)
: Object.values(updatedJourneyRetrieve?.journey[0]);

if (updatedJourneys) {
// regardless of upsert vs publish-only mode, we need to retrieve the events/transactionalEmail and their dependencies
const updatedEvents = [];
const updatedTransactionalEmails = [];
for (const journey of updatedJourneys) {
// multi-step journeys
updatedEvents.push(journey.triggers?.[0]?.metaData?.r__event_key);
// transactional-send journeys
updatedTransactionalEmails.push(
journey.activities?.[0]?.configurationArguments
?.r__transactionalEmail_key
);
}
/** @type {TypeKeyCombo} */
const eventTransEmailCombo = {};
if (updatedEvents.filter(Boolean).length) {
eventTransEmailCombo.event = updatedEvents.filter(Boolean);
} else if (publishedJourneyCounter.multiStep) {
Util.logger.error(`Could not find events for the published journeys`);
}
if (updatedTransactionalEmails.filter(Boolean).length) {
eventTransEmailCombo.transactionalEmail =
updatedTransactionalEmails.filter(Boolean);
Util.logger.info('Retrieving relevant transactionalEmails');
} else if (publishedJourneyCounter.transactional) {
Util.logger.error(
`Could not find transactional Emails for the published journeys`
);
}

const toBeRetrievedTypes = Object.keys(eventTransEmailCombo);
if (toBeRetrievedTypes.length) {
Util.logger.info(
'Retrieving relevant ' +
toBeRetrievedTypes.map((item) => item + 's').join(', ')
);
await retriever.retrieve(toBeRetrievedTypes, eventTransEmailCombo);
}
}
} catch (ex) {
Util.logger.errorStack(ex, 'retrieve failed');
}
}
// reload published journeys including their events/transactionalEmails
await this._reRetrieve(
executedKeyArr,
publishedJourneyCounter.transactional,
publishedJourneyCounter.multiStep
);

Util.logger.info(
`Published ${executedKeyArr.filter(Boolean).length} of ${keyArr.length} items`
Expand All @@ -2206,6 +2146,81 @@ class Journey extends MetadataType {
return executedKeyArr.filter(Boolean);
}

/**
*
* @param {string[]} executedKeyArr list of journey keys
* @param {number} transactionalCounter how many transactiona-send journeys did we expect to refresh
* @param {number} multiStepCounter how many multi-step journeys did we expect to refresh
* @returns {Promise.<void>} -
*/
static async _reRetrieve(executedKeyArr, transactionalCounter, multiStepCounter) {
if (!executedKeyArr.filter(Boolean).length) {
return;
}
Util.logger.info('Re-retrieving published journeys');
const retriever = new Retriever(this.properties, this.buObject);
try {
// we need to retrieve the updated journeys and all dependencies
const updatedJourneyRetrieve = await retriever.retrieve(
['journey'],
executedKeyArr.filter(Boolean)
);

/** @type {MetadataTypeItem[]} */
const updatedJourneys =
updatedJourneyRetrieve?.journey?.length > 1
? Object.values(
updatedJourneyRetrieve?.journey.reduce(
(previousValue, currentValue) =>
Object.assign(previousValue, currentValue),
{}
)
)
: Object.values(updatedJourneyRetrieve?.journey[0]);

if (updatedJourneys) {
// regardless of upsert vs publish-only mode, we need to retrieve the events/transactionalEmail and their dependencies
const updatedEvents = [];
const updatedTransactionalEmails = [];
for (const journey of updatedJourneys) {
// multi-step journeys
updatedEvents.push(journey.triggers?.[0]?.metaData?.r__event_key);
// transactional-send journeys
updatedTransactionalEmails.push(
journey.activities?.[0]?.configurationArguments?.r__transactionalEmail_key
);
}
/** @type {TypeKeyCombo} */
const eventTransEmailCombo = {};
if (updatedEvents.filter(Boolean).length) {
eventTransEmailCombo.event = updatedEvents.filter(Boolean);
} else if (multiStepCounter) {
Util.logger.error(`Could not find events for the published journeys`);
}
if (updatedTransactionalEmails.filter(Boolean).length) {
eventTransEmailCombo.transactionalEmail =
updatedTransactionalEmails.filter(Boolean);
Util.logger.info('Retrieving relevant transactionalEmails');
} else if (transactionalCounter) {
Util.logger.error(
`Could not find transactional Emails for the published journeys`
);
}

const toBeRetrievedTypes = Object.keys(eventTransEmailCombo);
if (toBeRetrievedTypes.length) {
Util.logger.info(
'Retrieving relevant ' +
toBeRetrievedTypes.map((item) => item + 's').join(', ')
);
await retriever.retrieve(toBeRetrievedTypes, eventTransEmailCombo);
}
}
} catch (ex) {
Util.logger.errorStack(ex, 'retrieve failed');
}
}

/**
* helper for {@link Journey.publish} and {@link Journey.validate}
*
Expand Down Expand Up @@ -2774,6 +2789,7 @@ class Journey extends MetadataType {
const refreshedKeyArr = [];
const tsKeys = [];
const rateLimit = pLimit(10);
const transactionalJourneyKeys = [];
await Promise.all(
keyArr.map((key) =>
rateLimit(async () => {
Expand All @@ -2789,6 +2805,7 @@ class Journey extends MetadataType {
const result = await this._refreshItem(key, journeyCache);
if (result) {
refreshedKeyArr.push(key);
transactionalJourneyKeys.push(key);
}
} else {
Util.logger.error(
Expand Down Expand Up @@ -2882,6 +2899,14 @@ class Journey extends MetadataType {
Util.logger.info(Util.getGrayMsg('No triggeredSends found to refresh'));
}

// reload refreshed transactional journeys including their transactionalEmails
if (transactionalJourneyKeys.length) {
// reset transactionalEmail cache to trigger re-caching it.
cache.clearCache(this.buObject.mid, 'transactionalEmail');

await this._reRetrieve(transactionalJourneyKeys, transactionalJourneyKeys.length, 0);
}

Util.logger.info(
`Refreshed ${refreshedKeyArr.length} of ${keyArr.length} ${this.definition.type}`
);
Expand Down

0 comments on commit 82baad1

Please sign in to comment.