From 0de3eb714290942fbc8f1141fad8ed3b7c0bef93 Mon Sep 17 00:00:00 2001 From: gowthaman Date: Thu, 23 May 2024 19:24:30 +0530 Subject: [PATCH] add expense and up kotlin --- build.gradle.kts | 6 ++-- src/main/kotlin/com/restapi/Main.kt | 15 +++++----- .../com/restapi/controllers/Entities.kt | 30 ++++++++++++++++++- src/main/kotlin/com/restapi/domain/db.kt | 12 ++++---- 4 files changed, 45 insertions(+), 18 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 747f748..f3a5391 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,8 +1,8 @@ import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar plugins { - kotlin("jvm") version "1.9.23" - kotlin("kapt") version "1.9.23" + kotlin("jvm") version "2.0.0" + kotlin("kapt") version "2.0.0" id("idea") id("io.ebean") version "13.23.2" id("com.github.johnrengelman.shadow") version "8.1.1" @@ -33,8 +33,6 @@ dependencies { implementation("ch.qos.logback:logback-classic:1.4.12") implementation("org.bitbucket.b_c:jose4j:0.9.4") implementation("redis.clients:jedis:5.0.2") - implementation("org.jetbrains.kotlin:kotlin-scripting-jsr223:1.9.23") - implementation("org.jetbrains.kotlin:kotlin-script-runtime:1.9.23") implementation("org.bouncycastle:bcprov-jdk18on:1.76") implementation("org.bouncycastle:bcpkix-jdk18on:1.76") implementation("org.yaml:snakeyaml:2.2") diff --git a/src/main/kotlin/com/restapi/Main.kt b/src/main/kotlin/com/restapi/Main.kt index 857b6b3..659ce72 100644 --- a/src/main/kotlin/com/restapi/Main.kt +++ b/src/main/kotlin/com/restapi/Main.kt @@ -37,7 +37,7 @@ fun main(args: Array) { val viewRole = Role.Standard(Action.VIEW) val createRole = Role.Standard(Action.CREATE) val updateRole = Role.Standard(Action.UPDATE) - val approveOrRejectRole = Role.Standard(Action.APPROVE) + val appAdmin = Role.Explicit("ROLE_ADMIN") //todo, create roles in keycloak based on entity and actions @@ -301,14 +301,15 @@ fun main(args: Array) { } } - get("/{entity}/{id}", Entities::view, Roles(adminRole, viewRole)) - post("/{entity}/search", Entities::search, Roles(adminRole, viewRole)) - post("/{entity}", Entities::create, Roles(adminRole, createRole)) + post("/{entity}/next", Entities::getNextSeqNo, Roles(adminRole, viewRole, appAdmin)) + get("/{entity}/{id}", Entities::view, Roles(adminRole, viewRole, appAdmin)) + post("/{entity}/search", Entities::search, Roles(adminRole, viewRole, appAdmin)) + post("/{entity}", Entities::create, Roles(adminRole, createRole, appAdmin)) - put("/{entity}/{id}", Entities::update, Roles(adminRole, updateRole)) - patch("/{entity}/{id}", Entities::patch, Roles(adminRole, updateRole)) - delete("/{entity}/{id}", Entities::delete, Roles(adminRole, Role.Standard(Action.DELETE))) + put("/{entity}/{id}", Entities::update, Roles(adminRole, updateRole, appAdmin)) + patch("/{entity}/{id}", Entities::patch, Roles(adminRole, updateRole, appAdmin)) + delete("/{entity}/{id}", Entities::delete, Roles(adminRole, Role.Standard(Action.DELETE), appAdmin)) } }.exception(DuplicateKeyException::class.java, Exceptions.dupKeyExceptionHandler) .exception(DataIntegrityException::class.java, Exceptions.dataIntegrityException) diff --git a/src/main/kotlin/com/restapi/controllers/Entities.kt b/src/main/kotlin/com/restapi/controllers/Entities.kt index b78972e..874bf7c 100644 --- a/src/main/kotlin/com/restapi/controllers/Entities.kt +++ b/src/main/kotlin/com/restapi/controllers/Entities.kt @@ -11,6 +11,7 @@ import com.restapi.domain.Session.findDataModelByEntityAndUniqId import com.restapi.integ.Scripting import io.javalin.http.BadRequestResponse import io.javalin.http.Context +import io.javalin.http.HttpStatus import io.javalin.http.bodyAsClass import org.slf4j.LoggerFactory import java.time.LocalDate @@ -84,6 +85,7 @@ class QueryParamDeSerializer : JsonDeserializer() { object Entities { private val logger = LoggerFactory.getLogger("Entities") + private val OK = mapOf("status" to true) fun delete(ctx: Context) { val e = database.findDataModelByEntityAndUniqId(ctx.pathParam("entity"), ctx.pathParam("id")) e.deletedBy = Session.currentUser() @@ -116,6 +118,7 @@ object Entities { e.data.putAll(newData) e.update() + ctx.json(OK) } private fun verifyKeys(newData: Map) { @@ -152,6 +155,30 @@ object Entities { } + fun getNextSeqNo(ctx: Context) { + val entity = ctx.pathParam("entity").uppercase() + + val prefix = "$entity/" + val plantId = ctx.queryParam("plantId") ?: throw BadRequestResponse("plantId not sent") + val plant = database.find(Plant::class.java) + .where() + .eq("plantId", plantId) + .findOne() ?: throw BadRequestResponse("plant missing for $plantId") + + val inventoryPrefix = plant.prefixes?.get(entity) ?: prefix + + val cnt = (database.find(DataModel::class.java) + .where() + .eq("entityName", entity.lowercase()) + .eq("data->>'plantId'", plantId) + .findCount() + 1) + .toString() + .padStart(6, '0') + + val seq = SequenceNumber(inventoryPrefix + cnt) + ctx.json(seq).status(HttpStatus.OK) + } + fun view(it: Context) { database.save( AuditLog().apply { @@ -264,6 +291,7 @@ object Entities { this.data = dataModel.data } ) + ctx.json(OK) } private fun isValidDate(f: String) = try { @@ -290,7 +318,7 @@ object Entities { data class SearchParams( val params: Map = mapOf(), - val dateRange: List = emptyList() + val dateRange: List = listOf(LocalDate.now().minusDays(7), LocalDate.now()) ) data class SequenceNumber(val number: String) diff --git a/src/main/kotlin/com/restapi/domain/db.kt b/src/main/kotlin/com/restapi/domain/db.kt index 36fe1ce..a062342 100644 --- a/src/main/kotlin/com/restapi/domain/db.kt +++ b/src/main/kotlin/com/restapi/domain/db.kt @@ -137,6 +137,11 @@ object Session { } + val objectMapper = jacksonObjectMapper().apply { + configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) + configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) + findAndRegisterModules() + } private val sc = DatabaseConfig().apply { loadFromProperties(Properties().apply { @@ -150,13 +155,8 @@ object Session { currentUserProvider = CurrentUserProvider { currentUser.get().userName } objectMapper = Session.objectMapper } - val database: Database = DatabaseFactory.create(sc) - val objectMapper = jacksonObjectMapper().apply { - configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) - configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) - findAndRegisterModules() - } + fun currentUser() = currentUser.get().userName fun currentTenant() = currentUser.get().tenant