reference type checks entity record id and entity name only

This commit is contained in:
simonredfern 2025-12-06 02:21:30 +01:00
parent 9d92c1d300
commit cc812f230f
2 changed files with 24 additions and 8 deletions

View File

@ -4,6 +4,7 @@ import java.util.regex.Pattern
import code.api.util.ErrorMessages.DynamicEntityInstanceValidateFail
import code.api.util.{APIUtil, CallContext, NewStyle}
import code.util.Helper.MdcLoggable
import com.openbankproject.commons.ExecutionContext.Implicits.global
import com.openbankproject.commons.model.enums.{DynamicEntityFieldType, DynamicEntityOperation}
import com.openbankproject.commons.model._
@ -144,7 +145,7 @@ trait DynamicEntityT {
}
}
object ReferenceType {
object ReferenceType extends MdcLoggable {
private def recoverFn(fieldName: String, value: String, entityName: String): PartialFunction[Throwable, String] = {
case _: Throwable => s"entity '$entityName' not found by the value '$value', the field name is '$fieldName'."
@ -360,14 +361,18 @@ object ReferenceType {
} else {
val dynamicEntityName = typeName.replace("reference:", "")
val errorMsg = s"""$dynamicEntityName not found by the id value '$value', propertyName is '$propertyName'"""
NewStyle.function.invokeDynamicConnector(DynamicEntityOperation.GET_ONE,dynamicEntityName, None, Some(value), None, None, None, false,callContext)
.recover {
case _: Throwable => errorMsg
}
.map {
case (Full(_), _) => ""
case _ => errorMsg
logger.info(s"========== Validating reference field: propertyName='$propertyName', typeName='$typeName', dynamicEntityName='$dynamicEntityName', value='$value' ==========")
Future {
val exists = code.DynamicData.MappedDynamicDataProvider.existsById(dynamicEntityName, value)
if (exists) {
logger.info(s"========== Reference validation SUCCESS: propertyName='$propertyName', dynamicEntityName='$dynamicEntityName', value='$value' ==========")
""
} else {
logger.warn(s"========== Reference validation FAILED: propertyName='$propertyName', dynamicEntityName='$dynamicEntityName', value='$value' ==========")
errorMsg
}
}
}
}
}

View File

@ -25,6 +25,17 @@ object MappedDynamicDataProvider extends DynamicDataProvider with CustomJsonForm
saveOrUpdate(bankId, entityName, requestBody, userId, isPersonalEntity, dynamicData)
}
// Separate method for reference validation - only checks ID and entity name exist
def existsById(entityName: String, id: String): Boolean = {
println(s"========== Reference validation: checking if DynamicDataId='$id' exists for DynamicEntityName='$entityName' ==========")
val exists = DynamicData.count(
By(DynamicData.DynamicDataId, id),
By(DynamicData.DynamicEntityName, entityName)
) > 0
println(s"========== Reference validation result: exists=$exists ==========")
exists
}
override def get(bankId: Option[String],entityName: String, id: String, userId: Option[String], isPersonalEntity: Boolean): Box[DynamicDataT] = {
if(bankId.isEmpty && !isPersonalEntity ){ //isPersonalEntity == false, get all the data, no need for specific userId.
//forced the empty also to a error here. this is get Dynamic by Id, if it return Empty, better show the error in this level.