@@ -59,10 +59,13 @@ class OSIdentityOperationExecutor: OSOperationExecutor {
5959 // Hook each uncached Request to the model in the store
6060 for (index, request) in addRequestQueue. enumerated ( ) . reversed ( ) {
6161 if let identityModel = OneSignalUserManagerImpl . sharedInstance. identityModelStore. getModel ( modelId: request. identityModel. modelId) {
62- // The model exists in the store, set it to be the Request's models
62+ // 1. The model exists in the store, so set it to be the Request's models
63+ request. identityModel = identityModel
64+ } else if let identityModel = OSUserExecutor . identityModels [ request. identityModel. modelId] {
65+ // 2. The model exists in the user executor
6366 request. identityModel = identityModel
6467 } else if !request. prepareForExecution ( ) {
65- // The models do not exist AND this request cannot be sent, drop this Request
68+ // 3. The models do not exist AND this request cannot be sent, drop this Request
6669 addRequestQueue. remove ( at: index)
6770 }
6871 }
@@ -75,10 +78,13 @@ class OSIdentityOperationExecutor: OSOperationExecutor {
7578 // Hook each uncached Request to the model in the store
7679 for (index, request) in removeRequestQueue. enumerated ( ) . reversed ( ) {
7780 if let identityModel = OneSignalUserManagerImpl . sharedInstance. identityModelStore. getModel ( modelId: request. identityModel. modelId) {
78- // The model exists in the store, set it to be the Request's model
81+ // 1. The model exists in the store, so set it to be the Request's model
82+ request. identityModel = identityModel
83+ } else if let identityModel = OSUserExecutor . identityModels [ request. identityModel. modelId] {
84+ // 2. The model exists in the user executor
7985 request. identityModel = identityModel
8086 } else if !request. prepareForExecution ( ) {
81- // The model does not exist AND this request cannot be sent, drop this Request
87+ // 3. The model does not exist AND this request cannot be sent, drop this Request
8288 removeRequestQueue. remove ( at: index)
8389 }
8490 }
@@ -158,47 +164,59 @@ class OSIdentityOperationExecutor: OSOperationExecutor {
158164 }
159165
160166 func executeAddAliasesRequest( _ request: OSRequestAddAliases ) {
167+ guard !request. sentToClient else {
168+ return
169+ }
170+ guard request. prepareForExecution ( ) else {
171+ return
172+ }
173+ request. sentToClient = true
174+
161175 OneSignalLog . onesignalLog ( . LL_VERBOSE, message: " OSIdentityOperationExecutor: executeAddAliasesRequest making request: \( request) " )
162176
163177 OneSignalClient . shared ( ) . execute ( request) { _ in
164- // Mock a response
165- // TODO: Is there even a response to hydrate?
166- let response = [ " onesignalId " : UUID ( ) . uuidString, " label01 " : " id01 " ]
167-
168- // On success, remove request from cache, and hydrate model
169- // For example, if app restarts and we read in operations between sending this off and getting the response
178+ // No hydration from response
179+ // On success, remove request from cache
170180 self . addRequestQueue. removeAll ( where: { $0 == request} )
171181 OneSignalUserDefaults . initShared ( ) . saveCodeableData ( forKey: OS_IDENTITY_EXECUTOR_ADD_REQUEST_QUEUE_KEY, withValue: self . addRequestQueue)
172-
173- // instead: modelstore.hydratewithresponse with modelid passed in.. request.modeltoupdate.modelId
174- // store can determine if modelid is same, then hydrate or do nothign
175- request. identityModel. hydrate ( response)
176-
177182 } onFailure: { error in
178- self . addRequestQueue. removeAll ( where: { $0 == request} )
179- OneSignalLog . onesignalLog ( . LL_ERROR, message: error. debugDescription)
183+ // TODO: What happened, maybe alias exists on another user
184+ OneSignalLog . onesignalLog ( . LL_ERROR, message: " OSIdentityOperationExecutor add aliases request failed with error: \( error. debugDescription) " )
185+ // TODO: Differentiate error cases
186+ // If the error is not retryable, remove from cache and queue
187+ if let nsError = error as? NSError ,
188+ nsError. code < 500 && nsError. code != 0 {
189+ self . addRequestQueue. removeAll ( where: { $0 == request} )
190+ OneSignalUserDefaults . initShared ( ) . saveCodeableData ( forKey: OS_IDENTITY_EXECUTOR_ADD_REQUEST_QUEUE_KEY, withValue: self . addRequestQueue)
191+ }
180192 }
181193 }
182194
183195 func executeRemoveAliasRequest( _ request: OSRequestRemoveAlias ) {
196+ guard !request. sentToClient else {
197+ return
198+ }
199+ guard request. prepareForExecution ( ) else {
200+ return
201+ }
202+ request. sentToClient = true
203+
184204 OneSignalLog . onesignalLog ( . LL_VERBOSE, message: " OSIdentityOperationExecutor: executeRemoveAliasRequest making request: \( request) " )
185205
186206 OneSignalClient . shared ( ) . execute ( request) { _ in
187-
188- // Mock a response
189- // TODO: Is there even a response to hydrate?
190- let response = [ " onesignalId " : UUID ( ) . uuidString, " label01 " : " id01 " ]
191-
192- // On success, remove request from cache, and hydrate model
193- // For example, if app restarts and we read in operations between sending this off and getting the response
207+ // There is nothing to hydrate
208+ // On success, remove request from cache
194209 self . removeRequestQueue. removeAll ( where: { $0 == request} )
195210 OneSignalUserDefaults . initShared ( ) . saveCodeableData ( forKey: OS_IDENTITY_EXECUTOR_REMOVE_REQUEST_QUEUE_KEY, withValue: self . removeRequestQueue)
196-
197- request. identityModel. hydrate ( response)
198-
199211 } onFailure: { error in
200- self . removeRequestQueue. removeAll ( where: { $0 == request} )
201- OneSignalLog . onesignalLog ( . LL_ERROR, message: error. debugDescription)
212+ OneSignalLog . onesignalLog ( . LL_ERROR, message: " OSIdentityOperationExecutor remove alias request failed with error: \( error. debugDescription) " )
213+ // TODO: Differentiate error cases
214+ // If the error is not retryable, remove from cache and queue
215+ if let nsError = error as? NSError ,
216+ nsError. code < 500 && nsError. code != 0 {
217+ self . removeRequestQueue. removeAll ( where: { $0 == request} )
218+ OneSignalUserDefaults . initShared ( ) . saveCodeableData ( forKey: OS_IDENTITY_EXECUTOR_REMOVE_REQUEST_QUEUE_KEY, withValue: self . removeRequestQueue)
219+ }
202220 }
203221 }
204222}
0 commit comments