From 112e5e90e5e10729473eaf637cef134ba49acc3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20Del=C3=A8gue?= Date: Wed, 16 Oct 2024 16:53:05 +0200 Subject: [PATCH] Incoherent behavior with template and expired consent --- nio-server/app/models/ConsentFact.scala | 23 +++++++++++++------ .../app/service/ConsentManagerService.scala | 2 +- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/nio-server/app/models/ConsentFact.scala b/nio-server/app/models/ConsentFact.scala index 4b01319..bc6e568 100644 --- a/nio-server/app/models/ConsentFact.scala +++ b/nio-server/app/models/ConsentFact.scala @@ -54,6 +54,14 @@ object DoneBy { } case class Consent(key: String, label: String, checked: Boolean, expiredAt: Option[LocalDateTime] = None) { + + def isActive: Bool = { + val now = LocalDateTime.now(Clock.systemUTC()) + c.expiredAt.isEmpty || c.expiredAt.exists(d => d.isAfter(now)) + } + + def isExpired: Bool = !isActive + def asXml(): Elem = {key} @@ -87,6 +95,9 @@ object Consent { } case class ConsentGroup(key: String, label: String, consents: Seq[Consent]) { + + def activeConsents: Seq[Consent] = this.consents.filter(_.isActive) + def asXml(): Elem = {key} @@ -424,7 +435,7 @@ case class ConsentFact( {version} - {groups.map(_.asXml())} + {groups.filter(cf => cf.consents).map(_.asXml())} { @@ -472,12 +483,10 @@ case class ConsentFact( if (showExpiredConsents) { this } else { - val now = LocalDateTime.now(Clock.systemUTC()) - this.copy(groups = this.groups.map(group => - group.copy(consents = group.consents.toList.filter(c => - c.expiredAt.isEmpty || c.expiredAt.exists(d => d.isAfter(now)) - )) - )) + this.copy(groups = this.groups + .map(group => group.copy(consents = group.activeConsents)) + .filter(group => group.consents.nonEmpty) + ) } } } diff --git a/nio-server/app/service/ConsentManagerService.scala b/nio-server/app/service/ConsentManagerService.scala index bc0431c..b900ab4 100644 --- a/nio-server/app/service/ConsentManagerService.scala +++ b/nio-server/app/service/ConsentManagerService.scala @@ -396,7 +396,7 @@ class ConsentManagerService( val groupsUpdated: Seq[ConsentGroup] = template.groups.map { group => - val maybeGroup = consentFact.groups.find(cg => cg.key == group.key && cg.label == group.label) + val maybeGroup = consentFact.filterExpiredConsent(true).find(cg => cg.key == group.key && cg.label == group.label) mergeConsentGroup(maybeGroup, group) }