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

Better typechecking of user role #615

Merged
merged 4 commits into from
Mar 4, 2025
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 @@ -10,6 +10,7 @@ package no.ndla.common.model.api.myndla

import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import io.circe.{Decoder, Encoder}
import no.ndla.common.model.domain.myndla.UserRole
import sttp.tapir.Schema.annotations.description

case class MyNDLAGroupDTO(
Expand All @@ -31,7 +32,7 @@ case class MyNDLAUserDTO(
@description("Email address of the user") email: String,
@description("Name of the user") displayName: String,
@description("Favorite subjects of the user") favoriteSubjects: Seq[String],
@description("User role") role: String,
@description("User role") role: UserRole,
@description("User root organization") organization: String,
@description("User groups") groups: Seq[MyNDLAGroupDTO],
@description("Whether arena is explicitly enabled for the user") arenaEnabled: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ case class MyNDLAUser(
id: Long,
feideId: String,
favoriteSubjects: Seq[String],
userRole: UserRole.Value,
userRole: UserRole,
lastUpdated: NDLADate,
organization: String,
groups: Seq[MyNDLAGroup],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import no.ndla.common.model.NDLADate

case class MyNDLAUserDocument(
favoriteSubjects: Seq[String],
userRole: UserRole.Value,
userRole: UserRole,
lastUpdated: NDLADate,
organization: String,
groups: Seq[MyNDLAGroup],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,23 @@

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

import io.circe.{Decoder, Encoder}
import enumeratum.*
import com.scalatsi.{TSNamedType, TSType}
import com.scalatsi.TypescriptType.{TSLiteralString, TSUnion}
import enumeratum.{CirceEnum, EnumEntry}

object UserRole extends Enumeration {
val EMPLOYEE: UserRole.Value = Value("employee")
val STUDENT: UserRole.Value = Value("student")
sealed abstract class UserRole(override val entryName: String) extends EnumEntry {
override def toString: String = entryName
}
object UserRole extends Enum[UserRole] with CirceEnum[UserRole] {
case object EMPLOYEE extends UserRole("employee")
case object STUDENT extends UserRole("student")

def valueOf(s: String): Option[FolderStatus.Value] = FolderStatus.values.find(_.toString == s)
def valueOf(s: Option[String]): Option[FolderStatus.Value] = s.flatMap(valueOf)
val values: IndexedSeq[UserRole] = findValues

implicit val encoder: Encoder[UserRole.Value] = Encoder.encodeEnumeration(UserRole)
implicit val decoder: Decoder[UserRole.Value] = Decoder.decodeEnumeration(UserRole)
def all: Seq[String] = UserRole.values.map(_.entryName)
def valueOf(s: String): Option[UserRole] = UserRole.withNameOption(s)

implicit val availability: TSNamedType[UserRole] =
TSType.alias[UserRole]("UserRole", TSUnion(values.map(e => TSLiteralString(e.entryName))))
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ trait FolderConverterService {
email = domainUserData.email,
displayName = domainUserData.displayName,
favoriteSubjects = domainUserData.favoriteSubjects,
role = domainUserData.userRole.toString,
role = domainUserData.userRole,
organization = domainUserData.organization,
groups = domainUserData.groups.map(toApiGroup),
arenaEnabled = domainUserData.arenaEnabled,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ class FolderConverterServiceTest extends UnitTestSuite with TestEnvironment {
email = "example@email.com",
displayName = "Feide",
favoriteSubjects = Seq("a", "b"),
role = "student",
role = UserRole.STUDENT,
organization = "oslo",
groups = Seq(MyNDLAGroupDTO(id = "id", displayName = "oslo", isPrimarySchool = true, parentId = None)),
arenaEnabled = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class UserServiceTest extends UnitTestSuite with TestEnvironment {
email = "example@email.com",
displayName = "Feide",
favoriteSubjects = Seq("r", "e"),
role = "student",
role = UserRole.STUDENT,
organization = "oslo",
groups = Seq(MyNDLAGroupDTO(id = "id", displayName = "oslo", isPrimarySchool = false, parentId = None)),
arenaEnabled = false,
Expand Down Expand Up @@ -188,7 +188,7 @@ class UserServiceTest extends UnitTestSuite with TestEnvironment {
email = "example@email.com",
displayName = "Feide",
favoriteSubjects = Seq("r", "e"),
role = "student",
role = UserRole.STUDENT,
organization = "oslo",
groups = Seq(MyNDLAGroupDTO(id = "id", displayName = "oslo", isPrimarySchool = true, parentId = None)),
arenaEnabled = false,
Expand Down Expand Up @@ -260,7 +260,7 @@ class UserServiceTest extends UnitTestSuite with TestEnvironment {
email = "example@email.com",
displayName = "Feide",
favoriteSubjects = Seq("r", "e"),
role = "student",
role = UserRole.STUDENT,
organization = "oslo",
groups = Seq(MyNDLAGroupDTO(id = "id", displayName = "oslo", isPrimarySchool = true, parentId = None)),
arenaEnabled = false,
Expand Down Expand Up @@ -335,7 +335,7 @@ class UserServiceTest extends UnitTestSuite with TestEnvironment {
email = "example@email.com",
displayName = "Feide",
favoriteSubjects = Seq("r", "e"),
role = "student",
role = UserRole.STUDENT,
organization = "oslo",
groups = Seq(MyNDLAGroupDTO(id = "id", displayName = "oslo", isPrimarySchool = true, parentId = None)),
arenaEnabled = false,
Expand Down
1 change: 1 addition & 0 deletions project/myndlaapi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ object myndlaapi extends Module {
"ConfigMetaRestrictedDTO",
"no.ndla.common.model.api.myndla.MyNDLAUserDTO",
"no.ndla.common.model.api.myndla.UpdatedMyNDLAUserDTO",
"no.ndla.common.model.domain.myndla.UserRole",
"no.ndla.common.model.domain.ResourceType",
"config.ConfigMetaDTO",
"FolderDTO",
Expand Down
4 changes: 3 additions & 1 deletion typescript/types-backend/myndla-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface IMyNDLAUserDTO {
email: string
displayName: string
favoriteSubjects: string[]
role: string
role: UserRole
organization: string
groups: IMyNDLAGroupDTO[]
arenaEnabled: boolean
Expand Down Expand Up @@ -130,3 +130,5 @@ export interface IUserFolderDTO {
}

export type ResourceType = ("article" | "audio" | "concept" | "image" | "learningpath" | "multidisciplinary" | "topic" | "video")

export type UserRole = ("employee" | "student")