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

Return a record with email instead of failing the request. #219

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
19 changes: 13 additions & 6 deletions src/main/scala/thurloe/database/ThurloeDatabaseConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,22 @@ case object ThurloeDatabaseConnector extends DataAccess with LazyLogging {
} else if (results.size == 1) {
Future.successful(results.head)
} else {
Future.failed(
InvalidDatabaseStateException(
s"Too many results returned from Thurloe's DB (${results.size}) for userId: $userId and key: $key" +
s"\nResults: ${results.map(thurloeRecord => s"KeyValuePair: ${thurloeRecord.userKeyValuePair}")}"
)
)
handleMultipleThurloeRecords(results)
}
} yield result.copy(userKeyValuePair = result.userKeyValuePair.copy(userId = userId))

private def handleMultipleThurloeRecords(results: Seq[UserKeyValuePairWithId]) = {
val recordWithEmail = results.filter(thurloeRecord => thurloeRecord.userKeyValuePair.keyValuePair.key == "email" || thurloeRecord.userKeyValuePair.keyValuePair.key == "contactEmail")
if (recordWithEmail.nonEmpty) {
Future.successful(results.head)
} else {
Future.failed(InvalidDatabaseStateException(
s"Too many results returned from Thurloe's DB (${results.size})" +
s"\nResults: ${results.map(thurloeRecord => s"KeyValuePair: ${thurloeRecord.userKeyValuePair}")}"
))
}
}

def lookup(samDao: SamDAO, userId: String, key: String): Future[UserKeyValuePair] =
lookupIncludingDatabaseId(userId, key, samDao) map {
_.userKeyValuePair
Expand Down