-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProcessMissionActionInfractionEnvTarget.kt
58 lines (50 loc) · 2.64 KB
/
ProcessMissionActionInfractionEnvTarget.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package fr.gouv.dgampa.rapportnav.domain.use_cases.mission.infraction.v2
import fr.gouv.dgampa.rapportnav.config.UseCase
import fr.gouv.dgampa.rapportnav.domain.entities.mission.nav.infraction.InfractionEntity
import fr.gouv.dgampa.rapportnav.domain.entities.mission.nav.infraction.InfractionEnvTargetEntity
import fr.gouv.dgampa.rapportnav.domain.repositories.mission.infraction.IInfractionEnvTargetRepository
import fr.gouv.dgampa.rapportnav.domain.repositories.mission.infraction.IInfractionRepository
import fr.gouv.dgampa.rapportnav.infrastructure.api.bff.model.infraction.Infraction
import fr.gouv.dgampa.rapportnav.infrastructure.api.bff.model.infraction.InfractionsByVessel
import java.util.*
@UseCase
class ProcessMissionActionInfractionEnvTarget(
private val infractionRepo: IInfractionRepository,
private val infractionEnvTargetRepo: IInfractionEnvTargetRepository,
) {
fun execute(actionId: String, infractionByVessels: List<InfractionsByVessel>?): List<InfractionEntity>? {
val infractions =
infractionByVessels?.flatMap { it.infractions }?.filter { it.controlId != null || it.controlType != null }
val infractionIds = infractions?.map { it.id } ?: listOf()
val databaseInfractions = infractionRepo
.findAllByActionId(actionId)
.map { it.toInfractionEntity() }
val toDeleteInfractions = databaseInfractions.filter { !infractionIds.contains(it.id.toString()) }
delete(toDeleteInfractions)
return save(infractions) //TODO: is not equals save
}
fun save(infractions: List<Infraction>?): List<InfractionEntity>? {
// check target / create or Update target
// create infractionEntity -- > save target
//save infraction
return infractions?.map {
// todo check if it has changed
val target = it.target
val newTarget = InfractionEnvTargetEntity(
id = UUID.fromString(target?.id),
missionId = it.missionId,
actionId = it.actionId,
infractionId = UUID.fromString(it.id),
vesselIdentifier = target?.vesselIdentifier,
vesselSize = target?.vesselSize,
vesselType = target?.vesselType,
identityControlledPerson = target?.identityControlledPerson!!
)
infractionEnvTargetRepo.save(infractionTarget = newTarget, infraction = it.toInfractionEntity())
infractionRepo.save(it.toInfractionEntity()).toInfractionEntity()
}
}
fun delete(infractions: List<InfractionEntity>?) {
infractions?.forEach { infractionRepo.deleteById(it.id) }
}
}