|
| 1 | +import Types from './types'; |
| 2 | +import { IMParticleWebSDKInstance } from './mp-instance'; |
| 3 | +import { MPID } from '@mparticle/web-sdk'; |
| 4 | +import { Dictionary } from './utils'; |
| 5 | +import KitBlocker from './kitBlocking'; |
| 6 | +import { MPForwarder } from './forwarders.interfaces'; |
| 7 | + |
| 8 | +interface IFilteredMparticleUser { |
| 9 | + getUserIdentities(): { userIdentities: Dictionary<string> }; |
| 10 | + getMPID(): MPID; |
| 11 | + getUserAttributesLists(forwarder: MPForwarder): Dictionary<string[]>; |
| 12 | + getAllUserAttributes(): Dictionary; |
| 13 | +} |
| 14 | + |
| 15 | +export default function filteredMparticleUser( |
| 16 | + mpid: MPID, |
| 17 | + forwarder: MPForwarder | { userAttributeFilters: number[] }, |
| 18 | + mpInstance: IMParticleWebSDKInstance, |
| 19 | + kitBlocker?: KitBlocker |
| 20 | +): IFilteredMparticleUser { |
| 21 | + return { |
| 22 | + getUserIdentities: function(): { userIdentities: Dictionary<string> } { |
| 23 | + let currentUserIdentities: Dictionary<string> = {}; |
| 24 | + const identities = mpInstance._Store.getUserIdentities(mpid); |
| 25 | + |
| 26 | + for (const identityType in identities) { |
| 27 | + if (identities.hasOwnProperty(identityType)) { |
| 28 | + const identityName = Types.IdentityType.getIdentityName( |
| 29 | + mpInstance._Helpers.parseNumber(identityType) |
| 30 | + ); |
| 31 | + if ( |
| 32 | + !kitBlocker || |
| 33 | + (kitBlocker && |
| 34 | + !kitBlocker.isIdentityBlocked(identityName)) |
| 35 | + ) |
| 36 | + //if identity type is not blocked |
| 37 | + currentUserIdentities[identityName] = |
| 38 | + identities[identityType]; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + currentUserIdentities = (mpInstance._Helpers.filterUserIdentitiesForForwarders as Function)( |
| 43 | + currentUserIdentities, |
| 44 | + (forwarder as MPForwarder).userIdentityFilters |
| 45 | + ); |
| 46 | + |
| 47 | + return { |
| 48 | + userIdentities: currentUserIdentities, |
| 49 | + }; |
| 50 | + }, |
| 51 | + getMPID: function(): MPID { |
| 52 | + return mpid; |
| 53 | + }, |
| 54 | + getUserAttributesLists: function(forwarder: MPForwarder): Dictionary<string[]> { |
| 55 | + let userAttributes: Dictionary; |
| 56 | + let userAttributesLists: Dictionary<string[]> = {}; |
| 57 | + |
| 58 | + userAttributes = getAllUserAttributes(); |
| 59 | + for (const key in userAttributes) { |
| 60 | + if ( |
| 61 | + userAttributes.hasOwnProperty(key) && |
| 62 | + Array.isArray(userAttributes[key]) |
| 63 | + ) { |
| 64 | + if ( |
| 65 | + !kitBlocker || |
| 66 | + (kitBlocker && !kitBlocker.isAttributeKeyBlocked(key)) |
| 67 | + ) { |
| 68 | + userAttributesLists[key] = userAttributes[key].slice(); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + userAttributesLists = (mpInstance._Helpers.filterUserAttributes as Function)( |
| 74 | + userAttributesLists, |
| 75 | + forwarder.userAttributeFilters |
| 76 | + ); |
| 77 | + |
| 78 | + return userAttributesLists; |
| 79 | + }, |
| 80 | + getAllUserAttributes: getAllUserAttributes, |
| 81 | + }; |
| 82 | + |
| 83 | + function getAllUserAttributes(): Dictionary { |
| 84 | + let userAttributesCopy: Dictionary = {}; |
| 85 | + const userAttributes = mpInstance._Store.getUserAttributes(mpid); |
| 86 | + |
| 87 | + if (userAttributes) { |
| 88 | + for (const prop in userAttributes) { |
| 89 | + if (userAttributes.hasOwnProperty(prop)) { |
| 90 | + if ( |
| 91 | + !kitBlocker || |
| 92 | + (kitBlocker && |
| 93 | + !kitBlocker.isAttributeKeyBlocked(prop)) |
| 94 | + ) { |
| 95 | + if (Array.isArray(userAttributes[prop])) { |
| 96 | + userAttributesCopy[prop] = (userAttributes[ |
| 97 | + prop |
| 98 | + ] as string[]).slice(); |
| 99 | + } else { |
| 100 | + userAttributesCopy[prop] = userAttributes[prop]; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + userAttributesCopy = (mpInstance._Helpers.filterUserAttributes as Function)( |
| 108 | + userAttributesCopy, |
| 109 | + (forwarder as MPForwarder).userAttributeFilters |
| 110 | + ); |
| 111 | + |
| 112 | + return userAttributesCopy; |
| 113 | + } |
| 114 | +} |
0 commit comments