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

Add config for ai enabled #333

Merged
merged 1 commit into from
Nov 10, 2023
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 @@ -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