Skip to content

Commit

Permalink
Pb with lastupdate on offers
Browse files Browse the repository at this point in the history
  • Loading branch information
larousso committed May 10, 2023
1 parent d923cb5 commit 056e8d2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
4 changes: 3 additions & 1 deletion nio-server/app/models/ConsentFact.scala
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ object PartialConsentOffer {
partialOffers.find(pg => pg.key == o.key).fold(o) { po =>
o.copy(
label = po.label.getOrElse(o.label),
lastUpdate = po.lastUpdate.getOrElse(o.lastUpdate),
version = po.version.getOrElse(o.version),
groups = PartialConsentGroup.merge(po.groups.toList.flatten, o.groups)
)
}
Expand Down Expand Up @@ -310,7 +312,7 @@ case class PartialConsentFact(
userId = userId.getOrElse(lastConsentFact.userId),
doneBy = doneBy.getOrElse(lastConsentFact.doneBy),
version = version.getOrElse(currentVersion.num),
lastUpdate = lastUpdate.getOrElse(DateTime.now(DateTimeZone.UTC)),
lastUpdate = lastUpdate.getOrElse(lastConsentFact.lastUpdate),
lastUpdateSystem = DateTime.now(DateTimeZone.UTC),
groups = groups.map(g => PartialConsentGroup.merge(g, lastConsentFact.groups)).getOrElse(lastConsentFact.groups),
offers = offers.map(o => PartialConsentOffer.merge(o, lastConsentFact.offers)).getOrElse(lastConsentFact.offers),
Expand Down
45 changes: 43 additions & 2 deletions nio-server/test/controllers/ConsentControllerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,7 @@ class ConsentControllerSpec extends TestUtils {
key = "offer1",
label = "offer one",
version = 1,
lastUpdate = DateTime.now().minusHours(2).withMillis(0),
groups = Seq(
ConsentGroup(
key = "maifNotifs",
Expand Down Expand Up @@ -871,6 +872,30 @@ class ConsentControllerSpec extends TestUtils {
)
)

val updatedDate = DateTime.now().minusHours(1).withMillis(0)

val patchConsentFact = PartialConsentFact(
offers = Some(Seq(
PartialConsentOffer(
key = "offer1",
label = Option.none[String],
lastUpdate = Some(updatedDate),
version = Option.none[Int],
groups = Some(Seq(
PartialConsentGroup(
key = "maifNotifs",
label = Option.none[String],
consents = Some(Seq(
PartialConsent(key = "phone", label = Option.none[String], checked = true),
PartialConsent(key = "mail", label = Option.none[String], checked = true),
PartialConsent(key = "sms", label = Option.none[String], checked = false)
))
)
))
)
))
)

val basicConsentFact: ConsentFact = ConsentFact(
userId = userId,
doneBy = DoneBy(userId = userId, role = "USER"),
Expand Down Expand Up @@ -973,6 +998,20 @@ class ConsentControllerSpec extends TestUtils {
(offer2group1perm3 \ "label").as[String] mustBe "Par SMS / MMS / VMS"
(offer2group1perm3 \ "checked").as[Boolean] mustBe false

// Patch offer :
val responsePatch = patchJson(s"/$tenant/organisations/$orgKey/users/$userId", Json.toJson(patchConsentFact))
println(responsePatch.json)
responsePatch.status mustBe OK

val patchedValue: JsValue = responsePatch.json

val patchedOffers: JsArray = (patchedValue \ "offers").as[JsArray]
patchedOffers.value.length mustBe 2

(patchedOffers \ 0 \ "key").as[String] mustBe "offer1"
(patchedOffers \ 0 \ "lastUpdate").as[DateTime](DateUtils.utcDateTimeFormats) mustBe updatedDate


}

"version consent > version org on error" in {
Expand Down Expand Up @@ -1011,10 +1050,12 @@ class ConsentControllerSpec extends TestUtils {
val consentWithVersionEqualToOrgAndStrucEq: ConsentFact =
basicConsentFact.copy(offers = Some(Seq(offerToConsentOffer(offer2).copy(version = 3))))

putJson(
val r = putJson(
s"/$tenant/organisations/$orgKey/users/$userId",
consentWithVersionEqualToOrgAndStrucEq.asJson()
).status mustBe OK
)
println(r.json)
r.status mustBe OK

val consentWithVersionEqualToOrgAndStrucDif: ConsentFact =
basicConsentFact.copy(offers = Some(Seq(offerToConsentOffer(offer2V3OnError))))
Expand Down

0 comments on commit 056e8d2

Please sign in to comment.