11package com.onesignal.user.internal.operations.impl.executors
22
33import com.onesignal.common.NetworkUtils
4+ import com.onesignal.common.consistency.IamFetchReadyCondition
5+ import com.onesignal.common.consistency.enums.IamFetchRywTokenKey
6+ import com.onesignal.common.consistency.models.IConsistencyManager
47import com.onesignal.common.exceptions.BackendException
58import com.onesignal.common.modeling.ModelChangeTags
69import com.onesignal.core.internal.operations.ExecutionResponse
@@ -31,12 +34,13 @@ internal class UpdateUserOperationExecutor(
3134 private val _propertiesModelStore : PropertiesModelStore ,
3235 private val _buildUserService : IRebuildUserService ,
3336 private val _newRecordState : NewRecordsState ,
37+ private val _consistencyManager : IConsistencyManager ,
3438) : IOperationExecutor {
3539 override val operations: List <String >
3640 get() = listOf (SET_TAG , DELETE_TAG , SET_PROPERTY , TRACK_SESSION_START , TRACK_SESSION_END , TRACK_PURCHASE )
3741
38- override suspend fun execute (ops : List <Operation >): ExecutionResponse {
39- Logging .log(LogLevel .DEBUG , " UpdateUserOperationExecutor(operation: $ops )" )
42+ override suspend fun execute (operations : List <Operation >): ExecutionResponse {
43+ Logging .log(LogLevel .DEBUG , " UpdateUserOperationExecutor(operation: $operations )" )
4044
4145 var appId: String? = null
4246 var onesignalId: String? = null
@@ -45,7 +49,7 @@ internal class UpdateUserOperationExecutor(
4549 var deltasObject = PropertiesDeltasObject ()
4650 var refreshDeviceMetadata = false
4751
48- for (operation in ops ) {
52+ for (operation in operations ) {
4953 when (operation) {
5054 is SetTagOperation -> {
5155 if (appId == null ) {
@@ -83,7 +87,8 @@ internal class UpdateUserOperationExecutor(
8387 // that exist in this group.
8488 val sessionCount = if (deltasObject.sessionCount != null ) deltasObject.sessionCount!! + 1 else 1
8589
86- deltasObject = PropertiesDeltasObject (deltasObject.sessionTime, sessionCount, deltasObject.amountSpent, deltasObject.purchases)
90+ deltasObject =
91+ PropertiesDeltasObject (deltasObject.sessionTime, sessionCount, deltasObject.amountSpent, deltasObject.purchases)
8792 refreshDeviceMetadata = true
8893 }
8994 is TrackSessionEndOperation -> {
@@ -94,9 +99,15 @@ internal class UpdateUserOperationExecutor(
9499
95100 // The session time we pass up is the total session time across all `TrackSessionEndOperation`
96101 // operations that exist in this group.
97- val sessionTime = if (deltasObject.sessionTime != null ) deltasObject.sessionTime!! + operation.sessionTime else operation.sessionTime
102+ val sessionTime =
103+ if (deltasObject.sessionTime != null ) {
104+ deltasObject.sessionTime!! + operation.sessionTime
105+ } else {
106+ operation.sessionTime
107+ }
98108
99- deltasObject = PropertiesDeltasObject (sessionTime, deltasObject.sessionCount, deltasObject.amountSpent, deltasObject.purchases)
109+ deltasObject =
110+ PropertiesDeltasObject (sessionTime, deltasObject.sessionCount, deltasObject.amountSpent, deltasObject.purchases)
100111 }
101112 is TrackPurchaseOperation -> {
102113 if (appId == null ) {
@@ -107,7 +118,12 @@ internal class UpdateUserOperationExecutor(
107118 // The amount spent we pass up is the total amount spent across all `TrackPurchaseOperation`
108119 // operations that exist in this group, while the purchases is the union of all
109120 // `TrackPurchaseOperation` operations that exist in this group.
110- val amountSpent = if (deltasObject.amountSpent != null ) deltasObject.amountSpent!! + operation.amountSpent else operation.amountSpent
121+ val amountSpent =
122+ if (deltasObject.amountSpent != null ) {
123+ deltasObject.amountSpent!! + operation.amountSpent
124+ } else {
125+ operation.amountSpent
126+ }
111127 val purchasesArray = if (deltasObject.purchases != null ) deltasObject.purchases!! .toMutableList() else mutableListOf ()
112128
113129 for (purchase in operation.purchases) {
@@ -122,18 +138,25 @@ internal class UpdateUserOperationExecutor(
122138
123139 if (appId != null && onesignalId != null ) {
124140 try {
125- _userBackend .updateUser(
126- appId,
127- IdentityConstants .ONESIGNAL_ID ,
128- onesignalId,
129- propertiesObject,
130- refreshDeviceMetadata,
131- deltasObject,
132- )
141+ val rywToken =
142+ _userBackend .updateUser(
143+ appId,
144+ IdentityConstants .ONESIGNAL_ID ,
145+ onesignalId,
146+ propertiesObject,
147+ refreshDeviceMetadata,
148+ deltasObject,
149+ )
150+
151+ if (rywToken != null ) {
152+ _consistencyManager .setRywToken(onesignalId, IamFetchRywTokenKey .USER , rywToken)
153+ } else {
154+ _consistencyManager .resolveConditionsWithID(IamFetchReadyCondition .ID )
155+ }
133156
134157 if (_identityModelStore .model.onesignalId == onesignalId) {
135158 // go through and make sure any properties are in the correct model state
136- for (operation in ops ) {
159+ for (operation in operations ) {
137160 when (operation) {
138161 is SetTagOperation ->
139162 _propertiesModelStore .model.tags.setStringProperty(
0 commit comments