Skip to content

Commit

Permalink
Merge pull request #333 from NDLANO/move-ai-config-to-backend
Browse files Browse the repository at this point in the history
Add config for ai enabled
  • Loading branch information
gunnarvelle authored Nov 10, 2023
2 parents 6f1b3bf + 39a8e01 commit 68ce104
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import no.ndla.learningpathapi.db.migrationwithdependencies.{
V13__StoreNDLAStepsAsIframeTypes,
V14__ConvertLanguageUnknown,
V15__MergeDuplicateLanguageFields,
V31__ArenaDefaultEnabledOrgs
V31__ArenaDefaultEnabledOrgs,
V33__AiDefaultEnabledOrgs
}
import no.ndla.learningpathapi.integration.DataSource
import org.flywaydb.core.Flyway
Expand All @@ -33,7 +34,8 @@ trait DBMigrator {
new V13__StoreNDLAStepsAsIframeTypes(props),
new V14__ConvertLanguageUnknown(props),
new V15__MergeDuplicateLanguageFields(props),
new V31__ArenaDefaultEnabledOrgs(props)
new V31__ArenaDefaultEnabledOrgs(props),
new V33__AiDefaultEnabledOrgs(props)
)
.locations("no/ndla/learningpathapi/db/migration")
.dataSource(dataSource)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Part of NDLA learningpath-api
* Copyright (C) 2023 NDLA
*
* See LICENSE
*/

package no.ndla.learningpathapi.db.migrationwithdependencies

import io.circe.Json
import io.circe.syntax._
import no.ndla.common.model.NDLADate
import no.ndla.learningpathapi.LearningpathApiProperties
import org.flywaydb.core.api.migration.{BaseJavaMigration, Context}
import org.postgresql.util.PGobject
import scalikejdbc.{DB, DBSession, _}

class V33__AiDefaultEnabledOrgs(properties: LearningpathApiProperties) extends BaseJavaMigration {

override def migrate(context: Context): Unit = DB(context.getConnection)
.autoClose(false)
.withinTx { implicit session =>
insertConfig
}

private def insertConfig(implicit session: DBSession): Unit = {
val document = Json.obj(
"key" -> Json.fromString("AI_ENABLED_ORGS"),
"value" -> Json.obj("value" -> Json.fromValues(orgs.map(Json.fromString))),
"updatedAt" -> NDLADate.now().asJson,
"updatedBy" -> Json.fromString("System")
)

val dataObject = new PGobject()
dataObject.setType("jsonb")
dataObject.setValue(document.noSpaces)

val inserted = sql"""
INSERT INTO configtable(configkey, value)
VALUES (
'AI_ENABLED_ORGS',
$dataObject
)
""".update.apply()

if (inserted != 1) throw new RuntimeException("Failed to insert AI_ENABLED_ORGS")
}

private def orgs: List[String] = properties.Environment match {
case "local" | "test" =>
List(
"Agder fylkeskommune",
"Innlandet fylkeskommune",
"Møre og Romsdal fylkeskommune",
"Nordland fylkeskommune",
"Rogaland fylkeskommune",
"Troms og Finnmark fylkeskommune",
"Trøndelag fylkeskommune",
"Vestfold og Telemark fylkeskommune",
"Vestland fylkeskommune",
"Viken fylkeskommune",
"Universitetet i Rogn"
)
case "staging" | "prod" =>
List(
"Agder fylkeskommune",
"Nordland fylkeskommune",
"Rogaland fylkeskommune",
"Troms og Finnmark fylkeskommune",
"Trøndelag fylkeskommune",
"Vestland fylkeskommune"
)
case _ => List.empty
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ object ConfigKey extends Enum[ConfigKey] with CirceEnum[ConfigKey] {
case object LearningpathWriteRestricted extends ConfigKey("LEARNINGPATH_WRITE_RESTRICTED")
case object MyNDLAWriteRestricted extends ConfigKey("MY_NDLA_WRITE_RESTRICTED")
case object ArenaEnabledOrgs extends ConfigKey("ARENA_ENABLED_ORGS")
case object AiEnabledOrgs extends ConfigKey("AI_ENABLED_ORGS")

val values: IndexedSeq[ConfigKey] = findValues

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ case class ConfigMeta(
case ConfigKey.LearningpathWriteRestricted => validateBooleanKey(ConfigKey.LearningpathWriteRestricted)
case ConfigKey.MyNDLAWriteRestricted => validateBooleanKey(ConfigKey.MyNDLAWriteRestricted)
case ConfigKey.ArenaEnabledOrgs => validateStringListKey(ConfigKey.ArenaEnabledOrgs)
case ConfigKey.AiEnabledOrgs => validateStringListKey(ConfigKey.AiEnabledOrgs)
}
}

Expand Down

0 comments on commit 68ce104

Please sign in to comment.