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])
|
val response = apiService.temporaryReservation(userId.accountId, term.mapTo[TemporaryReservationRequest], term.mapTo[ValuationsRequest])
|
||||||
response match {
|
response match {
|
||||||
case Left(ex) =>
|
case Left(ex) =>
|
||||||
bot.sendMessage(userId.source, ex.getMessage)
|
warn(s"Service [${bookingData.serviceId.name}] is already booked. Ask to update term", ex)
|
||||||
end()
|
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)) =>
|
case Right((temporaryReservation, valuations)) =>
|
||||||
bot.sendMessage(userId.source, lang.confirmAppointment(term, valuations),
|
bot.sendMessage(userId.source, lang.confirmAppointment(term, valuations),
|
||||||
inlineKeyboard = createInlineKeyboard(Seq(Button(lang.cancel, Tags.Cancel), Button(lang.book, Tags.Book))))
|
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)
|
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 =
|
private def awaitReservation: Step =
|
||||||
monologue {
|
monologue {
|
||||||
case Msg(CallbackCommand(Tags.Cancel), bookingData: BookingData) =>
|
case Msg(CallbackCommand(Tags.Cancel), bookingData: BookingData) =>
|
||||||
@@ -207,7 +226,7 @@ class Book(val userId: UserId, bot: Bot, apiService: ApiService, dataService: Da
|
|||||||
} onReply {
|
} onReply {
|
||||||
case Msg(CallbackCommand(BooleanString(autobook)), bookingData: BookingData) =>
|
case Msg(CallbackCommand(BooleanString(autobook)), bookingData: BookingData) =>
|
||||||
val data = bookingData.copy(autobook = autobook)
|
val data = bookingData.copy(autobook = autobook)
|
||||||
if(autobook) goto(askMonitoringRebookOption) using data
|
if (autobook) goto(askMonitoringRebookOption) using data
|
||||||
else goto(createMonitoring) 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 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 city: String = "city"
|
||||||
|
|
||||||
override def clinic: String = "clinic"
|
override def clinic: String = "clinic"
|
||||||
|
|||||||
@@ -87,6 +87,8 @@ trait Lang {
|
|||||||
|
|
||||||
def rebookIfExists: String
|
def rebookIfExists: String
|
||||||
|
|
||||||
|
def visitAlreadyExists: String
|
||||||
|
|
||||||
def city: String
|
def city: String
|
||||||
|
|
||||||
def clinic: String
|
def clinic: String
|
||||||
|
|||||||
@@ -97,6 +97,8 @@ object Ua extends Lang {
|
|||||||
|
|
||||||
override def rebookIfExists: String = "<b>➡</b> Чи хотіли би ви змінити термін в разі, якщо резервація вже існує?"
|
override def rebookIfExists: String = "<b>➡</b> Чи хотіли би ви змінити термін в разі, якщо резервація вже існує?"
|
||||||
|
|
||||||
|
override def visitAlreadyExists: String = "<b>➡</b> Резервація для такого сервісу вже існує. Чі хотіли би ви змінити термін?"
|
||||||
|
|
||||||
override def city: String = "місто"
|
override def city: String = "місто"
|
||||||
|
|
||||||
override def clinic: String = "клініка"
|
override def clinic: String = "клініка"
|
||||||
|
|||||||
@@ -138,20 +138,29 @@ class ApiService extends SessionSupport {
|
|||||||
LuxmedApi.changeTerm(session.accessToken, session.tokenType, reservationId, reservationRequest)
|
LuxmedApi.changeTerm(session.accessToken, session.tokenType, reservationId, reservationRequest)
|
||||||
}
|
}
|
||||||
|
|
||||||
def updateTerm(accountId: Long, reservationId: Long, term: AvailableVisitsTermPresentation): Either[Throwable, ChangeTermResponse] = {
|
def updateReservedVisit(accountId: Long, term: AvailableVisitsTermPresentation): Either[Throwable, ChangeTermResponse] = {
|
||||||
val temporaryReservationRequest = term.mapTo[TemporaryReservationRequest]
|
val reservedVisitEither = reservedVisits(accountId, toDate = ZonedDateTime.now().plusMonths(6)).map(_.find(_.service.id == term.serviceId))
|
||||||
val valuationsRequest = term.mapTo[ValuationsRequest]
|
reservedVisitEither match {
|
||||||
val canTermBeChangedResponse = canTermBeChanged(accountId, reservationId)
|
case Right(Some(reservedVisit: ReservedVisit)) =>
|
||||||
if (canTermBeChangedResponse.exists(_.code == 204)) {
|
val reservationId = reservedVisit.reservationId
|
||||||
for {
|
val temporaryReservationRequest = term.mapTo[TemporaryReservationRequest]
|
||||||
okResponse <- temporaryReservationToChangeTerm(accountId, reservationId, temporaryReservationRequest, valuationsRequest)
|
val valuationsRequest = term.mapTo[ValuationsRequest]
|
||||||
(temporaryReservation, valuations) = okResponse
|
val canTermBeChangedResponse = canTermBeChanged(accountId, reservationId)
|
||||||
temporaryReservationId = temporaryReservation.id
|
if (canTermBeChangedResponse.exists(_.code == 204)) {
|
||||||
visitTermVariant = valuations.visitTermVariants.head
|
for {
|
||||||
reservationRequest = (temporaryReservationId, visitTermVariant, term).mapTo[ReservationRequest]
|
okResponse <- temporaryReservationToChangeTerm(accountId, reservationId, temporaryReservationRequest, valuationsRequest)
|
||||||
reservation <- changeTerm(accountId, reservationId, reservationRequest)
|
(temporaryReservation, valuations) = okResponse
|
||||||
} yield reservation
|
temporaryReservationId = temporaryReservation.id
|
||||||
} else Left(new RuntimeException(s"Term for reservation [$reservationId] can't be changed"))
|
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),
|
def visitsHistory(accountId: Long, fromDate: ZonedDateTime = ZonedDateTime.now().minusYears(1),
|
||||||
@@ -175,4 +184,6 @@ class ApiService extends SessionSupport {
|
|||||||
LuxmedApi.login(username, textEncryptor.decrypt(password))
|
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 {
|
apiService.reserveVisit(monitoring.accountId, term).toTry.recoverWith {
|
||||||
case _: ServiceIsAlreadyBookedException if monitoring.rebookIfExists =>
|
case _: ServiceIsAlreadyBookedException if monitoring.rebookIfExists =>
|
||||||
info(s"Service [${monitoring.serviceName}] is already booked. Trying to update term")
|
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)
|
apiService.updateReservedVisit(monitoring.accountId, term).toTry
|
||||||
reservation.toTry.flatMap { r =>
|
|
||||||
val reservationId = r.reservationId
|
|
||||||
apiService.updateTerm(monitoring.accountId, reservationId, term).toTry
|
|
||||||
}
|
|
||||||
case ex => Failure(ex)
|
case ex => Failure(ex)
|
||||||
}.toEither match {
|
}.toEither match {
|
||||||
case Right(_) =>
|
case Right(_) =>
|
||||||
|
|||||||
Reference in New Issue
Block a user