From 9efc01f418f5ef5dea7a795b6325486f921845b8 Mon Sep 17 00:00:00 2001 From: Aiman Sharief Date: Fri, 13 Oct 2023 10:44:37 +0530 Subject: [PATCH] Issue #KN-881 : Collaborator Service Refactor in Knowlg --- .../sunbird/content/actors/ContentActor.scala | 31 ++++++++++++++++++- .../controllers/v3/ContentController.scala | 13 +++++++- .../content-service/app/utils/ApiId.scala | 3 ++ .../content-service/app/utils/Constants.scala | 1 + content-api/content-service/conf/routes | 5 ++- .../common/exception/ResponseCode.java | 2 +- 6 files changed, 51 insertions(+), 4 deletions(-) diff --git a/content-api/content-actors/src/main/scala/org/sunbird/content/actors/ContentActor.scala b/content-api/content-actors/src/main/scala/org/sunbird/content/actors/ContentActor.scala index 89d19882f..9c4f265e5 100644 --- a/content-api/content-actors/src/main/scala/org/sunbird/content/actors/ContentActor.scala +++ b/content-api/content-actors/src/main/scala/org/sunbird/content/actors/ContentActor.scala @@ -14,7 +14,7 @@ import org.sunbird.content.util.{AcceptFlagManager, ContentConstants, CopyManage import org.sunbird.cloudstore.StorageService import org.sunbird.common.{ContentParams, Platform, Slug} import org.sunbird.common.dto.{Request, Response, ResponseHandler} -import org.sunbird.common.exception.ClientException +import org.sunbird.common.exception.{ClientException, ResponseCode} import org.sunbird.content.dial.DIALManager import org.sunbird.content.publish.mgr.PublishManager import org.sunbird.content.review.mgr.ReviewManager @@ -58,6 +58,7 @@ class ContentActor @Inject() (implicit oec: OntologyEngineContext, ss: StorageSe case "reviewContent" => reviewContent(request) case "rejectContent" => rejectContent(request) case "publishContent" => publishContent(request) + case "updateCollaborator" => updateCollaborators(request) case _ => ERROR(request.getOperation) } } @@ -338,4 +339,32 @@ class ContentActor @Inject() (implicit oec: OntologyEngineContext, ss: StorageSe }).flatMap(f => f) } + def updateCollaborators(request: Request): Future[Response] = { + DataNode.read(request).map(node => { + if(node.getMetadata.get("status") != "Draft"){ + val responseCode = if (NodeUtil.isRetired(node)) ResponseCode.RESOURCE_NOT_FOUND else ResponseCode.FORBIDDEN + ResponseHandler.ERROR(responseCode, "ERR_CONTENT_COLLABORATORS_UPDATE_FAILED", "Update collaborators failed") + } + val contentVersionKey = node.getMetadata.get("versionKey") + val distinctCollaborators = request.getRequest.getOrDefault("collaborators", new util.ArrayList[AnyRef]()).asInstanceOf[util.List[AnyRef]].asScala.toList.distinct.asJava + request.put("collaborators", distinctCollaborators) + request.put("versionKey", contentVersionKey) + compareCollaborators(request, node.getMetadata.get("collaborators").asInstanceOf[Array[AnyRef]].toList.asJava, distinctCollaborators) + val updateRequest = new Request(request) + updateRequest.putAll(Map("versionKey" -> contentVersionKey ,"collaborators" -> distinctCollaborators.toArray).asJava) + updateRequest.setContext(request.getContext) + updateRequest.getContext.put("identifier", request.getRequest.getOrDefault("identifier","")) + DataNode.update(updateRequest).map(updatedNode => { + ResponseHandler.OK.put("metadata", updatedNode.getMetadata) + }) + }).flatMap(f => f) + } + + def compareCollaborators(request: Request, oldCollaborators:util.List[AnyRef], newCollaborators:util.List[AnyRef]): Unit = { + val addedCollaborators = oldCollaborators.asScala.diff(newCollaborators.asScala) + val removedCollaborators = newCollaborators.asScala.diff(oldCollaborators.asScala) + + } + + } diff --git a/content-api/content-service/app/controllers/v3/ContentController.scala b/content-api/content-service/app/controllers/v3/ContentController.scala index 4534fe828..1c523fedf 100644 --- a/content-api/content-service/app/controllers/v3/ContentController.scala +++ b/content-api/content-service/app/controllers/v3/ContentController.scala @@ -7,7 +7,7 @@ import javax.inject.{Inject, Named} import org.sunbird.models.UploadParams import org.sunbird.common.dto.ResponseHandler import play.api.mvc.ControllerComponents -import utils.{ActorNames, ApiId, JavaJsonUtils} +import utils.{ActorNames, ApiId, Constants, JavaJsonUtils} import scala.collection.JavaConverters._ @@ -285,4 +285,15 @@ class ContentController @Inject()(@Named(ActorNames.CONTENT_ACTOR) contentActor: getResult(ApiId.IMPORT_CONTENT, contentActor, contentRequest) } + def updateCollaborator(identifier: String) = Action.async { implicit request => + val headers = commonHeaders() + val body = requestBody() + val content = body.getOrDefault("content", new java.util.HashMap()).asInstanceOf[java.util.Map[String, Object]] + content.putAll(headers) + content.putAll(Map(Constants.IDENTIFIER -> identifier, "mode" -> "edit").asJava) + val contentRequest = getRequest(content, headers, "updateCollaborator") + setRequestContext(contentRequest, version, objectType, schemaName) + getResult(ApiId.UPDATE_COLLABORATOR, contentActor, contentRequest) + } + } diff --git a/content-api/content-service/app/utils/ApiId.scala b/content-api/content-service/app/utils/ApiId.scala index 9c88b0cf0..95eafe200 100644 --- a/content-api/content-service/app/utils/ApiId.scala +++ b/content-api/content-service/app/utils/ApiId.scala @@ -108,4 +108,7 @@ object ApiId { //Asset License Validate API val ASSET_LICENSE_VALIDATE = "asset.url.validate" + + //Update Collaborator API + val UPDATE_COLLABORATOR = "api.content.collaborator.create" } diff --git a/content-api/content-service/app/utils/Constants.scala b/content-api/content-service/app/utils/Constants.scala index b10c214e5..877bdf5aa 100644 --- a/content-api/content-service/app/utils/Constants.scala +++ b/content-api/content-service/app/utils/Constants.scala @@ -15,4 +15,5 @@ object Constants { val CONTENT_OBJECT_TYPE: String = "Content" val COLLECTION_OBJECT_TYPE: String = "Collection" val ASSET_OBJECT_TYPE: String = "Asset" + val IDENTIFIER: String = "identifier" } \ No newline at end of file diff --git a/content-api/content-service/conf/routes b/content-api/content-service/conf/routes index ced461af2..ed67776d4 100644 --- a/content-api/content-service/conf/routes +++ b/content-api/content-service/conf/routes @@ -137,4 +137,7 @@ POST /collection/v4/import/:collectionId controllers.v4.CollectionCo GET /collection/v4/export/:collectionId controllers.v4.CollectionController.exportCollection(collectionId:String, fileType:Option[String]) POST /collection/v4/review/:identifier controllers.v4.CollectionController.review(identifier:String) POST /collection/v4/dialcode/reserve/:identifier controllers.v4.CollectionController.reserveDialCode(identifier:String) -POST /collection/v4/dialcode/release/:identifier controllers.v4.CollectionController.releaseDialCode(identifier:String) \ No newline at end of file +POST /collection/v4/dialcode/release/:identifier controllers.v4.CollectionController.releaseDialCode(identifier:String) + +# Collaboration V3 APIs +PATCH /collaborator/update/:contentId controllers.v3.ContentController.updateCollaborator(contentId:String) \ No newline at end of file diff --git a/platform-core/platform-common/src/main/java/org/sunbird/common/exception/ResponseCode.java b/platform-core/platform-common/src/main/java/org/sunbird/common/exception/ResponseCode.java index 2618f8fd3..9b7fc5023 100644 --- a/platform-core/platform-common/src/main/java/org/sunbird/common/exception/ResponseCode.java +++ b/platform-core/platform-common/src/main/java/org/sunbird/common/exception/ResponseCode.java @@ -2,7 +2,7 @@ public enum ResponseCode { - OK(200), CLIENT_ERROR(400), SERVER_ERROR(500), RESOURCE_NOT_FOUND(404), PARTIAL_SUCCESS(207); + OK(200), CLIENT_ERROR(400), SERVER_ERROR(500), RESOURCE_NOT_FOUND(404), PARTIAL_SUCCESS(207), FORBIDDEN(403); private int code;