|
| 1 | +/* |
| 2 | + * Copyright (c) 2023. Smart Operating Block |
| 3 | + * |
| 4 | + * Use of this source code is governed by an MIT-style |
| 5 | + * license that can be found in the LICENSE file or at |
| 6 | + * https://opensource.org/licenses/MIT. |
| 7 | + */ |
| 8 | + |
| 9 | +package infrastructure.api.handlers |
| 10 | + |
| 11 | +import application.presenter.api.room.RoomsId |
| 12 | +import application.presenter.api.tracking.toTrackingInfoDto |
| 13 | +import application.service.TrackingService |
| 14 | +import entity.room.RoomData |
| 15 | +import infrastructure.provider.Provider |
| 16 | +import io.vertx.core.CompositeFuture |
| 17 | +import io.vertx.core.Handler |
| 18 | +import io.vertx.ext.web.RoutingContext |
| 19 | +import kotlinx.serialization.encodeToString |
| 20 | +import kotlinx.serialization.json.Json |
| 21 | + |
| 22 | +/** |
| 23 | + * The handler for health professional tracking API. |
| 24 | + */ |
| 25 | +class ZoneHealthProfessionalTrackingHandler( |
| 26 | + private val provider: Provider, |
| 27 | +) : Handler<RoutingContext> { |
| 28 | + |
| 29 | + override fun handle(routingContext: RoutingContext) { |
| 30 | + routingContext.request().body().onSuccess { |
| 31 | + val operatingRoomId = Json.decodeFromString<RoomsId>(it.toString()).operatingRoomId |
| 32 | + val preOperatingRoomId = Json.decodeFromString<RoomsId>(it.toString()).preOperatingRoomId |
| 33 | + val result = TrackingService.ZoneHealthProfessionalTrackingInfo( |
| 34 | + RoomData.RoomId(preOperatingRoomId), |
| 35 | + RoomData.RoomId(operatingRoomId), |
| 36 | + provider.webClient, |
| 37 | + ).execute() |
| 38 | + |
| 39 | + CompositeFuture.all(result.first, result.second).onSuccess { |
| 40 | + CompositeFuture.all(result.first.result()).onSuccess { |
| 41 | + CompositeFuture.all(result.second.result()).onSuccess { |
| 42 | + val operating = result.first.result().map { hp -> |
| 43 | + hp.result().toTrackingInfoDto() |
| 44 | + } |
| 45 | + val preOperating = result.second.result().map { hp -> |
| 46 | + hp.result().toTrackingInfoDto() |
| 47 | + } |
| 48 | + routingContext.response().end(Json.encodeToString(operating + preOperating)) |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | +} |
0 commit comments