Skip to content

Commit

Permalink
Merge pull request #547 from NDLANO/learningpath-made-available-date
Browse files Browse the repository at this point in the history
learningpath-api: Adds `madeAvailable` field to learningpaths
  • Loading branch information
jnatten authored Nov 25, 2024
2 parents 000ebd9 + 3fbb176 commit fb54c7e
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ case class LearningPath(
owner: String,
copyright: LearningpathCopyright,
learningsteps: Option[Seq[LearningStep]] = None,
message: Option[Message] = None
message: Option[Message] = None,
madeAvailable: Option[NDLADate] = None
) extends Content {

def supportedLanguages: Seq[String] = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Part of NDLA learningpath-api
* Copyright (C) 2024 NDLA
*
* See LICENSE
*
*/

package no.ndla.learningpathapi.db.migration

import no.ndla.common.CirceUtil
import no.ndla.common.model.domain.learningpath.LearningPath
import no.ndla.common.model.domain.learningpath.LearningPathStatus.{PUBLISHED, UNLISTED}
import no.ndla.database.DocumentMigration

class V39__MadeAvailableForThePublished extends DocumentMigration {
override val columnName: String = "document"
override val tableName: String = "learningpaths"

protected def convertColumn(document: String): String = {
val oldLp = CirceUtil.unsafeParseAs[LearningPath](document)
val madeAvailable = oldLp.status match {
case UNLISTED | PUBLISHED => Some(oldLp.lastUpdated)
case _ => None
}

val newLearningPath = oldLp.copy(madeAvailable = madeAvailable)
CirceUtil.toJsonString(newLearningPath)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ case class LearningPathV2(
@description("True if authenticated user may edit this learningpath") canEdit: Boolean,
@description("The supported languages for this learningpath") supportedLanguages: Seq[String],
@description("Visible if administrator or owner of LearningPath") ownerId: Option[String],
@description("Message set by administrator. Visible if administrator or owner of LearningPath") message: Option[Message]
@description("Message set by administrator. Visible if administrator or owner of LearningPath") message: Option[Message],
@description("The date when this learningpath was made available to the public.") madeAvailable: Option[NDLADate]
)

object LearningPathV2 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ trait ConverterService {
lp.canEdit(userInfo),
supportedLanguages,
owner,
message
message,
lp.madeAvailable
)
)
} else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,22 @@ trait UpdateService {
case _ => valid.message
}

val updatedLearningPath = learningPathRepository.update(
valid.copy(message = newMessage, status = status, lastUpdated = clock.now())
val madeAvailable = valid.madeAvailable.orElse {
status match {
case LearningPathStatus.PUBLISHED | LearningPathStatus.UNLISTED => Some(clock.now())
case _ => None
}
}

val toUpdateWith = valid.copy(
message = newMessage,
status = status,
lastUpdated = clock.now(),
madeAvailable = madeAvailable
)

val updatedLearningPath = learningPathRepository.update(toUpdateWith)

updateSearchAndTaxonomy(updatedLearningPath, owner.tokenUser)
.flatMap(_ =>
converterService.asApiLearningpathV2(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class ConverterServiceTest extends UnitSuite with UnitTestEnvironment {
true,
List("nb"),
None,
None,
None
)
val domainLearningStep: LearningStep =
Expand Down Expand Up @@ -137,6 +138,7 @@ class ConverterServiceTest extends UnitSuite with UnitTestEnvironment {
canEdit = true,
List("nb", "en"),
None,
None,
None
)
)
Expand Down Expand Up @@ -188,6 +190,7 @@ class ConverterServiceTest extends UnitSuite with UnitTestEnvironment {
true,
List("nb", "en"),
None,
None,
None
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,26 @@ class UpdateServiceTest extends UnitSuite with UnitTestEnvironment {
verify(searchIndexService, times(1)).indexDocument(any[LearningPath])
}

test("That updateLearningPathStatusV2 updates madeAvailable when going to UNLISTED") {
when(learningPathRepository.withIdIncludingDeleted(eqTo(PUBLISHED_ID))(any[DBSession]))
.thenReturn(Some(PUBLISHED_LEARNINGPATH))
when(learningPathRepository.update(any[LearningPath])(any[DBSession]))
.thenReturn(PUBLISHED_LEARNINGPATH.copy(status = learningpath.LearningPathStatus.PRIVATE))
when(learningPathRepository.learningPathsWithIsBasedOn(PUBLISHED_ID)).thenReturn(List())
val nowDate = NDLADate.fromUnixTime(1337)
when(clock.now()).thenReturn(nowDate)
val user = PRIVATE_OWNER.copy(permissions = Set(LEARNINGPATH_API_ADMIN)).toCombined

service.updateLearningPathStatusV2(PUBLISHED_ID, LearningPathStatus.UNLISTED, user, "nb").failIfFailure

val expectedLearningPath = PUBLISHED_LEARNINGPATH.copy(
status = LearningPathStatus.UNLISTED,
lastUpdated = nowDate,
madeAvailable = Some(nowDate)
)
verify(learningPathRepository, times(1)).update(eqTo(expectedLearningPath))(any)
}

test(
"That updateLearningPathStatusV2 updates the status when the given user is not the owner, but is admin and the status is PUBLISHED"
) {
Expand Down

0 comments on commit fb54c7e

Please sign in to comment.