Skip to content

Commit

Permalink
refactor: Migrate Internal messages package MPCommerceMessage class t…
Browse files Browse the repository at this point in the history
…o kotlin Part 2
  • Loading branch information
Mansi-mParticle committed Nov 26, 2024
1 parent 24d6a81 commit 1440256
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 174 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package com.mparticle.internal.messages

import android.location.Location
import com.mparticle.commerce.CommerceEvent
import com.mparticle.commerce.Promotion
import com.mparticle.internal.Constants
import com.mparticle.internal.InternalSession
import com.mparticle.internal.MPUtility
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject

class MPCommerceMessage protected constructor(builder: Builder, session: InternalSession, location: Location?, mpId: Long) :
BaseMPMessage(builder, session, location, mpId) {

class Builder(commerceEvent: CommerceEvent) : BaseMPMessageBuilder(Constants.MessageType.COMMERCE_EVENT) {
init {
addCommerceEventInfo(this, commerceEvent)
}

val productAction: String?
get() {
val productActionObj = optJSONObject(Constants.Commerce.PRODUCT_ACTION_OBJECT)
if (productActionObj != null) {
return productActionObj.optString(Constants.Commerce.PRODUCT_ACTION)
}
return null
}

@Throws(JSONException::class)
override fun build(session: InternalSession, location: Location?, mpId: Long): BaseMPMessage {
return MPCommerceMessage(this, session, location, mpId)
}

companion object {
private fun addCommerceEventInfo(message: JSONObject, event: CommerceEvent) {
try {
event.screen?.let {
message.put(Constants.Commerce.SCREEN_NAME, it)
}
event.nonInteraction?.let {
message.put(Constants.Commerce.NON_INTERACTION, it)
}
event.currency?.let {
message.put(Constants.Commerce.CURRENCY, it)
}
event.customAttributeStrings?.let {
message.put(Constants.Commerce.ATTRIBUTES, MPUtility.mapToJson(it))
}
event.productAction?.let {
val productAction = JSONObject()
message.put(Constants.Commerce.PRODUCT_ACTION_OBJECT, productAction)
productAction.put(Constants.Commerce.PRODUCT_ACTION, event.productAction)
event.checkoutStep?.let {
productAction.put(Constants.Commerce.CHECKOUT_STEP, it)
}
event.checkoutOptions?.let {
productAction.put(Constants.Commerce.CHECKOUT_OPTIONS, it)
}
event.productListName?.let {
productAction.put(Constants.Commerce.PRODUCT_LIST_NAME, it)
}
event.productListSource?.let {
productAction.put(Constants.Commerce.PRODUCT_LIST_SOURCE, it)
}
event.transactionAttributes?.let { transactionAttributes ->
transactionAttributes.id.let {
productAction.put(Constants.Commerce.TRANSACTION_ID, it)
}
transactionAttributes.affiliation.let {
productAction.put(Constants.Commerce.TRANSACTION_AFFILIATION, it)
}

transactionAttributes.revenue.let {
productAction.put(Constants.Commerce.TRANSACTION_REVENUE, it)
}
transactionAttributes.tax.let {
productAction.put(Constants.Commerce.TRANSACTION_TAX, it)
}
transactionAttributes.shipping.let {
productAction.put(Constants.Commerce.TRANSACTION_SHIPPING, it)
}
transactionAttributes.couponCode.let {
productAction.put(Constants.Commerce.TRANSACTION_COUPON_CODE, it)
}
}
event.products?.takeIf { it.isNotEmpty() }?.let { products ->
val productsArray = JSONArray()
for (currentProduct in products) {
productsArray.put(JSONObject(currentProduct.toString()))
}
productAction.put(Constants.Commerce.PRODUCT_LIST, productsArray)
}
}

event.promotionAction?.let {
val promotionAction = JSONObject()
message.put(Constants.Commerce.PROMOTION_ACTION_OBJECT, promotionAction)
promotionAction.put(Constants.Commerce.PROMOTION_ACTION, event.promotionAction)
event.promotions?.takeIf { it.isNotEmpty() }?.let { eventPromotions ->
val promotions = JSONArray()
for (i in eventPromotions.indices) {
promotions.put(getPromotionJson(eventPromotions[i]))
}
promotionAction.put(Constants.Commerce.PROMOTION_LIST, promotions)
}
}

event.impressions?.takeIf { it.isNotEmpty() }?.let { eventImpressions ->
val impressionsArray = JSONArray()
for (impression in eventImpressions) {
val impressionJson = JSONObject()
impression.listName.let {
impressionJson.put(Constants.Commerce.IMPRESSION_LOCATION, it)
}

impression.products.takeIf { it.isNotEmpty() }?.let {
val productsJson = JSONArray()
impressionJson.put(Constants.Commerce.IMPRESSION_PRODUCT_LIST, productsJson)
for (product in impression.products) {
productsJson.put(JSONObject(product.toString()))
}
}
impressionJson.takeIf { it.length() > 0 }?.let {
impressionsArray.put(impressionJson)
}
}
impressionsArray.takeIf { it.length() > 0 }.let {
message.put(Constants.Commerce.IMPRESSION_OBJECT, impressionsArray)
}
}
} catch (jse: Exception) {
}
}

private fun getPromotionJson(promotion: Promotion): JSONObject {
val json = JSONObject()
try {
if (!MPUtility.isEmpty(promotion.id)) {
json.put("id", promotion.id)
}
if (!MPUtility.isEmpty(promotion.name)) {
json.put("nm", promotion.name)
}
if (!MPUtility.isEmpty(promotion.creative)) {
json.put("cr", promotion.creative)
}
if (!MPUtility.isEmpty(promotion.position)) {
json.put("ps", promotion.position)
}
} catch (jse: JSONException) {
}
return json
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import com.mparticle.MPEvent
import com.mparticle.MParticle
import com.mparticle.MockMParticle
import com.mparticle.commerce.CommerceEvent
import com.mparticle.commerce.Impression
import com.mparticle.commerce.Product
import com.mparticle.commerce.Promotion
import com.mparticle.internal.messages.BaseMPMessage
import com.mparticle.internal.messages.BaseMPMessageBuilder
import com.mparticle.internal.messages.MPCommerceMessage
Expand Down Expand Up @@ -98,4 +100,54 @@ class BaseMPMessageTest {
val message = builder.build(InternalSession(), null, 0)
Assert.assertNotNull(message)
}

@Test
@Throws(Exception::class)
fun testNullCartOnCommerceEventWithImpression() {
MParticle.setInstance(MockMParticle())
val impression = Impression("Test Products List", Product.Builder("foo", "bar", 10.0).build())

val event =
CommerceEvent.Builder(impression)
.screen("TestScreen")
.nonInteraction(true)
.customAttributes(
mapOf(
"size" to "XL",
"color" to "RED"
)
)
.currency("USD")
.build()
val builder = MPCommerceMessage.Builder(event)
.timestamp(12345) as MPCommerceMessage.Builder
val message = builder.build(InternalSession(), null, 0)

Assert.assertNotNull(message)
}

@Test
@Throws(Exception::class)
fun testNullCartOnCommerceEventWithPromotion() {
MParticle.setInstance(MockMParticle())

val event = Promotion().apply {
id = "1234"
creative = "test_banner_1"
name = "50% off sale"
position = "test_button"
}.let {
CommerceEvent.Builder(Promotion.CLICK, it)
.productListName("Testing Product List Name")
.productListSource("Testing source")
.checkoutStep(2)
.customAttributes(mapOf("Promotion ATT" to "Promotion Testing")).build()
}

val builder = MPCommerceMessage.Builder(event)
.timestamp(12345) as MPCommerceMessage.Builder
val message = builder.build(InternalSession(), null, 0)

Assert.assertNotNull(message)
}
}

0 comments on commit 1440256

Please sign in to comment.