-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ATL-6833 integrate ZIO failures and defects in wallet event con…
…troller (#1186) Signed-off-by: Benjamin Voiturier <benjamin.voiturier@iohk.io>
- Loading branch information
1 parent
628f2f0
commit 8bc2018
Showing
21 changed files
with
195 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 39 additions & 33 deletions
72
.../main/scala/org/hyperledger/identus/agent/walletapi/service/WalletManagementService.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,71 @@ | ||
package org.hyperledger.identus.agent.walletapi.service | ||
|
||
import org.hyperledger.identus.agent.walletapi.model.{Wallet, WalletSeed} | ||
import org.hyperledger.identus.agent.walletapi.storage.WalletNonSecretStorageError | ||
import org.hyperledger.identus.agent.walletapi.service.WalletManagementServiceError.{ | ||
DuplicatedWalletId, | ||
DuplicatedWalletSeed, | ||
TooManyPermittedWallet, | ||
TooManyWebhookError | ||
} | ||
import org.hyperledger.identus.event.notification.EventNotificationConfig | ||
import org.hyperledger.identus.shared.models.{WalletAccessContext, WalletAdministrationContext, WalletId} | ||
import org.hyperledger.identus.shared.models.* | ||
import zio.* | ||
|
||
import java.util.UUID | ||
import scala.language.implicitConversions | ||
|
||
sealed trait WalletManagementServiceError { | ||
final def toThrowable: Throwable = this | ||
sealed trait WalletManagementServiceError( | ||
val statusCode: StatusCode, | ||
val userFacingMessage: String | ||
) extends Failure { | ||
override val namespace: String = "WalletManagementServiceError" | ||
} | ||
|
||
object WalletManagementServiceError { | ||
final case class UnexpectedStorageError(cause: Throwable) extends WalletManagementServiceError | ||
final case class TooManyWebhookError(limit: Int, actual: Int) extends WalletManagementServiceError | ||
final case class DuplicatedWalletId(id: WalletId) extends WalletManagementServiceError | ||
final case class DuplicatedWalletSeed(id: WalletId) extends WalletManagementServiceError | ||
final case class TooManyPermittedWallet() extends WalletManagementServiceError | ||
|
||
given Conversion[WalletNonSecretStorageError, WalletManagementServiceError] = { | ||
case WalletNonSecretStorageError.TooManyWebhook(limit, actual) => TooManyWebhookError(limit, actual) | ||
case WalletNonSecretStorageError.DuplicatedWalletId(id) => DuplicatedWalletId(id) | ||
case WalletNonSecretStorageError.DuplicatedWalletSeed(id) => DuplicatedWalletSeed(id) | ||
case WalletNonSecretStorageError.UnexpectedError(cause) => UnexpectedStorageError(cause) | ||
} | ||
|
||
given Conversion[WalletManagementServiceError, Throwable] = { | ||
case UnexpectedStorageError(cause) => Exception(cause) | ||
case TooManyWebhookError(limit, actual) => | ||
Exception(s"Too many webhook created for a wallet. Limit $limit, Actual $actual.") | ||
case DuplicatedWalletId(id) => Exception(s"Duplicated wallet id: $id") | ||
case DuplicatedWalletSeed(id) => Exception(s"Duplicated wallet seed for wallet id: $id") | ||
case TooManyPermittedWallet() => | ||
Exception(s"The operation is not allowed because wallet access already exists for the current user.") | ||
} | ||
final case class TooManyWebhookError(walletId: WalletId, limit: Int) | ||
extends WalletManagementServiceError( | ||
StatusCode.UnprocessableContent, | ||
s"The maximum number of webhooks has been reached for the wallet: walletId=$walletId, limit=$limit" | ||
) | ||
|
||
final case class DuplicatedWalletId(walletId: WalletId) | ||
extends WalletManagementServiceError( | ||
StatusCode.UnprocessableContent, | ||
s"A wallet with the same ID already exist: walletId=$walletId" | ||
) | ||
final case class DuplicatedWalletSeed() | ||
extends WalletManagementServiceError( | ||
StatusCode.UnprocessableContent, | ||
s"A wallet with the same seed already exist" | ||
) | ||
final case class TooManyPermittedWallet() | ||
extends WalletManagementServiceError( | ||
StatusCode.BadRequest, | ||
s"The operation is not allowed because wallet access already exists for the current user" | ||
) | ||
} | ||
|
||
trait WalletManagementService { | ||
def createWallet( | ||
wallet: Wallet, | ||
seed: Option[WalletSeed] = None | ||
): ZIO[WalletAdministrationContext, WalletManagementServiceError, Wallet] | ||
): ZIO[WalletAdministrationContext, TooManyPermittedWallet | DuplicatedWalletId | DuplicatedWalletSeed, Wallet] | ||
|
||
def getWallet(walletId: WalletId): ZIO[WalletAdministrationContext, WalletManagementServiceError, Option[Wallet]] | ||
def findWallet(walletId: WalletId): URIO[WalletAdministrationContext, Option[Wallet]] | ||
|
||
def getWallets(walletIds: Seq[WalletId]): ZIO[WalletAdministrationContext, WalletManagementServiceError, Seq[Wallet]] | ||
def getWallets(walletIds: Seq[WalletId]): URIO[WalletAdministrationContext, Seq[Wallet]] | ||
|
||
/** @return A tuple containing a list of items and a count of total items */ | ||
def listWallets( | ||
offset: Option[Int] = None, | ||
limit: Option[Int] = None | ||
): ZIO[WalletAdministrationContext, WalletManagementServiceError, (Seq[Wallet], Int)] | ||
): URIO[WalletAdministrationContext, (Seq[Wallet], Int)] | ||
|
||
def listWalletNotifications: ZIO[WalletAccessContext, WalletManagementServiceError, Seq[EventNotificationConfig]] | ||
def listWalletNotifications: URIO[WalletAccessContext, Seq[EventNotificationConfig]] | ||
|
||
def createWalletNotification( | ||
config: EventNotificationConfig | ||
): ZIO[WalletAccessContext, WalletManagementServiceError, EventNotificationConfig] | ||
): ZIO[WalletAccessContext, TooManyWebhookError, Unit] | ||
|
||
def deleteWalletNotification(id: UUID): ZIO[WalletAccessContext, WalletManagementServiceError, Unit] | ||
def deleteWalletNotification(id: UUID): URIO[WalletAccessContext, Unit] | ||
} |
Oops, something went wrong.