Skip to content

Commit

Permalink
Issue #KN-881 : Collaborator Service Refactor in Knowlg
Browse files Browse the repository at this point in the history
  • Loading branch information
aimansharief committed Oct 13, 2023
1 parent b22e172 commit 9efc01f
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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)

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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._

Expand Down Expand Up @@ -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)
}

}
3 changes: 3 additions & 0 deletions content-api/content-service/app/utils/ApiId.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
1 change: 1 addition & 0 deletions content-api/content-service/app/utils/Constants.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
5 changes: 4 additions & 1 deletion content-api/content-service/conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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)
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 9efc01f

Please sign in to comment.