Skip to content

Commit

Permalink
Ajout de l'import de masse
Browse files Browse the repository at this point in the history
  • Loading branch information
larousso committed May 11, 2023
1 parent 37f2d6e commit a74dc29
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
6 changes: 2 additions & 4 deletions nio-server/app/controllers/ConsentController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,7 @@ class ConsentController(

val newLineSplit = Framing.delimiter(ByteString("\n"), 10000, allowTruncation = true)
val toJson = Flow[ByteString] via newLineSplit map (_.utf8String) filterNot (_.isEmpty) map (l => Json.parse(l))
def ndJson(implicit ec: ExecutionContext): BodyParser[Source[JsValue, _]] =
BodyParser(_ => Accumulator.source[ByteString].map(s => Right(s.via(toJson)))(ec))

def ndJson(implicit ec: ExecutionContext): BodyParser[Source[JsValue, _]] = BodyParser(_ => Accumulator.source[ByteString].map(s => Right(s.via(toJson)))(ec))

object ImportError {
implicit val format = Json.format[ImportError]
Expand All @@ -296,7 +294,7 @@ class ConsentController(
)
}

def batchImport(tenant: String, orgKey: String) = AuthAction(ndJson).async { implicit req =>
def batchImport(tenant: String, orgKey: String) = AuthAction.async(ndJson) { implicit req =>
val result: Future[JsValue] = req.body.mapAsync(10) { json =>
json.validate[ConsentFactCommand].fold(
{ err => FastFuture.successful(ImportResult(errorsCount = 1, errors = List(ImportError("json parsing error", detailedError = JsError.toJson(err), command = json)))) },
Expand Down
6 changes: 5 additions & 1 deletion nio-server/app/models/ConsentFact.scala
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,11 @@ object ConsentFactCommand {
case class UpdateConsentFact(userId: String, command: ConsentFact) extends ConsentFactCommand

object UpdateConsentFact {
val format = Json.format[UpdateConsentFact]
val format = OFormat[UpdateConsentFact](
((__ \ "userId").read[String] and
(__ \ "command").read[ConsentFact](ConsentFact.consentFactReadsWithoutIdAndLastUpdate))(UpdateConsentFact.apply _),
Json.writes[UpdateConsentFact]
)
}

implicit val format = Format(
Expand Down
23 changes: 23 additions & 0 deletions nio-server/test/controllers/ConsentControllerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,29 @@ class ConsentControllerSpec extends TestUtils {
.as[Boolean] mustBe user1Modified.groups(1).consents(2).checked
}

"batch import consent" in {

val commands = List(
Json.obj("userId" -> user6Modified.userId, "type" -> "Update", "command" -> user6ModifiedAsJson),
Json.obj("userId" -> user1.userId, "type" -> "Patch", "command" -> Json.obj(
"groups" -> Json.arr(
Json.obj(
"key" -> "maifNotifs",
"consents" -> Json.arr(
Json.obj("key" -> "phone", "checked" -> false)
)
)
)
))
).map(Json.stringify _).mkString("", "\n", "\n")

val response = postText(s"/$tenant/organisations/$organisationKey/users/_batch", commands)
println(response.body)
response.status mustBe OK
(response.json \ "successCount").validate[Int].get mustBe 2

}

"update user with a subset of consents compare to organisation version" in {
val response = putJson(
s"/$tenant/organisations/$organisationKey/users/$userId2",
Expand Down
3 changes: 3 additions & 0 deletions nio-server/test/utils/TestUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ trait TestUtils
def postJson(path: String, body: JsValue, headers: Seq[(String, String)] = jsonHeaders) =
callByType[JsValue](path = path, httpVerb = POST, body = body, headers = headers)

def postText(path: String, body: String, headers: Seq[(String, String)] = jsonHeaders) =
callByType[String](path = path, httpVerb = POST, body = body, headers = headers)

def postBinaryFile(path: String, body: File, api: Boolean = true, headers: Seq[(String, String)] = jsonHeaders) = {
val suffix = if (api) apiPath else serverHost
val futureResponse = ws
Expand Down

0 comments on commit a74dc29

Please sign in to comment.