Skip to content

Commit cbd9fd9

Browse files
committed
[User Model] Subscription Already Exists
* Updates to support creating a subscription when the subscription already exists for the owner.
1 parent 16ac66b commit cbd9fd9

File tree

3 files changed

+14
-27
lines changed

3 files changed

+14
-27
lines changed

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/backend/ISubscriptionBackendService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ interface ISubscriptionBackendService {
1414
* @param aliasValue The identifier within the [aliasLabel] that identifies the user to retrieve.
1515
* @param subscription The subscription to create.
1616
*
17-
* @return The ID of the subscription created.
17+
* @return The ID of the subscription created. Or null if the subscription is already part of the current user.
1818
*/
19-
suspend fun createSubscription(appId: String, aliasLabel: String, aliasValue: String, subscription: SubscriptionObject): String
19+
suspend fun createSubscription(appId: String, aliasLabel: String, aliasValue: String, subscription: SubscriptionObject): String?
2020

2121
/**
2222
* Update an existing subscription with the properties provided.

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/backend/impl/SubscriptionBackendService.kt

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,21 @@ internal class SubscriptionBackendService(
1111
private val _httpClient: IHttpClient
1212
) : ISubscriptionBackendService {
1313

14-
override suspend fun createSubscription(appId: String, aliasLabel: String, aliasValue: String, subscription: SubscriptionObject): String {
14+
override suspend fun createSubscription(appId: String, aliasLabel: String, aliasValue: String, subscription: SubscriptionObject): String? {
1515
val requestJSON = JSONObject()
1616
.put("subscription", JSONConverter.convertToJSON(subscription))
1717
.put("retain_previous_owner", true)
1818

1919
val response = _httpClient.post("apps/$appId/users/by/$aliasLabel/$aliasValue/subscriptions", requestJSON)
2020

2121
if (!response.isSuccess) {
22-
// TODO: Temporary work around code until resolved on backend
23-
if (response.statusCode == 500 && response.payload != null) {
24-
val payload = JSONObject(response.payload!!)
25-
if (payload.getBoolean("success") || payload.getJSONObject("subscription").has("id")) {
26-
val responseJSON = JSONObject(response.payload!!)
27-
val subscriptionJSON = responseJSON.safeJSONObject("subscription")
28-
if (subscriptionJSON == null || !subscriptionJSON.has("id")) {
29-
throw BackendException(response.statusCode, response.payload)
30-
}
31-
32-
return subscriptionJSON.getString("id")
33-
}
34-
} else if (response.statusCode == 400 && response.payload != null) {
35-
val errors = JSONObject(response.payload!!)
36-
.getJSONArray("errors")
37-
if (errors.length() > 0 && errors.getJSONObject(0).getString("title").contains("Subscription already belongs to target user")) {
38-
throw BackendException(409, response.payload)
39-
}
40-
}
41-
// TODO: End Temporary work around code until resolved on backend
4222
throw BackendException(response.statusCode, response.payload)
4323
}
4424

4525
val responseJSON = JSONObject(response.payload!!)
4626
val subscriptionJSON = responseJSON.safeJSONObject("subscription")
4727
if (subscriptionJSON == null || !subscriptionJSON.has("id")) {
48-
throw BackendException(response.statusCode, response.payload)
28+
return null
4929
}
5030

5131
return subscriptionJSON.getString("id")

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/operations/impl/executors/SubscriptionOperationExecutor.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,24 @@ internal class SubscriptionOperationExecutor(
9090
IdentityConstants.ONESIGNAL_ID,
9191
createOperation.onesignalId,
9292
subscription
93-
)
93+
) ?: return ExecutionResponse(ExecutionResult.SUCCESS)
9494

9595
// update the subscription model with the new ID, if it's still active.
9696
val subscriptionModel = _subscriptionModelStore.get(createOperation.subscriptionId)
97-
subscriptionModel?.setStringProperty(SubscriptionModel::id.name, backendSubscriptionId, ModelChangeTags.HYDRATE)
97+
subscriptionModel?.setStringProperty(
98+
SubscriptionModel::id.name,
99+
backendSubscriptionId,
100+
ModelChangeTags.HYDRATE
101+
)
98102

99103
if (_configModelStore.model.pushSubscriptionId == createOperation.subscriptionId) {
100104
_configModelStore.model.pushSubscriptionId = backendSubscriptionId
101105
}
102106

103-
return ExecutionResponse(ExecutionResult.SUCCESS, mapOf(createOperation.subscriptionId to backendSubscriptionId))
107+
return ExecutionResponse(
108+
ExecutionResult.SUCCESS,
109+
mapOf(createOperation.subscriptionId to backendSubscriptionId)
110+
)
104111
} catch (ex: BackendException) {
105112
return if (ex.statusCode == 409) {
106113
ExecutionResponse(ExecutionResult.FAIL_NORETRY)

0 commit comments

Comments
 (0)