Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

learningpath-api: Adds madeAvailable field to learningpaths #547

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dette kan vel i teorien være løgn dersom den har blitt oppdatert og republisert, men det er kanskje greit?

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
Loading