I am very new to Kotlin and I am trying to achieve this:
- I get a batch of
PersonDetailsto save or update - In the batch, the same
PersonDetailswith name is different locale can be present. - The end goal of this method is to merge the batch itself and also merge the existing entries in Database then return the updated data structure for saving into DB.
I am not very happy with the way I have coded, can you please review this code and give your review comments.
fun doProcess(batchToProcess: List<PersonDetail>) : Map<Long, PersonDetail> {
val idsInBatch = batchToProcess.map { it.lcmId }.toList()
val personDetailsFromDb = personDetailsRepository.getBatch(idsInBatch)
.map { it.lcmId to it }
.toMap()
val batchesOfPersonDetails = mutableMapOf<Long, PersonDetail>()
batchToProcess.map { aPerson ->
var personDetail = batchesOfPersonDetails[aPerson.lcmId]
if (personDetail == null) {
personDetail = personDetailsFromDb[aPerson.lcmId]
}
val updatedPersonDetail = when {
personDetail == null -> aPerson.toPersonDetail()
personDetail.isActive != aPerson.isActive -> {
personDetail.isActive = personDetail.isActive
personDetail
}
personDetail.localeDetails[aPerson.localeCode] != aPerson.localeName -> {
personDetail.localeDetails[aPerson.localeCode] = aPerson.localeName
personDetail
}
else -> {
logger.info { "Duplicate_Event for id ${aPerson.lcmId}" }
personDetail
}
}
batchesOfPersonDetails[updatedPersonDetail.lcmId] = updatedPersonDetail
}
return batchesOfPersonDetails
}