Skip to content

Commit

Permalink
Merge pull request #1714 from OneSignal/user-model/subscription-alrea…
Browse files Browse the repository at this point in the history
…dy-exists

[User Model] Subscription Already Exists
  • Loading branch information
brismithers authored and jinliu9508 committed Feb 6, 2024
2 parents cb0cf3f + d24100b commit f156e92
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ interface ISubscriptionBackendService {
* @param aliasValue The identifier within the [aliasLabel] that identifies the user to retrieve.
* @param subscription The subscription to create.
*
* @return The ID of the subscription created.
* @return The ID of the subscription created. Or null if the subscription is already part of the current user.
*/
suspend fun createSubscription(appId: String, aliasLabel: String, aliasValue: String, subscription: SubscriptionObject): String
suspend fun createSubscription(appId: String, aliasLabel: String, aliasValue: String, subscription: SubscriptionObject): String?

/**
* Update an existing subscription with the properties provided.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,21 @@ internal class SubscriptionBackendService(
private val _httpClient: IHttpClient
) : ISubscriptionBackendService {

override suspend fun createSubscription(appId: String, aliasLabel: String, aliasValue: String, subscription: SubscriptionObject): String {
override suspend fun createSubscription(appId: String, aliasLabel: String, aliasValue: String, subscription: SubscriptionObject): String? {
val requestJSON = JSONObject()
.put("subscription", JSONConverter.convertToJSON(subscription))
.put("retain_previous_owner", true)

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

if (!response.isSuccess) {
// TODO: Temporary work around code until resolved on backend
if (response.statusCode == 500 && response.payload != null) {
val payload = JSONObject(response.payload!!)
if (payload.getBoolean("success") || payload.getJSONObject("subscription").has("id")) {
val responseJSON = JSONObject(response.payload!!)
val subscriptionJSON = responseJSON.safeJSONObject("subscription")
if (subscriptionJSON == null || !subscriptionJSON.has("id")) {
throw BackendException(response.statusCode, response.payload)
}

return subscriptionJSON.getString("id")
}
} else if (response.statusCode == 400 && response.payload != null) {
val errors = JSONObject(response.payload!!)
.getJSONArray("errors")
if (errors.length() > 0 && errors.getJSONObject(0).getString("title").contains("Subscription already belongs to target user")) {
throw BackendException(409, response.payload)
}
}
// TODO: End Temporary work around code until resolved on backend
throw BackendException(response.statusCode, response.payload)
}

val responseJSON = JSONObject(response.payload!!)
val subscriptionJSON = responseJSON.safeJSONObject("subscription")
if (subscriptionJSON == null || !subscriptionJSON.has("id")) {
throw BackendException(response.statusCode, response.payload)
return null
}

return subscriptionJSON.getString("id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,24 @@ internal class SubscriptionOperationExecutor(
IdentityConstants.ONESIGNAL_ID,
createOperation.onesignalId,
subscription
)
) ?: return ExecutionResponse(ExecutionResult.SUCCESS)

// update the subscription model with the new ID, if it's still active.
val subscriptionModel = _subscriptionModelStore.get(createOperation.subscriptionId)
subscriptionModel?.setStringProperty(SubscriptionModel::id.name, backendSubscriptionId, ModelChangeTags.HYDRATE)
subscriptionModel?.setStringProperty(
SubscriptionModel::id.name,
backendSubscriptionId,
ModelChangeTags.HYDRATE
)

if (_configModelStore.model.pushSubscriptionId == createOperation.subscriptionId) {
_configModelStore.model.pushSubscriptionId = backendSubscriptionId
}

return ExecutionResponse(ExecutionResult.SUCCESS, mapOf(createOperation.subscriptionId to backendSubscriptionId))
return ExecutionResponse(
ExecutionResult.SUCCESS,
mapOf(createOperation.subscriptionId to backendSubscriptionId)
)
} catch (ex: BackendException) {
return if (ex.statusCode == 409) {
ExecutionResponse(ExecutionResult.FAIL_NORETRY)
Expand Down

0 comments on commit f156e92

Please sign in to comment.