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

Updated signUp logic to include username check in NameTransaction table #374

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions app/constants/Module.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ object Module {
val BLOCKCHAIN_TRANSACTION_MINT = "BLOCKCHAIN_TRANSACTION_MINT"
val BLOCKCHAIN_TRANSACTION_NUB = "BLOCKCHAIN_TRANSACTION_NUB"
val BLOCKCHAIN_TRANSACTION_BURN = "BLOCKCHAIN_TRANSACTION_BURN"
val BLOCKCHAIN_NAME_TRANSACTION = "BLOCKCHAIN_NAME_TRANSACTION"

val MASTER_TRANSACTION_SESSION_TOKEN = "MASTER_TRANSACTION_SESSION_TOKEN"
val MASTER_TRANSACTION_PUSH_NOTIFICATION_TOKEN = "MASTER_TRANSACTION_PUSH_NOTIFICATION_TOKEN"
Expand Down
3 changes: 3 additions & 0 deletions app/constants/Response.scala
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ object Response {
val NFT_ALREADY_SOLD = new Failure("NFT_ALREADY_SOLD")
val SIGNING_FAILED = new Failure("SIGNING_FAILED")
val TRANSACTION_NOT_FOUND = new Failure("TRANSACTION_NOT_FOUND")
val NAME_TRANSACTION_INSERT_FAILED = new Failure("NAME_TRANSACTION_INSERT_FAILED")
val NAME_TRANSACTION_UPSERT_FAILED = new Failure("NAME_TRANSACTION_INSERT_FAILED")
val NAME_TRANSACTION_NOT_FOUND = new Failure("NAME_TRANSACTION_NOT_FOUND")

class Failure(private val response: String) {
val message: String = PREFIX + FAILURE_PREFIX + response
Expand Down
8 changes: 6 additions & 2 deletions app/controllers/AccountController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class AccountController @Inject()(
masterKeys: master.Keys,
masterTransactionSessionTokens: masterTransaction.SessionTokens,
masterTransactionPushNotificationTokens: masterTransaction.PushNotificationTokens,
nameTransactions: masterTransaction.NameTransactions,
withUsernameToken: WithUsernameToken,
withLoginActionAsync: WithLoginActionAsync,
utilitiesNotification: utilities.Notification,
Expand Down Expand Up @@ -282,11 +283,14 @@ class AccountController @Inject()(
}

def checkUsernameAvailable(username: String): Action[AnyContent] = withoutLoginActionAsync { implicit loginState =>
implicit request =>
implicit request => {
val verifiedMnemonicExists = masterKeys.Service.checkVerifiedKeyExists(username)
val usernameExists = nameTransactions.Service.findByUsername(username)
for {
verifiedMnemonicExists <- verifiedMnemonicExists
} yield if (!verifiedMnemonicExists) Ok else NoContent
usernameExists <- usernameExists
} yield if (!verifiedMnemonicExists && usernameExists.isEmpty) Ok else NoContent
}
}

def forgetPasswordForm(): Action[AnyContent] = withoutLoginAction { implicit request =>
Expand Down
105 changes: 105 additions & 0 deletions app/models/masterTransaction/NameTransaction.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package models.masterTransaction

import play.api.Logger
import play.api.db.slick.DatabaseConfigProvider
import slick.jdbc.JdbcProfile

import javax.inject.{Inject, Singleton}
import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success}
import exceptions.BaseException
import org.postgresql.util.PSQLException
import play.db.NamedDatabase

case class NameTransaction(username: String, email: Option[String], idString: String, isClaimed: Boolean)

@Singleton
class NameTransactions @Inject()(
@NamedDatabase("explorer")
protected val databaseConfigProvider: DatabaseConfigProvider
)(implicit executionContext: ExecutionContext) {

val databaseConfig = databaseConfigProvider.get[JdbcProfile]
val db = databaseConfig.db

private implicit val logger: Logger = Logger(this.getClass)
private implicit val module: String = constants.Module.BLOCKCHAIN_NAME_TRANSACTION

import databaseConfig.profile.api._

private[models] val nameTransactionTable = TableQuery[NameTransactionTable]

case class NameTransactionSerialized(username: String, email: Option[String], idString: String, isClaimed: Boolean) {
def deserialize: NameTransaction = NameTransaction(username = username, email = email, idString = idString, isClaimed = isClaimed)
}

def serialize(nameTransaction: NameTransaction): NameTransactionSerialized =
NameTransactionSerialized(
username = nameTransaction.username,
email = nameTransaction.email,
idString = nameTransaction.idString,
isClaimed = nameTransaction.isClaimed
)

// Insert new NameTransaction
def add(nameTransaction: NameTransaction): Future[String] = db.run((nameTransactionTable returning nameTransactionTable.map(_.idString) += serialize(nameTransaction)).asTry).map {
case Success(result) => result
case Failure(exception) => exception match {
case psqlException: PSQLException => throw new BaseException(constants.Response.NAME_TRANSACTION_INSERT_FAILED, psqlException)
}
}

// Upsert (insert or update) NameTransaction
def upsert(nameTransaction: NameTransaction): Future[Int] = db.run(nameTransactionTable.insertOrUpdate(serialize(nameTransaction)).asTry).map {
case Success(result) => result
case Failure(exception) => exception match {
case psqlException: PSQLException => throw new BaseException(constants.Response.NAME_TRANSACTION_UPSERT_FAILED, psqlException)
}
}

// Fetch by idString
private def tryGetByIdString(idString: String): Future[NameTransactionSerialized] = db.run(nameTransactionTable.filter(_.idString === idString).result.head.asTry).map {
case Success(result) => result
case Failure(exception) => exception match {
case noSuchElementException: NoSuchElementException => throw new BaseException(constants.Response.NAME_TRANSACTION_NOT_FOUND, noSuchElementException)
}
}

// Fetch by username
private def tryGetByUsername(username: String): Future[NameTransactionSerialized] = db.run(nameTransactionTable.filter(_.username === username).result.head.asTry).map {
case Success(result) => result
case Failure(exception) => exception match {
case noSuchElementException: NoSuchElementException => throw new BaseException(constants.Response.NAME_TRANSACTION_NOT_FOUND, noSuchElementException)
}
}

// Get NameTransaction by idString
def getByIdString(idString: String): Future[Option[NameTransactionSerialized]] = db.run(nameTransactionTable.filter(_.idString === idString).result.headOption)

// Get NameTransaction by username
def getByUsername(username: String): Future[Option[NameTransactionSerialized]] = db.run(nameTransactionTable.filter(_.username === username).result.headOption)

private[models] class NameTransactionTable(tag: Tag) extends Table[NameTransactionSerialized](tag, "NameTransaction") {

def * = (username, email, idString, isClaimed) <> (NameTransactionSerialized.tupled, NameTransactionSerialized.unapply)

def username = column[String]("username")
def email = column[Option[String]]("email")
def idString = column[String]("idString", O.PrimaryKey)
def isClaimed = column[Boolean]("isClaimed")
}

// Service to provide additional functionality
object Service {
def tryGet(idString: String): Future[NameTransaction] = tryGetByIdString(idString).map(_.deserialize)

def tryFindByUsername(username: String): Future[NameTransaction] = tryGetByUsername(username).map(_.deserialize)

def insertOrUpdate(nameTransaction: NameTransaction): Future[Int] = upsert(nameTransaction)

def get(idString: String): Future[Option[NameTransaction]] = getByIdString(idString).map(_.map(_.deserialize))

def findByUsername(username: String): Future[Option[NameTransaction]] = getByUsername(username).map(_.map(_.deserialize))
}

}
10 changes: 10 additions & 0 deletions conf/evolutions/default/1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ CREATE TABLE IF NOT EXISTS MASTER_TRANSACTION."SessionToken"
PRIMARY KEY ("accountId")
);

CREATE TABLE IF NOT EXISTS MASTER_TRANSACTION."NameTransaction"
(
"username" VARCHAR NOT NULL,
"email" VARCHAR,
"idString" VARCHAR NOT NULL,
"isClaimed" BOOLEAN NOT NULL,
PRIMARY KEY ("username", "idString")
);

ALTER TABLE MASTER."Wallet"
ADD CONSTRAINT Wallet_Account_Id FOREIGN KEY ("accountId") REFERENCES MASTER."Account" ("id");
ALTER TABLE MASTER."NFT"
Expand Down Expand Up @@ -208,6 +217,7 @@ DROP TABLE IF EXISTS MASTER."Wallet" CASCADE;

DROP TABLE IF EXISTS MASTER_TRANSACTION."PushNotificationToken" CASCADE;
DROP TABLE IF EXISTS MASTER_TRANSACTION."SessionToken" CASCADE;
DROP TABLE IF EXISTS MASTER_TRANSACTION."NameTransaction" CASCADE;

DROP SCHEMA IF EXISTS MASTER CASCADE;
DROP SCHEMA IF EXISTS MASTER_TRANSACTION CASCADE;
Loading