Skip to content

Commit

Permalink
removing another execute usage
Browse files Browse the repository at this point in the history
  • Loading branch information
robertfmurdock committed Jul 19, 2023
1 parent 562a219 commit 1ca6311
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@ import com.zegreatrob.coupling.model.CouplingConnection
import com.zegreatrob.coupling.model.party.PartyId
import com.zegreatrob.coupling.model.player.Player
import com.zegreatrob.coupling.model.user.CurrentUserProvider
import com.zegreatrob.coupling.server.action.CannonProvider
import com.zegreatrob.coupling.server.action.user.UserIsAuthorizedWithDataAction
import com.zegreatrob.testmints.action.annotation.ActionMint
import com.zegreatrob.testmints.action.async.SuspendActionExecuteSyntax

@ActionMint
data class ConnectPartyUserCommand(val partyId: PartyId, val connectionId: String) {

interface Dispatcher :
UserIsAuthorizedWithDataAction.Dispatcher,
SuspendActionExecuteSyntax,
interface Dispatcher<out D> :
CannonProvider<D>,
CouplingConnectionSaveSyntax,
CouplingConnectionGetSyntax,
CurrentUserProvider {
CurrentUserProvider where D : UserIsAuthorizedWithDataAction.Dispatcher {

suspend fun perform(command: ConnectPartyUserCommand) = with(command) {
partyId.getAuthorizationData()?.let { (_, players) ->
Expand All @@ -28,7 +27,8 @@ data class ConnectPartyUserCommand(val partyId: PartyId, val connectionId: Strin
}
}

private suspend fun PartyId.getAuthorizationData() = execute(UserIsAuthorizedWithDataAction(this)).valueOrNull()
private suspend fun PartyId.getAuthorizationData() = cannon.fire(UserIsAuthorizedWithDataAction(this))
.valueOrNull()

private fun userPlayer(players: List<Player>, email: String): Player {
val existingPlayer = players.find { it.email == email }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.zegreatrob.coupling.server

import com.benasher44.uuid.Uuid
import com.zegreatrob.coupling.action.DispatchingActionExecutor
import com.zegreatrob.coupling.action.TraceIdProvider
import com.zegreatrob.coupling.repository.user.UserRepository
import com.zegreatrob.coupling.server.action.user.FindOrCreateUserActionDispatcher
Expand All @@ -12,7 +11,4 @@ class AuthActionDispatcher internal constructor(
override val traceId: Uuid,
) : TraceIdProvider,
FindOrCreateUserActionDispatcher,
UserRepository by userRepository,
DispatchingActionExecutor<AuthActionDispatcher> {
override val actionDispatcher = this
}
UserRepository by userRepository
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.zegreatrob.coupling.server

import com.benasher44.uuid.Uuid
import com.zegreatrob.coupling.action.DispatchingActionExecutor
import com.zegreatrob.coupling.action.LoggingActionPipe
import com.zegreatrob.coupling.action.TraceIdProvider
import com.zegreatrob.coupling.action.pairassignmentdocument.AssignPinsAction
Expand Down Expand Up @@ -44,6 +43,7 @@ import com.zegreatrob.coupling.server.action.player.ServerSavePlayerCommandDispa
import com.zegreatrob.coupling.server.action.secret.SecretListQuery
import com.zegreatrob.coupling.server.action.slack.ServerGrantSlackAccessCommandDispatcher
import com.zegreatrob.coupling.server.action.slack.SlackRepository
import com.zegreatrob.coupling.server.action.user.UserIsAuthorizedWithDataAction
import com.zegreatrob.coupling.server.action.user.UserQuery
import com.zegreatrob.coupling.server.entity.pairassignment.PairAssignmentDispatcher
import com.zegreatrob.coupling.server.entity.party.PartyDispatcher
Expand All @@ -60,26 +60,26 @@ import kotlinx.coroutines.async
import kotlin.js.json

interface ICommandDispatcher :
TraceIdProvider,
AwsManagementApiSyntax,
AwsSocketCommunicator,
ConnectPartyUserCommand.Dispatcher,
ConnectionsQuery.Dispatcher,
CurrentPairAssignmentDocumentQuery.Dispatcher,
DisconnectPartyUserCommand.Dispatcher,
GlobalStatsQuery.Dispatcher,
PairAssignmentDocumentListQuery.Dispatcher,
PartyDispatcher,
PinsQuery.Dispatcher,
PartyIntegrationQuery.Dispatcher,
PinsQuery.Dispatcher,
PlayersQuery.Dispatcher,
ReportDocCommand.Dispatcher,
RepositoryCatalog,
RetiredPlayersQuery.Dispatcher,
ServerGrantSlackAccessCommandDispatcher,
ScopeSyntax,
SecretListQuery.Dispatcher,
ServerGrantSlackAccessCommandDispatcher,
TraceIdProvider,
UserDispatcher,
UserIsAuthorizedWithDataAction.Dispatcher,
UserQuery.Dispatcher

class CommandDispatcher(
Expand All @@ -91,7 +91,8 @@ class CommandDispatcher(
) : ICommandDispatcher,
RepositoryCatalog by repositoryCatalog,
TraceIdProvider,
BroadcastAction.Dispatcher<ICommandDispatcher> {
BroadcastAction.Dispatcher<ICommandDispatcher>,
ConnectPartyUserCommand.Dispatcher<ICommandDispatcher> {
override val cannon: ActionCannon<ICommandDispatcher> = ActionCannon(this, LoggingActionPipe(traceId))

override val slackRepository: SlackRepository by lazy { FetchSlackRepository() }
Expand Down Expand Up @@ -124,7 +125,6 @@ class CurrentPartyDispatcher(
private val commandDispatcher: CommandDispatcher,
) :
ICommandDispatcher by commandDispatcher,
DispatchingActionExecutor<CurrentPartyDispatcher>,
CreatePairCandidateReportListAction.Dispatcher<CurrentPartyDispatcher>,
ShufflePairsAction.Dispatcher<CurrentPartyDispatcher>,
AssignPinsAction.Dispatcher,
Expand All @@ -147,7 +147,6 @@ class CurrentPartyDispatcher(
override val userId: String get() = commandDispatcher.userId
override val cannon: ActionCannon<CurrentPartyDispatcher> = ActionCannon(this, LoggingActionPipe(traceId))
suspend fun isAuthorized() = currentPartyId.validateAuthorized() != null
override val actionDispatcher = this
private suspend fun PartyId.validateAuthorized() = if (userIsAuthorized(this)) this else null

private suspend fun userIsAuthorized(partyId: PartyId) = currentUser.authorizedPartyIds.contains(partyId) ||
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.zegreatrob.coupling.server.express.route

import com.benasher44.uuid.uuid4
import com.benasher44.uuid.Uuid
import com.zegreatrob.coupling.action.LoggingActionPipe
import com.zegreatrob.coupling.action.valueOrNull
import com.zegreatrob.coupling.model.elements
import com.zegreatrob.coupling.model.party.PartyId
Expand All @@ -12,6 +13,7 @@ import com.zegreatrob.coupling.server.express.async
import com.zegreatrob.coupling.server.external.express.Handler
import com.zegreatrob.coupling.server.external.express.Request
import com.zegreatrob.coupling.server.secretRepository
import com.zegreatrob.testmints.action.ActionCannon

fun userLoadingMiddleware(): Handler = { request, _, next ->
val auth = request.auth
Expand All @@ -30,15 +32,20 @@ fun userLoadingMiddleware(): Handler = { request, _, next ->
null
}
} else {
UserDataService.authActionDispatcher("$userEmail", uuid4())
.invoke(FindOrCreateUserAction)
authCannon(userEmail, request.traceId)
.fire(FindOrCreateUserAction)
.valueOrNull()
}
.let(request::setUser)
}
}
}

private suspend fun authCannon(userEmail: Any?, traceId: Uuid) = ActionCannon(
UserDataService.authActionDispatcher("$userEmail", traceId),
LoggingActionPipe(traceId),
)

private suspend fun secretIsNotDeleted(secretId: String, partyId: PartyId): Boolean = secretRepository(secretId)
.getSecrets(partyId)
.elements
Expand Down

0 comments on commit 1ca6311

Please sign in to comment.