Skip to content

Commit e3e6e14

Browse files
committed
fix: misc issues and handle backward compatibility
1 parent 41ff3ed commit e3e6e14

File tree

3 files changed

+86
-14
lines changed

3 files changed

+86
-14
lines changed

processor/consent.go

+44-9
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,17 @@ import (
1313
)
1414

1515
type ConsentManagementInfo struct {
16-
AllowedConsentIDs []string `json:"allowedConsentIds"`
17-
DeniedConsentIDs []string `json:"deniedConsentIds"`
18-
Provider string `json:"provider"`
19-
ResolutionStrategy string `json:"resolutionStrategy"`
16+
AllowedConsentIDs []string
17+
DeniedConsentIDs []string
18+
Provider string
19+
ResolutionStrategy string
20+
}
21+
22+
type EventConsentManagementInfo struct {
23+
AllowedConsentIDs interface{} `json:"allowedConsentIds"`
24+
DeniedConsentIDs []string `json:"deniedConsentIds"`
25+
Provider string `json:"provider"`
26+
ResolutionStrategy string `json:"resolutionStrategy"`
2027
}
2128

2229
type GenericConsentManagementProviderData struct {
@@ -220,28 +227,56 @@ func getGenericConsentManagementData(dest *backendconfig.DestinationT) (ConsentP
220227
}
221228

222229
func getConsentManagementInfo(event types.SingularEventT) (ConsentManagementInfo, error) {
230+
consentManagementInfoFromEvent := EventConsentManagementInfo{}
223231
consentManagementInfo := ConsentManagementInfo{}
224232
if consentManagement, ok := misc.MapLookup(event, "context", "consentManagement").(map[string]interface{}); ok {
225233
consentManagementObjBytes, mErr := jsonrs.Marshal(consentManagement)
226234
if mErr != nil {
227235
return consentManagementInfo, fmt.Errorf("error marshalling consentManagement: %v", mErr)
228236
}
229237

230-
unmErr := jsonrs.Unmarshal(consentManagementObjBytes, &consentManagementInfo)
238+
unmErr := jsonrs.Unmarshal(consentManagementObjBytes, &consentManagementInfoFromEvent)
231239
if unmErr != nil {
232240
return consentManagementInfo, fmt.Errorf("error unmarshalling consentManagementInfo: %v", unmErr)
233241
}
234242

243+
consentManagementInfo.DeniedConsentIDs = consentManagementInfoFromEvent.DeniedConsentIDs
244+
consentManagementInfo.Provider = consentManagementInfoFromEvent.Provider
245+
consentManagementInfo.ResolutionStrategy = consentManagementInfoFromEvent.ResolutionStrategy
246+
247+
// This is to support really old version of the JS SDK v3 that sent this data as an object
248+
// for OneTrust provider.
249+
// Handle AllowedConsentIDs based on its type (array or map)
250+
switch val := consentManagementInfoFromEvent.AllowedConsentIDs.(type) {
251+
case []interface{}:
252+
// Convert []interface{} to []string
253+
consentManagementInfo.AllowedConsentIDs = make([]string, 0, len(val))
254+
for _, v := range val {
255+
if strVal, ok := v.(string); ok {
256+
consentManagementInfo.AllowedConsentIDs = append(consentManagementInfo.AllowedConsentIDs, strVal)
257+
}
258+
}
259+
case []string:
260+
// Already a string array
261+
consentManagementInfo.AllowedConsentIDs = val
262+
case map[string]interface{}:
263+
// Use keys from the map (legacy OneTrust format)
264+
consentManagementInfo.AllowedConsentIDs = lo.Keys(val)
265+
default:
266+
consentManagementInfo.AllowedConsentIDs = []string{}
267+
}
268+
235269
// Ideally, the clean up and filter is not needed for standard providers
236270
// but useful for custom providers where users send this data directly
237271
// to the SDKs.
238-
cleanupPredicate := func(consent string, _ int) (string, bool) {
239-
return strings.TrimSpace(consent), consent != ""
272+
sanitizePredicate := func(consent string, _ int) string {
273+
return strings.TrimSpace(consent)
240274
}
241275

242-
consentManagementInfo.AllowedConsentIDs = lo.FilterMap(consentManagementInfo.AllowedConsentIDs, cleanupPredicate)
243-
consentManagementInfo.DeniedConsentIDs = lo.FilterMap(consentManagementInfo.DeniedConsentIDs, cleanupPredicate)
276+
consentManagementInfo.AllowedConsentIDs = lo.Map(consentManagementInfo.AllowedConsentIDs, sanitizePredicate)
277+
consentManagementInfo.DeniedConsentIDs = lo.Map(consentManagementInfo.DeniedConsentIDs, sanitizePredicate)
244278

279+
// Filter out empty values
245280
filterPredicate := func(consent string, _ int) (string, bool) {
246281
return consent, consent != ""
247282
}

processor/consent_test.go

+41-4
Original file line numberDiff line numberDiff line change
@@ -2517,7 +2517,7 @@ func TestGetConsentManagementInfo(t *testing.T) {
25172517
"consent category 2",
25182518
},
25192519
"deniedConsentIds": []string{
2520-
"consent category 3",
2520+
" consent category 3 ",
25212521
"",
25222522
"consent category 4",
25232523
"",
@@ -2552,9 +2552,9 @@ func TestGetConsentManagementInfo(t *testing.T) {
25522552
"context": map[string]interface{}{
25532553
"consentManagement": map[string]interface{}{
25542554
"deniedConsentIds": []string{
2555-
"consent category 3",
2556-
"",
2557-
"consent category 4",
2555+
"consent category 3 ",
2556+
" ",
2557+
" consent category 4",
25582558
"",
25592559
},
25602560
},
@@ -2570,6 +2570,43 @@ func TestGetConsentManagementInfo(t *testing.T) {
25702570
ResolutionStrategy: "",
25712571
},
25722572
},
2573+
{
2574+
description: "should return consent management info when consent management data is sent from older SDKs with allowed consent IDs as an object",
2575+
input: types.SingularEventT{
2576+
"anonymousId": "123",
2577+
"type": "track",
2578+
"event": "test",
2579+
"properties": map[string]interface{}{
2580+
"category": "test",
2581+
},
2582+
"context": map[string]interface{}{
2583+
"consentManagement": map[string]interface{}{
2584+
"allowedConsentIds": map[string]interface{}{
2585+
"C0": "consent category 1",
2586+
"C1": "consent category 2",
2587+
},
2588+
"deniedConsentIds": []string{
2589+
"consent category 3 ",
2590+
" ",
2591+
" consent category 4",
2592+
"",
2593+
},
2594+
},
2595+
},
2596+
},
2597+
expected: ConsentManagementInfo{
2598+
Provider: "",
2599+
AllowedConsentIDs: []string{
2600+
"C0",
2601+
"C1",
2602+
},
2603+
DeniedConsentIDs: []string{
2604+
"consent category 3",
2605+
"consent category 4",
2606+
},
2607+
ResolutionStrategy: "",
2608+
},
2609+
},
25732610
}
25742611

25752612
for _, testCase := range testCases {

processor/processor_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3653,7 +3653,7 @@ var _ = Describe("Processor", Ordered, func() {
36533653
"destination-definition-name-enabled",
36543654
),
36553655
)),
3656-
).To(Equal(8)) // all except D13
3656+
).To(Equal(6)) // all except D6, D13, and D14
36573657

36583658
Expect(processor.isDestinationAvailable(eventWithDeniedConsentsGCM, SourceIDGCM, "")).To(BeTrue())
36593659
Expect(

0 commit comments

Comments
 (0)