def collectionEdits()

in app/controllers/FaciaToolController.scala [247:393]


  def collectionEdits(): Action[AnyContent] = AccessAPIAuthAction.async {
    implicit request =>
      FaciaToolMetrics.ApiUsageCount.increment()
      request.body.asJson.flatMap(_.asOpt[UpdateMessage]).map {
        case update: Update =>
          withModifyPermissionForCollections(Set(update.update.id)) {

            val collectionPriorities = collectionPermissions
              .getFrontsPermissionsPriorityByCollectionId(update.update.id)

            withModifyGroupPermissionForCollections(
              collectionPriorities,
              Set()
            ) {

              val identity = request.user

              val futureCollectionJson = updateActions
                .updateCollectionList(update.update.id, update.update, identity)
              futureCollectionJson.map { maybeCollectionJson =>
                val updatedCollections =
                  maybeCollectionJson.map(update.update.id -> _).toMap

                val shouldUpdateLive: Boolean = update.update.live

                val collectionIds = updatedCollections.keySet

                faciaPress.press(
                  PressCommand(
                    collectionIds,
                    live = shouldUpdateLive,
                    draft = (updatedCollections.values.exists(
                      _.draft.isEmpty
                    ) && shouldUpdateLive) || update.update.draft
                  )
                )

                if (updatedCollections.nonEmpty) {
                  structuredLogger.putLog(LogUpdate(update, identity.email))
                  Ok(Json.toJson(updatedCollections)).as("application/json")
                } else
                  NotFound
              }
            }
          }
        case remove: Remove =>
          withModifyPermissionForCollections(Set(remove.remove.id)) {

            val collectionPriorities = collectionPermissions
              .getFrontsPermissionsPriorityByCollectionId(remove.remove.id)

            withModifyGroupPermissionForCollections(
              collectionPriorities,
              Set()
            ) {

              val identity = request.user
              updateActions
                .updateCollectionFilter(
                  remove.remove.id,
                  remove.remove,
                  identity
                )
                .map { maybeCollectionJson =>
                  val updatedCollections =
                    maybeCollectionJson.map(remove.remove.id -> _).toMap
                  val shouldUpdateLive: Boolean = remove.remove.live
                  val collectionIds = updatedCollections.keySet
                  faciaPress.press(
                    PressCommand(
                      collectionIds,
                      live = shouldUpdateLive,
                      draft = (updatedCollections.values.exists(
                        _.draft.isEmpty
                      ) && shouldUpdateLive) || remove.remove.draft
                    )
                  )
                  structuredLogger.putLog(LogUpdate(remove, identity.email))
                  Ok(Json.toJson(updatedCollections)).as("application/json")
                }
            }
          }
        case updateAndRemove: UpdateAndRemove =>
          withModifyPermissionForCollections(
            Set(updateAndRemove.update.id, updateAndRemove.remove.id)
          ) {

            val fromCollectionPriorities =
              collectionPermissions.getFrontsPermissionsPriorityByCollectionId(
                updateAndRemove.update.id
              )
            val toCollectionPriorities =
              collectionPermissions.getFrontsPermissionsPriorityByCollectionId(
                updateAndRemove.remove.id
              )

            withModifyGroupPermissionForCollections(
              fromCollectionPriorities,
              toCollectionPriorities
            ) {
              val identity = request.user
              val futureUpdatedCollections =
                Future
                  .sequence(
                    List(
                      updateActions
                        .updateCollectionList(
                          updateAndRemove.update.id,
                          updateAndRemove.update,
                          identity
                        )
                        .map(_.map(updateAndRemove.update.id -> _)),
                      updateActions
                        .updateCollectionFilter(
                          updateAndRemove.remove.id,
                          updateAndRemove.remove,
                          identity
                        )
                        .map(_.map(updateAndRemove.remove.id -> _))
                    )
                  )
                  .map(_.flatten.toMap)

              futureUpdatedCollections.map { updatedCollections =>
                val shouldUpdateLive: Boolean =
                  updateAndRemove.remove.live || updateAndRemove.update.live
                val shouldUpdateDraft: Boolean =
                  updateAndRemove.remove.draft || updateAndRemove.update.draft
                val collectionIds = updatedCollections.keySet
                faciaPress.press(
                  PressCommand(
                    collectionIds,
                    live = shouldUpdateLive,
                    draft = (updatedCollections.values.exists(
                      _.draft.isEmpty
                    ) && shouldUpdateLive) || shouldUpdateDraft
                  )
                )
                structuredLogger
                  .putLog(LogUpdate(updateAndRemove, identity.email))
                Ok(Json.toJson(updatedCollections)).as("application/json")
              }
            }
          }
        case _ => Future.successful(NotAcceptable)
      } getOrElse Future.successful(NotFound)
  }