From 789f09f0c58e21cb3200ad933a269c40e9f530dc Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Mon, 9 May 2022 10:39:03 -0500 Subject: [PATCH] feat: add device and profile attribute setters --- .../main/java/io/customer/sdk/CustomerIO.kt | 15 ++++++++++- .../java/io/customer/sdk/CustomerIOClient.kt | 13 ++++++++++ .../java/io/customer/sdk/api/CustomerIOApi.kt | 1 + .../io/customer/sdk/CustomerIOClientTest.kt | 26 ++++++++++++++++++- .../java/io/customer/sdk/CustomerIOTest.kt | 9 +++++++ 5 files changed, 62 insertions(+), 2 deletions(-) diff --git a/sdk/src/main/java/io/customer/sdk/CustomerIO.kt b/sdk/src/main/java/io/customer/sdk/CustomerIO.kt index b91acd8d3..2e2a6235d 100644 --- a/sdk/src/main/java/io/customer/sdk/CustomerIO.kt +++ b/sdk/src/main/java/io/customer/sdk/CustomerIO.kt @@ -20,6 +20,9 @@ interface CustomerIOInstance { val sdkVersion: String // For security reasons, do not expose the SDK config as anyone can get the API key from the SDK including 3rd parties. + var profileAttributes: CustomAttributes + var deviceAttributes: CustomAttributes + fun identify(identifier: String) fun identify( @@ -310,11 +313,21 @@ class CustomerIO internal constructor( deviceToken = deviceToken ) + /** + * Use to provide attributes to the currently identified profile. + * + * Note: If there is not a profile identified, this request will be ignored. + */ + override var profileAttributes: CustomAttributes = emptyMap() + set(value) { + api.addCustomProfileAttributes(value) + } + /** * Use to provide additional and custom device attributes * apart from the ones the SDK is programmed to send to customer workspace. */ - var deviceAttributes: CustomAttributes = emptyMap() + override var deviceAttributes: CustomAttributes = emptyMap() set(value) { field = value diff --git a/sdk/src/main/java/io/customer/sdk/CustomerIOClient.kt b/sdk/src/main/java/io/customer/sdk/CustomerIOClient.kt index 899227a9c..2cc651c09 100644 --- a/sdk/src/main/java/io/customer/sdk/CustomerIOClient.kt +++ b/sdk/src/main/java/io/customer/sdk/CustomerIOClient.kt @@ -142,6 +142,19 @@ internal class CustomerIOClient( registerDeviceToken(existingDeviceToken, attributes) } + override fun addCustomProfileAttributes(attributes: CustomAttributes) { + logger.debug("adding profile attributes request made") + + val currentlyIdentifiedProfileId = preferenceRepository.getIdentifier() + + if (currentlyIdentifiedProfileId == null) { + logger.debug("no profile is currently identified. ignoring request to add attributes to a profile") + return + } + + identify(currentlyIdentifiedProfileId, attributes) + } + private fun createDeviceAttributes(customAddedAttributes: CustomAttributes): Map { if (!config.autoTrackDeviceAttributes) return customAddedAttributes diff --git a/sdk/src/main/java/io/customer/sdk/api/CustomerIOApi.kt b/sdk/src/main/java/io/customer/sdk/api/CustomerIOApi.kt index d9b1007f1..66d5d8a94 100644 --- a/sdk/src/main/java/io/customer/sdk/api/CustomerIOApi.kt +++ b/sdk/src/main/java/io/customer/sdk/api/CustomerIOApi.kt @@ -9,6 +9,7 @@ internal interface CustomerIOApi { fun clearIdentify() fun registerDeviceToken(deviceToken: String, attributes: CustomAttributes) fun addCustomDeviceAttributes(attributes: CustomAttributes) + fun addCustomProfileAttributes(attributes: CustomAttributes) fun deleteDeviceToken() fun trackMetric(deliveryID: String, event: MetricEvent, deviceToken: String) fun screen(name: String, attributes: CustomAttributes) diff --git a/sdk/src/sharedTest/java/io/customer/sdk/CustomerIOClientTest.kt b/sdk/src/sharedTest/java/io/customer/sdk/CustomerIOClientTest.kt index 7fbc8dd10..b00f456fc 100644 --- a/sdk/src/sharedTest/java/io/customer/sdk/CustomerIOClientTest.kt +++ b/sdk/src/sharedTest/java/io/customer/sdk/CustomerIOClientTest.kt @@ -243,7 +243,6 @@ class CustomerIOClientTest : BaseTest() { customerIOClient.addCustomDeviceAttributes(givenAttributes) - // a token got registered verify(backgroundQueueMock).queueRegisterDevice( givenIdentifier, Device( @@ -254,6 +253,31 @@ class CustomerIOClientTest : BaseTest() { ) } + // addCustomProfileAttributes + + @Test + fun addCustomProfileAttributes_givenProfileIdentified_expectDoNotIdentifyProfile() { + val givenAttributes = mapOf(String.random to String.random) + + customerIOClient.addCustomProfileAttributes(givenAttributes) + + // do not identify profile + verifyNoInteractions(backgroundQueueMock) + } + + @Test + fun addCustomProfileAttributes_givenExistingProfileIdentified_expectAddAttributesToProfile() { + val givenAttributes = mapOf(String.random to String.random) + val givenIdentifier = String.random + prefRepository.saveIdentifier(givenIdentifier) + whenever(backgroundQueueMock.queueIdentifyProfile(anyOrNull(), anyOrNull(), anyOrNull())).thenReturn(QueueModifyResult(true, QueueStatus(siteId, 1))) + + customerIOClient.addCustomProfileAttributes(givenAttributes) + + // assert that attributes have been added to a profile + verify(backgroundQueueMock).queueIdentifyProfile(givenIdentifier, givenIdentifier, givenAttributes) + } + // deleteDeviceToken @Test diff --git a/sdk/src/sharedTest/java/io/customer/sdk/CustomerIOTest.kt b/sdk/src/sharedTest/java/io/customer/sdk/CustomerIOTest.kt index 77a934163..a68386495 100644 --- a/sdk/src/sharedTest/java/io/customer/sdk/CustomerIOTest.kt +++ b/sdk/src/sharedTest/java/io/customer/sdk/CustomerIOTest.kt @@ -98,4 +98,13 @@ class CustomerIOTest : BaseTest() { verify(apiMock).addCustomDeviceAttributes(givenAttributes) } + + @Test + fun profileAttributes_givenSetValue_expectMakeRequestToAddAttributes() { + val givenAttributes = mapOf(String.random to String.random) + + customerIO.profileAttributes = givenAttributes + + verify(apiMock).addCustomProfileAttributes(givenAttributes) + } }