From 5140e33fcebe012fcabf464d65bcd53e69a39079 Mon Sep 17 00:00:00 2001 From: Thijs Petter Date: Sat, 30 Jan 2021 13:58:13 +0100 Subject: [PATCH] Temporary API to retrieve where used information This is temporary as it actually exposes information to which platform owner should not have any access --- .../service/api/platform/PlatformRoute.scala | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/case-service/src/main/scala/org/cafienne/service/api/platform/PlatformRoute.scala b/case-service/src/main/scala/org/cafienne/service/api/platform/PlatformRoute.scala index b1bc0df83..f0fab11e9 100644 --- a/case-service/src/main/scala/org/cafienne/service/api/platform/PlatformRoute.scala +++ b/case-service/src/main/scala/org/cafienne/service/api/platform/PlatformRoute.scala @@ -37,6 +37,7 @@ class PlatformRoute(platformQueries: PlatformQueries)(override implicit val user enableTenant ~ getUserInformation ~ updateUserInformation ~ + getWhereUsedInformation ~ getUpdateStatus } @@ -222,6 +223,56 @@ class PlatformRoute(platformQueries: PlatformQueries)(override implicit val user } } + @Path("/where-used-info") + @POST + @Operation( + summary = "Get where used information across the platform for a list of users to be updated", + description = "Get where used information across the platform for a list of users to be updated", + tags = Array("platform"), + responses = Array( + new ApiResponse(description = "String message with the usage statistics", responseCode = "200"), + new ApiResponse(description = "Not able to perform the action", responseCode = "500") + ) + ) + @RequestBody(description = "List of new user information", required = true, content = Array(new Content(schema = new Schema(implementation = classOf[TenantAPI.PlatformUsersUpdateFormat])))) + @Consumes(Array("application/json")) + def getWhereUsedInformation = post { + validOwner { _ => + pathPrefix("where-used-info") { + pathEndOrSingleSlash { + import spray.json.DefaultJsonProtocol._ + import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ + + implicit val userFormat = jsonFormat2(TenantAPI.PlatformUserUpdateFormat) + implicit val listFormat = jsonFormat1(TenantAPI.PlatformUsersUpdateFormat) + entity(as[TenantAPI.PlatformUsersUpdateFormat]) { list => + val newUserIds = list.users.map(u => u.newUserId) + val existingUserIds = list.users.map(u => u.existingUserId) + val startWhereUsedQueries = System.currentTimeMillis() + val queries = for { + tenantsByUser <- platformQueries.whereUsedInTenants(existingUserIds) + casesByUser <- platformQueries.whereUsedInCases(existingUserIds) + tenantsByNewUser <- platformQueries.whereUsedInTenants(newUserIds) + casesByNewUser <- platformQueries.whereUsedInCases(newUserIds) + } yield (tenantsByUser, casesByUser, tenantsByNewUser, casesByNewUser) + onComplete(queries) { + case Success(value) => { + val finishedWhereUsedQueries = System.currentTimeMillis() + val queryTimingMsg = s"Query took ${finishedWhereUsedQueries - startWhereUsedQueries} millis" + val existingMsg = s"Existing user ids are found in ${value._1.size} tenants and ${value._2.size} cases; query took ${finishedWhereUsedQueries - startWhereUsedQueries} millis" + val newMsg = s"New user ids are found in ${value._3.size} tenants and ${value._4.size} cases" + + val msg = s"$queryTimingMsg\n$existingMsg\n$newMsg" + complete(StatusCodes.OK, msg) + } + case Failure(t) => handleFailure(t) + } + } + } + } + } + } + @Path("/update-status") @GET @Operation(