show a username when appointment is booked

This commit is contained in:
Eugene Zadyra
2023-03-21 10:28:32 +01:00
parent 535ecaa0cf
commit c1ceb3d7b6
13 changed files with 68 additions and 20 deletions

View File

@@ -0,0 +1,23 @@
databaseChangeLog:
- changeSet:
id: 06
author: dyrkin
preConditions:
onFail: MARK_RAN
not:
columnExists:
tableName: monitoring
columnName: username
changes:
- addColumn:
tableName: monitoring
columns:
- column:
name: username
type: VARCHAR(255)
- addNotNullConstraint:
tableName: monitoring
columnName: username
columnDataType: VARCHAR(255)
defaultNullValue: "you"

View File

@@ -16,4 +16,7 @@ databaseChangeLog:
relativeToChangelogFile: true
- include:
file: changelog/05-drop-bugs-table.yml
relativeToChangelogFile: true
relativeToChangelogFile: true
- include:
file: changelog/06-add-username-to-monitoring.yml
relativeToChangelogFile: true

View File

@@ -51,7 +51,7 @@ class Account(val userId: UserId, bot: Bot, dataService: DataService, val locali
userMaybe.foreach { user =>
user.activeAccountId = accountId
dataService.saveUser(user)
router ! SwitchAccount(UserId(account.userId, account.accountId, userId.source))
router ! SwitchAccount(UserId(account.userId, account.username, account.accountId, userId.source))
bot.sendMessage(userId.source, lang.accountSwitched(account.username))
}
case None =>

View File

@@ -46,8 +46,8 @@ class Auth(
chat = getChat(userId.get)
chat ! cmd
stay()
case Msg(LoggedIn(forwardCommand, uId, aId), _) =>
val id = UserId(uId, aId, source)
case Msg(LoggedIn(forwardCommand, uId, username, aId), _) =>
val id = UserId(uId, username, aId, source)
val cmd = forwardCommand.cmd
userId = Some(id)
chat = getChat(id, reInit = true)
@@ -72,7 +72,7 @@ class Auth(
def getUserId: Option[UserId] = {
val userIdMaybe = dataService.findUserAndAccountIdBySource(source)
userIdMaybe.map { case (uId, aId) => UserId(uId, aId, source) }
userIdMaybe.map { case (uId, username, aId) => UserId(uId, username, aId, source) }
}
beforeDestroy {

View File

@@ -52,10 +52,10 @@ class Login(
goto(requestUsername)
case Right(session) =>
val credentials = dataService.saveCredentials(source, username, password)
userId = UserId(credentials.userId, credentials.accountId, source)
userId = UserId(credentials.userId, credentials.username, credentials.accountId, source)
apiService.addSession(credentials.accountId, session)
bot.sendMessage(source, lang.loginAndPasswordAreOk)
originator ! LoggedIn(forwardCommand, credentials.userId, credentials.accountId)
originator ! LoggedIn(forwardCommand, credentials.userId, credentials.username, credentials.accountId)
end()
}
}
@@ -65,8 +65,8 @@ object Login {
case class ForwardCommand(cmd: Command)
case class UserId(userId: Long, accountId: Long, source: MessageSource)
case class UserId(userId: Long, username: String, accountId: Long, source: MessageSource)
case class LoggedIn(forwardCommand: ForwardCommand, userId: Long, accountId: Long)
case class LoggedIn(forwardCommand: ForwardCommand, userId: Long, username: String, accountId: Long)
}

View File

@@ -300,7 +300,7 @@ object En extends Lang {
|<b>➡</b> Create new monitoring /book""".stripMargin
override def appointmentIsBooked(term: TermExt, monitoring: Monitoring): String =
s"""👍 We just booked an appointment for you!
s"""👍 We just booked an appointment for ${monitoring.username}!
|
|⏱ <b>${formatDateTime(term.term.dateTimeFrom, locale)}</b>
|${capitalize(doctor)}: ${term.term.doctor.firstName} ${term.term.doctor.lastName}

View File

@@ -302,7 +302,7 @@ object Pl extends Lang {
|<b>➡</b> Stwórz nowy monitoring przez /book""".stripMargin
override def appointmentIsBooked(term: TermExt, monitoring: Monitoring): String =
s"""👍 Zarezerwowaliśmy za Ciebie termin!
s"""👍 Zarezerwowaliśmy termin dla ${monitoring.username}!
|
|⏱ <b>${formatDateTime(term.term.dateTimeFrom, locale)}</b>
|${capitalize(doctor)}: ${term.term.doctor.firstName} ${term.term.doctor.lastName}

View File

@@ -300,7 +300,7 @@ object Ua extends Lang {
|<b>➡</b> Створити новий моніторінг /book""".stripMargin
override def appointmentIsBooked(term: TermExt, monitoring: Monitoring): String =
s"""👍 Ми зерезевували візит для вас!
s"""👍 Ми зерезевували візит для ${monitoring.username}!
|
|⏱ <b>${formatDateTime(term.term.dateTimeFrom, locale)}</b>
|${capitalize(doctor)}: ${term.term.doctor.firstName} ${term.term.doctor.lastName}

View File

@@ -265,6 +265,16 @@ class DataRepository(@Autowired em: EntityManager) {
.toSeq
}
def getUsernameById(userId: Long): Option[String] = {
em.createQuery(
"select credentials.username from Credentials credentials where credentials.userId = :userId",
classOf[String]
).setParameter("userId", userId)
.getResultList
.asScala
.headOption
}
def findUserCredentialsByUserIdAndAccountId(userId: Long, accountId: Long): Option[Credentials] = {
em.createQuery(
"""select credentials from Credentials credentials where credentials.userId = :userId

View File

@@ -11,6 +11,10 @@ class Monitoring extends RecordId {
@Column(name = "user_id", nullable = false)
var userId: JLong = _
@BeanProperty
@Column(name = "username", nullable = false)
var username: String = _
@BeanProperty
@Column(name = "account_id", nullable = false)
var accountId: JLong = _
@@ -99,6 +103,7 @@ class Monitoring extends RecordId {
object Monitoring {
def apply(
userId: Long,
username: String,
accountId: Long,
chatId: String,
sourceSystemId: Long,
@@ -123,6 +128,7 @@ object Monitoring {
): Monitoring = {
val monitoring = new Monitoring
monitoring.userId = userId
monitoring.username = username
monitoring.accountId = accountId
monitoring.chatId = chatId
monitoring.sourceSystemId = sourceSystemId

View File

@@ -80,11 +80,12 @@ class DataService {
dataRepository.findSettings(userId)
}
def findUserAndAccountIdBySource(source: MessageSource): Option[(Long, Long)] = {
val userIdMaybe = dataRepository.findUserId(source.chatId, source.sourceSystem.id).map(_.toLong)
userIdMaybe.flatMap(userId =>
dataRepository.findAccountId(userId).map(_.toLong).map(accountId => userId -> accountId)
)
def findUserAndAccountIdBySource(source: MessageSource): Option[(Long, String, Long)] = {
for {
userId <- dataRepository.findUserId(source.chatId, source.sourceSystem.id).map(_.toLong)
accountId <- dataRepository.findAccountId(userId).map(_.toLong)
username <- dataRepository.getUsernameById(userId)
} yield (userId, username, accountId)
}
def findCredentialsByUsername(username: String, userId: Long): Option[Credentials] = {
@@ -95,6 +96,10 @@ class DataService {
dataRepository.getUserCredentials(userId)
}
def getUsernameById(userId: Long): Option[String] = {
dataRepository.getUsernameById(userId)
}
def findUserCredentialsByAccountId(userId: Long, accountId: Long): Option[Credentials] = {
dataRepository.findUserCredentialsByUserIdAndAccountId(userId, accountId)
}

View File

@@ -22,6 +22,7 @@ package object util {
val (userId, bookingData) = data
Monitoring(
userId = userId.userId,
username = userId.username,
accountId = userId.accountId,
chatId = userId.source.chatId,
sourceSystemId = userId.source.sourceSystem.id,

View File

@@ -11,7 +11,7 @@ class AuthSpec extends AkkaTestKit {
"An Auth actor " when {
val source = MessageSource(TelegramMessageSourceSystem, "1")
val userId = UserId(1L, 1L, source)
val userId = UserId(1L, "", 1L, source)
"user is unauthorized" must {
val unauthorizedHelpActor = ConversationTestProbe[UnauthorizedHelp]()
@@ -53,7 +53,7 @@ class AuthSpec extends AkkaTestKit {
"forward initial message to chat actor after the user has logged in" in {
val cmd = Command(source, Message("1", Some("any")))
val msg = LoggedIn(ForwardCommand(cmd), 1L, 1L)
val msg = LoggedIn(ForwardCommand(cmd), 1L, "", 1L)
auth ! msg
chatActor.expectMsg(cmd)
}
@@ -75,7 +75,7 @@ class AuthSpec extends AkkaTestKit {
val loginActorFactory: MessageSourceWithOriginatorTo[Login] = (_, _) => loginActor.conversation
val chatActorFactory: UserIdTo[Chat] = _ => chatActor.conversation
val dataService = mock(classOf[DataService])
when(dataService.findUserAndAccountIdBySource(source)).thenReturn(Some(userId.userId, userId.accountId))
when(dataService.findUserAndAccountIdBySource(source)).thenReturn(Some(userId.userId, "", userId.accountId))
val auth = new Auth(source, dataService, unauthorizedHelpFactory, loginActorFactory, chatActorFactory)(system)