@@ -22,6 +22,7 @@ import { Cache } from "../../../utils/cache/cache";
2222import { CmabClient } from "./cmab_client" ;
2323import { v4 as uuidV4 } from 'uuid' ;
2424import murmurhash from "murmurhash" ;
25+ import { a } from "vitest/dist/chunks/suite.CcK46U-P" ;
2526
2627export type CmabDecision = {
2728 variationId : string ,
@@ -32,14 +33,14 @@ export interface CmabService {
3233 /**
3334 * Get variation id for the user
3435 * @param {OptimizelyUserContext } userContext
35- * @param {string } experimentId
36+ * @param {string } ruleId
3637 * @param {OptimizelyDecideOption[] } options
3738 * @return {Promise<CmabDecision> }
3839 */
3940 getDecision (
4041 projectConfig : ProjectConfig ,
4142 userContext : OptimizelyUserContext ,
42- experimentId : string ,
43+ ruleId : string ,
4344 options : OptimizelyDecideOption [ ]
4445 ) : Promise < CmabDecision >
4546}
@@ -76,7 +77,7 @@ export class DefaultCmabService implements CmabService {
7677 const filteredAttributes = this . filterAttributes ( projectConfig , userContext , ruleId ) ;
7778
7879 if ( options . includes ( OptimizelyDecideOption . IGNORE_CMAB_CACHE ) ) {
79- return this . fetchVariation ( ruleId , userContext . getUserId ( ) , filteredAttributes ) ;
80+ return this . fetchDecision ( ruleId , userContext . getUserId ( ) , filteredAttributes ) ;
8081 }
8182
8283 if ( options . includes ( OptimizelyDecideOption . RESET_CMAB_CACHE ) ) {
@@ -90,7 +91,9 @@ export class DefaultCmabService implements CmabService {
9091 }
9192
9293 const cachedValue = await this . cmabCache . get ( cacheKey ) ;
93- const attributesHash = String ( murmurhash . v3 ( JSON . stringify ( filteredAttributes ) ) ) ;
94+
95+ const attributesJson = JSON . stringify ( filteredAttributes , Object . keys ( filteredAttributes ) . sort ( ) ) ;
96+ const attributesHash = String ( murmurhash . v3 ( attributesJson ) ) ;
9497
9598 if ( cachedValue ) {
9699 if ( cachedValue . attributesHash === attributesHash ) {
@@ -100,7 +103,7 @@ export class DefaultCmabService implements CmabService {
100103 }
101104 }
102105
103- const variation = await this . fetchVariation ( ruleId , userContext . getUserId ( ) , filteredAttributes ) ;
106+ const variation = await this . fetchDecision ( ruleId , userContext . getUserId ( ) , filteredAttributes ) ;
104107 this . cmabCache . set ( cacheKey , {
105108 attributesHash,
106109 variationId : variation . variationId ,
@@ -110,7 +113,7 @@ export class DefaultCmabService implements CmabService {
110113 return variation ;
111114 }
112115
113- private async fetchVariation (
116+ private async fetchDecision (
114117 ruleId : string ,
115118 userId : string ,
116119 attributes : UserAttributes ,
@@ -126,19 +129,20 @@ export class DefaultCmabService implements CmabService {
126129 ruleId : string
127130 ) : UserAttributes {
128131 const filteredAttributes : UserAttributes = { } ;
129- const attributes = userContext . getAttributes ( ) ;
132+ const userAttributes = userContext . getAttributes ( ) ;
130133
131134 const experiment = projectConfig . experimentIdMap [ ruleId ] ;
132135 if ( ! experiment || ! experiment . cmab ) {
133136 return filteredAttributes ;
134137 }
135138
136139 const cmabAttributeIds = experiment . cmab . attributeIds ;
137-
138- Object . keys ( attributes ) . forEach ( ( key ) => {
139- const attributeId = projectConfig . attributeKeyMap [ key ] . id ;
140- if ( cmabAttributeIds . includes ( attributeId ) ) {
141- filteredAttributes [ key ] = attributes [ key ] ;
140+
141+ cmabAttributeIds . forEach ( ( aid ) => {
142+ const attribute = projectConfig . attributeIdMap [ aid ] ;
143+
144+ if ( userAttributes . hasOwnProperty ( attribute . key ) ) {
145+ filteredAttributes [ attribute . key ] = userAttributes [ attribute . key ] ;
142146 }
143147 } ) ;
144148
0 commit comments