mirror of
https://github.com/dyrkin/luxmed-bot.git
synced 2025-12-21 13:23:05 +01:00
Ask user to update term if service is already booked
This commit is contained in:
@@ -145,8 +145,10 @@ class Book(val userId: UserId, bot: Bot, apiService: ApiService, dataService: Da
|
||||
val response = apiService.temporaryReservation(userId.accountId, term.mapTo[TemporaryReservationRequest], term.mapTo[ValuationsRequest])
|
||||
response match {
|
||||
case Left(ex) =>
|
||||
bot.sendMessage(userId.source, ex.getMessage)
|
||||
end()
|
||||
warn(s"Service [${bookingData.serviceId.name}] is already booked. Ask to update term", ex)
|
||||
bot.sendMessage(userId.source, lang.visitAlreadyExists,
|
||||
inlineKeyboard = createInlineKeyboard(Seq(Button(lang.yes, Tags.RebookYes), Button(lang.no, Tags.RebookNo)), columns = 1))
|
||||
goto(awaitRebookDecision) using bookingData.copy(term = Some(term))
|
||||
case Right((temporaryReservation, valuations)) =>
|
||||
bot.sendMessage(userId.source, lang.confirmAppointment(term, valuations),
|
||||
inlineKeyboard = createInlineKeyboard(Seq(Button(lang.cancel, Tags.Cancel), Button(lang.book, Tags.Book))))
|
||||
@@ -168,6 +170,23 @@ class Book(val userId: UserId, bot: Bot, apiService: ApiService, dataService: Da
|
||||
goto(askMonitoringAutobookOption)
|
||||
}
|
||||
|
||||
private def awaitRebookDecision: Step =
|
||||
monologue {
|
||||
case Msg(CallbackCommand(Tags.RebookYes), bookingData: BookingData) =>
|
||||
apiService.updateReservedVisit(userId.accountId, bookingData.term.get) match {
|
||||
case Right(success) =>
|
||||
debug(s"Successfully confirmed: $success")
|
||||
bot.sendMessage(userId.source, lang.appointmentIsConfirmed)
|
||||
case Left(ex) =>
|
||||
error("Error during reservation", ex)
|
||||
bot.sendMessage(userId.source, ex.getMessage)
|
||||
}
|
||||
end()
|
||||
case Msg(CallbackCommand(Tags.RebookNo), _) =>
|
||||
info("User doesn't want to change term")
|
||||
end()
|
||||
}
|
||||
|
||||
private def awaitReservation: Step =
|
||||
monologue {
|
||||
case Msg(CallbackCommand(Tags.Cancel), bookingData: BookingData) =>
|
||||
@@ -207,7 +226,7 @@ class Book(val userId: UserId, bot: Bot, apiService: ApiService, dataService: Da
|
||||
} onReply {
|
||||
case Msg(CallbackCommand(BooleanString(autobook)), bookingData: BookingData) =>
|
||||
val data = bookingData.copy(autobook = autobook)
|
||||
if(autobook) goto(askMonitoringRebookOption) using data
|
||||
if (autobook) goto(askMonitoringRebookOption) using data
|
||||
else goto(createMonitoring) using data
|
||||
}
|
||||
|
||||
|
||||
@@ -97,6 +97,8 @@ object En extends Lang {
|
||||
|
||||
override def rebookIfExists: String = "<b>➡</b> Do you want to update term if reservation already exists?"
|
||||
|
||||
override def visitAlreadyExists: String = "<b>➡</b> The same service is already booked. Do you want to update term?"
|
||||
|
||||
override def city: String = "city"
|
||||
|
||||
override def clinic: String = "clinic"
|
||||
|
||||
@@ -87,6 +87,8 @@ trait Lang {
|
||||
|
||||
def rebookIfExists: String
|
||||
|
||||
def visitAlreadyExists: String
|
||||
|
||||
def city: String
|
||||
|
||||
def clinic: String
|
||||
|
||||
@@ -97,6 +97,8 @@ object Ua extends Lang {
|
||||
|
||||
override def rebookIfExists: String = "<b>➡</b> Чи хотіли би ви змінити термін в разі, якщо резервація вже існує?"
|
||||
|
||||
override def visitAlreadyExists: String = "<b>➡</b> Резервація для такого сервісу вже існує. Чі хотіли би ви змінити термін?"
|
||||
|
||||
override def city: String = "місто"
|
||||
|
||||
override def clinic: String = "клініка"
|
||||
|
||||
@@ -138,20 +138,29 @@ class ApiService extends SessionSupport {
|
||||
LuxmedApi.changeTerm(session.accessToken, session.tokenType, reservationId, reservationRequest)
|
||||
}
|
||||
|
||||
def updateTerm(accountId: Long, reservationId: Long, term: AvailableVisitsTermPresentation): Either[Throwable, ChangeTermResponse] = {
|
||||
val temporaryReservationRequest = term.mapTo[TemporaryReservationRequest]
|
||||
val valuationsRequest = term.mapTo[ValuationsRequest]
|
||||
val canTermBeChangedResponse = canTermBeChanged(accountId, reservationId)
|
||||
if (canTermBeChangedResponse.exists(_.code == 204)) {
|
||||
for {
|
||||
okResponse <- temporaryReservationToChangeTerm(accountId, reservationId, temporaryReservationRequest, valuationsRequest)
|
||||
(temporaryReservation, valuations) = okResponse
|
||||
temporaryReservationId = temporaryReservation.id
|
||||
visitTermVariant = valuations.visitTermVariants.head
|
||||
reservationRequest = (temporaryReservationId, visitTermVariant, term).mapTo[ReservationRequest]
|
||||
reservation <- changeTerm(accountId, reservationId, reservationRequest)
|
||||
} yield reservation
|
||||
} else Left(new RuntimeException(s"Term for reservation [$reservationId] can't be changed"))
|
||||
def updateReservedVisit(accountId: Long, term: AvailableVisitsTermPresentation): Either[Throwable, ChangeTermResponse] = {
|
||||
val reservedVisitEither = reservedVisits(accountId, toDate = ZonedDateTime.now().plusMonths(6)).map(_.find(_.service.id == term.serviceId))
|
||||
reservedVisitEither match {
|
||||
case Right(Some(reservedVisit: ReservedVisit)) =>
|
||||
val reservationId = reservedVisit.reservationId
|
||||
val temporaryReservationRequest = term.mapTo[TemporaryReservationRequest]
|
||||
val valuationsRequest = term.mapTo[ValuationsRequest]
|
||||
val canTermBeChangedResponse = canTermBeChanged(accountId, reservationId)
|
||||
if (canTermBeChangedResponse.exists(_.code == 204)) {
|
||||
for {
|
||||
okResponse <- temporaryReservationToChangeTerm(accountId, reservationId, temporaryReservationRequest, valuationsRequest)
|
||||
(temporaryReservation, valuations) = okResponse
|
||||
temporaryReservationId = temporaryReservation.id
|
||||
visitTermVariant = valuations.visitTermVariants.head
|
||||
reservationRequest = (temporaryReservationId, visitTermVariant, term).mapTo[ReservationRequest]
|
||||
reservation <- changeTerm(accountId, reservationId, reservationRequest)
|
||||
} yield reservation
|
||||
} else left(s"Term for reservation [$reservationId] can't be changed")
|
||||
case Left(ex) =>
|
||||
Left(ex)
|
||||
case _ =>
|
||||
left(s"Existing reservation for service [${term.serviceId}] not found. Nothing to update")
|
||||
}
|
||||
}
|
||||
|
||||
def visitsHistory(accountId: Long, fromDate: ZonedDateTime = ZonedDateTime.now().minusYears(1),
|
||||
@@ -175,4 +184,6 @@ class ApiService extends SessionSupport {
|
||||
LuxmedApi.login(username, textEncryptor.decrypt(password))
|
||||
}
|
||||
|
||||
private def left(msg: String) = Left(new RuntimeException(msg))
|
||||
|
||||
}
|
||||
|
||||
@@ -149,11 +149,7 @@ class MonitoringService extends Logger {
|
||||
apiService.reserveVisit(monitoring.accountId, term).toTry.recoverWith {
|
||||
case _: ServiceIsAlreadyBookedException if monitoring.rebookIfExists =>
|
||||
info(s"Service [${monitoring.serviceName}] is already booked. Trying to update term")
|
||||
val reservation = apiService.reservedVisits(monitoring.accountId, toDate = ZonedDateTime.now().plusMonths(6)).map(_.head)
|
||||
reservation.toTry.flatMap { r =>
|
||||
val reservationId = r.reservationId
|
||||
apiService.updateTerm(monitoring.accountId, reservationId, term).toTry
|
||||
}
|
||||
apiService.updateReservedVisit(monitoring.accountId, term).toTry
|
||||
case ex => Failure(ex)
|
||||
}.toEither match {
|
||||
case Right(_) =>
|
||||
|
||||
Reference in New Issue
Block a user