Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[User Model] Subscription Already Exists #1714

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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