Skip to content

Commit

Permalink
WIP WIP WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jnatten committed Oct 23, 2024
1 parent 56a6f4a commit 8467388
Show file tree
Hide file tree
Showing 19 changed files with 197 additions and 130 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*
* Part of NDLA myndla-api
* Part of NDLA common
* Copyright (C) 2024 NDLA
*
* See LICENSE
*
*/

package no.ndla.myndlaapi.model.domain
package no.ndla.common.model.domain.myndla

import no.ndla.common.errors.InvalidStatusException
import sttp.tapir.CodecFormat.TextPlain
import sttp.tapir.{Codec, CodecFormat, DecodeResult, Schema}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Part of NDLA common
* Copyright (C) 2024 NDLA
*
* See LICENSE
*/

package no.ndla.common.model.domain.myndla

import com.scalatsi.TypescriptType.{TSLiteralString, TSUnion}
import com.scalatsi.{TSNamedType, TSType}
import enumeratum.*
import io.circe.{Decoder, Encoder}
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import no.ndla.common.model.NDLADate

case class MyNDLAGroup(id: String, displayName: String, isPrimarySchool: Boolean, parentId: Option[String])
object MyNDLAGroup {
implicit val encoder: Encoder[MyNDLAGroup] = deriveEncoder
implicit val decoder: Decoder[MyNDLAGroup] = deriveDecoder
}

case class MyNDLAUserDocument(
favoriteSubjects: Seq[String],
userRole: UserRole.Value,
lastUpdated: NDLADate,
organization: String,
groups: Seq[MyNDLAGroup],
username: String,
displayName: String,
email: String,
arenaEnabled: Boolean,
arenaGroups: List[ArenaGroup],
shareName: Boolean
) {
def toFullUser(id: Long, feideId: String): MyNDLAUser = {
MyNDLAUser(
id = id,
feideId = feideId,
favoriteSubjects = favoriteSubjects,
userRole = userRole,
lastUpdated = lastUpdated,
organization = organization,
groups = groups,
username = username,
displayName = displayName,
email = email,
arenaEnabled = arenaEnabled,
shareName = shareName,
arenaGroups = arenaGroups
)
}
}

object MyNDLAUserDocument {
implicit val encoder: Encoder[MyNDLAUserDocument] = deriveEncoder
implicit val decoder: Decoder[MyNDLAUserDocument] = deriveDecoder
}

sealed trait ArenaGroup extends EnumEntry
object ArenaGroup extends Enum[ArenaGroup] with CirceEnum[ArenaGroup] {
case object ADMIN extends ArenaGroup
override def values: IndexedSeq[ArenaGroup] = findValues

implicit val enumTsType: TSNamedType[ArenaGroup] =
TSType.alias[ArenaGroup]("ArenaGroup", TSUnion(values.map(e => TSLiteralString(e.entryName))))
}

case class MyNDLAUser(
id: Long,
feideId: String,
favoriteSubjects: Seq[String],
userRole: UserRole.Value,
lastUpdated: NDLADate,
organization: String,
groups: Seq[MyNDLAGroup],
username: String,
displayName: String,
email: String,
arenaEnabled: Boolean,
arenaGroups: List[ArenaGroup],
shareName: Boolean
) {
// Keeping FEIDE and our data in sync
def wasUpdatedLast24h: Boolean = NDLADate.now().isBefore(lastUpdated.minusSeconds(10))

def isStudent: Boolean = userRole == UserRole.STUDENT
def isTeacher: Boolean = userRole == UserRole.EMPLOYEE
def isAdmin: Boolean = arenaGroups.contains(ArenaGroup.ADMIN)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/*
* Part of NDLA myndla-api
* Part of NDLA common
* Copyright (C) 2024 NDLA
*
* See LICENSE
*
*/

package no.ndla.myndlaapi.model.domain
package no.ndla.common.model.domain.myndla

import io.circe.{Decoder, Encoder}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package no.ndla.myndlaapi.controller

import no.ndla.common.model.api.CommaSeparatedList.*
import no.ndla.common.model.domain.ResourceType
import no.ndla.common.model.domain.myndla.FolderStatus
import no.ndla.myndlaapi.model.api.{
Folder,
FolderSortRequest,
Expand All @@ -25,7 +26,6 @@ import no.ndla.myndlaapi.model.domain.FolderSortObject.{
RootFolderSorting,
SharedFolderSorting
}
import no.ndla.myndlaapi.model.domain.FolderStatus
import no.ndla.myndlaapi.service.{FolderReadService, FolderWriteService}
import no.ndla.network.tapir.NoNullJsonPrinter.jsonBody
import no.ndla.network.tapir.Parameters.feideHeader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package no.ndla.myndlaapi.model.domain

import cats.implicits.catsSyntaxOptionId
import no.ndla.common.model.NDLADate
import no.ndla.common.model.domain.myndla.FolderStatus
import no.ndla.network.model.FeideID
import scalikejdbc.*

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,92 +8,12 @@

package no.ndla.myndlaapi.model.domain

import com.scalatsi.TypescriptType.{TSLiteralString, TSUnion}
import com.scalatsi.{TSNamedType, TSType}
import enumeratum.*
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import io.circe.{Decoder, Encoder}
import no.ndla.common.CirceUtil
import no.ndla.common.model.NDLADate
import no.ndla.network.model.FeideID
import no.ndla.common.model.domain.myndla.{MyNDLAUser, MyNDLAUserDocument}
import scalikejdbc.*

case class MyNDLAGroup(id: String, displayName: String, isPrimarySchool: Boolean, parentId: Option[String])
object MyNDLAGroup {
implicit val encoder: Encoder[MyNDLAGroup] = deriveEncoder
implicit val decoder: Decoder[MyNDLAGroup] = deriveDecoder
}

case class MyNDLAUserDocument(
favoriteSubjects: Seq[String],
userRole: UserRole.Value,
lastUpdated: NDLADate,
organization: String,
groups: Seq[MyNDLAGroup],
username: String,
displayName: String,
email: String,
arenaEnabled: Boolean,
arenaGroups: List[ArenaGroup],
shareName: Boolean
) {
def toFullUser(id: Long, feideId: FeideID): MyNDLAUser = {
MyNDLAUser(
id = id,
feideId = feideId,
favoriteSubjects = favoriteSubjects,
userRole = userRole,
lastUpdated = lastUpdated,
organization = organization,
groups = groups,
username = username,
displayName = displayName,
email = email,
arenaEnabled = arenaEnabled,
shareName = shareName,
arenaGroups = arenaGroups
)
}
}

object MyNDLAUserDocument {
implicit val encoder: Encoder[MyNDLAUserDocument] = deriveEncoder
implicit val decoder: Decoder[MyNDLAUserDocument] = deriveDecoder
}

sealed trait ArenaGroup extends EnumEntry
object ArenaGroup extends Enum[ArenaGroup] with CirceEnum[ArenaGroup] {
case object ADMIN extends ArenaGroup
override def values: IndexedSeq[ArenaGroup] = findValues

implicit val enumTsType: TSNamedType[ArenaGroup] =
TSType.alias[ArenaGroup]("ArenaGroup", TSUnion(values.map(e => TSLiteralString(e.entryName))))

}

case class MyNDLAUser(
id: Long,
feideId: FeideID,
favoriteSubjects: Seq[String],
userRole: UserRole.Value,
lastUpdated: NDLADate,
organization: String,
groups: Seq[MyNDLAGroup],
username: String,
displayName: String,
email: String,
arenaEnabled: Boolean,
arenaGroups: List[ArenaGroup],
shareName: Boolean
) {
// Keeping FEIDE and our data in sync
def wasUpdatedLast24h: Boolean = NDLADate.now().isBefore(lastUpdated.minusSeconds(10))

def isStudent: Boolean = userRole == UserRole.STUDENT
def isTeacher: Boolean = userRole == UserRole.EMPLOYEE
def isAdmin: Boolean = arenaGroups.contains(ArenaGroup.ADMIN)
}

object MyNDLAUser extends SQLSyntaxSupport[MyNDLAUser] {
implicit val encoder: Encoder[MyNDLAUser] = deriveEncoder
implicit val decoder: Decoder[MyNDLAUser] = deriveDecoder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import com.typesafe.scalalogging.StrictLogging
import no.ndla.common.errors.{NotFoundException, RollbackException}
import no.ndla.common.model.NDLADate
import no.ndla.common.model.domain.ResourceType
import no.ndla.common.model.domain.myndla.FolderStatus
import no.ndla.common.{CirceUtil, Clock}
import no.ndla.myndlaapi.{maybeUuidBinder, uuidBinder, uuidParameterFactory}
import no.ndla.myndlaapi.model.domain.{
Folder,
FolderResource,
FolderStatus,
MyNDLAUser,
NDLASQLException,
NewFolderData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ package no.ndla.myndlaapi.service
import cats.implicits.*
import no.ndla.common.Clock
import no.ndla.common.errors.ValidationException
import no.ndla.common.model.domain.myndla
import no.ndla.common.model.domain.myndla.FolderStatus
import no.ndla.myndlaapi.model.api.{Folder, Owner}
import no.ndla.myndlaapi.model.{api, domain}
import no.ndla.myndlaapi.model.domain.{MyNDLAGroup, MyNDLAUser}
Expand Down Expand Up @@ -73,13 +75,13 @@ trait FolderConverterService {

def mergeFolder(existing: domain.Folder, updated: api.UpdatedFolder): domain.Folder = {
val name = updated.name.getOrElse(existing.name)
val status = updated.status.flatMap(domain.FolderStatus.valueOf).getOrElse(existing.status)
val status = updated.status.flatMap(FolderStatus.valueOf).getOrElse(existing.status)
val description = updated.description.orElse(existing.description)

val shared = (existing.status, status) match {
case (domain.FolderStatus.PRIVATE, domain.FolderStatus.SHARED) => Some(clock.now())
case (domain.FolderStatus.SHARED, domain.FolderStatus.SHARED) => existing.shared
case (domain.FolderStatus.SHARED, domain.FolderStatus.PRIVATE) => None
case (myndla.FolderStatus.PRIVATE, myndla.FolderStatus.SHARED) => Some(clock.now())
case (myndla.FolderStatus.SHARED, myndla.FolderStatus.SHARED) => existing.shared
case (myndla.FolderStatus.SHARED, myndla.FolderStatus.PRIVATE) => None
case _ => None
}

Expand Down Expand Up @@ -156,7 +158,7 @@ trait FolderConverterService {
parentId: Option[UUID],
newRank: Int
): Try[domain.NewFolderData] = {
val newStatus = domain.FolderStatus.valueOf(newFolder.status).getOrElse(domain.FolderStatus.PRIVATE)
val newStatus = myndla.FolderStatus.valueOf(newFolder.status).getOrElse(myndla.FolderStatus.PRIVATE)

Success(
domain.NewFolderData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import no.ndla.common.Clock
import no.ndla.common.errors.NotFoundException
import no.ndla.common.implicits.TryQuestionMark
import no.ndla.common.model.api.SingleResourceStats
import no.ndla.common.model.domain.ResourceType
import no.ndla.common.model.domain.{ResourceType, myndla}
import no.ndla.common.model.domain.myndla.FolderStatus
import no.ndla.myndlaapi.FavoriteFolderDefaultName
import no.ndla.myndlaapi.model.api.{ExportedUserData, Folder, Resource, UserFolder}
import no.ndla.myndlaapi.model.{api, domain}
import no.ndla.myndlaapi.model.domain.FolderStatus
import no.ndla.myndlaapi.repository.{FolderRepository, UserRepository}
import no.ndla.network.clients.FeideApiClient
import no.ndla.network.model.{FeideAccessToken, FeideID}
Expand Down Expand Up @@ -263,7 +263,7 @@ trait FolderReadService {
val favoriteFolder = domain.NewFolderData(
parentId = None,
name = FavoriteFolderDefaultName,
status = domain.FolderStatus.PRIVATE,
status = myndla.FolderStatus.PRIVATE,
rank = 1,
description = None
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,12 @@ import no.ndla.common.errors.{AccessDeniedException, NotFoundException, Validati
import no.ndla.common.implicits.TryQuestionMark
import no.ndla.common.model.NDLADate
import no.ndla.common.model.domain.ResourceType
import no.ndla.common.model.domain.myndla.FolderStatus
import no.ndla.myndlaapi.integration.SearchApiClient
import no.ndla.myndlaapi.model.domain.FolderSortObject.{
FolderSorting,
ResourceSorting,
RootFolderSorting,
SharedFolderSorting
}
import no.ndla.myndlaapi.model.api.{
ExportedUserData,
Folder,
FolderSortRequest,
NewFolder,
NewResource,
Resource,
UpdatedFolder,
UpdatedResource
}
import no.ndla.myndlaapi.model.domain.FolderSortObject.{FolderSorting, ResourceSorting, RootFolderSorting, SharedFolderSorting}
import no.ndla.myndlaapi.model.api.{ExportedUserData, Folder, FolderSortRequest, NewFolder, NewResource, Resource, UpdatedFolder, UpdatedResource}
import no.ndla.myndlaapi.model.{api, domain}
import no.ndla.myndlaapi.model.domain.{
CopyableFolder,
FolderAndDirectChildren,
FolderSortException,
FolderStatus,
Rankable,
SavedSharedFolder
}
import no.ndla.myndlaapi.model.domain.{CopyableFolder, FolderAndDirectChildren, FolderSortException, FolderStatus, Rankable, SavedSharedFolder}
import no.ndla.myndlaapi.repository.{FolderRepository, UserRepository}
import no.ndla.network.clients.FeideApiClient
import no.ndla.network.model.{FeideAccessToken, FeideID}
Expand Down Expand Up @@ -77,7 +57,7 @@ trait FolderWriteService {
updatedFolder: UpdatedFolder
): Try[_] = {
getMyNDLAUser(feideId, feideAccessToken).flatMap(myNDLAUser => {
if (myNDLAUser.isStudent && updatedFolder.status.contains(domain.FolderStatus.SHARED.toString))
if (myNDLAUser.isStudent && updatedFolder.status.contains(FolderStatus.SHARED.toString))
Failure(AccessDeniedException("You do not have necessary permissions to share folders."))
else
canWriteNow(myNDLAUser).flatMap {
Expand Down
3 changes: 2 additions & 1 deletion myndla-api/src/test/scala/no/ndla/myndlaapi/TestData.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ package no.ndla.myndlaapi

import no.ndla.common.model.NDLADate
import no.ndla.common.model.domain.ResourceType
import no.ndla.common.model.domain.myndla.FolderStatus
import no.ndla.myndlaapi.model.api
import no.ndla.myndlaapi.model.domain.{FolderStatus, MyNDLAUser, NewFolderData, Resource, ResourceDocument, UserRole}
import no.ndla.myndlaapi.model.domain.{MyNDLAUser, NewFolderData, Resource, ResourceDocument, UserRole}
import no.ndla.myndlaapi.model.domain

import java.util.UUID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import io.circe.parser
import no.ndla.common.CirceUtil
import no.ndla.common.model.NDLADate
import no.ndla.common.model.domain.ResourceType
import no.ndla.common.model.domain.myndla.FolderStatus
import no.ndla.myndlaapi.model.api
import no.ndla.myndlaapi.model.domain
import no.ndla.myndlaapi.model.api.{Breadcrumb, Folder}
import no.ndla.myndlaapi.model.domain.{FolderStatus, NewFolderData, ResourceDocument}
import no.ndla.myndlaapi.model.domain.{NewFolderData, ResourceDocument}
import no.ndla.myndlaapi.{ComponentRegistry, MainClass, MyNdlaApiProperties, UnitSuite}
import no.ndla.network.clients.FeideExtendedUserInfo
import no.ndla.scalatestsuite.IntegrationSuite
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ package no.ndla.myndlaapi.e2e

import no.ndla.common.CirceUtil
import no.ndla.common.model.NDLADate
import no.ndla.common.model.domain.myndla.FolderStatus
import no.ndla.myndlaapi.model.api.Folder
import no.ndla.myndlaapi.model.api
import no.ndla.myndlaapi.model.domain.FolderStatus
import no.ndla.myndlaapi.{ComponentRegistry, MainClass, MyNdlaApiProperties, UnitSuite}
import no.ndla.network.clients.FeideExtendedUserInfo
import no.ndla.scalatestsuite.IntegrationSuite
Expand Down
Loading

0 comments on commit 8467388

Please sign in to comment.