Coverage Summary for Class: ViewProductSegmentNode (cloud.mindbox.mobile_sdk.inapp.domain.models)

Class Method, % Branch, % Line, % Instruction, %
ViewProductSegmentNode 100% (6/6) 57.3% (47/82) 97.9% (47/48) 96.3% (497/516)
ViewProductSegmentNode$fetchTargetingInfo$1
ViewProductSegmentNode$getOperationsSet$1
ViewProductSegmentNode$gson$2 100% (1/1) 100% (1/1) 100% (2/2)
ViewProductSegmentNode$inAppSegmentationRepository$2 100% (1/1) 100% (1/1) 100% (2/2)
ViewProductSegmentNode$inAppTargetingErrorRepository$2 100% (1/1) 100% (1/1) 100% (2/2)
ViewProductSegmentNode$mobileConfigRepository$2 100% (1/1) 100% (1/1) 100% (2/2)
ViewProductSegmentNode$sessionStorageManager$2 100% (1/1) 100% (1/1) 100% (2/2)
ViewProductSegmentNode$WhenMappings
Total 100% (11/11) 57.3% (47/82) 98.1% (52/53) 96.4% (507/526)


 package cloud.mindbox.mobile_sdk.inapp.domain.models
 
 import cloud.mindbox.mobile_sdk.di.mindboxInject
 import cloud.mindbox.mobile_sdk.inapp.domain.extensions.shouldTrackTargetingError
 import cloud.mindbox.mobile_sdk.logger.mindboxLogE
 import cloud.mindbox.mobile_sdk.models.operation.request.OperationBodyRequest
 
 internal data class ViewProductSegmentNode(
     override val type: String,
     val kind: Kind,
     val segmentationExternalId: String,
     val segmentExternalId: String,
 ) : OperationNodeBase(type) {
 
     private val mobileConfigRepository by mindboxInject { mobileConfigRepository }
     private val inAppSegmentationRepository by mindboxInject { inAppSegmentationRepository }
     private val inAppTargetingErrorRepository by mindboxInject { inAppTargetingErrorRepository }
     private val gson by mindboxInject { gson }
     private val sessionStorageManager by mindboxInject { sessionStorageManager }
 
     override suspend fun fetchTargetingInfo(data: TargetingData) {
         if (data !is TargetingData.OperationBody) return
         val body = gson.fromJson(data.operationBody, OperationBodyRequest::class.java)
         body?.viewProductRequest?.product?.ids?.ids?.entries?.firstOrNull()?.also { entry ->
             val value = entry.value?.takeIf { it.isNotBlank() } ?: return
             val product = entry.key to value
             if (inAppSegmentationRepository.getProductSegmentationFetched(product) != ProductSegmentationFetchStatus.SEGMENTATION_FETCH_SUCCESS) {
                 runCatching {
                     inAppSegmentationRepository.fetchProductSegmentation(
                         product
                     )
                 }.onFailure { error ->
                     if (error is ProductSegmentationError) {
                         sessionStorageManager.processedProductSegmentations[product] =
                             ProductSegmentationFetchStatus.SEGMENTATION_FETCH_ERROR
                         if (error.shouldTrackTargetingError()) {
                             inAppTargetingErrorRepository.saveError(
                                 key = TargetingErrorKey.ProductSegmentation(product),
                                 error = error
                             )
                         }
                         mindboxLogE("Error fetching product segmentations for product $product")
                     }
                 }
             }
         } ?: return
     }
 
     override fun checkTargeting(data: TargetingData): Boolean {
         if (data !is TargetingData.OperationBody) return false
 
         val body = gson.fromJson(data.operationBody, OperationBodyRequest::class.java)
         val entry = body?.viewProductRequest?.product?.ids?.ids?.entries?.firstOrNull() ?: return false
         val value = entry.value?.takeIf { it.isNotBlank() } ?: return false
         val product = entry.key to value
         val segmentationsResult = inAppSegmentationRepository.getProductSegmentations(product).flatMap {
             it?.productSegmentations?.firstOrNull()?.productList ?: emptyList()
         }
         if (segmentationsResult.isEmpty()) return false
         return when (kind) {
             Kind.POSITIVE -> segmentationsResult.any { segmentationWrapper -> segmentationWrapper.segmentationExternalId == segmentationExternalId && segmentationWrapper.segmentExternalId == segmentExternalId }
             Kind.NEGATIVE -> segmentationsResult.find { it.segmentationExternalId == segmentationExternalId }
                 ?.segmentExternalId
                 ?.let { it != segmentExternalId } == true
         }
     }
 
     override suspend fun getOperationsSet(): Set<String> {
         return mobileConfigRepository.getOperations()[OperationName.VIEW_PRODUCT]?.systemName?.let {
             setOf(it)
         } ?: setOf()
     }
 
     override fun hasProductSegmentationNode(): Boolean = true
 }