Skip to content

Commit 7ecc6f0

Browse files
committed
fix the logic to correctly create guarantor
1 parent 06875c6 commit 7ecc6f0

6 files changed

Lines changed: 23 additions & 12 deletions

File tree

core/data/src/commonMain/kotlin/com/mifos/core/data/mappers/loan/GuarantorMapper.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import com.mifos.core.network.dto.loans.GuarantorTypeDto
2525
fun GuarantorTemplateDto.toDomain(): GuarantorTemplate =
2626
GuarantorTemplate(
2727
allowedClientRelationshipTypes = allowedClientRelationshipTypes.map { it.toDomain() },
28+
guarantorTypeOptions = guarantorTypeOptions.map { it.toDomain() },
2829
)
2930

3031
fun GuarantorRelationshipOptionDto.toDomain(): GuarantorRelationshipOption =
@@ -49,7 +50,7 @@ fun CreateGuarantorInput.toDto(): GuarantorRequestDto {
4950
locale = locale,
5051
firstname = firstname,
5152
lastname = lastname,
52-
dateOfBirth = dateOfBirth,
53+
dob = dob,
5354
addressLine1 = addressLine1,
5455
addressLine2 = addressLine2,
5556
city = city,

core/model/src/commonMain/kotlin/com/mifos/core/model/objects/account/loan/guarantor/CreateGuarantorInput.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ data class CreateGuarantorInput(
1515
val clientRelationshipTypeId: Long,
1616
val dateFormat: String = "dd-MM-yyyy",
1717
val entityId: Int? = null,
18-
val guarantorTypeId: Long?,
18+
val guarantorTypeId: Long,
1919
val locale: String = DateConstants.LOCALE,
2020
val firstname: String? = null,
2121
val lastname: String? = null,
22-
val dateOfBirth: String? = null,
22+
val dob: String? = null,
2323
val addressLine1: String? = null,
2424
val addressLine2: String? = null,
2525
val city: String? = null,

core/model/src/commonMain/kotlin/com/mifos/core/model/objects/account/loan/guarantor/GuarantorTemplate.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ package com.mifos.core.model.objects.account.loan.guarantor
1111

1212
data class GuarantorTemplate(
1313
val allowedClientRelationshipTypes: List<GuarantorRelationshipOption>,
14+
val guarantorTypeOptions: List<GuarantorType>,
1415
)
1516

1617
data class GuarantorRelationshipOption(

core/network/src/commonMain/kotlin/com/mifos/core/network/dto/loans/GuarantorRequestDto.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ data class GuarantorRequestDto(
1616
val clientRelationshipTypeId: Long,
1717
val dateFormat: String,
1818
val entityId: Int?,
19-
val guarantorTypeId: Long?,
19+
val guarantorTypeId: Long,
2020
val locale: String,
2121

2222
val firstname: String?,
2323
val lastname: String?,
24-
val dateOfBirth: String?,
24+
val dob: String?,
2525
val addressLine1: String?,
2626
val addressLine2: String?,
2727
val city: String?,

core/network/src/commonMain/kotlin/com/mifos/core/network/dto/loans/GuarantorTemplateDto.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import kotlinx.serialization.Serializable
1313
@Serializable
1414
data class GuarantorTemplateDto(
1515
val allowedClientRelationshipTypes: List<GuarantorRelationshipOptionDto> = emptyList(),
16+
val guarantorTypeOptions: List<GuarantorTypeDto>,
17+
1618
)
1719

1820
@Serializable

feature/loan/src/commonMain/kotlin/com/mifos/feature/loan/createGuarantor/CreateGuarantorViewModel.kt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ internal class CreateGuarantorViewModel(
7171
existingClient = action.checked,
7272
clientSearchQuery = "",
7373
selectedClientId = null,
74+
existingGuarantorTypeId = null,
7475
searchedClientOptions = emptyList(),
7576
clientError = null,
7677
relationshipError = null,
@@ -199,6 +200,9 @@ internal class CreateGuarantorViewModel(
199200
it.copy(
200201
viewState = CreateGuarantorState.ViewState.Success,
201202
guarantorRelationshipOptions = result.data.allowedClientRelationshipTypes,
203+
externalGuarantorTypeId = result.data.guarantorTypeOptions.firstOrNull { guarantorType ->
204+
guarantorType.code == "guarantor.external"
205+
}?.id,
202206
)
203207
}
204208
}
@@ -229,7 +233,7 @@ internal class CreateGuarantorViewModel(
229233
val template = result.data
230234
mutableStateFlow.update {
231235
it.copy(
232-
guarantorTypeId = template.guarantorType.id,
236+
existingGuarantorTypeId = template.guarantorType.id,
233237
fetchingGuarantorAccountTemplate = false,
234238
)
235239
}
@@ -258,7 +262,8 @@ internal class CreateGuarantorViewModel(
258262

259263
// Safe extraction since validateInput enforces these are non-null
260264
val relationshipId = computedRelationshipId ?: return
261-
val guarantorTypeId = state.guarantorTypeId ?: return
265+
val guarantorTypeId =
266+
state.existingGuarantorTypeId ?: state.externalGuarantorTypeId ?: return
262267

263268
mutableStateFlow.update {
264269
it.copy(
@@ -273,10 +278,10 @@ internal class CreateGuarantorViewModel(
273278
val request = CreateGuarantorInput(
274279
clientRelationshipTypeId = relationshipId,
275280
entityId = state.selectedClientId,
276-
guarantorTypeId = if (state.existingClient) guarantorTypeId else null,
281+
guarantorTypeId = guarantorTypeId,
277282
firstname = state.firstName.takeIf { !state.existingClient },
278283
lastname = state.lastName.takeIf { !state.existingClient },
279-
dateOfBirth = state.dateOfBirthMillis?.let(DateHelper::getDateAsStringFromLong),
284+
dob = state.dateOfBirthMillis?.let(DateHelper::getDateAsStringFromLong),
280285
addressLine1 = state.addressLine1.takeIf { it.isNotBlank() },
281286
addressLine2 = state.addressLine2.takeIf { it.isNotBlank() },
282287
city = state.city.takeIf { it.isNotBlank() },
@@ -328,8 +333,7 @@ internal class CreateGuarantorViewModel(
328333
return false
329334
}
330335

331-
val guarantorTypeId = state.guarantorTypeId
332-
if (computedRelationshipId == null || (guarantorTypeId == null && state.existingClient)) {
336+
if ((state.existingGuarantorTypeId == null && state.existingClient) || state.externalGuarantorTypeId == null) {
333337
sendEvent(
334338
CreateGuarantorEvent.ShowMessage(
335339
Res.string.feature_loan_create_guarantor_missing_configuration,
@@ -389,10 +393,12 @@ internal class CreateGuarantorViewModel(
389393
}
390394
mutableStateFlow.update { it.copy(searchedClientOptions = clients) }
391395
}
396+
392397
is DataState.Error -> {
393398
mutableStateFlow.update { it.copy(searchedClientOptions = emptyList()) }
394399
sendEvent(CreateGuarantorEvent.ShowMessage(Res.string.feature_loan_load_clients_failure))
395400
}
401+
396402
DataState.Loading -> Unit
397403
}
398404
}
@@ -423,7 +429,8 @@ data class CreateGuarantorState(
423429
val mobile: String = "",
424430
val residencePhone: String = "",
425431

426-
val guarantorTypeId: Long? = null,
432+
val existingGuarantorTypeId: Long? = null,
433+
val externalGuarantorTypeId: Long? = null,
427434

428435
val clientError: StringResource? = null,
429436
val relationshipError: StringResource? = null,

0 commit comments

Comments
 (0)