-
Notifications
You must be signed in to change notification settings - Fork 257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DC-792] injects matchingKey in the perform block #2119
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -197,6 +197,13 @@ const isSyncMode = (value: unknown): value is SyncMode => { | |
return syncModeTypes.find((validValue) => value === validValue) !== undefined | ||
} | ||
|
||
const INTERNAL_HIDDEN_FIELDS = ['__segment_internal_sync_mode', '__segment_internal_matching_key'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! more tidy than before 👏 |
||
const removeInternalHiddenFields = (mapping: JSONObject): JSONObject => { | ||
return Object.keys(mapping).reduce((acc, key) => { | ||
return INTERNAL_HIDDEN_FIELDS.includes(key) ? acc : { ...acc, [key]: mapping[key] } | ||
}, {}) | ||
} | ||
|
||
/** | ||
* Action is the beginning step for all partner actions. Entrypoints always start with the | ||
* MapAndValidateInput step. | ||
|
@@ -265,18 +272,16 @@ export class Action<Settings, Payload extends JSONLikeObject, AudienceSettings = | |
// TODO cleanup results... not sure it's even used | ||
const results: Result[] = [] | ||
|
||
// Remove internal hidden fields | ||
const mapping: JSONObject = removeInternalHiddenFields(bundle.mapping) | ||
|
||
// Resolve/transform the mapping with the input data | ||
let payload = transform(bundle.mapping, bundle.data) as Payload | ||
let payload = transform(mapping, bundle.data) as Payload | ||
results.push({ output: 'Mappings resolved' }) | ||
|
||
// Remove empty values (`null`, `undefined`, `''`) when not explicitly accepted | ||
payload = removeEmptyValues(payload, this.schema, true) as Payload | ||
|
||
// Remove internal hidden field | ||
if (bundle.mapping && '__segment_internal_sync_mode' in bundle.mapping) { | ||
delete payload['__segment_internal_sync_mode'] | ||
} | ||
|
||
// Validate the resolved payload against the schema | ||
if (this.schema) { | ||
const schemaKey = `${this.destinationName}:${this.definition.title}` | ||
|
@@ -297,6 +302,10 @@ export class Action<Settings, Payload extends JSONLikeObject, AudienceSettings = | |
|
||
const syncMode = this.definition.syncMode ? bundle.mapping?.['__segment_internal_sync_mode'] : undefined | ||
|
||
const matchingKey = Object.values(this.definition.fields).find((field) => field.category === 'identifier') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Safe to remove this check to simplify? Can we just make an attempt to pass There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah I guess I was trying to be defensive lol. By the way, do you think there should be validation to make sure that the value in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I considered removing the condition to simplify future changes. If we decide to change the field category used as the indicator for matching keys, fewer parts of the codebase will need to be updated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also no need for validation since even if someone creates a |
||
? bundle.mapping?.['__segment_internal_matching_key'] | ||
: undefined | ||
|
||
// Construct the data bundle to send to an action | ||
const dataBundle = { | ||
rawData: bundle.data, | ||
|
@@ -312,7 +321,8 @@ export class Action<Settings, Payload extends JSONLikeObject, AudienceSettings = | |
stateContext: bundle.stateContext, | ||
audienceSettings: bundle.audienceSettings, | ||
hookOutputs, | ||
syncMode: isSyncMode(syncMode) ? syncMode : undefined | ||
syncMode: isSyncMode(syncMode) ? syncMode : undefined, | ||
matchingKey: matchingKey ? String(matchingKey) : undefined | ||
} | ||
|
||
// Construct the request client and perform the action | ||
|
@@ -329,16 +339,10 @@ export class Action<Settings, Payload extends JSONLikeObject, AudienceSettings = | |
throw new IntegrationError('This action does not support batched requests.', 'NotImplemented', 501) | ||
} | ||
|
||
let payloads = transformBatch(bundle.mapping, bundle.data) as Payload[] | ||
// Remove internal hidden fields | ||
const mapping: JSONObject = removeInternalHiddenFields(bundle.mapping) | ||
|
||
// Remove internal hidden field | ||
if (bundle.mapping && '__segment_internal_sync_mode' in bundle.mapping) { | ||
for (const payload of payloads) { | ||
if (payload) { | ||
delete payload['__segment_internal_sync_mode'] | ||
} | ||
} | ||
} | ||
let payloads = transformBatch(mapping, bundle.data) as Payload[] | ||
|
||
// Validate the resolved payloads against the schema | ||
if (this.schema) { | ||
|
@@ -373,6 +377,9 @@ export class Action<Settings, Payload extends JSONLikeObject, AudienceSettings = | |
|
||
if (this.definition.performBatch) { | ||
const syncMode = this.definition.syncMode ? bundle.mapping?.['__segment_internal_sync_mode'] : undefined | ||
const matchingKey = Object.values(this.definition.fields).find((field) => field.category === 'identifier') | ||
? bundle.mapping?.['__segment_internal_matching_key'] | ||
: undefined | ||
const data = { | ||
rawData: bundle.data, | ||
rawMapping: bundle.mapping, | ||
|
@@ -387,7 +394,8 @@ export class Action<Settings, Payload extends JSONLikeObject, AudienceSettings = | |
transactionContext: bundle.transactionContext, | ||
stateContext: bundle.stateContext, | ||
hookOutputs, | ||
syncMode: isSyncMode(syncMode) ? syncMode : undefined | ||
syncMode: isSyncMode(syncMode) ? syncMode : undefined, | ||
matchingKey: matchingKey ? String(matchingKey) : undefined | ||
} | ||
const output = await this.performRequest(this.definition.performBatch, data) | ||
results[0].data = output as JSONObject | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still trying to understand the multiple identifiers case, suppose we had an additional
identifier
field 'email' here. Then we would automatically push a 'Record Matching' dropdown field that users would pick userID or email from, and depending on that thematchingKey
would pull the correct value from the payload for the perform block? Is that a good understanding?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nick-Ag Exactly 💯 . So in the UI, the selected field out of all the
identifier
fields defined would have been stored inmapping['__segment_internal_matching_key']
, which is exactly whatmatchingKey
is set to.