Skip to content

Commit d79df98

Browse files
committed
WIP FS-UID Normalization
1 parent 623b991 commit d79df98

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

packages/optimizely-sdk/lib/core/odp/odp_manager.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ export abstract class OdpManager {
156156
* @param {OdpEvent} > ODP Event to send to event manager
157157
*/
158158
public sendEvent({ type, action, identifiers, data }: OdpEvent): void {
159+
let mType = type;
160+
161+
if (typeof mType != 'string' || mType === '') {
162+
mType = 'fullstack';
163+
}
164+
159165
if (!this.enabled) {
160166
throw new Error(ERROR_MESSAGES.ODP_NOT_ENABLED);
161167
}
@@ -172,10 +178,46 @@ export abstract class OdpManager {
172178
throw new Error(ERROR_MESSAGES.ODP_SEND_EVENT_FAILED_EVENT_MANAGER_MISSING);
173179
}
174180

175-
this.eventManager.sendEvent(new OdpEvent(type, action, identifiers, data));
181+
if (typeof action != 'string' || action === '') {
182+
throw new Error('ODP action is not valid (cannot be empty).');
183+
}
184+
185+
const normalizedIdentifiers = normalizeFsUserIdentifiers(identifiers);
186+
187+
this.eventManager.sendEvent(new OdpEvent(mType, action, normalizedIdentifiers, data));
176188
}
177189

178190
public abstract isVuidEnabled(): boolean;
179191

180192
public abstract getVuid(): string | undefined;
181193
}
194+
195+
function normalizeFsUserIdentifiers(identifiers: Map<string, string>): Map<string, string> {
196+
let fsUserId;
197+
const mIdentifiers = structuredClone(identifiers);
198+
199+
if (identifiers.has('fs-user-id')) {
200+
fsUserId = identifiers.get('fs-user-id');
201+
mIdentifiers.delete('fs-user-id');
202+
}
203+
204+
if (identifiers.has('FS-USER-ID')) {
205+
if (fsUserId) {
206+
throw new Error('Conflicting variant of fs_user_id.');
207+
}
208+
209+
fsUserId = identifiers.get('FS-USER-ID');
210+
mIdentifiers.delete('FS-USER-ID');
211+
}
212+
213+
if (identifiers.has('FS_USER_ID')) {
214+
if (fsUserId) {
215+
throw new Error('Conflicting variant of fs_user_id.');
216+
}
217+
218+
fsUserId = identifiers.get('FS_USER_ID');
219+
mIdentifiers.delete('FS_USER_ID');
220+
}
221+
222+
return mIdentifiers;
223+
}

packages/optimizely-sdk/lib/index.browser.tests.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ describe('javascript-sdk (Browser)', function() {
770770
sinon.assert.called(fakeEventManager.start);
771771
});
772772

773-
it('should send an odp event with sendOdpEvent', async () => {
773+
it('should send an odp event when calling sendOdpEvent with valid parameters', async () => {
774774
const fakeEventManager = {
775775
updateSettings: sinon.spy(),
776776
start: sinon.spy(),
@@ -802,6 +802,38 @@ describe('javascript-sdk (Browser)', function() {
802802
sinon.assert.called(fakeEventManager.sendEvent);
803803
});
804804

805+
it('should not send an odp event when calling sendOdpEvent with an empty string as the action', async () => {
806+
const fakeEventManager = {
807+
updateSettings: sinon.spy(),
808+
start: sinon.spy(),
809+
stop: sinon.spy(),
810+
registerVuid: sinon.spy(),
811+
identifyUser: sinon.spy(),
812+
sendEvent: sinon.spy(),
813+
flush: sinon.spy(),
814+
};
815+
816+
const client = optimizelyFactory.createInstance({
817+
datafile: testData.getOdpIntegratedConfigWithSegments(),
818+
errorHandler: fakeErrorHandler,
819+
eventDispatcher: fakeEventDispatcher,
820+
eventBatchSize: null,
821+
logger,
822+
odpOptions: {
823+
eventManager: fakeEventManager,
824+
},
825+
});
826+
827+
const readyData = await client.onReady();
828+
assert.equal(readyData.success, true);
829+
assert.isUndefined(readyData.reason);
830+
831+
client.sendOdpEvent('');
832+
833+
sinon.assert.called(logger.error);
834+
// sinon.assert.called(fakeEventManager.sendEvent);
835+
});
836+
805837
it('should log an error when attempting to send an odp event when odp is disabled', async () => {
806838
const client = optimizelyFactory.createInstance({
807839
datafile: testData.getTestProjectConfigWithFeatures(),

0 commit comments

Comments
 (0)