Skip to content

Commit

Permalink
add operation checks to all executors
Browse files Browse the repository at this point in the history
To ensure operations are not silently skipped each executor now checks
if it can process all the operations. This is important since bugs can
go unnoticed for a while and without this it's hard to debug too.
  • Loading branch information
jkasten2 committed Apr 9, 2024
1 parent 7fe2010 commit d0d2b84
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ internal class IdentityOperationExecutor(
override suspend fun execute(operations: List<Operation>): ExecutionResponse {
Logging.debug("IdentityOperationExecutor(operations: $operations)")

if (operations.any { it !is SetAliasOperation && it !is DeleteAliasOperation }) {
throw Exception("Unrecognized operation(s)! Attempted operations:\n$operations")
}

if (operations.any { it is SetAliasOperation } &&
operations.any { it is DeleteAliasOperation }
) {
throw Exception("Can't process SetAliasOperation and DeleteAliasOperation at the same time.")
}

// An alias group is an appId/onesignalId/aliasLabel combo, so we only care
// about the last operation in the group, as that will be the effective end
// state to this specific alias for this user.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ internal class LoginUserFromSubscriptionOperationExecutor(
override suspend fun execute(operations: List<Operation>): ExecutionResponse {
Logging.debug("LoginUserFromSubscriptionOperationExecutor(operation: $operations)")

if (operations.size > 1) {
throw Exception("Only supports one operation! Attempted operations:\n$operations")
}

val startingOp = operations.first()

if (startingOp is LoginUserFromSubscriptionOperation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ internal class LoginUserOperationExecutor(
is TransferSubscriptionOperation -> subscriptions = createSubscriptionsFromOperation(operation, subscriptions)
is UpdateSubscriptionOperation -> subscriptions = createSubscriptionsFromOperation(operation, subscriptions)
is DeleteSubscriptionOperation -> subscriptions = createSubscriptionsFromOperation(operation, subscriptions)
else -> throw Exception("Unrecognized operation: $operation")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ internal class RefreshUserOperationExecutor(
override suspend fun execute(operations: List<Operation>): ExecutionResponse {
Logging.log(LogLevel.DEBUG, "RefreshUserOperationExecutor(operation: $operations)")

if (operations.any { it !is RefreshUserOperation }) {
throw Exception("Unrecognized operation(s)! Attempted operations:\n$operations")
}

val startingOp = operations.first()
if (startingOp is RefreshUserOperation) {
return getUser(startingOp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,17 @@ internal class SubscriptionOperationExecutor(
return if (startingOp is CreateSubscriptionOperation) {
createSubscription(startingOp, operations)
} else if (operations.any { it is DeleteSubscriptionOperation }) {
deleteSubscription(operations.first { it is DeleteSubscriptionOperation } as DeleteSubscriptionOperation)
if (operations.size > 1) {
throw Exception("Only supports one operation! Attempted operations:\n$operations")
}
val deleteSubOps = operations.filterIsInstance<DeleteSubscriptionOperation>()
deleteSubscription(deleteSubOps.first())
} else if (startingOp is UpdateSubscriptionOperation) {
updateSubscription(startingOp, operations)
} else if (startingOp is TransferSubscriptionOperation) {
if (operations.size > 1) {
throw Exception("TransferSubscriptionOperation only supports one operation! Attempted operations:\n$operations")
}
transferSubscription(startingOp)
} else {
throw Exception("Unrecognized operation: $startingOp")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ internal class UpdateUserOperationExecutor(

deltasObject = PropertiesDeltasObject(deltasObject.sessionTime, deltasObject.sessionCount, amountSpent, purchasesArray)
}
else -> throw Exception("Unrecognized operation: $operation")
}
}

Expand Down

0 comments on commit d0d2b84

Please sign in to comment.